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.transport.xstream;
    19
    20  import com.thoughtworks.xstream.XStream;
    21  import org.codehaus.activemq.message.Packet;
    22  import org.codehaus.activemq.message.TextWireFormat;
    23  import org.codehaus.activemq.message.WireFormat;
    24
    25  import javax.jms.JMSException;
    26  import java.io.DataInput;
    27  import java.io.DataOutput;
    28  import java.io.IOException;
    29
    30  /***
    31   * A {@link WireFormat} implementation which uses the
    32   * <a href="http://xstream.codehaus.org/>XStream</a> library to marshall
    33   * commands onto the wire
    34   *
    35   * @version $Revision: 1.2 $
    36   */
    37  public class XStreamWireFormat extends TextWireFormat {
    38      private XStream xStream;
    39
    40      public Packet readPacket(DataInput in) throws IOException {
    41          String text = in.readUTF();
    42          return (Packet) getXStream().fromXML(text);
    43      }
    44
    45      public Packet readPacket(int firstByte, DataInput in) throws IOException {
    46          String text = in.readUTF();
    47          return (Packet) getXStream().fromXML(text);
    48      }
    49
    50      public void writePacket(Packet packet, DataOutput out) throws IOException, JMSException {
    51          String text = getXStream().toXML(packet);
    52          out.writeUTF(text);
    53      }
    54
    55      public WireFormat copy() {
    56          return new XStreamWireFormat();
    57      }
    58
    59      public String toString(Packet packet) {
    60          return getXStream().toXML(packet);
    61      }
    62
    63      public Packet fromString(String xml) {
    64          return (Packet) getXStream().fromXML(xml);
    65      }
    66
    67      /***
    68       * Can this wireformat process packets of this version
    69       * @param version the version number to test
    70       * @return true if can accept the version
    71       */
    72      public boolean canProcessWireFormatVersion(int version){
    73          return true;
    74      }
    75
    76      /***
    77       * @return the current version of this wire format
    78       */
    79      public int getCurrentWireFormatVersion(){
    80          return 1;
    81      }
    82
    83      // Properties
    84      //-------------------------------------------------------------------------
    85      public XStream getXStream() {
    86          if (xStream == null) {
    87              xStream = createXStream();
    88          }
    89          return xStream;
    90      }
    91
    92      public void setXStream(XStream xStream) {
    93          this.xStream = xStream;
    94      }
    95
    96      // Implementation methods
    97      //-------------------------------------------------------------------------
    98      protected XStream createXStream() {
    99          return new XStream();
    100     }
    101 }