| Home >> All >> org >> [ activemq Javadoc ] |
Source code: org/activemq/ActiveMQMessageTransformation.java
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.activemq; 19 20 import org.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.1.1.1 $ 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 return (ActiveMQMessage)message; 72 } 73 else { 74 ActiveMQMessage activeMessage = null; 75 if (message instanceof ObjectMessage) { 76 ObjectMessage objMsg = (ObjectMessage) message; 77 ActiveMQObjectMessage msg = new ActiveMQObjectMessage(); 78 msg.setObject(objMsg.getObject()); 79 activeMessage = msg; 80 } 81 else if (message instanceof TextMessage) { 82 TextMessage textMsg = (TextMessage) message; 83 ActiveMQTextMessage msg = new ActiveMQTextMessage(); 84 msg.setText(textMsg.getText()); 85 activeMessage = msg; 86 } 87 else if (message instanceof MapMessage) { 88 MapMessage mapMsg = (MapMessage) message; 89 ActiveMQMapMessage msg = new ActiveMQMapMessage(); 90 for (Enumeration iter = mapMsg.getMapNames(); iter.hasMoreElements();) { 91 String name = iter.nextElement().toString(); 92 msg.setObject(name, mapMsg.getObject(name)); 93 } 94 activeMessage = msg; 95 } 96 else if (message instanceof BytesMessage) { 97 BytesMessage bytesMsg = (BytesMessage) message; 98 bytesMsg.reset(); 99 ActiveMQBytesMessage msg = new ActiveMQBytesMessage(); 100 try { 101 for (; ;) { 102 msg.writeByte(bytesMsg.readByte()); 103 } 104 } 105 catch (JMSException e) { 106 } 107 activeMessage = msg; 108 } 109 else if (message instanceof StreamMessage) { 110 StreamMessage streamMessage = (StreamMessage) message; 111 streamMessage.reset(); 112 ActiveMQStreamMessage msg = new ActiveMQStreamMessage(); 113 Object obj = null; 114 try { 115 while ((obj = streamMessage.readObject()) != null) { 116 msg.writeObject(obj); 117 } 118 } 119 catch (JMSException e) { 120 } 121 activeMessage = msg; 122 } 123 else { 124 activeMessage = new ActiveMQMessage(); 125 } 126 activeMessage.setJMSMessageID(message.getJMSMessageID()); 127 activeMessage.setJMSCorrelationID(message.getJMSCorrelationID()); 128 activeMessage.setJMSReplyTo(transformDestination(message.getJMSReplyTo())); 129 activeMessage.setJMSDestination(transformDestination(message.getJMSDestination())); 130 activeMessage.setJMSDeliveryMode(message.getJMSDeliveryMode()); 131 activeMessage.setJMSRedelivered(message.getJMSRedelivered()); 132 activeMessage.setJMSType(message.getJMSType()); 133 activeMessage.setJMSExpiration(message.getJMSExpiration()); 134 activeMessage.setJMSPriority(message.getJMSPriority()); 135 activeMessage.setJMSTimestamp(message.getJMSTimestamp()); 136 for (Enumeration propertyNames = message.getPropertyNames(); propertyNames.hasMoreElements();) { 137 String name = propertyNames.nextElement().toString(); 138 Object obj = message.getObjectProperty(name); 139 activeMessage.setObjectProperty(name, obj); 140 } 141 return activeMessage; 142 } 143 } 144 }