KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > xstream > XStreamMessageTransformer


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.util.xstream;
19
20 import com.thoughtworks.xstream.XStream;
21 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
22 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
23 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
24 import com.thoughtworks.xstream.io.xml.XppReader;
25 import org.apache.activemq.MessageTransformerSupport;
26
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.jms.MessageConsumer JavaDoc;
30 import javax.jms.MessageProducer JavaDoc;
31 import javax.jms.ObjectMessage JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.io.StringReader JavaDoc;
36 import java.io.StringWriter JavaDoc;
37
38 /**
39  * Transforms object messages to text messages using {@link XStream}
40  *
41  * @version $Revision: 475701 $
42  */

43 public class XStreamMessageTransformer extends MessageTransformerSupport {
44     private XStream xStream;
45
46
47     public Message JavaDoc producerTransform(Session JavaDoc session, MessageProducer JavaDoc producer, Message JavaDoc message) throws JMSException JavaDoc {
48         if (message instanceof ObjectMessage JavaDoc) {
49             TextMessage JavaDoc answer = session.createTextMessage(marshall(session, producer, (ObjectMessage JavaDoc) message));
50             copyProperties(message, answer);
51             return answer;
52         }
53         return message;
54     }
55
56
57     public Message JavaDoc consumerTransform(Session JavaDoc session, MessageConsumer JavaDoc consumer, Message JavaDoc message) throws JMSException JavaDoc {
58         if (message instanceof TextMessage JavaDoc) {
59             TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
60             Object JavaDoc object = unmarshall(session, consumer, textMessage);
61             if (object instanceof Serializable JavaDoc) {
62                 ObjectMessage JavaDoc answer = session.createObjectMessage((Serializable JavaDoc) object);
63                 copyProperties(message, answer);
64                 return answer;
65             }
66             else {
67                 throw new JMSException JavaDoc("Object is not serializable: " + object);
68             }
69         }
70         return message;
71     }
72
73
74     // Properties
75
// -------------------------------------------------------------------------
76
public XStream getXStream() {
77         if (xStream == null) {
78             xStream = createXStream();
79         }
80         return xStream;
81     }
82
83     public void setXStream(XStream xStream) {
84         this.xStream = xStream;
85     }
86
87     // Implementation methods
88
// -------------------------------------------------------------------------
89
protected XStream createXStream() {
90         return new XStream();
91     }
92
93     /**
94      * Marshalls the Object in the {@link ObjectMessage} to a string using XML encoding
95      */

96     protected String JavaDoc marshall(Session JavaDoc session, MessageProducer JavaDoc producer, ObjectMessage JavaDoc objectMessage) throws JMSException JavaDoc {
97         Serializable JavaDoc object = objectMessage.getObject();
98         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
99         HierarchicalStreamWriter out = new PrettyPrintWriter(buffer);
100         getXStream().marshal(object, out);
101         return buffer.toString();
102     }
103
104     /**
105      * Unmarshalls the Object using XML encoding of the String
106      */

107     protected Object JavaDoc unmarshall(Session JavaDoc session, MessageConsumer JavaDoc consumer, TextMessage JavaDoc textMessage) throws JMSException JavaDoc {
108         HierarchicalStreamReader in = new XppReader(new StringReader JavaDoc(textMessage.getText()));
109         return getXStream().unmarshal(in);
110     }
111
112 }
113
Popular Tags