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 "AbstactTransport.java". Description: 010"A base implementation of TransportLayer." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132004. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025*/ 026 027package ca.uhn.hl7v2.protocol.impl; 028 029import java.util.HashMap; 030import java.util.Map; 031 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035import ca.uhn.hl7v2.protocol.TransportException; 036import ca.uhn.hl7v2.protocol.TransportLayer; 037import ca.uhn.hl7v2.protocol.Transportable; 038 039/** 040 * A base implementation of <code>TransportLayer</code> which looks after the 041 * addition of common metadata to each inbound <code>Transportable</code>. 042 * 043 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 044 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $ 045 */ 046public abstract class AbstractTransport implements TransportLayer { 047 048 private static Logger log = LoggerFactory.getLogger(AbstractTransport.class); 049 050 private Map<String, Object> myCommonMetadata; 051 private boolean myIsConnected = false; 052 053 public AbstractTransport() { 054 myCommonMetadata = new HashMap<String, Object>(); 055 } 056 057 /** 058 * @see ca.uhn.hl7v2.protocol.TransportLayer#getCommonMetadata() 059 */ 060 public Map<String, Object> getCommonMetadata() { 061 return myCommonMetadata; 062 } 063 064 /** 065 * Delegates to <code>doReceive()</code> and adds common metadata 066 * to the resulting <code>Transportable</code> before it is returned. 067 */ 068 public Transportable receive() throws TransportException { 069 if (!isConnected()) { 070 throw new TransportException("Can't receive because TransportLayer is not connected"); 071 } 072 073 Transportable message = doReceive(); 074 if (message != null) { 075 message.getMetadata().putAll(myCommonMetadata); 076 } 077 078 log.debug("Received: {} ", (message == null ? null : message.getMessage())); 079 080 return message; 081 } 082 083 /** 084 * Called by receive(), which then adds common metadata. 085 * 086 * @return Transportable the next available message 087 * @throws TransportException 088 */ 089 public abstract Transportable doReceive() throws TransportException; 090 091 /** 092 * Delegates to <code>doSend()</code> after checking that we are connected. 093 * 094 * @see ca.uhn.hl7v2.protocol.TransportLayer#send(Transportable) 095 */ 096 public void send(Transportable theMessage) throws TransportException { 097 if (!isConnected()) { 098 throw new TransportException("Can't send because TransportLayer is not connected"); 099 } 100 101 doSend(theMessage); 102 103 log.debug("Sent: {}", (theMessage == null ? null : theMessage.getMessage())); 104 } 105 106 /** 107 * The method send() delegates to this method after checking whether we are 108 * connected. 109 * @param theMessage 110 * @throws TransportException 111 */ 112 public abstract void doSend(Transportable theMessage) throws TransportException; 113 114 /** 115 * Sets isConnected() to false, then calls doConnect(), then sets isConnected() to 116 * true. 117 * @see ca.uhn.hl7v2.protocol.TransportLayer#connect() 118 */ 119 public void connect() throws TransportException { 120 myIsConnected = false; 121 doConnect(); 122 myIsConnected = true; 123 } 124 125 /** 126 * Performs connection as described in TransportLayer.connect(). The 127 * connect() method of this class delegates to doConnect() after some 128 * internal housekeeping. 129 * 130 * @throws TransportException 131 */ 132 public abstract void doConnect() throws TransportException; 133 134 /** 135 * @see ca.uhn.hl7v2.protocol.TransportLayer#isConnected() 136 */ 137 public boolean isConnected() { 138 return myIsConnected; 139 } 140 141 /** 142 * @see ca.uhn.hl7v2.protocol.TransportLayer#disconnect() 143 */ 144 public void disconnect() throws TransportException { 145 myIsConnected = false; 146 doDisconnect(); 147 } 148 149 /** 150 * Performs disconnection as described in TransportLayer.disconnect(). The 151 * disconnect() method of this class delegates to doDisconnect() after some 152 * internal housekeeping. 153 * 154 * @throws TransportException 155 */ 156 public abstract void doDisconnect() throws TransportException; 157 158}