KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > xstream > XStreamWireFormat


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.xstream;
19
20 import java.io.Reader JavaDoc;
21
22 import org.apache.activemq.command.Command;
23 import org.apache.activemq.transport.util.TextWireFormat;
24 import org.apache.activemq.wireformat.WireFormat;
25
26 import com.thoughtworks.xstream.XStream;
27
28 /**
29  * A {@link WireFormat} implementation which uses the <a
30  * HREF="http://xstream.codehaus.org/>XStream</a> library to marshall commands
31  * onto the wire
32  *
33  * @version $Revision$
34  */

35 public class XStreamWireFormat extends TextWireFormat {
36     private XStream xStream;
37     private int version;
38
39     public int getVersion() {
40         return version;
41     }
42
43     public void setVersion(int version) {
44         this.version = version;
45     }
46
47
48     public WireFormat copy() {
49         return new XStreamWireFormat();
50     }
51
52
53     public Object JavaDoc unmarshalText(String JavaDoc text) {
54         return (Command) getXStream().fromXML(text);
55     }
56     
57     public Object JavaDoc unmarshalText(Reader JavaDoc reader) {
58         return (Command) getXStream().fromXML(reader);
59     }
60
61     public String JavaDoc marshalText(Object JavaDoc command) {
62         return getXStream().toXML(command);
63     }
64
65     /**
66      * Can this wireformat process packets of this version
67      *
68      * @param version
69      * 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
102     
103 }
104
Popular Tags