Coverage Report - ca.uhn.hl7v2.app.LazyConnection
 
Classes in this File Line Coverage Branch Coverage Complexity
LazyConnection
64%
18/28
55%
11/20
2.143
LazyConnection$LazyInitiator
78%
11/14
50%
2/4
2.143
 
 1  
 /*
 2  
  The contents of this file are subject to the Mozilla Public License Version 1.1
 3  
  (the "License"); you may not use this file except in compliance with the License.
 4  
  You may obtain a copy of the License at http://www.mozilla.org/MPL/
 5  
  Software distributed under the License is distributed on an "AS IS" basis,
 6  
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
 7  
  specific language governing rights and limitations under the License.
 8  
 
 9  
  The Original Code is "LazyConnection.java".  Description:
 10  
  "Connection that lazily connects right before sending the first message"
 11  
 
 12  
  The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
  2013.  All Rights Reserved.
 14  
 
 15  
  Contributor(s): ______________________________________.
 16  
 
 17  
  Alternatively, the contents of this file may be used under the terms of the
 18  
  GNU General Public License (the "GPL"), in which case the provisions of the GPL are
 19  
  applicable instead of those above.  If you wish to allow use of your version of this
 20  
  file only under the terms of the GPL and not to allow others to use your version
 21  
  of this file under the MPL, indicate your decision by deleting  the provisions above
 22  
  and replace  them with the notice and other provisions required by the GPL License.
 23  
  If you do not delete the provisions above, a recipient may use your version of
 24  
  this file under either the MPL or the GPL.
 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  
  * A LazyConnection behaves like an ordinary connection except for the fact
 40  
  * that the actual socket connection is created lazily right before the
 41  
  * first message is sent over the associated Initiator. Server-side connections
 42  
  * are always non-lazy.
 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  
 }