View Javadoc

1   /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "JMSTransport.java".  Description: 
10  "A TransportLayer that exchanges messages through JMS destinations." 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2004.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25  
26  */
27  package ca.uhn.hl7v2.protocol.impl;
28  
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.Map;
32  
33  import javax.jms.JMSException;
34  import javax.jms.Message;
35  import javax.jms.TextMessage;
36  
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import ca.uhn.hl7v2.protocol.JMSDestination;
41  import ca.uhn.hl7v2.protocol.TransportException;
42  import ca.uhn.hl7v2.protocol.TransportLayer;
43  import ca.uhn.hl7v2.protocol.Transportable;
44  
45  /**
46   * A <code>TransportLayer</code> that exchanges messages through JMS destinations.   
47   * 
48   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
49   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
50   */
51  public class JMSTransport extends AbstractTransport implements TransportLayer {
52  
53      private static final Logger log = LoggerFactory.getLogger(URLTransport.class);    
54  
55      public static final String INBOUND_DESTINATION_NAME_KEY = "INBOUND_DESTINATION_NAME";
56      public static final String INBOUND_CLIENT_ID_KEY = "INBOUND_CLIENT_ID";
57      public static final String INBOUND_CONNECTION_METADATA_KEY = "INBOUND_CONNECTION_METADATA";
58      public static final String OUTBOUND_DESTINATION_NAME_KEY = "OUTBOUND_DESTINATION_NAME";
59      public static final String OUTBOUND_CLIENT_ID_KEY = "OUTBOUND_CLIENT_ID";
60      public static final String OUTBOUND_CONNECTION_METADATA_KEY = "OUTBOUND_CONNECTION_METADATA";
61       
62      private JMSDestination myInbound;
63      private JMSDestination myOutbound;
64      private Map<String, Object> myMetadata;
65      
66      /**
67       * @param theInboundDestination wrapper around the Queue or Topic to which outgoing messages 
68       *      are to be sent
69       * @param theOutboundDestination wrapper around the Queue or Topic from which incoming messages
70       *      are to be retrieved
71       */
72      public JMSTransport(JMSDestination theInboundDestination, JMSDestination theOutboundDestination) {
73          myInbound = theInboundDestination;
74          myOutbound = theOutboundDestination;
75      }
76      
77      /**
78       * @param theConnection JMS connection over which messages are exchanged 
79       * @param theDestination JMS destination to which messages are produced and 
80       *      from which messages are consumed 
81       */
82      public JMSTransport() {
83          myMetadata = makeMetadata();
84      }
85      
86      /** 
87       * Sets common metadata on the basis of connection and destination.  
88       */ 
89      private Map<String, Object> makeMetadata() {
90          Map<String, Object> md = new HashMap<String, Object>();
91          try {
92              md.put(INBOUND_CLIENT_ID_KEY, myInbound.getConnection().getClientID());
93              md.put(INBOUND_CONNECTION_METADATA_KEY, myInbound.getConnection().getMetaData());
94              md.put(INBOUND_DESTINATION_NAME_KEY, myInbound.getName());
95              md.put(OUTBOUND_CLIENT_ID_KEY, myOutbound.getConnection().getClientID());
96              md.put(OUTBOUND_CONNECTION_METADATA_KEY, myOutbound.getConnection().getMetaData());
97              md.put(OUTBOUND_DESTINATION_NAME_KEY, myOutbound.getName());
98          } catch (JMSException e) {
99              log.error("Error setting JMSTransport metadata", e);
100         }
101         return md;
102     }
103     
104 //    /**
105 //     * @param theDestination a Queue or Topic 
106 //     * @return either getQueueName() or getTopicName() 
107 //     */
108 //    private static String getName(Destination theDestination) throws JMSException {
109 //        String name = null;
110 //        
111 //        if (theDestination instanceof Queue) {
112 //            name = ((Queue) theDestination).getQueueName();
113 //        } else if (theDestination instanceof Topic) {
114 //            name = ((Topic) theDestination).getTopicName();
115 //        } else {
116 //            throw new IllegalArgumentException("We don't support Destinations of type " 
117 //                + theDestination.getClass().getName());
118 //        }
119 //        return name;
120 //    }
121 
122     /** 
123      * @see ca.uhn.hl7v2.protocol.Transport#doSend(ca.uhn.hl7v2.protocol.Transportable)
124      */
125     public void doSend(Transportable theMessage) throws TransportException {
126         try {            
127             Message message = toMessage(theMessage);
128             myOutbound.send(message);
129         } catch (JMSException e) {
130             throw new TransportException(e);
131         }
132     } 
133     
134     /**
135      * Fills a JMS message object with text and metadata from the given 
136      * <code>Transportable</code>.  The default implementation obtains a 
137      * the Message from getMessage(), and expects this to be a TextMessage.   
138      * Override this method if you want to use a different message type.  
139      * 
140      * @param theSource a Transportable from which to obtain data for filling the 
141      *      given Message
142      * @return a Message containing data from the given Transportable
143      */
144     protected Message toMessage(Transportable theSource) throws TransportException {
145         Message message;
146         try {
147             message = myOutbound.createMessage();
148          
149             if ( !(message instanceof TextMessage)) {
150                 throw new TransportException("This implementation expects getMessage() to return "
151                     + " a TextMessage.  Override this method if another message type is to be used");
152             }
153 
154             ((TextMessage) message).setText(theSource.getMessage());
155         
156             Iterator<String> it = theSource.getMetadata().keySet().iterator();
157             while (it.hasNext()) {
158                 Object key = it.next();
159                 Object val = theSource.getMetadata().get(key);
160                 message.setObjectProperty(key.toString(), val);
161             }
162         } catch (JMSException e) {
163             throw new TransportException(e);
164         }       
165         
166         return message;
167     }
168     
169     /**
170      * Copies data from the given Message into a Transportable.  The default 
171      * implementation expects a TextMessage, but this can be overridden.  
172      * 
173      * @param theMessage a JMS Message from which to obtain data  
174      * @return a Transportable containing data from the given Message
175      */
176     protected Transportable toTransportable(Message theMessage) throws TransportException {
177         if ( !(theMessage instanceof TextMessage)) {
178             throw new TransportException("This implementation expects getMessage() to return "
179                 + " a TextMessage.  Override this method if another message type is to be used");
180         }
181         
182         Transportable result = null;
183         try {
184             String text = ((TextMessage) theMessage).getText();
185             result = new TransportableImpl(text);
186             result.getMetadata().putAll(getCommonMetadata());
187         } catch (JMSException e) {
188             throw new TransportException(e);
189         }
190 
191         return result;
192     }
193     
194     /** 
195      * @see ca.uhn.hl7v2.protocol.AbstractTransport#doReceive()
196      */
197     public Transportable doReceive() throws TransportException {
198         Transportable result = null;
199         try {
200             Message message = myInbound.receive();
201             result = toTransportable(message);
202         } catch (JMSException e) {
203             throw new TransportException(e);            
204         }
205         return result;
206     }
207 
208     /** 
209      * Returns metadata under the static keys defined by this class.  
210      *  
211      * @see ca.uhn.hl7v2.protocol.TransportLayer#getCommonMetadata()
212      */
213     public Map<String, Object> getCommonMetadata() {
214         return myMetadata;
215     }
216 
217     /** 
218      * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doConnect()
219      */
220     public void doConnect() throws TransportException {
221         try {
222             myInbound.connect();
223             if (myInbound != myOutbound) {
224                 myOutbound.connect();
225             }
226         } catch (JMSException e) {
227             throw new TransportException(e);
228         }
229     }
230 
231     /** 
232      * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doDisconnect()
233      */
234     public void doDisconnect() throws TransportException {
235         try {
236             myInbound.disconnect();
237             if (myInbound != myOutbound) {
238                 myOutbound.disconnect();
239             }
240         } catch (JMSException e) {
241             throw new TransportException(e);
242         }
243     }
244     
245 }