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;
033import java.nio.charset.Charset;
034
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * Represents a particular "lower layer protocol" over which HL7 messages can be 
040 * sent.  An example is the "minimal lower layer protocol" defines in the HL7
041 * implementation guide (appendix C) - this is implemented by the class 
042 * MinLowerLayerProtocol.  Implementations should call the static method 
043 * <code>logCharacterReceived()</code> when a character is read from a remote system.  
044 * This method may or may not log receipt, as configured (see docs for this method).
045 *
046 * @author  Bryan Tripp
047 */
048public abstract class LowerLayerProtocol {
049
050    private static final Logger log = LoggerFactory.getLogger(LowerLayerProtocol.class);
051    protected Charset charset;
052
053    /** 
054     * Returns a particular implementation of LowerLayerProtocol.
055     * 
056     *  @deprecated as there is now the choice between {@link MinLowerLayerProtocol}
057     *  and {@link ExtendedMinLowerLayerProtocol}.
058     */
059    public static LowerLayerProtocol makeLLP() {
060        return new MinLowerLayerProtocol();
061    }
062
063    /**
064     * Returns a particular implementation of LowerLayerProtocol
065     * @param respectMSH18
066     * @return LowerLayerProtocol implementation
067     */
068    public static LowerLayerProtocol makeLLP(boolean respectMSH18) {
069        return new MinLowerLayerProtocol(respectMSH18);
070    }
071    
072    /** 
073     * Returns an HL7Reader that implements message reading according to 
074     * this protocol.  
075     */ 
076    public abstract HL7Reader getReader(InputStream in) throws LLPException;
077          
078    /** 
079     * Returns an HL7Writer that implements message writing according to 
080     * this protocol.  
081     */ 
082    public abstract HL7Writer getWriter(OutputStream out) throws LLPException;
083    
084    /**
085     * <p>
086     * Logs the fact that a character has been received, if configured to do so.
087     * </p>
088     * <p>
089     * This logging is enabled by configuring the underlying log system to allow
090     * the logger named "<code>ca.uhn.hl7v2.llp.LowerLayerProtocol</code>" to log
091     * events at a level of "trace".
092     * </p>
093     */
094    public static void logCharacterReceived(int c) {
095        log.trace("Char received: {} ({})", c, (char) c);
096    }
097
098    /**
099     * Provides a charset to use for character encoding
100     * @param theCharset The charset to use
101     * @since 1.3
102     */
103    public void setCharset(Charset theCharset) {
104        charset = theCharset;
105    }
106
107    /**
108     * Provides a charset to use for character encoding
109     * @param charsetName The name of the charset to use
110     * @since 2.1
111     */
112    public void setCharset(String charsetName) {
113        charset = Charset.forName(charsetName);
114    }
115}
116