1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.JMSException;
33 import javax.jms.Message;
34 import javax.jms.TextMessage;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import ca.uhn.hl7v2.protocol.JMSDestination;
40 import ca.uhn.hl7v2.protocol.TransportException;
41 import ca.uhn.hl7v2.protocol.TransportLayer;
42 import ca.uhn.hl7v2.protocol.Transportable;
43
44
45
46
47
48
49
50 public class JMSTransport extends AbstractTransport implements TransportLayer {
51
52 private static final Logger log = LoggerFactory.getLogger(URLTransport.class);
53
54 public static final String INBOUND_DESTINATION_NAME_KEY = "INBOUND_DESTINATION_NAME";
55 public static final String INBOUND_CLIENT_ID_KEY = "INBOUND_CLIENT_ID";
56 public static final String INBOUND_CONNECTION_METADATA_KEY = "INBOUND_CONNECTION_METADATA";
57 public static final String OUTBOUND_DESTINATION_NAME_KEY = "OUTBOUND_DESTINATION_NAME";
58 public static final String OUTBOUND_CLIENT_ID_KEY = "OUTBOUND_CLIENT_ID";
59 public static final String OUTBOUND_CONNECTION_METADATA_KEY = "OUTBOUND_CONNECTION_METADATA";
60
61 private JMSDestination myInbound;
62 private JMSDestination myOutbound;
63 private Map<String, Object> myMetadata;
64
65
66
67
68
69
70
71 public JMSTransport(JMSDestination/hl7v2/protocol/JMSDestination.html#JMSDestination">JMSDestination theInboundDestination, JMSDestination theOutboundDestination) {
72 myInbound = theInboundDestination;
73 myOutbound = theOutboundDestination;
74 }
75
76
77 public JMSTransport() {
78 myMetadata = makeMetadata();
79 }
80
81
82
83
84 private Map<String, Object> makeMetadata() {
85 Map<String, Object> md = new HashMap<>();
86 try {
87 md.put(INBOUND_CLIENT_ID_KEY, myInbound.getConnection().getClientID());
88 md.put(INBOUND_CONNECTION_METADATA_KEY, myInbound.getConnection().getMetaData());
89 md.put(INBOUND_DESTINATION_NAME_KEY, myInbound.getName());
90 md.put(OUTBOUND_CLIENT_ID_KEY, myOutbound.getConnection().getClientID());
91 md.put(OUTBOUND_CONNECTION_METADATA_KEY, myOutbound.getConnection().getMetaData());
92 md.put(OUTBOUND_DESTINATION_NAME_KEY, myOutbound.getName());
93 } catch (JMSException e) {
94 log.error("Error setting JMSTransport metadata", e);
95 }
96 return md;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public void doSend(Transportable theMessage) throws TransportException {
121 try {
122 Message message = toMessage(theMessage);
123 myOutbound.send(message);
124 } catch (JMSException e) {
125 throw new TransportException(e);
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138
139 protected Message toMessage(Transportable theSource) throws TransportException {
140 TextMessage message;
141 try {
142 message = myOutbound.createMessage();
143
144 if (message == null) {
145 throw new TransportException("This implementation expects getMessage() to return "
146 + " a TextMessage. Override this method if another message type is to be used");
147 }
148
149 message.setText(theSource.getMessage());
150
151 for (Object key : theSource.getMetadata().keySet()) {
152 Object val = theSource.getMetadata().get(key);
153 message.setObjectProperty(key.toString(), val);
154 }
155 } catch (JMSException e) {
156 throw new TransportException(e);
157 }
158
159 return message;
160 }
161
162
163
164
165
166
167
168
169 protected Transportable toTransportable(Message theMessage) throws TransportException {
170 if ( !(theMessage instanceof TextMessage)) {
171 throw new TransportException("This implementation expects getMessage() to return "
172 + " a TextMessage. Override this method if another message type is to be used");
173 }
174
175 Transportable result;
176 try {
177 String text = ((TextMessage) theMessage).getText();
178 result = new TransportableImpl(text);
179 result.getMetadata().putAll(getCommonMetadata());
180 } catch (JMSException e) {
181 throw new TransportException(e);
182 }
183
184 return result;
185 }
186
187
188
189
190 public Transportable doReceive() throws TransportException {
191 Transportable result;
192 try {
193 Message message = myInbound.receive();
194 result = toTransportable(message);
195 } catch (JMSException e) {
196 throw new TransportException(e);
197 }
198 return result;
199 }
200
201
202
203
204
205
206 public Map<String, Object> getCommonMetadata() {
207 return myMetadata;
208 }
209
210
211
212
213 public void doConnect() throws TransportException {
214 try {
215 myInbound.connect();
216 if (myInbound != myOutbound) {
217 myOutbound.connect();
218 }
219 } catch (JMSException e) {
220 throw new TransportException(e);
221 }
222 }
223
224
225
226
227 public void doDisconnect() throws TransportException {
228 try {
229 myInbound.disconnect();
230 if (myInbound != myOutbound) {
231 myOutbound.disconnect();
232 }
233 } catch (JMSException e) {
234 throw new TransportException(e);
235 }
236 }
237
238 }