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 "LowerLayerProtocol.java".  Description: 
010    "Represents a particular "lower layer protocol" over which HL7 messages can be 
011      sent" 
012    
013    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
014    2001.  All Rights Reserved. 
015    
016    Contributor(s): ______________________________________. 
017    
018    Alternatively, the contents of this file may be used under the terms of the 
019    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
020    applicable instead of those above.  If you wish to allow use of your version of this 
021    file only under the terms of the GPL and not to allow others to use your version 
022    of this file under the MPL, indicate your decision by deleting  the provisions above 
023    and replace  them with the notice and other provisions required by the GPL License.  
024    If you do not delete the provisions above, a recipient may use your version of 
025    this file under either the MPL or the GPL. 
026    
027    */
028    
029    package ca.uhn.hl7v2.llp;
030    
031    import java.io.InputStream;
032    import java.io.OutputStream;
033    
034    import ca.uhn.log.HapiLog;
035    import ca.uhn.log.HapiLogFactory;
036    
037    /**
038     * Represents a particular "lower layer protocol" over which HL7 messages can be 
039     * sent.  An example is the "minimal lower layer protocol" defines in the HL7
040     * implementation guide (appendix C) - this is implemented by the class 
041     * MinLowerLayerProtocol.  Implementations should call the static method 
042     * <code>logCharacterReceived()</code> when a character is read from a remote system.  
043     * This method may or may not log receipt, as configured (see docs for this method).  
044     * @author  Bryan Tripp
045     */
046    public abstract class LowerLayerProtocol {
047    
048        private static final HapiLog log = HapiLogFactory.getHapiLog(LowerLayerProtocol.class);
049        private static boolean logChars = false;
050        
051        /** 
052         * Creates a new LowerLayerProtocol.  
053         */
054        protected LowerLayerProtocol() {
055            String logCharsProp = System.getProperty("ca.uhn.hl7v2.llp.logBytesRead");
056            if (logCharsProp != null && logCharsProp.equalsIgnoreCase("TRUE")) {
057                log.info("Setting ca.uhn.hl7v2.llp.logBytesRead to TRUE");
058                logChars = true;
059            }
060        }
061        
062        /** 
063         * Returns a particular implementation of LowerLayerProtocol.  Currently 
064         * MinLowerLayerProtocol is the default and there are no other options ... 
065         * the idea is that this will eventually be configurable.     
066         */
067        public static LowerLayerProtocol makeLLP() {
068            return new MinLowerLayerProtocol();
069        }
070        
071        /** 
072         * Returns an HL7Reader that implements message reading according to 
073         * this protocol.  
074         */ 
075        public abstract HL7Reader getReader(InputStream in) throws LLPException;
076              
077        /** 
078         * Returns an HL7Writer that implements message writing according to 
079         * this protocol.  
080         */ 
081        public abstract HL7Writer getWriter(OutputStream out) throws LLPException;
082        
083        /**
084         * Logs the fact that a character has been received, if configured to do so.  This is a 
085         * debuging feature.  This is enabled by setting the system property 
086         * ca.uhn.hl7v2.llp.logBytesRead=TRUE (before LowerLayerProtocol is instantiated).  
087         * A message is written to standard out for each character.  THIS IS VERY SLOW and should
088         * normally be turned off. 
089         */
090        public static void logCharacterReceived(int c) {
091            if (logChars) {
092                System.out.println("Char received: " + c + " (" + (char) c + ")");
093            }
094        }
095        
096    }