View Javadoc

1   package ca.uhn.hl7v2.hoh.relay.listener;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.springframework.beans.factory.BeanNameAware;
7   import org.springframework.beans.factory.DisposableBean;
8   import org.springframework.beans.factory.InitializingBean;
9   
10  import ca.uhn.hl7v2.app.SimpleServer;
11  import ca.uhn.hl7v2.hoh.util.Validate;
12  import ca.uhn.hl7v2.llp.ExtendedMinLowerLayerProtocol;
13  import ca.uhn.hl7v2.llp.LowerLayerProtocol;
14  import ca.uhn.hl7v2.parser.GenericModelClassFactory;
15  import ca.uhn.hl7v2.parser.Parser;
16  import ca.uhn.hl7v2.parser.PipeParser;
17  import ca.uhn.hl7v2.protocol.ApplicationRouter.AppRoutingData;
18  import ca.uhn.hl7v2.protocol.ReceivingApplication;
19  
20  public class RelayMllpListener implements InitializingBean, DisposableBean, IRelayListener, BeanNameAware {
21  
22  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RelayMllpListener.class);
23  	private List<ReceivingApplication> myApplications = new ArrayList<ReceivingApplication>();
24  	private List<AppRoutingData> myAppRoutingData = new ArrayList<AppRoutingData>();
25  	private String myBeanName;
26  	private int myPort;
27  	private SimpleServer myServer;
28  
29  	/**
30  	 * Fired automatically by the container when
31  	 * the bean is ready to start
32  	 */
33  	public void afterPropertiesSet() throws Exception {
34  		if (myPort <= 0) {
35  			throw new IllegalStateException("Port not set");
36  		}
37  		
38  		LowerLayerProtocol llp = new ExtendedMinLowerLayerProtocol();
39  		Parser parser = new PipeParser(new GenericModelClassFactory());
40  		myServer = new SimpleServer(myPort, llp, parser);
41  
42  		for (int i = 0; i < myAppRoutingData.size(); i++) {
43  			myServer.registerApplication(myAppRoutingData.get(i), myApplications.get(i));
44  		}
45  		
46  		ourLog.info("Starting listener on port {}", myPort);
47  		myServer.startAndWait();
48  		ourLog.info("Listener on port {} has started, and is ready for processing", myPort);
49  
50  		if (myServer.getServiceExitedWithException() != null) {
51  			Throwable ex = myServer.getServiceExitedWithException();
52  			ourLog.error("Server failed to start", ex);
53  			if (ex instanceof Exception) {
54  				throw (Exception) ex;
55  			} else {
56  				throw new Error(ex);
57  			}
58  		}
59  
60  	}
61  
62  	
63  	/**
64  	 * Fired automatically by the container when
65  	 * the bean is shutting down
66  	 */
67  	public void destroy() throws Exception {
68  		ourLog.info("Stopping listener on port {}", myPort);
69  		myServer.stopAndWait();
70  		ourLog.info("Listener on port {} has stopped", myPort);
71  	}
72  
73  	public String getBeanName() {
74  		return myBeanName;
75  	}
76  
77  
78  	public void registerApplication(AppRoutingData theAppRouting, ReceivingApplication theReceivingApplication) {
79  		Validate.notNull(theAppRouting, "appRouting");
80  		Validate.notNull(theReceivingApplication, "receivingApplication");
81  		
82  		if (myServer != null) {
83  			myServer.registerApplication(theAppRouting, theReceivingApplication);
84  		} else {
85  			myAppRoutingData.add(theAppRouting);
86  			myApplications.add(theReceivingApplication);
87  		}
88  	}
89  
90  
91  	public void setBeanName(String theBeanName) {
92  		myBeanName = theBeanName;
93  	}
94  
95  	public void setPort(int thePort) {
96  		myPort = thePort;
97  	}
98  
99  
100 }