001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.activemq.util.oxm;
019    
020    import java.io.Serializable;
021    import java.io.StringReader;
022    import java.io.StringWriter;
023    
024    import javax.jms.JMSException;
025    import javax.jms.ObjectMessage;
026    import javax.jms.Session;
027    import javax.jms.TextMessage;
028    
029    import com.thoughtworks.xstream.XStream;
030    import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
031    import com.thoughtworks.xstream.io.HierarchicalStreamReader;
032    import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
033    import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
034    import com.thoughtworks.xstream.io.xml.XppReader;
035    
036    /**
037     * Transforms object messages to text messages and vice versa using
038     * {@link XStream}
039     * 
040     */
041    public class XStreamMessageTransformer extends AbstractXMLMessageTransformer {
042    
043        private XStream xStream;
044        
045        /**
046         * Specialized driver to be used with stream readers and writers
047         */
048        private HierarchicalStreamDriver streamDriver;
049        
050        // Properties
051        // -------------------------------------------------------------------------
052        public XStream getXStream() {
053            if (xStream == null) {
054                xStream = createXStream();
055            }
056            return xStream;
057        }
058    
059        public void setXStream(XStream xStream) {
060            this.xStream = xStream;
061        }
062    
063        public HierarchicalStreamDriver getStreamDriver() {
064                    return streamDriver;
065            }
066    
067            public void setStreamDriver(HierarchicalStreamDriver streamDriver) {
068                    this.streamDriver = streamDriver;
069            }
070            
071            // Implementation methods
072        // -------------------------------------------------------------------------
073        protected XStream createXStream() {
074            return new XStream();
075        }   
076            
077        /**
078         * Marshalls the Object in the {@link ObjectMessage} to a string using XML
079         * encoding
080         */
081        protected String marshall(Session session, ObjectMessage objectMessage) throws JMSException {
082            Serializable object = objectMessage.getObject();
083            StringWriter buffer = new StringWriter();
084            HierarchicalStreamWriter out;
085            if (streamDriver != null) {
086                    out = streamDriver.createWriter(buffer);
087            } else {
088                    out = new PrettyPrintWriter(buffer);
089            }
090            getXStream().marshal(object, out);
091            return buffer.toString();
092        }
093    
094        /**
095         * Unmarshalls the XML encoded message in the {@link TextMessage} to an
096         * Object
097         */
098        protected Object unmarshall(Session session, TextMessage textMessage) throws JMSException {
099            HierarchicalStreamReader in;
100            if (streamDriver != null) {
101                    in = streamDriver.createReader(new StringReader(textMessage.getText()));
102            } else {
103                    in = new XppReader(new StringReader(textMessage.getText()));
104            }
105            return getXStream().unmarshal(in);
106        }
107    
108    }