Advertisement
Guest User

pipeline

a guest
Apr 20th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Xml;
  6. using System.ComponentModel;
  7. using Microsoft.BizTalk.Component.Interop;
  8. using Microsoft.BizTalk.Message.Interop;
  9.  
  10. using System.Drawing;
  11. using System.Resources;
  12. using System.Reflection;
  13. using System.Diagnostics;
  14. using System.Collections;
  15. using System.ComponentModel.Design;
  16. using Microsoft.BizTalk.Component;
  17. using Microsoft.BizTalk.Messaging;
  18.  
  19.  
  20. namespace MessageBatchPipelineCompoent
  21. {
  22.     [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
  23.     [ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]
  24.     [System.Runtime.InteropServices.Guid("6118B8F0-8684-4ba2-87B4-8336D70BD4F7")]
  25.     public class DisassemblePipeline : IBaseComponent,
  26.         IDisassemblerComponent,
  27.         IComponentUI,
  28.         IPersistPropertyBag
  29.     {
  30.         //Used to hold disassembled messages
  31.         private System.Collections.Queue qOutputMsgs = new System.Collections.Queue();
  32.         private string systemPropertiesNamespace = @"http://schemas.microsoft.com/BizTalk/2003/system-properties";
  33.        
  34.  
  35.         /// <summary>
  36.         /// Batch size used to batch records
  37.         /// </summary>
  38.         private int _BatchSize;
  39.         public int BatchSize
  40.         {
  41.             get { return _BatchSize; }
  42.             set { _BatchSize = value; }
  43.         }
  44.  
  45.  
  46.         /// <summary>
  47.         /// Default constructor
  48.         /// </summary>
  49.         public DisassemblePipeline()
  50.         {
  51.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: Constructor"); // Debug
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Description of pipeline
  56.         /// </summary>
  57.         public string Description
  58.         {
  59.             get
  60.             {
  61.                 return "Component to batch (break) large message into multiple small messages";
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Name of pipeline
  67.         /// </summary>
  68.         public string Name
  69.         {
  70.             get
  71.             {
  72.                 return "MessageBatchComponent";
  73.             }
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Pipeline version
  78.         /// </summary>
  79.         public string Version
  80.         {
  81.             get
  82.             {
  83.                 return "1.0.0.10";
  84.             }
  85.         }
  86.  
  87.  
  88.         /// <summary>
  89.         /// Returns collecton of errors
  90.         /// </summary>
  91.         public System.Collections.IEnumerator Validate(object projectSystem)
  92.         {
  93.             return null;
  94.         }
  95.  
  96.         /// <summary>
  97.         /// Returns icon of pipeline
  98.         /// </summary>
  99.         public System.IntPtr Icon
  100.         {
  101.             get
  102.             {
  103.                 return new System.IntPtr();
  104.             }
  105.         }
  106.  
  107.  
  108.         /// <summary>
  109.         /// Class GUID
  110.         /// </summary>
  111.         public void GetClassID(out Guid classID)
  112.         {
  113.             classID = new Guid("ACC3F15A-C389-4a5d-8F8E-2A951CDC4C19");
  114.         }
  115.  
  116.         /// <summary>
  117.         /// InitNew
  118.         /// </summary>
  119.         public void InitNew()
  120.         {
  121.         }
  122.  
  123.  
  124.         /// <summary>
  125.         /// Load property from property bag
  126.         /// </summary>
  127.         public virtual void Load(IPropertyBag propertyBag, int errorLog)
  128.         {
  129.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: Load"); // Debug
  130.             object val = null;
  131.             try
  132.             {
  133.                 val = ReadPropertyBag(propertyBag, "BatchSize");
  134.                 //propertyBag.Read("BatchSize", out val, 0);
  135.             }
  136.             catch (ArgumentException ex)
  137.             {
  138.                 throw new ArgumentException("Error reading propertybag: " + ex.Message);
  139.             }
  140.             catch (Exception ex)
  141.             {
  142.                 throw new ApplicationException("Error reading propertybag: " + ex.Message);
  143.             }
  144.  
  145.             if (val != null)
  146.                 _BatchSize = (int)val;
  147.             else
  148.                 _BatchSize = 1;
  149.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: BatchSize: " + BatchSize.ToString()); // Debug
  150.  
  151.         }
  152.  
  153.         private object ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName)
  154.         {
  155.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: ReadPropertyBag, propName: " + propName); // Debug
  156.             object val = null;
  157.             try
  158.             {
  159.                 pb.Read(propName, out val, 0);
  160.             }
  161.             catch (System.ArgumentException)
  162.             {
  163.                 return val;
  164.             }
  165.             catch (System.Exception e)
  166.             {
  167.                 throw new System.ApplicationException(e.Message);
  168.             }
  169.             return val;
  170.         }
  171.  
  172.         /// <summary>
  173.         /// Write property to property bag
  174.         /// </summary>
  175.         public virtual void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
  176.         {
  177.             object val = (object)BatchSize;
  178.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: Save: BatchSize " + val.ToString()); // Debug
  179.             propertyBag.Write("BatchSize", ref val);
  180.         }
  181.  
  182.         /// <summary>
  183.         /// Disassembles (breaks) message into small messages as per batch size
  184.         /// </summary>
  185.         public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
  186.         {
  187.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: Disassemble Start"); // Debug
  188.             string originalDataString;
  189.             try
  190.             {
  191.                 //fetch original message
  192.                 Stream originalMessageStream = pInMsg.BodyPart.GetOriginalDataStream();
  193.                 byte[] bufferOriginalMessage = new byte[originalMessageStream.Length];
  194.                 originalMessageStream.Read(bufferOriginalMessage, 0, Convert.ToInt32(originalMessageStream.Length));
  195.                 originalDataString = System.Text.ASCIIEncoding.ASCII.GetString(bufferOriginalMessage);
  196.  
  197.                 System.Diagnostics.Debugger.Log(0, null, "DEBUG: originalDataString: " + originalDataString); // Debug
  198.             }
  199.             catch (Exception ex)
  200.             {
  201.                 throw new ApplicationException("Error in reading original message: " + ex.Message);
  202.             }
  203.  
  204.             XmlDocument originalMessageDoc = new XmlDocument();
  205.             StringBuilder messageString;
  206.             try
  207.             {
  208.                 //load original message
  209.                 originalMessageDoc.LoadXml(originalDataString);
  210.  
  211.                 System.Diagnostics.Debugger.Log(0, null, "DEBUG: originalMessageDoc: " + originalMessageDoc.ToString()); // Debug
  212.  
  213.                 //fetch namespace and root element
  214.                 string namespaceURI = originalMessageDoc.DocumentElement.NamespaceURI;
  215.                 string rootElement = originalMessageDoc.DocumentElement.Name;
  216.  
  217.                 //start batching messages
  218.                 int counter = 0;
  219.  
  220.                 messageString = new StringBuilder();
  221.                 messageString.Append("<" + rootElement + " xmlns:ns0='" + namespaceURI + "'>");
  222.                 foreach (XmlNode childNode in originalMessageDoc.DocumentElement.ChildNodes)
  223.                 {
  224.                     counter = counter + 1;
  225.                     System.Diagnostics.Debugger.Log(0, null, "DEBUG: counter: " + counter); // Debug
  226.                     if (counter > BatchSize)
  227.                     {
  228.                         messageString.Append("</" + rootElement + ">");
  229.  
  230.                         //Queue message
  231.                         CreateOutgoingMessage(pContext, messageString.ToString(), namespaceURI, rootElement);
  232.  
  233.                         counter = 1;
  234.                         messageString.Remove(0, messageString.Length);
  235.                         messageString.Append("<" + rootElement + " xmlns:ns0='" + namespaceURI + "'>");
  236.                         messageString.Append(childNode.OuterXml);
  237.                     }
  238.                     else
  239.                     {
  240.                         messageString.Append(childNode.OuterXml);
  241.                     }
  242.                     System.Diagnostics.Debugger.Log(0, null, "DEBUG: foreach--messageString: " + messageString); // Debug
  243.                 }
  244.  
  245.                 messageString.Append("</" + rootElement + ">");
  246.                 CreateOutgoingMessage(pContext, messageString.ToString(), namespaceURI, rootElement);
  247.             }
  248.             catch (Exception ex)
  249.             {
  250.                 throw new ApplicationException("Error in writing outgoing messages: " + ex.Message);
  251.             }
  252.             finally
  253.             {
  254.                 messageString = null;
  255.                 originalMessageDoc = null;
  256.             }
  257.  
  258.         }
  259.  
  260.         /// <summary>
  261.         /// Used to pass output messages`to next stage
  262.         /// </summary>
  263.         public IBaseMessage GetNext(IPipelineContext pContext)
  264.         {
  265.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: GetNext"); // Debug
  266.             if (qOutputMsgs.Count > 0)
  267.                 return (IBaseMessage)qOutputMsgs.Dequeue();
  268.             else
  269.                 return null;
  270.         }
  271.  
  272.         /// <summary>
  273.         /// Queue outgoing messages
  274.         /// </summary>
  275.         private void CreateOutgoingMessage(IPipelineContext pContext, String messageString, string namespaceURI, string rootElement)
  276.         {
  277.             System.Diagnostics.Debugger.Log(0, null, "DEBUG: CreateOutgoingMessage"); // Debug
  278.             IBaseMessage outMsg;
  279.  
  280.             try
  281.             {
  282.  
  283.                 System.Diagnostics.Debugger.Log(0, null, "DEBUG: messageString: " + messageString); // Debug
  284.  
  285.                 //create outgoing message
  286.                 outMsg = pContext.GetMessageFactory().CreateMessage();
  287.                 outMsg.AddPart("Body", pContext.GetMessageFactory().CreateMessagePart(), true);
  288.                 outMsg.Context.Promote("MessageType", systemPropertiesNamespace, namespaceURI + "#" + rootElement.Replace("ns0:", ""));
  289.  
  290.                 byte[] bufferOoutgoingMessage = System.Text.ASCIIEncoding.ASCII.GetBytes(messageString);
  291.                 outMsg.BodyPart.Data = new MemoryStream(bufferOoutgoingMessage);
  292.  
  293.                 System.Diagnostics.Debugger.Log(0, null, "DEBUG: outMsg: " + outMsg.ToString()); // Debug
  294.  
  295.                 qOutputMsgs.Enqueue(outMsg);
  296.             }
  297.             catch (Exception ex)
  298.             {
  299.                 throw new ApplicationException("Error in queueing outgoing messages: " + ex.Message);
  300.             }
  301.         }
  302.  
  303.  
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement