001    /**
002     *
003     * Copyright 2005 LogicBlaze, Inc.
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * 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     *
017     **/
018    package org.logicblaze.lingo.jms.marshall;
019    
020    import com.thoughtworks.xstream.XStream;
021    import org.logicblaze.lingo.LingoInvocation;
022    import org.logicblaze.lingo.jms.Requestor;
023    import org.springframework.remoting.support.RemoteInvocation;
024    import org.springframework.remoting.support.RemoteInvocationResult;
025    
026    import javax.jms.JMSException;
027    import javax.jms.Message;
028    import javax.jms.TextMessage;
029    
030    
031    /**
032     * Uses XStream to marshall requests and responses into and out of messages.
033     *
034     * @version $Revision: 1.1 $
035     */
036    public class XStreamMarshaller extends DefaultMarshaller {
037        private XStream xStream;
038    
039        public Message createRequestMessage(Requestor requestor, LingoInvocation invocation) throws JMSException {
040            String xml = toXML(invocation);
041            return requestor.getSession().createTextMessage(xml);
042        }
043    
044        public RemoteInvocationResult extractInvocationResult(Message message) throws JMSException {
045            if (message instanceof TextMessage) {
046                TextMessage textMessage = (TextMessage) message;
047                String text = textMessage.getText();
048                return (RemoteInvocationResult) fromXML(text);
049            }
050            return super.extractInvocationResult(message);
051        }
052    
053        public RemoteInvocation readRemoteInvocation(Message message) throws JMSException {
054            if (message instanceof TextMessage) {
055                TextMessage textMessage = (TextMessage) message;
056                String text = textMessage.getText();
057                return (RemoteInvocation) fromXML(text);
058            }
059            return super.readRemoteInvocation(message);
060        }
061    
062    
063        // Properties
064        //-------------------------------------------------------------------------
065        public XStream getXStream() {
066            if (xStream == null) {
067                xStream = createXStream();
068            }
069            return xStream;
070        }
071    
072        public void setXStream(XStream xStream) {
073            this.xStream = xStream;
074        }
075    
076        // Implementation methods
077        //-------------------------------------------------------------------------
078        protected XStream createXStream() {
079            XStream answer = new XStream();
080            answer.alias("invoke", LingoInvocation.class);
081            return answer;
082        }
083    
084        protected Object fromXML(String xml) {
085            return getXStream().fromXML(xml);
086        }
087    
088        protected String toXML(Object object) {
089            return getXStream().toXML(object);
090        }
091    
092    
093    }