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.10 $
    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       * @param message
    63       * @return an ActiveMQMessage
    64       * @throws JMSException if an error occurs
    65       */
    66      public static final ActiveMQMessage transformMessage(Message message) throws JMSException {
    67          if (message instanceof ActiveMQMessage) {
    68              return (ActiveMQMessage) message;
    69          }
    70          else {
    71              ActiveMQMessage activeMessage = null;
    72              if (message instanceof ObjectMessage) {
    73                  javax.jms.ObjectMessage objMsg = (javax.jms.ObjectMessage) message;
    74                  ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
    75                  msg.setObject(objMsg.getObject());
    76                  activeMessage = msg;
    77              }
    78              else if (message instanceof TextMessage) {
    79                  javax.jms.TextMessage textMsg = (javax.jms.TextMessage) message;
    80                  ActiveMQTextMessage msg = new ActiveMQTextMessage();
    81                  msg.setText(textMsg.getText());
    82                  activeMessage = msg;
    83              }
    84              else if (message instanceof MapMessage) {
    85                  javax.jms.MapMessage mapMsg = (javax.jms.MapMessage) message;
    86                  ActiveMQMapMessage msg = new ActiveMQMapMessage();
    87                  for (Enumeration iter = mapMsg.getMapNames(); iter.hasMoreElements();) {
    88                      String name = iter.nextElement().toString();
    89                      msg.setObject(name, mapMsg.getObject(name));
    90                  }
    91                  activeMessage = msg;
    92              }
    93              else if (message instanceof BytesMessage) {
    94                  javax.jms.BytesMessage bytesMsg = (javax.jms.BytesMessage) message;
    95                  bytesMsg.reset();
    96                  ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    97                  try {
    98                      for (; ;) {
    99                          msg.writeByte(bytesMsg.readByte());
    100                     }
    101                 }
    102                 catch (JMSException e) {
    103                 }
    104                 activeMessage = msg;
    105             }
    106             else if (message instanceof StreamMessage) {
    107                 javax.jms.StreamMessage streamMessage = (javax.jms.StreamMessage) message;
    108                 streamMessage.reset();
    109                 ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    110                 Object obj = null;
    111                 try {
    112                     while ((obj = streamMessage.readObject()) != null) {
    113                         msg.writeObject(obj);
    114                     }
    115                 }
    116                 catch (JMSException e) {
    117                 }
    118                 activeMessage = msg;
    119             }
    120             else {
    121                 activeMessage = new ActiveMQMessage();
    122             }
    123             activeMessage.setJMSMessageID(message.getJMSMessageID());
    124             activeMessage.setJMSCorrelationID(message.getJMSCorrelationID());
    125             activeMessage.setJMSReplyTo(transformDestination(message.getJMSReplyTo()));
    126             activeMessage.setJMSDestination(transformDestination(message.getJMSDestination()));
    127             activeMessage.setJMSDeliveryMode(message.getJMSDeliveryMode());
    128             activeMessage.setJMSRedelivered(message.getJMSRedelivered());
    129             activeMessage.setJMSType(message.getJMSType());
    130             activeMessage.setJMSExpiration(message.getJMSExpiration());
    131             activeMessage.setJMSPriority(message.getJMSPriority());
    132             activeMessage.setJMSTimestamp(message.getJMSTimestamp());
    133             for (Enumeration propertyNames = message.getPropertyNames(); propertyNames.hasMoreElements();) {
    134                 String name = propertyNames.nextElement().toString();
    135                 Object obj = message.getObjectProperty(name);
    136                 activeMessage.setObjectProperty(name, obj);
    137             }
    138             return activeMessage;
    139         }
    140     }
    141 }