KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ActiveMQMessageTransformation


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

18 package org.apache.activemq;
19
20 import java.util.Enumeration JavaDoc;
21
22 import javax.jms.BytesMessage JavaDoc;
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.MessageEOFException JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.MapMessage JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.ObjectMessage JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.StreamMessage JavaDoc;
31 import javax.jms.TemporaryQueue JavaDoc;
32 import javax.jms.TemporaryTopic JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34 import javax.jms.Topic JavaDoc;
35
36
37 import org.apache.activemq.command.ActiveMQBytesMessage;
38 import org.apache.activemq.command.ActiveMQDestination;
39 import org.apache.activemq.command.ActiveMQMapMessage;
40 import org.apache.activemq.command.ActiveMQMessage;
41 import org.apache.activemq.command.ActiveMQObjectMessage;
42 import org.apache.activemq.command.ActiveMQQueue;
43 import org.apache.activemq.command.ActiveMQStreamMessage;
44 import org.apache.activemq.command.ActiveMQTempQueue;
45 import org.apache.activemq.command.ActiveMQTempTopic;
46 import org.apache.activemq.command.ActiveMQTextMessage;
47 import org.apache.activemq.command.ActiveMQTopic;
48
49 /**
50  * A helper class for converting normal JMS interfaces into ActiveMQ specific ones.
51  *
52  * @version $Revision: 1.1 $
53  */

54 public class ActiveMQMessageTransformation {
55
56     /**
57      * Creates a an available JMS message from another provider.
58      *
59      * @param destination - Destination to be converted into ActiveMQ's implementation.
60      * @return ActiveMQDestination - ActiveMQ's implementation of the destination.
61      * @throws JMSException if an error occurs
62      */

63     public static ActiveMQDestination transformDestination(Destination JavaDoc destination) throws JMSException JavaDoc {
64         ActiveMQDestination activeMQDestination = null;
65         
66         if (destination != null) {
67             if (destination instanceof ActiveMQDestination) {
68                 return (ActiveMQDestination) destination;
69                 
70             }
71             else {
72                 if (destination instanceof TemporaryQueue JavaDoc) {
73                     activeMQDestination = new ActiveMQTempQueue(((Queue JavaDoc) destination).getQueueName());
74                 }
75                 else if (destination instanceof TemporaryTopic JavaDoc) {
76                     activeMQDestination = new ActiveMQTempTopic(((Topic JavaDoc) destination).getTopicName());
77                 }
78                 else if (destination instanceof Queue JavaDoc) {
79                     activeMQDestination = new ActiveMQQueue(((Queue JavaDoc) destination).getQueueName());
80                 }
81                 else if (destination instanceof Topic JavaDoc) {
82                     activeMQDestination = new ActiveMQTopic(((Topic JavaDoc) destination).getTopicName());
83                 }
84             }
85         }
86
87         return activeMQDestination;
88     }
89     
90     
91     /**
92      * Creates a fast shallow copy of the current ActiveMQMessage or creates a whole new
93      * message instance from an available JMS message from another provider.
94      *
95      * @param message - Message to be converted into ActiveMQ's implementation.
96      * @param connection
97      * @return ActiveMQMessage - ActiveMQ's implementation object of the message.
98      * @throws JMSException if an error occurs
99      */

100     public static final ActiveMQMessage transformMessage(Message message, ActiveMQConnection connection) throws JMSException JavaDoc {
101         if (message instanceof ActiveMQMessage) {
102             return (ActiveMQMessage) message;
103
104         } else {
105             ActiveMQMessage activeMessage = null;
106
107             if (message instanceof BytesMessage JavaDoc) {
108                 BytesMessage JavaDoc bytesMsg = (BytesMessage JavaDoc) message;
109                 bytesMsg.reset();
110                 ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
111                 msg.setConnection(connection);
112                 try {
113                     for (;;) {
114                         // Reads a byte from the message stream until the stream
115
// is empty
116
msg.writeByte(bytesMsg.readByte());
117                     }
118                 } catch (MessageEOFException JavaDoc e) {
119                     // if an end of message stream as expected
120
} catch (JMSException JavaDoc e) {
121                 }
122
123                 activeMessage = msg;
124             } else if (message instanceof MapMessage JavaDoc) {
125                 MapMessage JavaDoc mapMsg = (MapMessage JavaDoc) message;
126                 ActiveMQMapMessage msg = new ActiveMQMapMessage();
127                 msg.setConnection(connection);
128                 Enumeration JavaDoc iter = mapMsg.getMapNames();
129
130                 while (iter.hasMoreElements()) {
131                     String JavaDoc name = iter.nextElement().toString();
132                     msg.setObject(name, mapMsg.getObject(name));
133                 }
134
135                 activeMessage = msg;
136             } else if (message instanceof ObjectMessage JavaDoc) {
137                 ObjectMessage JavaDoc objMsg = (ObjectMessage JavaDoc) message;
138                 ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
139                 msg.setConnection(connection);
140                 msg.setObject(objMsg.getObject());
141                 msg.storeContent();
142                 activeMessage = msg;
143             } else if (message instanceof StreamMessage JavaDoc) {
144                 StreamMessage JavaDoc streamMessage = (StreamMessage JavaDoc) message;
145                 streamMessage.reset();
146                 ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
147                 msg.setConnection(connection);
148                 Object JavaDoc obj = null;
149
150                 try {
151                     while ((obj = streamMessage.readObject()) != null) {
152                         msg.writeObject(obj);
153                     }
154                 } catch (MessageEOFException JavaDoc e) {
155                     // if an end of message stream as expected
156
} catch (JMSException JavaDoc e) {
157                 }
158
159                 activeMessage = msg;
160             } else if (message instanceof TextMessage JavaDoc) {
161                 TextMessage JavaDoc textMsg = (TextMessage JavaDoc) message;
162                 ActiveMQTextMessage msg = new ActiveMQTextMessage();
163                 msg.setConnection(connection);
164                 msg.setText(textMsg.getText());
165                 activeMessage = msg;
166             } else {
167                 activeMessage = new ActiveMQMessage();
168                 activeMessage.setConnection(connection);
169             }
170
171             copyProperties(message, activeMessage);
172
173             return activeMessage;
174         }
175     }
176
177     /**
178      * Copies the standard JMS and user defined properties from the givem message to the specified message
179      *
180      * @param fromMessage the message to take the properties from
181      * @param toMesage the message to add the properties to
182      * @throws JMSException
183      */

184     public static void copyProperties(Message fromMessage, Message toMesage) throws JMSException JavaDoc {
185         toMesage.setJMSMessageID(fromMessage.getJMSMessageID());
186         toMesage.setJMSCorrelationID(fromMessage.getJMSCorrelationID());
187         toMesage.setJMSReplyTo(transformDestination(fromMessage.getJMSReplyTo()));
188         toMesage.setJMSDestination(transformDestination(fromMessage.getJMSDestination()));
189         toMesage.setJMSDeliveryMode(fromMessage.getJMSDeliveryMode());
190         toMesage.setJMSRedelivered(fromMessage.getJMSRedelivered());
191         toMesage.setJMSType(fromMessage.getJMSType());
192         toMesage.setJMSExpiration(fromMessage.getJMSExpiration());
193         toMesage.setJMSPriority(fromMessage.getJMSPriority());
194         toMesage.setJMSTimestamp(fromMessage.getJMSTimestamp());
195
196         Enumeration JavaDoc propertyNames = fromMessage.getPropertyNames();
197
198         while (propertyNames.hasMoreElements()) {
199             String JavaDoc name = propertyNames.nextElement().toString();
200             Object JavaDoc obj = fromMessage.getObjectProperty(name);
201             toMesage.setObjectProperty(name, obj);
202         }
203     }
204 }
205
Popular Tags