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 "AbstractJMSTransport.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.Map;
31  
32  import javax.jms.Connection;
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.TransportException;
41  import ca.uhn.hl7v2.protocol.TransportLayer;
42  import ca.uhn.hl7v2.protocol.Transportable;
43  
44  /**
45   * A <code>TransportLayer</code> that exchanges messages through JMS destinations.   
46   * 
47   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
48   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
49   */
50  public abstract class AbstractJMSTransport extends AbstractTransport implements TransportLayer {
51  
52      private static final Logger log = LoggerFactory.getLogger(URLTransport.class);    
53  
54      public static final String CLIENT_ID_KEY = "CLIENT_ID";
55      public static final String CONNECTION_METADATA_KEY = "CONNECTION_METADATA";
56      public static final String DESTINATION_NAME_KEY = "DESTINATION_NAME";
57       
58      private final Map<String, Object> myMetadata;
59  
60      public AbstractJMSTransport() {
61          myMetadata = makeMetadata();
62      }
63      
64      /** 
65       * Sets common metadata on the basis of connection and destination.  
66       */ 
67      private Map<String, Object> makeMetadata() {
68          Map<String, Object> md = new HashMap<>();
69          try {
70              md.put(CLIENT_ID_KEY, getConnection().getClientID());
71          } catch (JMSException e) {
72              log.error("Error setting JMSTransport metadata", e);
73          }
74          
75          try {
76              md.put(CONNECTION_METADATA_KEY, getConnection().getMetaData());
77          } catch (JMSException e) {
78              log.error("Error setting JMSTransport metadata", e);
79          }
80          
81          try {
82              md.put(DESTINATION_NAME_KEY, getDestinationName());
83          } catch (JMSException e) {
84              log.error("Error setting JMSTransport metadata", e);
85          }
86          return md;
87      }
88      
89      /**
90       * @return the name of the destination at which messages are 
91       *      written and read 
92       */
93      protected abstract String getDestinationName() throws JMSException;
94      
95      /**
96       * @return the QueueConnection or TopicConnection over which messages
97       *      are transported 
98       */
99      public abstract Connection getConnection();
100     
101     /**
102      * @return a new JMS Message created on the sending Session.
103      * @throws JMSException
104      */
105     protected abstract Message getMessage() throws JMSException;
106     
107     /**
108      * Sends a message to the underlying Destination 
109      * 
110      * @param theMessage 
111      * @throws JMSException
112      */
113     protected abstract void sendJMS(Message theMessage) throws JMSException;
114     
115     /**
116      * @return the next available message from the underlying Destination
117      * @throws JMSException
118      */
119     protected abstract Message receiveJMS() throws JMSException;
120     
121 //    /**
122 //     * @param theDestination a Queue or Topic 
123 //     * @return either getQueueName() or getTopicName() 
124 //     */
125 //    private static String getName(Destination theDestination) throws JMSException {
126 //        String name = null;
127 //        
128 //        if (theDestination instanceof Queue) {
129 //            name = ((Queue) theDestination).getQueueName();
130 //        } else if (theDestination instanceof Topic) {
131 //            name = ((Topic) theDestination).getTopicName();
132 //        } else {
133 //            throw new IllegalArgumentException("We don't support Destinations of type " 
134 //                + theDestination.getClass().getName());
135 //        }
136 //        return name;
137 //    }
138 
139     /** 
140      * @see AbstractTransport#doSend(ca.uhn.hl7v2.protocol.Transportable)
141      */
142     public void doSend(Transportable theMessage) throws TransportException {
143         try {            
144             Message message = toMessage(theMessage);
145             sendJMS(message);            
146         } catch (JMSException e) {
147             throw new TransportException(e);
148         }
149     } 
150     
151     /**
152      * Fills a JMS message object with text and metadata from the given 
153      * <code>Transportable</code>.  The default implementation obtains a 
154      * the Message from getMessage(), and expects this to be a TextMessage.   
155      * Override this method if you want to use a different message type.  
156      * 
157      * @param theSource a Transportable from which to obtain data for filling the 
158      *      given Message
159      * @return a Message containing data from the given Transportable
160      */
161     protected Message toMessage(Transportable theSource) throws TransportException {
162         Message message;
163         try {
164             message = getMessage();
165          
166             if ( !(message instanceof TextMessage)) {
167                 throw new TransportException("This implementation expects getMessage() to return "
168                     + " a TextMessage.  Override this method if another message type is to be used");
169             }
170 
171             ((TextMessage) message).setText(theSource.getMessage());
172 
173             for (String key : theSource.getMetadata().keySet()) {
174                 Object val = theSource.getMetadata().get(key);
175                 message.setObjectProperty(key, val);
176             }
177         } catch (JMSException e) {
178             throw new TransportException(e);
179         }       
180         
181         return message;
182     }
183     
184     /**
185      * Copies data from the given Message into a Transportable.  The default 
186      * implementation expects a TextMessage, but this can be overridden.  
187      * 
188      * @param theMessage a JMS Message from which to obtain data  
189      * @return a Transportable containing data from the given Message
190      */
191     protected Transportable toTransportable(Message theMessage) throws TransportException {
192         if ( !(theMessage instanceof TextMessage)) {
193             throw new TransportException("This implementation expects getMessage() to return "
194                 + " a TextMessage.  Override this method if another message type is to be used");
195         }
196         
197         Transportable result;
198         try {
199             String text = ((TextMessage) theMessage).getText();
200             result = new TransportableImpl(text);
201             result.getMetadata().putAll(getCommonMetadata());
202         } catch (JMSException e) {
203             throw new TransportException(e);
204         }
205 
206         return result;
207     }
208     
209     /** 
210      * @see AbstractTransport#doReceive()
211      */
212     public Transportable doReceive() throws TransportException {
213         Transportable result;
214         try {
215             Message message = receiveJMS();
216             result = toTransportable(message);
217         } catch (JMSException e) {
218             throw new TransportException(e);            
219         }
220         return result;
221     }
222 
223     /** 
224      * Returns metadata under the static keys defined by this class.  
225      *  
226      * @see ca.uhn.hl7v2.protocol.TransportLayer#getCommonMetadata()
227      */
228     public Map<String, Object> getCommonMetadata() {
229         return myMetadata;
230     }
231     
232 }