Coverage Report - ca.uhn.hl7v2.protocol.impl.MLLPTransport
 
Classes in this File Line Coverage Branch Coverage Complexity
MLLPTransport
75%
37/49
100%
8/8
4
 
 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 "MLLPTransport.java".  Description: 
 10  
 "An implementation of the HL7 Minimal Lower Layer Protocol." 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2004.  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.protocol.impl;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.io.InputStream;
 31  
 import java.util.Properties;
 32  
 
 33  
 import ca.uhn.hl7v2.llp.LLPException;
 34  
 import ca.uhn.hl7v2.llp.MinLLPReader;
 35  
 import ca.uhn.hl7v2.llp.MinLLPWriter;
 36  
 import ca.uhn.hl7v2.protocol.StreamSource;
 37  
 import ca.uhn.hl7v2.protocol.TransportException;
 38  
 import ca.uhn.hl7v2.protocol.TransportLayer;
 39  
 import ca.uhn.hl7v2.protocol.Transportable;
 40  
 
 41  
 /**
 42  
  * An implementation of the HL7 Minimal Lower Layer Protocol (see 
 43  
  * HL7 implementation guide appendix C).  Note that this is the most common 
 44  
  * protocol used in HL7 interfaces.  
 45  
  * 
 46  
  * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
 47  
  * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
 48  
  */
 49  
 public class MLLPTransport extends AbstractTransport implements TransportLayer {
 50  
 
 51  
     private MinLLPReader myReader;
 52  
     private MinLLPWriter myWriter;
 53  
     
 54  
     private StreamSource myStreamSource;
 55  
     private Properties myCharsetMappings;
 56  
     
 57  
     /**
 58  
      * @param theStreamSource the provider of input and output streams connected
 59  
      *      to the remote server 
 60  
      */
 61  12
     public MLLPTransport(StreamSource theStreamSource) throws TransportException {
 62  12
         myStreamSource = theStreamSource;
 63  12
         myCharsetMappings = loadCharsetMappings();
 64  12
     }
 65  
     
 66  
     private static Properties loadCharsetMappings() throws TransportException {
 67  12
         Properties mappings = new Properties();
 68  12
         String resource = "ca/uhn/hl7v2/protocol/impl/charset_map.properties";
 69  12
         InputStream in = MLLPTransport.class.getClassLoader().getResourceAsStream(resource);
 70  
         try {
 71  12
             mappings.load(in);
 72  0
         } catch (IOException e) {
 73  0
             throw new TransportException("Can't load character set mappings from " + resource, e);
 74  12
         }
 75  12
         return mappings;        
 76  
     }
 77  
 
 78  
     /** 
 79  
      * @see ca.uhn.hl7v2.protocol.AstractTransport#doSend(ca.uhn.hl7v2.protocol.Transportable)
 80  
      */
 81  
     public void doSend(Transportable theMessage) throws TransportException {
 82  
         try {
 83  7
             String charset = (String) theMessage.getMetadata().get("MSH-18");
 84  7
             if (charset != null) {
 85  1
                 charset = myCharsetMappings.getProperty(charset, charset); //default to self if no match
 86  1
                 myWriter.writeMessage(theMessage.getMessage(), charset); 
 87  
             } else {
 88  6
                 myWriter.writeMessage(theMessage.getMessage());
 89  
             }
 90  0
         } catch (LLPException e) {
 91  0
             throw new TransportException(e);
 92  0
         } catch (IOException e) {
 93  0
             throw new TransportException(e);
 94  7
         }
 95  7
     }
 96  
 
 97  
     /** 
 98  
      * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doReceive()
 99  
      */
 100  
     public Transportable doReceive() throws TransportException {
 101  26
         Transportable result = null;
 102  
         try {
 103  26
             String message = myReader.getMessage();
 104  23
             if (message != null) {
 105  6
                 result = new TransportableImpl(message);                
 106  
             }
 107  0
         } catch (LLPException e) {
 108  0
             throw new TransportException(e);
 109  3
         } catch (IOException e) {
 110  3
             throw new TransportException(e);
 111  23
         }
 112  23
         return result;
 113  
     }
 114  
     
 115  
     /**
 116  
      * @see ca.uhn.hl7v2.protocol.AbstractTransport#doConnect()
 117  
      */
 118  
     public void doConnect() throws TransportException {
 119  16
         myStreamSource.connect();
 120  
         try {
 121  10
             myReader = new MinLLPReader(myStreamSource.getInboundStream());
 122  10
             myWriter = new MinLLPWriter(myStreamSource.getOutboundStream());
 123  0
         } catch (IOException e) {
 124  0
             throw new TransportException(e);
 125  10
         } 
 126  10
     }
 127  
 
 128  
     /**
 129  
      * @see ca.uhn.hl7v2.protocol.TransportLayer#disconnect()
 130  
      */
 131  
     public void doDisconnect() throws TransportException {
 132  
         try {
 133  9
             if (myReader != null) myReader.close();
 134  9
             if (myWriter != null) myWriter.close();
 135  0
         } catch (IOException e) {
 136  0
             throw new TransportException(e);
 137  
         } finally {
 138  9
             myReader = null;
 139  9
             myWriter = null;
 140  9
         }        
 141  9
         myStreamSource.disconnect();
 142  9
     }
 143  
     
 144  
 }