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.io.IOException; |
30 | |
import java.net.MalformedURLException; |
31 | |
import java.net.ServerSocket; |
32 | |
import java.net.URL; |
33 | |
import java.util.ArrayList; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.List; |
36 | |
import java.util.StringTokenizer; |
37 | |
|
38 | |
import org.slf4j.Logger; |
39 | |
import org.slf4j.LoggerFactory; |
40 | |
|
41 | |
import ca.uhn.hl7v2.HL7Exception; |
42 | |
import ca.uhn.hl7v2.protocol.ApplicationRouter; |
43 | |
import ca.uhn.hl7v2.protocol.Processor; |
44 | |
import ca.uhn.hl7v2.protocol.ProcessorContext; |
45 | |
import ca.uhn.hl7v2.protocol.SafeStorage; |
46 | |
import ca.uhn.hl7v2.protocol.TransportException; |
47 | |
import ca.uhn.hl7v2.protocol.TransportLayer; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 20 | public class HL7Server { |
56 | |
|
57 | 5 | private static Logger log = LoggerFactory.getLogger(HL7Server.class); |
58 | |
|
59 | |
private final ServerSocket myServerSocket; |
60 | |
private ServerSocket myServerSocket2; |
61 | |
private final ApplicationRouter myRouter; |
62 | |
private final SafeStorage myStorage; |
63 | |
|
64 | 10 | private boolean myIsRunning = false; |
65 | |
private List<Processor> myProcessors; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 10 | public HL7Server(ServerSocket theServerSocket, ApplicationRouter theRouter, SafeStorage theStorage) { |
75 | 10 | myServerSocket = theServerSocket; |
76 | 10 | myRouter = theRouter; |
77 | 10 | myStorage = theStorage; |
78 | 10 | initProcessorList(); |
79 | 10 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public HL7Server(ServerSocket theLocallyDriven, ServerSocket theRemotelyDriven, |
91 | 0 | ApplicationRouter theRouter, SafeStorage theStorage) { |
92 | |
|
93 | 0 | myServerSocket = theLocallyDriven; |
94 | 0 | myServerSocket2 = theRemotelyDriven; |
95 | 0 | myRouter = theRouter; |
96 | 0 | myStorage = theStorage; |
97 | 0 | initProcessorList(); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
private void initProcessorList() { |
102 | 10 | myProcessors = new ArrayList<Processor>(); |
103 | |
|
104 | 10 | final List<Processor> processors = myProcessors; |
105 | 10 | Thread cleaner = new Thread() { |
106 | |
public void run() { |
107 | |
try { |
108 | 10 | Thread.sleep(1000); |
109 | 10 | } catch (InterruptedException e) {} |
110 | |
|
111 | 10 | synchronized (processors) { |
112 | 10 | Iterator<Processor> it = processors.iterator(); |
113 | 20 | while (it.hasNext()) { |
114 | 10 | Processor proc = it.next(); |
115 | 10 | if (!proc.getContext().getLocallyDrivenTransportLayer().isConnected() |
116 | 5 | || !proc.getContext().getRemotelyDrivenTransportLayer().isConnected()) { |
117 | 5 | it.remove(); |
118 | |
} |
119 | 10 | } |
120 | 10 | } |
121 | 10 | } |
122 | |
}; |
123 | 10 | cleaner.start(); |
124 | 10 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public Processor accept(String theAddress) throws TransportException { |
138 | 20 | TransportLayer transport = getTransport(myServerSocket, theAddress); |
139 | 20 | ProcessorContext context = null; |
140 | |
|
141 | 20 | if (myServerSocket2 == null) { |
142 | 20 | transport.connect(); |
143 | 10 | context = new ProcessorContextImpl(myRouter, transport, myStorage); |
144 | |
} else { |
145 | 0 | TransportLayer transport2 = getTransport(myServerSocket2, theAddress); |
146 | 0 | DualTransportConnector connector = new DualTransportConnector(transport, transport2); |
147 | 0 | connector.connect(); |
148 | |
|
149 | 0 | context = new ProcessorContextImpl(myRouter, transport, transport2, myStorage); |
150 | |
} |
151 | 10 | return new ProcessorImpl(context, true); |
152 | |
} |
153 | |
|
154 | |
private static TransportLayer getTransport(ServerSocket theServerSocket, String theAddress) throws TransportException { |
155 | 20 | ServerSocketStreamSource ss = new ServerSocketStreamSource(theServerSocket, theAddress); |
156 | 20 | return new MLLPTransport(ss); |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public void start(final String theAddress) { |
168 | 10 | final HL7Server server = this; |
169 | 10 | Runnable acceptor = new Runnable() { |
170 | |
public void run() { |
171 | 20 | while (server.isRunning()) { |
172 | |
try { |
173 | 20 | Processor p = server.accept(theAddress); |
174 | 10 | if (!myIsRunning) { |
175 | 0 | p.stop(); |
176 | |
} else { |
177 | 10 | server.newProcessor(p); |
178 | 10 | Thread.sleep(1); |
179 | |
} |
180 | 0 | } catch (TransportException e) { |
181 | 0 | log.error(e.getMessage(), e); |
182 | 0 | } catch (InterruptedException e) { |
183 | 10 | } |
184 | |
} |
185 | 0 | } |
186 | |
}; |
187 | |
|
188 | 10 | myIsRunning = true; |
189 | |
|
190 | 10 | Thread thd = new Thread(acceptor); |
191 | 10 | thd.start(); |
192 | 10 | } |
193 | |
|
194 | |
private void newProcessor(Processor theProcessor) { |
195 | 10 | synchronized (myProcessors) { |
196 | 10 | myProcessors.add(theProcessor); |
197 | 10 | } |
198 | 10 | } |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public void stop() { |
204 | 5 | myIsRunning = false; |
205 | 5 | synchronized (myProcessors) { |
206 | 5 | for (Processor next : myProcessors) { |
207 | 5 | next.stop(); |
208 | 5 | } |
209 | 5 | } |
210 | 5 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public boolean isRunning() { |
222 | 20 | return myIsRunning; |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public Processor[] getProcessors() { |
229 | 5 | synchronized (myProcessors) { |
230 | 5 | return (Processor[]) myProcessors.toArray(new Processor[0]); |
231 | 0 | } |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
private static URL getURL(String theUrlSpec) throws MalformedURLException { |
242 | 0 | URL url = null; |
243 | 0 | if (theUrlSpec.startsWith("classpath:")) { |
244 | 0 | StringTokenizer tok = new StringTokenizer(theUrlSpec, ":", false); |
245 | 0 | tok.nextToken(); |
246 | 0 | String resource = tok.nextToken(); |
247 | 0 | url = Thread.currentThread().getContextClassLoader().getResource(resource); |
248 | 0 | } else { |
249 | 0 | url = new URL(theUrlSpec); |
250 | |
} |
251 | 0 | return url; |
252 | |
} |
253 | |
|
254 | |
public static void main(String[] args) { |
255 | 0 | if (args.length < 1 || args.length > 3) { |
256 | 0 | System.out.println("Usage: HL7Server (shared_port | (locally_driven_port remotely_driven_port)) app_binding_URL"); |
257 | 0 | System.exit(1); |
258 | |
} |
259 | |
|
260 | 0 | SafeStorage storage = new NullSafeStorage(); |
261 | 0 | ApplicationRouter router = new ApplicationRouterImpl(); |
262 | |
|
263 | |
try { |
264 | 0 | HL7Server server = null; |
265 | 0 | String appURL = null; |
266 | 0 | if (args.length == 2) { |
267 | 0 | int port = Integer.parseInt(args[0]); |
268 | 0 | server = new HL7Server(new ServerSocket(port), router, storage); |
269 | 0 | appURL = args[1]; |
270 | 0 | } else { |
271 | 0 | int localPort = Integer.parseInt(args[0]); |
272 | 0 | int remotePort = Integer.parseInt(args[1]); |
273 | 0 | server = new HL7Server(new ServerSocket(localPort), new ServerSocket(remotePort), router, storage); |
274 | 0 | appURL = args[2]; |
275 | |
} |
276 | |
|
277 | 0 | ApplicationLoader.loadApplications(router, getURL(appURL)); |
278 | |
|
279 | 0 | server.start(null); |
280 | |
|
281 | 0 | } catch (NumberFormatException e) { |
282 | 0 | System.out.println("Port arguments must be integers"); |
283 | 0 | System.exit(2); |
284 | 0 | } catch (IOException e) { |
285 | 0 | e.printStackTrace(); |
286 | 0 | System.exit(3); |
287 | 0 | } catch (HL7Exception e) { |
288 | 0 | e.printStackTrace(); |
289 | 0 | System.exit(4); |
290 | 0 | } catch (ClassNotFoundException e) { |
291 | 0 | e.printStackTrace(); |
292 | 0 | System.exit(5); |
293 | 0 | } catch (InstantiationException e) { |
294 | 0 | e.printStackTrace(); |
295 | 0 | System.exit(6); |
296 | 0 | } catch (IllegalAccessException e) { |
297 | 0 | e.printStackTrace(); |
298 | 0 | System.exit(7); |
299 | 0 | } |
300 | |
|
301 | 0 | } |
302 | |
} |