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