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.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
46
47
48
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
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
91
92
93 protected abstract String getDestinationName() throws JMSException;
94
95
96
97
98
99 public abstract Connection getConnection();
100
101
102
103
104
105 protected abstract Message getMessage() throws JMSException;
106
107
108
109
110
111
112
113 protected abstract void sendJMS(Message theMessage) throws JMSException;
114
115
116
117
118
119 protected abstract Message receiveJMS() throws JMSException;
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
153
154
155
156
157
158
159
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
186
187
188
189
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
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
225
226
227
228 public Map<String, Object> getCommonMetadata() {
229 return myMetadata;
230 }
231
232 }