Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JMSTransport |
|
| 3.0;3 |
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 | 0 | 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 | 0 | public JMSTransport(JMSDestination theInboundDestination, JMSDestination theOutboundDestination) { |
73 | 0 | myInbound = theInboundDestination; |
74 | 0 | myOutbound = theOutboundDestination; |
75 | 0 | } |
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 | 0 | public JMSTransport() { |
83 | 0 | myMetadata = makeMetadata(); |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * Sets common metadata on the basis of connection and destination. | |
88 | */ | |
89 | private Map<String, Object> makeMetadata() { | |
90 | 0 | Map<String, Object> md = new HashMap<String, Object>(); |
91 | try { | |
92 | 0 | md.put(INBOUND_CLIENT_ID_KEY, myInbound.getConnection().getClientID()); |
93 | 0 | md.put(INBOUND_CONNECTION_METADATA_KEY, myInbound.getConnection().getMetaData()); |
94 | 0 | md.put(INBOUND_DESTINATION_NAME_KEY, myInbound.getName()); |
95 | 0 | md.put(OUTBOUND_CLIENT_ID_KEY, myOutbound.getConnection().getClientID()); |
96 | 0 | md.put(OUTBOUND_CONNECTION_METADATA_KEY, myOutbound.getConnection().getMetaData()); |
97 | 0 | md.put(OUTBOUND_DESTINATION_NAME_KEY, myOutbound.getName()); |
98 | 0 | } catch (JMSException e) { |
99 | 0 | log.error("Error setting JMSTransport metadata", e); |
100 | 0 | } |
101 | 0 | 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 | 0 | Message message = toMessage(theMessage); |
128 | 0 | myOutbound.send(message); |
129 | 0 | } catch (JMSException e) { |
130 | 0 | throw new TransportException(e); |
131 | 0 | } |
132 | 0 | } |
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 | 0 | message = myOutbound.createMessage(); |
148 | ||
149 | 0 | if ( !(message instanceof TextMessage)) { |
150 | 0 | 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 | 0 | ((TextMessage) message).setText(theSource.getMessage()); |
155 | ||
156 | 0 | Iterator<String> it = theSource.getMetadata().keySet().iterator(); |
157 | 0 | while (it.hasNext()) { |
158 | 0 | Object key = it.next(); |
159 | 0 | Object val = theSource.getMetadata().get(key); |
160 | 0 | message.setObjectProperty(key.toString(), val); |
161 | 0 | } |
162 | 0 | } catch (JMSException e) { |
163 | 0 | throw new TransportException(e); |
164 | 0 | } |
165 | ||
166 | 0 | 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 | 0 | if ( !(theMessage instanceof TextMessage)) { |
178 | 0 | 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 | 0 | Transportable result = null; |
183 | try { | |
184 | 0 | String text = ((TextMessage) theMessage).getText(); |
185 | 0 | result = new TransportableImpl(text); |
186 | 0 | result.getMetadata().putAll(getCommonMetadata()); |
187 | 0 | } catch (JMSException e) { |
188 | 0 | throw new TransportException(e); |
189 | 0 | } |
190 | ||
191 | 0 | return result; |
192 | } | |
193 | ||
194 | /** | |
195 | * @see ca.uhn.hl7v2.protocol.AbstractTransport#doReceive() | |
196 | */ | |
197 | public Transportable doReceive() throws TransportException { | |
198 | 0 | Transportable result = null; |
199 | try { | |
200 | 0 | Message message = myInbound.receive(); |
201 | 0 | result = toTransportable(message); |
202 | 0 | } catch (JMSException e) { |
203 | 0 | throw new TransportException(e); |
204 | 0 | } |
205 | 0 | 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 | 0 | return myMetadata; |
215 | } | |
216 | ||
217 | /** | |
218 | * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doConnect() | |
219 | */ | |
220 | public void doConnect() throws TransportException { | |
221 | try { | |
222 | 0 | myInbound.connect(); |
223 | 0 | if (myInbound != myOutbound) { |
224 | 0 | myOutbound.connect(); |
225 | } | |
226 | 0 | } catch (JMSException e) { |
227 | 0 | throw new TransportException(e); |
228 | 0 | } |
229 | 0 | } |
230 | ||
231 | /** | |
232 | * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doDisconnect() | |
233 | */ | |
234 | public void doDisconnect() throws TransportException { | |
235 | try { | |
236 | 0 | myInbound.disconnect(); |
237 | 0 | if (myInbound != myOutbound) { |
238 | 0 | myOutbound.disconnect(); |
239 | } | |
240 | 0 | } catch (JMSException e) { |
241 | 0 | throw new TransportException(e); |
242 | 0 | } |
243 | 0 | } |
244 | ||
245 | } |