1 /* 2 The contents of this file are subject to the Mozilla Public License Version 1.1 3 (the "License"); you may not use this file except in compliance with the License. 4 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 5 Software distributed under the License is distributed on an "AS IS" basis, 6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 7 specific language governing rights and limitations under the License. 8 9 The Original Code is "AbstactTransport.java". Description: 10 "A base implementation of TransportLayer." 11 12 The Initial Developer of the Original Code is University Health Network. Copyright (C) 13 2004. All Rights Reserved. 14 15 Contributor(s): ______________________________________. 16 17 Alternatively, the contents of this file may be used under the terms of the 18 GNU General Public License (the "GPL"), in which case the provisions of the GPL are 19 applicable instead of those above. If you wish to allow use of your version of this 20 file only under the terms of the GPL and not to allow others to use your version 21 of this file under the MPL, indicate your decision by deleting the provisions above 22 and replace them with the notice and other provisions required by the GPL License. 23 If you do not delete the provisions above, a recipient may use your version of 24 this file under either the MPL or the GPL. 25 */ 26 27 package ca.uhn.hl7v2.protocol.impl; 28 29 import java.util.HashMap; 30 import java.util.Map; 31 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 import ca.uhn.hl7v2.protocol.TransportException; 36 import ca.uhn.hl7v2.protocol.TransportLayer; 37 import ca.uhn.hl7v2.protocol.Transportable; 38 39 /** 40 * A base implementation of <code>TransportLayer</code> which looks after the 41 * addition of common metadata to each inbound <code>Transportable</code>. 42 * 43 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 44 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $ 45 */ 46 public abstract class AbstractTransport implements TransportLayer { 47 48 private static final Logger log = LoggerFactory.getLogger(AbstractTransport.class); 49 50 private final Map<String, Object> myCommonMetadata; 51 private boolean myIsConnected = false; 52 53 public AbstractTransport() { 54 myCommonMetadata = new HashMap<>(); 55 } 56 57 /** 58 * @see ca.uhn.hl7v2.protocol.TransportLayer#getCommonMetadata() 59 */ 60 public Map<String, Object> getCommonMetadata() { 61 return myCommonMetadata; 62 } 63 64 /** 65 * Delegates to <code>doReceive()</code> and adds common metadata 66 * to the resulting <code>Transportable</code> before it is returned. 67 */ 68 public Transportable receive() throws TransportException { 69 if (!isConnected()) { 70 throw new TransportException("Can't receive because TransportLayer is not connected"); 71 } 72 73 Transportable message = doReceive(); 74 if (message != null) { 75 message.getMetadata().putAll(myCommonMetadata); 76 } 77 78 log.debug("Received: {} ", (message == null ? null : message.getMessage())); 79 80 return message; 81 } 82 83 /** 84 * Called by receive(), which then adds common metadata. 85 * 86 * @return Transportable the next available message 87 * @throws TransportException 88 */ 89 public abstract Transportable doReceive() throws TransportException; 90 91 /** 92 * Delegates to <code>doSend()</code> after checking that we are connected. 93 * 94 * @see ca.uhn.hl7v2.protocol.TransportLayer#send(Transportable) 95 */ 96 public void send(Transportable theMessage) throws TransportException { 97 if (!isConnected()) { 98 throw new TransportException("Can't send because TransportLayer is not connected"); 99 } 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 }