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 "AbstactTransport.java".  Description: 
010    "A base implementation of TransportLayer." 
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.util.HashMap;
030    import java.util.Map;
031    
032    import ca.uhn.hl7v2.protocol.TransportException;
033    import ca.uhn.hl7v2.protocol.TransportLayer;
034    import ca.uhn.hl7v2.protocol.Transportable;
035    import ca.uhn.log.HapiLog;
036    import ca.uhn.log.HapiLogFactory;
037    
038    /**
039     * A base implementation of <code>TransportLayer</code> which looks after the 
040     * addition of common metadata to each inbound <code>Transportable</code>.
041     *   
042     * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
043     * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:26 $ by $Author: jamesagnew $
044     */
045    public abstract class AbstractTransport implements TransportLayer {
046        
047        private static HapiLog log = HapiLogFactory.getHapiLog(AbstractTransport.class);
048        
049        private Map myCommonMetadata;
050        private boolean myIsConnected = false;
051        
052        public AbstractTransport() {
053            myCommonMetadata = new HashMap();
054        }
055            
056        /**
057         * @see ca.uhn.hl7v2.protocol.TransportLayer#getCommonMetadata()
058         */
059        public Map getCommonMetadata() {
060            return myCommonMetadata;
061        }
062    
063        /**
064         * Delegates to <code>doReceive()</code> and adds common metadata
065         * to the resulting <code>Transportable</code> before it is returned.
066         */
067        public Transportable receive() throws TransportException {
068            if (!isConnected()) {
069                throw new TransportException("Can't receive because TransportLayer is not connected");
070            }
071            
072            Transportable message = doReceive();
073            if (message != null) {
074                message.getMetadata().putAll(myCommonMetadata);
075            }
076            
077            log.info("Received: " + (message == null ? null : message.getMessage()));
078                 
079            return message;
080        }
081    
082        /**
083         * Called by receive(), which then adds common metadata.   
084         * 
085         * @return Transportable the next available message 
086         * @throws TransportException
087         */    
088        public abstract Transportable doReceive() throws TransportException;
089        
090        /**
091         * Delegates to <code>doSend()</code> after checking that we are connected. 
092         * 
093         * @see ca.uhn.hl7v2.protocol.TransportLayer#send(Transportable) 
094         */
095        public void send(Transportable theMessage) throws TransportException {
096            if (!isConnected()) {
097                throw new TransportException("Can't send because TransportLayer is not connected");
098            }
099            
100            doSend(theMessage);
101            
102            log.info("Sent: " + (theMessage == null ? null : theMessage.getMessage()));
103        }
104        
105        /**
106         * The method send() delegates to this method after checking whether we are 
107         * connected.   
108         * @param theMessage
109         * @throws TransportException
110         */
111        public abstract void doSend(Transportable theMessage) throws TransportException;
112        
113        /**
114         * Sets isConnected() to false, then calls doConnect(), then sets isConnected() to 
115         * true. 
116         * @see ca.uhn.hl7v2.protocol.TransportLayer#connect()  
117         */
118        public void connect() throws TransportException {
119            myIsConnected = false;
120            doConnect();
121            myIsConnected = true;
122        }
123        
124        /**
125         * Performs connection as described in TransportLayer.connect().  The 
126         * connect() method of this class delegates to doConnect() after some
127         * internal housekeeping.
128         *   
129         * @throws TransportException
130         */
131        public abstract void doConnect() throws TransportException;
132        
133        /**
134         * @see ca.uhn.hl7v2.protocol.TransportLayer#isConnected()  
135         */
136        public boolean isConnected() {
137            return myIsConnected;
138        }
139        
140        /**
141         * @see ca.uhn.hl7v2.protocol.TransportLayer#disconnect()
142         */
143        public void disconnect() throws TransportException {
144            myIsConnected = false;
145            doDisconnect();
146        }
147        
148        /**
149         * Performs disconnection as described in TransportLayer.disconnect().  The 
150         * disconnect() method of this class delegates to doDisconnect() after some
151         * internal housekeeping.
152         *   
153         * @throws TransportException
154         */
155        public abstract void doDisconnect() throws TransportException;
156        
157    }