Home » activemq-parent-5.3.1-source-release » org.apache » activemq » util » xstream » [javadoc | source]

    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.activemq.util.xstream;
   18   
   19   import java.io.Serializable;
   20   import java.io.StringReader;
   21   import java.io.StringWriter;
   22   
   23   import javax.jms.JMSException;
   24   import javax.jms.Message;
   25   import javax.jms.MessageConsumer;
   26   import javax.jms.MessageProducer;
   27   import javax.jms.ObjectMessage;
   28   import javax.jms.Session;
   29   import javax.jms.TextMessage;
   30   
   31   import org.apache.activemq.MessageTransformerSupport;
   32   
   33   import com.thoughtworks.xstream.XStream;
   34   import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
   35   import com.thoughtworks.xstream.io.HierarchicalStreamReader;
   36   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
   37   import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
   38   import com.thoughtworks.xstream.io.xml.XppReader;
   39   
   40   /**
   41    * Transforms object messages to text messages and vice versa using
   42    * {@link XStream}
   43    * 
   44    * @deprecated as of 5.3.0 release replaced by {@link org.apache.activemq.util.oxm.XStreamMessageTransformer}
   45    * 
   46    * @version $Revision: 814586 $
   47    */
   48   @Deprecated
   49   public class XStreamMessageTransformer extends MessageTransformerSupport {
   50   
   51       protected MessageTransform transformType;
   52       private XStream xStream;
   53       
   54       /**
   55        * Specialized driver to be used with stream readers and writers
   56        */
   57       private HierarchicalStreamDriver streamDriver;
   58   
   59       /**
   60        * Defines the type of transformation. If XML (default), - producer
   61        * transformation transforms from Object to XML. - consumer transformation
   62        * transforms from XML to Object. If OBJECT, - producer transformation
   63        * transforms from XML to Object. - consumer transformation transforms from
   64        * Object to XML. If ADAPTIVE, - producer transformation transforms from
   65        * Object to XML, or XML to Object depending on the type of the original
   66        * message - consumer transformation transforms from XML to Object, or
   67        * Object to XML depending on the type of the original message
   68        */
   69       public enum MessageTransform {
   70           XML, OBJECT, ADAPTIVE
   71       };
   72   
   73   
   74       public XStreamMessageTransformer() {
   75           this(MessageTransform.XML);
   76       }
   77   
   78       public XStreamMessageTransformer(MessageTransform transformType) {
   79           this.transformType = transformType;
   80       }
   81   
   82       public Message consumerTransform(Session session, MessageConsumer consumer, Message message) throws JMSException {
   83           switch (transformType) {
   84           case XML:
   85               return (message instanceof TextMessage) ? textToObject(session, (TextMessage)message) : message;
   86           case OBJECT:
   87               return (message instanceof ObjectMessage) ? objectToText(session, (ObjectMessage)message) : message;
   88           case ADAPTIVE:
   89               return (message instanceof TextMessage) ? textToObject(session, (TextMessage)message) : (message instanceof ObjectMessage) ? objectToText(session, (ObjectMessage)message) : message;
   90           default:
   91           }
   92           return message;
   93       }
   94   
   95       public Message producerTransform(Session session, MessageProducer producer, Message message) throws JMSException {
   96           switch (transformType) {
   97           case XML:
   98               return (message instanceof ObjectMessage) ? objectToText(session, (ObjectMessage)message) : message;
   99           case OBJECT:
  100               return (message instanceof TextMessage) ? textToObject(session, (TextMessage)message) : message;
  101           case ADAPTIVE:
  102               return (message instanceof TextMessage) ? textToObject(session, (TextMessage)message) : (message instanceof ObjectMessage) ? objectToText(session, (ObjectMessage)message) : message;
  103           default:
  104           }
  105           return message;
  106       }
  107   
  108       // Properties
  109       // -------------------------------------------------------------------------
  110       public XStream getXStream() {
  111           if (xStream == null) {
  112               xStream = createXStream();
  113           }
  114           return xStream;
  115       }
  116   
  117       public void setXStream(XStream xStream) {
  118           this.xStream = xStream;
  119       }
  120   
  121       public HierarchicalStreamDriver getStreamDriver() {
  122   		return streamDriver;
  123       }
  124   
  125   	public void setStreamDriver(HierarchicalStreamDriver streamDriver) {
  126   		this.streamDriver = streamDriver;
  127       }
  128   
  129   	// Implementation methods
  130       // -------------------------------------------------------------------------
  131       protected XStream createXStream() {
  132           return new XStream();
  133       }
  134   
  135       public MessageTransform getTransformType() {
  136           return transformType;
  137       }
  138   
  139       public void setTransformType(MessageTransform transformType) {
  140           this.transformType = transformType;
  141       }
  142   
  143       /**
  144        * Transforms an incoming XML encoded {@link TextMessage} to an
  145        * {@link ObjectMessage}
  146        * 
  147        * @param session - JMS session currently being used
  148        * @param textMessage - text message to transform to object message
  149        * @return ObjectMessage
  150        * @throws JMSException
  151        */
  152       protected ObjectMessage textToObject(Session session, TextMessage textMessage) throws JMSException {
  153           Object object = unmarshall(session, textMessage);
  154           if (object instanceof Serializable) {
  155               ObjectMessage answer = session.createObjectMessage((Serializable)object);
  156               copyProperties(textMessage, answer);
  157               return answer;
  158           } else {
  159               throw new JMSException("Object is not serializable: " + object);
  160           }
  161       }
  162   
  163       /**
  164        * Transforms an incoming {@link ObjectMessage} to an XML encoded
  165        * {@link TextMessage}
  166        * 
  167        * @param session - JMS session currently being used
  168        * @param objectMessage - object message to transform to text message
  169        * @return XML encoded TextMessage
  170        * @throws JMSException
  171        */
  172       protected TextMessage objectToText(Session session, ObjectMessage objectMessage) throws JMSException {
  173           TextMessage answer = session.createTextMessage(marshall(session, objectMessage));
  174           copyProperties(objectMessage, answer);
  175           return answer;
  176       }
  177   
  178       /**
  179        * Marshalls the Object in the {@link ObjectMessage} to a string using XML
  180        * encoding
  181        */
  182       protected String marshall(Session session, ObjectMessage objectMessage) throws JMSException {
  183           Serializable object = objectMessage.getObject();
  184           StringWriter buffer = new StringWriter();
  185           HierarchicalStreamWriter out;
  186           if (streamDriver != null) {
  187           	out = streamDriver.createWriter(buffer);
  188           } else {
  189           	out = new PrettyPrintWriter(buffer);
  190           }
  191           getXStream().marshal(object, out);
  192           return buffer.toString();
  193       }
  194   
  195       /**
  196        * Unmarshalls the XML encoded message in the {@link TextMessage} to an
  197        * Object
  198        */
  199       protected Object unmarshall(Session session, TextMessage textMessage) throws JMSException {
  200           HierarchicalStreamReader in;
  201           if (streamDriver != null) {
  202           	in = streamDriver.createReader(new StringReader(textMessage.getText()));
  203           } else {
  204           	in = new XppReader(new StringReader(textMessage.getText()));
  205           }
  206           return getXStream().unmarshal(in);
  207       }
  208   
  209   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » util » xstream » [javadoc | source]