SimpleMessageConverter.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Messaging » Nms » Support » Converter » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Messaging » Nms » Support » Converter » SimpleMessageConverter.cs
#region License

/*
 * Copyright  2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

using System;
using System.Collections;
using System.Runtime.Serialization;
using Apache.NMS;

namespace Spring.Messaging.Nms.Support.Converter{
    /// <summary> A simple message converter that can handle ITextMessages, IBytesMessages,
    /// IMapMessages, and IObjectMessages. Used as default by NmsTemplate, for
    /// <code>ConvertAndSend</code> and <code>ReceiveAndConvert</code> operations.
    ///
    /// <p>Converts a String to a NMS ITextMessage, a byte array to a NMS IBytesMessage,
    /// a Map to a NMS IMapMessage, and a Serializable object to a NMS IObjectMessage
    /// (or vice versa).</p>
    ///
    ///
    /// </summary>
    /// <author>Juergen Hoeller</author>
    /// <author>Mark Pollack (.NET)</author>
    public class SimpleMessageConverter : IMessageConverter
    {
        /// <summary> Convert a .NET object to a NMS Message using the supplied session
        /// to create the message object.
        /// </summary>
        /// <param name="objectToConvert">the object to convert
        /// </param>
        /// <param name="session">the Session to use for creating a NMS Message
        /// </param>
        /// <returns> the NMS Message
        /// </returns>
        /// <throws>NMSException if thrown by NMS API methods </throws>
        /// <throws>MessageConversionException in case of conversion failure </throws>
        public IMessage ToMessage(object objectToConvert, ISession session)
        {
            if (objectToConvert is IMessage)
            {
                return (IMessage) objectToConvert;
            }
            else if (objectToConvert is string)
            {
                return CreateMessageForString((string) objectToConvert, session);
            }
            else if (objectToConvert is sbyte[])
            {
                return CreateMessageForByteArray((byte[]) objectToConvert, session);
            }
            else if (objectToConvert is IDictionary)
            {
                return CreateMessageForMap((IDictionary) objectToConvert, session);
            }
            else if (objectToConvert != null && objectToConvert.GetType().IsSerializable)
            {
                return
                    CreateMessageForSerializable(objectToConvert, session);
            }
            else
            {
                throw new MessageConversionException("Cannot convert object [" + objectToConvert + "] to NMS message");
            }
        }

        /// <summary> Convert from a NMS Message to a .NET object.</summary>
        /// <param name="messageToConvert">the message to convert
        /// </param>
        /// <returns> the converted .NET object
        /// </returns>
        /// <throws>MessageConversionException in case of conversion failure </throws>
        public object FromMessage(IMessage messageToConvert)
        {
            if (messageToConvert is ITextMessage)
            {
                return ExtractStringFromMessage((ITextMessage) messageToConvert);
            }
            else if (messageToConvert is IBytesMessage)
            {
                return ExtractByteArrayFromMessage((IBytesMessage) messageToConvert);
            }
            else if (messageToConvert is IMapMessage)
            {
                return ExtractMapFromMessage((IMapMessage) messageToConvert);
            }
            else if (messageToConvert is IObjectMessage)
            {
                return ExtractSerializableFromMessage((IObjectMessage) messageToConvert);
            }
            else
            {
                return messageToConvert;
            }
        }

        #region To Converter Methods

        /// <summary> Create a NMS ITextMessage for the given String.</summary>
        /// <param name="text">the String to convert
        /// </param>
        /// <param name="session">current NMS session
        /// </param>
        /// <returns> the resulting message
        /// </returns>
        /// <throws>  NMSException if thrown by NMS methods </throws>
        protected virtual ITextMessage CreateMessageForString(string text, ISession session)
        {
            return session.CreateTextMessage((text));
        }

        /// <summary> Create a NMS IBytesMessage for the given byte array.</summary>
        /// <param name="bytes">the byyte array to convert
        /// </param>
        /// <param name="session">current NMS session
        /// </param>
        /// <returns> the resulting message
        /// </returns>
        /// <throws>  NMSException if thrown by NMS methods </throws>
        protected virtual IBytesMessage CreateMessageForByteArray(byte[] bytes, ISession session)
        {
            IBytesMessage message = session.CreateBytesMessage();
            message.Content = bytes;
            return message;
        }

        /// <summary> Create a NMS IMapMessage for the given Map.</summary>
        /// <param name="map">the Map to convert
        /// </param>
        /// <param name="session">current NMS session
        /// </param>
        /// <returns> the resulting message
        /// </returns>
        /// <throws>  NMSException if thrown by NMS methods </throws>
        protected virtual IMapMessage CreateMessageForMap(IDictionary map, ISession session)
        {
            IMapMessage mapMessage = session.CreateMapMessage();
            foreach (DictionaryEntry entry in map)
            {
                if (!(entry.Key is string))
                {
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Class.getName' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    throw new MessageConversionException("Cannot convert non-String key of type [" +
                                                         (entry.Key != null ? entry.Key.GetType().FullName : null) +
                                                         "] to IMapMessage entry");
                }
                mapMessage.Body[entry.Key.ToString()] = entry.Value;
            }
            return mapMessage;
        }

        /// <summary> Create a NMS IObjectMessage for the given Serializable object.</summary>
        /// <param name="objectToSend">the Serializable object to convert
        /// </param>
        /// <param name="session">current NMS session
        /// </param>
        /// <returns> the resulting message
        /// </returns>
        /// <throws>  NMSException if thrown by NMS methods </throws>
        protected virtual IObjectMessage CreateMessageForSerializable(
            object objectToSend, ISession session)
        {
            return session.CreateObjectMessage(objectToSend);
        }

        #endregion

        #region From Converter Mehtods

        /// <summary> Extract a String from the given ITextMessage.</summary>
        /// <param name="message">the message to convert
        /// </param>
        /// <returns> the resulting String
        /// </returns>
        /// <throws>  NMSException if thrown by NMS methods </throws>
        protected virtual string ExtractStringFromMessage(ITextMessage message)
        {
            return message.Text;
        }

        /// <summary> Extract a byte array from the given IBytesMessage.</summary>
        /// <param name="message">the message to convert
        /// </param>
        /// <returns> the resulting byte array
        /// </returns>
        /// <throws>  NMSException if thrown by NMS methods </throws>
        protected virtual byte[] ExtractByteArrayFromMessage(IBytesMessage message)
        {
      return message.Content;
        }

        /// <summary> Extract a IDictionary from the given IMapMessage.</summary>
        /// <param name="message">the message to convert
        /// </param>
        /// <returns> the resulting Map
        /// </returns>
        /// <throws>NMSException if thrown by NMS methods </throws>
        protected virtual IDictionary ExtractMapFromMessage(IMapMessage message)
        {
            IDictionary dictionary = new Hashtable();
            IEnumerator e = message.Body.Keys.GetEnumerator();
            while (e.MoveNext())
            {
                String key = (String) e.Current;
                dictionary.Add(key, message.Body[key]);
            }
            return dictionary;
        }

        /// <summary>
        /// Extracts the serializable object from the given object message.
        /// </summary>
        /// <param name="message">The message to convert.</param>
        /// <returns>The resulting serializable object.</returns>
        protected virtual object ExtractSerializableFromMessage(
            IObjectMessage message)
        {
            return message.Body;
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.