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    package org.apache.activemq.transport.xstream;
018    
019    import java.io.IOException;
020    import java.io.Reader;
021    
022    import com.thoughtworks.xstream.XStream;
023    import org.apache.activemq.command.Command;
024    import org.apache.activemq.command.MarshallAware;
025    import org.apache.activemq.command.MessageDispatch;
026    import org.apache.activemq.transport.util.TextWireFormat;
027    import org.apache.activemq.wireformat.WireFormat;
028    
029    /**
030     * A {@link WireFormat} implementation which uses the <a
031     * href="http://xstream.codehaus.org/>XStream</a> library to marshall commands
032     * onto the wire
033     *
034     *
035     */
036    public class XStreamWireFormat extends TextWireFormat {
037        private XStream xStream;
038        private int version;
039    
040        public int getVersion() {
041            return version;
042        }
043    
044        public void setVersion(int version) {
045            this.version = version;
046        }
047    
048        public WireFormat copy() {
049            return new XStreamWireFormat();
050        }
051    
052        public Object unmarshalText(String text) {
053            return (Command)getXStream().fromXML(text);
054        }
055    
056        public Object unmarshalText(Reader reader) {
057            return (Command)getXStream().fromXML(reader);
058        }
059    
060        public String marshalText(Object command) throws IOException {
061            if (command instanceof MarshallAware) {
062                ((MarshallAware)command).beforeMarshall(this);
063            } else if(command instanceof MessageDispatch) {
064                MessageDispatch dispatch = (MessageDispatch) command;
065                if (dispatch != null && dispatch.getMessage() != null) {
066                    dispatch.getMessage().beforeMarshall(this);
067                }
068            }
069    
070            return getXStream().toXML(command);
071        }
072    
073        /**
074         * Can this wireformat process packets of this version
075         *
076         * @param version the version number to test
077         * @return true if can accept the version
078         */
079        public boolean canProcessWireFormatVersion(int version) {
080            return true;
081        }
082    
083        /**
084         * @return the current version of this wire format
085         */
086        public int getCurrentWireFormatVersion() {
087            return 1;
088        }
089    
090        // Properties
091        // -------------------------------------------------------------------------
092        public XStream getXStream() {
093            if (xStream == null) {
094                xStream = createXStream();
095                // make it work in OSGi env
096                xStream.setClassLoader(getClass().getClassLoader());
097            }
098            return xStream;
099        }
100    
101        public void setXStream(XStream xStream) {
102            this.xStream = xStream;
103        }
104    
105        // Implementation methods
106        // -------------------------------------------------------------------------
107        protected XStream createXStream() {
108            return new XStream();
109        }
110    
111    }