KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jms > transformers > AbstractJmsTransformer


1 /*
2  * $Id: AbstractJmsTransformer.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jms.transformers;
12
13 import java.util.Iterator JavaDoc;
14
15 import javax.jms.Destination JavaDoc;
16 import javax.jms.JMSException JavaDoc;
17 import javax.jms.Message JavaDoc;
18 import javax.jms.Session JavaDoc;
19
20 import org.mule.config.MuleProperties;
21 import org.mule.impl.RequestContext;
22 import org.mule.providers.jms.JmsConnector;
23 import org.mule.providers.jms.JmsConstants;
24 import org.mule.providers.jms.JmsMessageUtils;
25 import org.mule.transformers.AbstractTransformer;
26 import org.mule.umo.UMOEventContext;
27 import org.mule.umo.UMOException;
28 import org.mule.umo.UMOMessage;
29 import org.mule.umo.endpoint.UMOImmutableEndpoint;
30 import org.mule.umo.provider.UMOConnector;
31 import org.mule.umo.provider.UMOMessageDispatcher;
32 import org.mule.umo.transformer.TransformerException;
33 import org.mule.util.StringUtils;
34
35 /**
36  * <code>AbstractJmsTransformer</code> is an abstract class that should be used for
37  * all transformers where a JMS message will be the transformed or transformee
38  * object. It provides services for compressing and uncompressing messages.
39  */

40
41 public abstract class AbstractJmsTransformer extends AbstractTransformer
42 {
43     public static final char REPLACEMENT_CHAR = '_';
44
45     /**
46      * Encode a String so that is is a valid Java identifier
47      *
48      * @param name the String to encode
49      * @return a valid JMS header name
50      */

51     public static String JavaDoc encodeHeader(String JavaDoc name)
52     {
53         if (StringUtils.isEmpty(name))
54         {
55             throw new IllegalArgumentException JavaDoc("Header name to encode must not be null or empty");
56         }
57
58         int i = 0, length = name.length();
59         while (i < length && Character.isJavaIdentifierPart(name.charAt(i)))
60         {
61             // zip through
62
i++;
63         }
64
65         if (i == length)
66         {
67             // String is already valid
68
return name;
69         }
70         else
71         {
72             // make a copy, fix up remaining characters
73
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
74             for (int j = i; j < length; j++)
75             {
76                 if (!Character.isJavaIdentifierPart(sb.charAt(j)))
77                 {
78                     sb.setCharAt(j, REPLACEMENT_CHAR);
79                 }
80             }
81             return sb.toString();
82         }
83     }
84
85     public AbstractJmsTransformer()
86     {
87         super();
88     }
89
90     protected Message JavaDoc transformToMessage(Object JavaDoc src) throws TransformerException
91     {
92         try
93         {
94             Message JavaDoc result;
95
96             if (src instanceof Message JavaDoc)
97             {
98                 result = (Message JavaDoc)src;
99                 result.clearProperties();
100             }
101             else
102             {
103                 result = JmsMessageUtils.toMessage(src, this.getSession());
104             }
105
106             // set the event properties on the Message
107
UMOEventContext ctx = RequestContext.getEventContext();
108             if (ctx == null)
109             {
110                 logger.warn("There is no current event context");
111                 return result;
112             }
113
114             this.setJmsProperties(ctx.getMessage(), result);
115
116             return result;
117         }
118         catch (Exception JavaDoc e)
119         {
120             throw new TransformerException(this, e);
121         }
122     }
123
124     protected Object JavaDoc transformFromMessage(Message JavaDoc source) throws TransformerException
125     {
126         try
127         {
128             if (logger.isDebugEnabled())
129             {
130                 logger.debug("Message type received is: " + source.getClass().getName());
131             }
132
133             // Try to figure out our endpoint's JMS Specification and fall back to
134
// 1.0.2 if none is set.
135
String JavaDoc jmsSpec = JmsConstants.JMS_SPECIFICATION_102B;
136             UMOImmutableEndpoint endpoint = this.getEndpoint();
137             if (endpoint != null)
138             {
139                 UMOConnector connector = endpoint.getConnector();
140                 if (connector instanceof JmsConnector)
141                 {
142                     jmsSpec = ((JmsConnector)connector).getSpecification();
143                 }
144             }
145
146             return JmsMessageUtils.toObject(source, jmsSpec);
147         }
148         catch (Exception JavaDoc e)
149         {
150             throw new TransformerException(this, e);
151         }
152     }
153
154     protected void setJmsProperties(UMOMessage umoMessage, Message JavaDoc msg) throws JMSException JavaDoc
155     {
156         for (Iterator JavaDoc iterator = umoMessage.getPropertyNames().iterator(); iterator.hasNext();)
157         {
158             String JavaDoc key = iterator.next().toString();
159
160             if (!JmsConstants.JMS_PROPERTY_NAMES.contains(key))
161             {
162                 Object JavaDoc value = umoMessage.getProperty(key);
163
164                 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(key))
165                 {
166                     msg.setJMSCorrelationID(umoMessage.getCorrelationId());
167                 }
168
169                 // We dont want to set the ReplyTo property again as it will be set
170
// using JMSReplyTo
171
if (!(MuleProperties.MULE_REPLY_TO_PROPERTY.equals(key) && value instanceof Destination JavaDoc))
172                 {
173                     try
174                     {
175                         msg.setObjectProperty(encodeHeader(key), value);
176                     }
177                     catch (JMSException JavaDoc e)
178                     {
179                         // Various Jms servers have slightly different rules to what
180
// can be set as an object property on the message
181
// As such we have to take a hit n' hope approach
182
if (logger.isDebugEnabled())
183                         {
184                             logger.debug("Unable to set property '" + encodeHeader(key) + "' of type "
185                                          + value.getClass().getName() + "': " + e.getMessage());
186                         }
187                     }
188                 }
189             }
190         }
191     }
192
193     protected Session JavaDoc getSession() throws UMOException
194     {
195         // The session can be closed together with the dispatcher, so it is more
196
// reliable to get it from the dispatcher each time
197
UMOImmutableEndpoint endpoint = this.getEndpoint();
198         if (endpoint != null)
199         {
200             UMOMessageDispatcher dispatcher = endpoint.getConnector().getDispatcher(endpoint);
201             return (Session JavaDoc)dispatcher.getDelegateSession();
202         }
203         else
204         {
205             throw new TransformerException(this, new IllegalStateException JavaDoc(
206                 "This transformer needs a valid endpoint"));
207         }
208     }
209
210 }
211
Popular Tags