001/**
002The 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. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "LowerLayerProtocol.java".  Description: 
010"Represents a particular "lower layer protocol" over which HL7 messages can be 
011  sent" 
012
013The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0142001.  All Rights Reserved. 
015
016Contributor(s): ______________________________________. 
017
018Alternatively, the contents of this file may be used under the terms of the 
019GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
020applicable instead of those above.  If you wish to allow use of your version of this 
021file only under the terms of the GPL and not to allow others to use your version 
022of this file under the MPL, indicate your decision by deleting  the provisions above 
023and replace  them with the notice and other provisions required by the GPL License.  
024If you do not delete the provisions above, a recipient may use your version of 
025this file under either the MPL or the GPL. 
026
027*/
028
029package ca.uhn.hl7v2.llp;
030
031import java.io.InputStream;
032import java.io.OutputStream;
033
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
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 */
046public abstract class LowerLayerProtocol {
047
048    private static final Logger log = LoggerFactory.getLogger(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     * <p>
085     * Logs the fact that a character has been received, if configured to do so.  This is a 
086     * debugging feature.  This is enabled by setting the system property 
087     * ca.uhn.hl7v2.llp.logBytesRead=TRUE (before LowerLayerProtocol is instantiated).  
088     * A message is logged for each character.  THIS IS VERY SLOW and should
089     * normally be turned off. 
090     * </p><p>
091     * Note that as of HAPI 1.3, this will be logged using commons-logging at
092     * a level of "info" instead of being printed to System.out
093     * </p>
094     */
095    public static void logCharacterReceived(int c) {
096        if (logChars) {
097            log.info("Char received: " + c + " (" + (char) c + ")");
098        }
099    }
100    
101}
102