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  import org.logicblaze.lingo.LingoInvocation;
22  import org.logicblaze.lingo.jms.Requestor;
23  import org.springframework.remoting.support.RemoteInvocation;
24  import org.springframework.remoting.support.RemoteInvocationResult;
25  
26  import javax.jms.JMSException;
27  import javax.jms.Message;
28  import javax.jms.TextMessage;
29  
30  
31  /***
32   * Uses XStream to marshall requests and responses into and out of messages.
33   *
34   * @version $Revision: 1.1 $
35   */
36  public class XStreamMarshaller extends DefaultMarshaller {
37      private XStream xStream;
38  
39      public Message createRequestMessage(Requestor requestor, LingoInvocation invocation) throws JMSException {
40          String xml = toXML(invocation);
41          return requestor.getSession().createTextMessage(xml);
42      }
43  
44      public RemoteInvocationResult extractInvocationResult(Message message) throws JMSException {
45          if (message instanceof TextMessage) {
46              TextMessage textMessage = (TextMessage) message;
47              String text = textMessage.getText();
48              return (RemoteInvocationResult) fromXML(text);
49          }
50          return super.extractInvocationResult(message);
51      }
52  
53      public RemoteInvocation readRemoteInvocation(Message message) throws JMSException {
54          if (message instanceof TextMessage) {
55              TextMessage textMessage = (TextMessage) message;
56              String text = textMessage.getText();
57              return (RemoteInvocation) fromXML(text);
58          }
59          return super.readRemoteInvocation(message);
60      }
61  
62  
63      // Properties
64      //-------------------------------------------------------------------------
65      public XStream getXStream() {
66          if (xStream == null) {
67              xStream = createXStream();
68          }
69          return xStream;
70      }
71  
72      public void setXStream(XStream xStream) {
73          this.xStream = xStream;
74      }
75  
76      // Implementation methods
77      //-------------------------------------------------------------------------
78      protected XStream createXStream() {
79          XStream answer = new XStream();
80          answer.alias("invoke", LingoInvocation.class);
81          return answer;
82      }
83  
84      protected Object fromXML(String xml) {
85          return getXStream().fromXML(xml);
86      }
87  
88      protected String toXML(Object object) {
89          return getXStream().toXML(object);
90      }
91  
92  
93  }