001    /*
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "MLLPTransport.java".  Description: 
010    "An implementation of the HL7 Minimal Lower Layer Protocol." 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2004.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL. 
025    */
026    
027    package ca.uhn.hl7v2.protocol.impl;
028    
029    import java.io.IOException;
030    import java.io.InputStream;
031    import java.util.Properties;
032    
033    import ca.uhn.hl7v2.llp.LLPException;
034    import ca.uhn.hl7v2.llp.MinLLPReader;
035    import ca.uhn.hl7v2.llp.MinLLPWriter;
036    import ca.uhn.hl7v2.protocol.StreamSource;
037    import ca.uhn.hl7v2.protocol.TransportException;
038    import ca.uhn.hl7v2.protocol.TransportLayer;
039    import ca.uhn.hl7v2.protocol.Transportable;
040    
041    /**
042     * An implementation of the HL7 Minimal Lower Layer Protocol (see 
043     * HL7 implementation guide appendix C).  Note that this is the most common 
044     * protocol used in HL7 interfaces.  
045     * 
046     * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
047     * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:26 $ by $Author: jamesagnew $
048     */
049    public class MLLPTransport extends AbstractTransport implements TransportLayer {
050    
051        private MinLLPReader myReader;
052        private MinLLPWriter myWriter;
053        
054        private StreamSource myStreamSource;
055        private Properties myCharsetMappings;
056        
057        /**
058         * @param theStreamSource the provider of input and output streams connected
059         *      to the remote server 
060         */
061        public MLLPTransport(StreamSource theStreamSource) throws TransportException {
062            myStreamSource = theStreamSource;
063            myCharsetMappings = loadCharsetMappings();
064        }
065        
066        private static Properties loadCharsetMappings() throws TransportException {
067            Properties mappings = new Properties();
068            String resource = "ca/uhn/hl7v2/protocol/impl/charset_map.properties";
069            InputStream in = MLLPTransport.class.getClassLoader().getResourceAsStream(resource);
070            try {
071                mappings.load(in);
072            } catch (IOException e) {
073                throw new TransportException("Can't load character set mappings from " + resource, e);
074            }
075            return mappings;        
076        }
077    
078        /** 
079         * @see ca.uhn.hl7v2.protocol.AstractTransport#doSend(ca.uhn.hl7v2.protocol.Transportable)
080         */
081        public void doSend(Transportable theMessage) throws TransportException {
082            try {
083                String charset = (String) theMessage.getMetadata().get("MSH-18");
084                if (charset != null) {
085                    charset = myCharsetMappings.getProperty(charset, charset); //default to self if no match
086                    myWriter.writeMessage(theMessage.getMessage(), charset); 
087                } else {
088                    myWriter.writeMessage(theMessage.getMessage());
089                }
090            } catch (LLPException e) {
091                throw new TransportException(e);
092            } catch (IOException e) {
093                throw new TransportException(e);
094            }
095        }
096    
097        /** 
098         * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doReceive()
099         */
100        public Transportable doReceive() throws TransportException {
101            Transportable result = null;
102            try {
103                String message = myReader.getMessage();
104                if (message != null) {
105                    result = new TransportableImpl(message);                
106                }
107            } catch (LLPException e) {
108                throw new TransportException(e);
109            } catch (IOException e) {
110                throw new TransportException(e);
111            }
112            return result;
113        }
114        
115        /**
116         * @see ca.uhn.hl7v2.protocol.AbstractTransport#doConnect()
117         */
118        public void doConnect() throws TransportException {
119            myStreamSource.connect();
120            try {
121                myReader = new MinLLPReader(myStreamSource.getInboundStream());
122                myWriter = new MinLLPWriter(myStreamSource.getOutboundStream());
123            } catch (IOException e) {
124                throw new TransportException(e);
125            } 
126        }
127    
128        /**
129         * @see ca.uhn.hl7v2.protocol.TransportLayer#disconnect()
130         */
131        public void doDisconnect() throws TransportException {
132            try {
133                if (myReader != null) myReader.close();
134                if (myWriter != null) myWriter.close();
135            } catch (IOException e) {
136                throw new TransportException(e);
137            } finally {
138                myReader = null;
139                myWriter = null;
140            }        
141            myStreamSource.disconnect();
142        }
143        
144    }