View Javadoc

1   /***
2    *
3    * Copyright 2004 Protique Ltd
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * 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   **/
18  package org.codehaus.activemq;
19  
20  import org.codehaus.activemq.message.*;
21  
22  import javax.jms.*;
23  import java.util.Enumeration;
24  
25  
26  /***
27   * Transforms foreign Destinations and Messages to ActiveMQ formats
28   *
29   * @version $Revision: 1.12 $
30   */
31  class ActiveMQMessageTransformation {
32      /***
33       * @param destination
34       * @return an ActiveMQDestination
35       * @throws JMSException if an error occurs
36       */
37      static ActiveMQDestination transformDestination(Destination destination) throws JMSException {
38          ActiveMQDestination result = null;
39          if (destination != null) {
40              if (destination instanceof ActiveMQDestination) {
41                  result = (ActiveMQDestination) destination;
42              }
43              else {
44                  if (destination instanceof TemporaryQueue) {
45                      result = new ActiveMQTemporaryQueue(((Queue) destination).getQueueName());
46                  }
47                  else if (destination instanceof TemporaryTopic) {
48                      result = new ActiveMQTemporaryTopic(((Topic) destination).getTopicName());
49                  }
50                  else if (destination instanceof Queue) {
51                      result = new ActiveMQQueue(((Queue) destination).getQueueName());
52                  }
53                  else if (destination instanceof Topic) {
54                      result = new ActiveMQTopic(((Topic) destination).getTopicName());
55                  }
56              }
57          }
58          return result;
59      }
60  
61      /***
62       * Creates a fast shallow copy of the current ActiveMQMessage or creates a whole new
63       * message instance from an available JMS message from another provider.
64       *
65       * @param message
66       * @return an ActiveMQMessage
67       * @throws JMSException if an error occurs
68       */
69      public static final ActiveMQMessage transformMessage(Message message) throws JMSException {
70          if (message instanceof ActiveMQMessage) {
71              ActiveMQMessage answer = ((ActiveMQMessage) message).shallowCopy();
72              // lets clear the cached stuff
73              answer.setJMSMessageIdentity(null);
74              return answer;
75          }
76          else {
77              ActiveMQMessage activeMessage = null;
78              if (message instanceof ObjectMessage) {
79                  ObjectMessage objMsg = (ObjectMessage) message;
80                  ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
81                  msg.setObject(objMsg.getObject());
82                  activeMessage = msg;
83              }
84              else if (message instanceof TextMessage) {
85                  TextMessage textMsg = (TextMessage) message;
86                  ActiveMQTextMessage msg = new ActiveMQTextMessage();
87                  msg.setText(textMsg.getText());
88                  activeMessage = msg;
89              }
90              else if (message instanceof MapMessage) {
91                  MapMessage mapMsg = (MapMessage) message;
92                  ActiveMQMapMessage msg = new ActiveMQMapMessage();
93                  for (Enumeration iter = mapMsg.getMapNames(); iter.hasMoreElements();) {
94                      String name = iter.nextElement().toString();
95                      msg.setObject(name, mapMsg.getObject(name));
96                  }
97                  activeMessage = msg;
98              }
99              else if (message instanceof BytesMessage) {
100                 BytesMessage bytesMsg = (BytesMessage) message;
101                 bytesMsg.reset();
102                 ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
103                 try {
104                     for (; ;) {
105                         msg.writeByte(bytesMsg.readByte());
106                     }
107                 }
108                 catch (JMSException e) {
109                 }
110                 activeMessage = msg;
111             }
112             else if (message instanceof StreamMessage) {
113                 StreamMessage streamMessage = (StreamMessage) message;
114                 streamMessage.reset();
115                 ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
116                 Object obj = null;
117                 try {
118                     while ((obj = streamMessage.readObject()) != null) {
119                         msg.writeObject(obj);
120                     }
121                 }
122                 catch (JMSException e) {
123                 }
124                 activeMessage = msg;
125             }
126             else {
127                 activeMessage = new ActiveMQMessage();
128             }
129             activeMessage.setJMSMessageID(message.getJMSMessageID());
130             activeMessage.setJMSCorrelationID(message.getJMSCorrelationID());
131             activeMessage.setJMSReplyTo(transformDestination(message.getJMSReplyTo()));
132             activeMessage.setJMSDestination(transformDestination(message.getJMSDestination()));
133             activeMessage.setJMSDeliveryMode(message.getJMSDeliveryMode());
134             activeMessage.setJMSRedelivered(message.getJMSRedelivered());
135             activeMessage.setJMSType(message.getJMSType());
136             activeMessage.setJMSExpiration(message.getJMSExpiration());
137             activeMessage.setJMSPriority(message.getJMSPriority());
138             activeMessage.setJMSTimestamp(message.getJMSTimestamp());
139             for (Enumeration propertyNames = message.getPropertyNames(); propertyNames.hasMoreElements();) {
140                 String name = propertyNames.nextElement().toString();
141                 Object obj = message.getObjectProperty(name);
142                 activeMessage.setObjectProperty(name, obj);
143             }
144             return activeMessage;
145         }
146     }
147 }