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