| 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.app; |
| 28 | |
|
| 29 | |
import java.io.IOException; |
| 30 | |
import java.net.InetAddress; |
| 31 | |
import java.util.concurrent.ExecutorService; |
| 32 | |
import java.util.concurrent.TimeUnit; |
| 33 | |
|
| 34 | |
import ca.uhn.hl7v2.HL7Exception; |
| 35 | |
import ca.uhn.hl7v2.llp.LLPException; |
| 36 | |
import ca.uhn.hl7v2.model.Message; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public class LazyConnection implements Connection { |
| 45 | |
|
| 46 | |
private ConnectionData data; |
| 47 | |
private ExecutorService executor; |
| 48 | |
private Connection activeConnection; |
| 49 | |
private LazyInitiator initiator; |
| 50 | |
|
| 51 | 15 | public LazyConnection(ConnectionData data, ExecutorService executor) { |
| 52 | 15 | this.data = data; |
| 53 | 15 | this.executor = executor; |
| 54 | 15 | this.initiator = new LazyInitiator(this); |
| 55 | 15 | } |
| 56 | |
|
| 57 | |
public void activate() { |
| 58 | 0 | if (isEstablished()) activeConnection.activate(); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
public Initiator getInitiator() { |
| 62 | 40 | if (isEstablished()) return activeConnection.getInitiator(); |
| 63 | 10 | return initiator; |
| 64 | |
} |
| 65 | |
|
| 66 | |
public void close() { |
| 67 | 25 | if (isEstablished()) { |
| 68 | 15 | activeConnection.close(); |
| 69 | 15 | activeConnection = null; |
| 70 | |
} |
| 71 | 25 | } |
| 72 | |
|
| 73 | |
public boolean isOpen() { |
| 74 | 15 | return isEstablished() && activeConnection.isOpen(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
public ExecutorService getExecutorService() { |
| 78 | 0 | if (isEstablished()) return activeConnection.getExecutorService(); |
| 79 | 0 | return executor; |
| 80 | |
} |
| 81 | |
|
| 82 | |
public InetAddress getRemoteAddress() { |
| 83 | 5 | if (isEstablished()) |
| 84 | 0 | return activeConnection.getRemoteAddress(); |
| 85 | 5 | return null; |
| 86 | |
} |
| 87 | |
|
| 88 | |
public Integer getRemotePort() { |
| 89 | 0 | if (isEstablished()) { |
| 90 | 0 | return activeConnection.getRemotePort(); |
| 91 | |
} |
| 92 | 0 | return null; |
| 93 | |
} |
| 94 | |
|
| 95 | |
boolean isEstablished() { |
| 96 | 140 | return activeConnection != null && activeConnection.isOpen(); |
| 97 | |
} |
| 98 | |
|
| 99 | |
void establishConnection() throws HL7Exception { |
| 100 | |
try { |
| 101 | 15 | activeConnection = ConnectionFactory.openEagerly(data, executor); |
| 102 | 0 | } catch (Exception e) { |
| 103 | 0 | throw new HL7Exception(e); |
| 104 | 15 | } |
| 105 | 15 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
static class LazyInitiator implements Initiator { |
| 109 | |
|
| 110 | |
private LazyConnection connection; |
| 111 | 15 | private long timeoutMillis = 10000; |
| 112 | |
|
| 113 | 15 | LazyInitiator(LazyConnection connection) { |
| 114 | 15 | this.connection = connection; |
| 115 | 15 | } |
| 116 | |
|
| 117 | |
public synchronized Message sendAndReceive(Message out) throws HL7Exception, LLPException, IOException { |
| 118 | 15 | if (!connection.isEstablished()) { |
| 119 | 15 | connection.establishConnection(); |
| 120 | 15 | setTimeout(timeoutMillis, TimeUnit.MILLISECONDS); |
| 121 | |
} |
| 122 | 15 | return connection.getInitiator().sendAndReceive(out); |
| 123 | |
} |
| 124 | |
|
| 125 | |
public synchronized void setTimeout(long timeout, TimeUnit timeunit) { |
| 126 | 15 | if (connection.isEstablished()) |
| 127 | 15 | connection.getInitiator().setTimeout(timeout, timeunit); |
| 128 | |
else |
| 129 | 0 | this.timeoutMillis = timeunit.toMillis(timeout); |
| 130 | 15 | } |
| 131 | |
|
| 132 | |
public void setTimeoutMillis(int timeout) { |
| 133 | 0 | setTimeout(timeout, TimeUnit.MILLISECONDS); |
| 134 | 0 | } |
| 135 | |
} |
| 136 | |
|
| 137 | |
} |