KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > jms > JmsMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.jms;
18
19 import org.apache.servicemix.jbi.jaxp.SourceMarshaler;
20
21 import javax.jbi.messaging.MessagingException;
22 import javax.jbi.messaging.NormalizedMessage;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27 import javax.xml.transform.Source JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * Marshalls JMS messages into and out of NMS messages
35  *
36  * @version $Revision: 426415 $
37  */

38 public class JmsMarshaler {
39     private SourceMarshaler sourceMarshaler;
40     
41     private boolean needJavaIdentifiers;
42
43     public JmsMarshaler() {
44         this(new SourceMarshaler());
45     }
46
47     public JmsMarshaler(SourceMarshaler sourceMarshaler) {
48         this.sourceMarshaler = sourceMarshaler;
49     }
50
51     /**
52      * Marshalls the JMS message into an NMS message
53      *
54      * @throws MessagingException
55      */

56     public void toNMS(NormalizedMessage normalizedMessage, Message message) throws JMSException JavaDoc, MessagingException {
57         addNmsProperties(normalizedMessage, message);
58         if (message instanceof TextMessage JavaDoc) {
59             TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
60             Source JavaDoc source = sourceMarshaler.asSource(textMessage.getText());
61             normalizedMessage.setContent(source);
62         }
63
64         // lets add the message to the NMS
65
// Fix SM-178 : the message exchange is not serializable
66
//normalizedMessage.setProperty("org.apache.servicemix.jms.message", message);
67
}
68
69     public Message createMessage(NormalizedMessage normalizedMessage, Session JavaDoc session) throws JMSException JavaDoc, TransformerException JavaDoc {
70         // lets create a text message
71
String JavaDoc xml = messageAsString(normalizedMessage);
72         TextMessage JavaDoc message = session.createTextMessage(xml);
73         addJmsProperties(message, normalizedMessage);
74         return message;
75     }
76
77     // Properties
78
//-------------------------------------------------------------------------
79
public SourceMarshaler getSourceMarshaller() {
80         return sourceMarshaler;
81     }
82
83     public void setSourceMarshaller(SourceMarshaler sourceMarshaler) {
84         this.sourceMarshaler = sourceMarshaler;
85     }
86
87     // Implementation methods
88
//-------------------------------------------------------------------------
89

90     /**
91      * Converts the inbound message to a String that can be sent
92      */

93     protected String JavaDoc messageAsString(NormalizedMessage normalizedMessage) throws TransformerException JavaDoc {
94         return sourceMarshaler.asString(normalizedMessage.getContent());
95     }
96
97     /**
98      * Appends properties on the NMS to the JMS Message
99      */

100     protected void addJmsProperties(Message message, NormalizedMessage normalizedMessage) throws JMSException JavaDoc {
101         for (Iterator JavaDoc iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) {
102             String JavaDoc name = (String JavaDoc) iter.next();
103             Object JavaDoc value = normalizedMessage.getProperty(name);
104             if (shouldIncludeHeader(normalizedMessage, name, value)) {
105                 message.setObjectProperty(name, value);
106             }
107         }
108     }
109
110     protected void addNmsProperties(NormalizedMessage message, Message jmsMessage) throws JMSException JavaDoc {
111         Enumeration JavaDoc enumeration = jmsMessage.getPropertyNames();
112         while (enumeration.hasMoreElements()) {
113             String JavaDoc name = (String JavaDoc) enumeration.nextElement();
114             Object JavaDoc value = jmsMessage.getObjectProperty(name);
115             message.setProperty(name, value);
116         }
117     }
118
119     /**
120      * Decides whether or not the given header should be included in the JMS message.
121      * By default this includes all suitable typed values
122      */

123     protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String JavaDoc name, Object JavaDoc value) {
124         return (value instanceof String JavaDoc || value instanceof Number JavaDoc || value instanceof Date JavaDoc) &&
125                (!isNeedJavaIdentifiers() || isJavaIdentifier(name));
126     }
127     
128     private static boolean isJavaIdentifier(String JavaDoc s) {
129         int n = s.length();
130         if (n==0) return false;
131         if (!Character.isJavaIdentifierStart(s.charAt(0)))
132             return false;
133         for (int i = 1; i < n; i++)
134             if (!Character.isJavaIdentifierPart(s.charAt(i)))
135                 return false;
136         return true;
137       }
138
139     /**
140      * @return Returns the needJavaIdentifiers.
141      */

142     public boolean isNeedJavaIdentifiers() {
143         return needJavaIdentifiers;
144     }
145
146     /**
147      * @param needJavaIdentifiers The needJavaIdentifiers to set.
148      */

149     public void setNeedJavaIdentifiers(boolean needJavaIdentifiers) {
150         this.needJavaIdentifiers = needJavaIdentifiers;
151     }
152 }
153
Popular Tags