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