View Javadoc

1   /***
2    *
3    * Copyright 2005 LogicBlaze, Inc.
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.logicblaze.lingo.jms.marshall;
19  
20  import com.thoughtworks.xstream.XStream;
21  
22  import org.logicblaze.lingo.LingoInvocation;
23  import org.logicblaze.lingo.jms.Requestor;
24  import org.springframework.remoting.support.RemoteInvocation;
25  import org.springframework.remoting.support.RemoteInvocationResult;
26  
27  import javax.jms.JMSException;
28  import javax.jms.Message;
29  import javax.jms.Session;
30  import javax.jms.TextMessage;
31  
32  /***
33   * Uses XStream to marshall requests and responses into and out of messages.
34   * 
35   * @version $Revision: 1.3 $
36   */
37  public class XStreamMarshaller extends DefaultMarshaller {
38      private XStream xStream;
39  
40      public Message createRequestMessage(Requestor requestor, LingoInvocation invocation) throws JMSException {
41          String xml = toXML(invocation);
42          return requestor.getSession().createTextMessage(xml);
43      }
44  
45      public Message createResponseMessage(Session session, RemoteInvocationResult result, Message requestMessage)
46              throws JMSException {
47          String xml = toXML(result);
48          TextMessage message = session.createTextMessage(xml);
49          appendMessageHeaders(message, session, result);
50          return message;
51      }
52  
53      public RemoteInvocationResult extractInvocationResult(Message message) throws JMSException {
54          if (message instanceof TextMessage) {
55              TextMessage textMessage = (TextMessage) message;
56              String text = textMessage.getText();
57              return (RemoteInvocationResult) fromXML(text);
58          }
59          return super.extractInvocationResult(message);
60      }
61  
62      public RemoteInvocation readRemoteInvocation(Message message) throws JMSException {
63          if (message instanceof TextMessage) {
64              TextMessage textMessage = (TextMessage) message;
65              String text = textMessage.getText();
66              return (RemoteInvocation) fromXML(text);
67          }
68          return super.readRemoteInvocation(message);
69      }
70      
71      public Message createObjectMessage(Session session, Object value) throws JMSException {
72          String xml = toXML(value);
73          TextMessage message = session.createTextMessage(xml);
74          appendMessageHeaders(message, session, value);
75          return message;
76      }
77  
78      public Object readMessage(Message message) throws JMSException {
79          if (message instanceof TextMessage) {
80              TextMessage textMessage = (TextMessage) message;
81              String text = textMessage.getText();
82              return fromXML(text);
83          }
84          return super.readMessage(message);
85      }
86  
87      // Properties
88      // -------------------------------------------------------------------------
89      public XStream getXStream() {
90          if (xStream == null) {
91              xStream = createXStream();
92          }
93          return xStream;
94      }
95  
96      public void setXStream(XStream xStream) {
97          this.xStream = xStream;
98      }
99  
100     // Implementation methods
101     // -------------------------------------------------------------------------
102     protected XStream createXStream() {
103         XStream answer = new XStream();
104         answer.alias("invoke", LingoInvocation.class);
105         answer.alias("result", RemoteInvocationResult.class);
106         return answer;
107     }
108 
109     protected Object fromXML(String xml) {
110         return getXStream().fromXML(xml);
111     }
112 
113     protected String toXML(Object object) {
114         return getXStream().toXML(object);
115     }
116 
117 }