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 "AbstractJMSTransport.java". Description: 010"A TransportLayer that exchanges messages through JMS destinations." 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 javax.jms.Connection; 033import javax.jms.JMSException; 034import javax.jms.Message; 035import javax.jms.TextMessage; 036 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040import ca.uhn.hl7v2.protocol.TransportException; 041import ca.uhn.hl7v2.protocol.TransportLayer; 042import ca.uhn.hl7v2.protocol.Transportable; 043 044/** 045 * A <code>TransportLayer</code> that exchanges messages through JMS destinations. 046 * 047 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 048 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $ 049 */ 050public abstract class AbstractJMSTransport extends AbstractTransport implements TransportLayer { 051 052 private static final Logger log = LoggerFactory.getLogger(URLTransport.class); 053 054 public static final String CLIENT_ID_KEY = "CLIENT_ID"; 055 public static final String CONNECTION_METADATA_KEY = "CONNECTION_METADATA"; 056 public static final String DESTINATION_NAME_KEY = "DESTINATION_NAME"; 057 058 private final Map<String, Object> myMetadata; 059 060 public AbstractJMSTransport() { 061 myMetadata = makeMetadata(); 062 } 063 064 /** 065 * Sets common metadata on the basis of connection and destination. 066 */ 067 private Map<String, Object> makeMetadata() { 068 Map<String, Object> md = new HashMap<>(); 069 try { 070 md.put(CLIENT_ID_KEY, getConnection().getClientID()); 071 } catch (JMSException e) { 072 log.error("Error setting JMSTransport metadata", e); 073 } 074 075 try { 076 md.put(CONNECTION_METADATA_KEY, getConnection().getMetaData()); 077 } catch (JMSException e) { 078 log.error("Error setting JMSTransport metadata", e); 079 } 080 081 try { 082 md.put(DESTINATION_NAME_KEY, getDestinationName()); 083 } catch (JMSException e) { 084 log.error("Error setting JMSTransport metadata", e); 085 } 086 return md; 087 } 088 089 /** 090 * @return the name of the destination at which messages are 091 * written and read 092 */ 093 protected abstract String getDestinationName() throws JMSException; 094 095 /** 096 * @return the QueueConnection or TopicConnection over which messages 097 * are transported 098 */ 099 public abstract Connection getConnection(); 100 101 /** 102 * @return a new JMS Message created on the sending Session. 103 * @throws JMSException 104 */ 105 protected abstract Message getMessage() throws JMSException; 106 107 /** 108 * Sends a message to the underlying Destination 109 * 110 * @param theMessage 111 * @throws JMSException 112 */ 113 protected abstract void sendJMS(Message theMessage) throws JMSException; 114 115 /** 116 * @return the next available message from the underlying Destination 117 * @throws JMSException 118 */ 119 protected abstract Message receiveJMS() throws JMSException; 120 121// /** 122// * @param theDestination a Queue or Topic 123// * @return either getQueueName() or getTopicName() 124// */ 125// private static String getName(Destination theDestination) throws JMSException { 126// String name = null; 127// 128// if (theDestination instanceof Queue) { 129// name = ((Queue) theDestination).getQueueName(); 130// } else if (theDestination instanceof Topic) { 131// name = ((Topic) theDestination).getTopicName(); 132// } else { 133// throw new IllegalArgumentException("We don't support Destinations of type " 134// + theDestination.getClass().getName()); 135// } 136// return name; 137// } 138 139 /** 140 * @see AbstractTransport#doSend(ca.uhn.hl7v2.protocol.Transportable) 141 */ 142 public void doSend(Transportable theMessage) throws TransportException { 143 try { 144 Message message = toMessage(theMessage); 145 sendJMS(message); 146 } catch (JMSException e) { 147 throw new TransportException(e); 148 } 149 } 150 151 /** 152 * Fills a JMS message object with text and metadata from the given 153 * <code>Transportable</code>. The default implementation obtains a 154 * the Message from getMessage(), and expects this to be a TextMessage. 155 * Override this method if you want to use a different message type. 156 * 157 * @param theSource a Transportable from which to obtain data for filling the 158 * given Message 159 * @return a Message containing data from the given Transportable 160 */ 161 protected Message toMessage(Transportable theSource) throws TransportException { 162 Message message; 163 try { 164 message = getMessage(); 165 166 if ( !(message instanceof TextMessage)) { 167 throw new TransportException("This implementation expects getMessage() to return " 168 + " a TextMessage. Override this method if another message type is to be used"); 169 } 170 171 ((TextMessage) message).setText(theSource.getMessage()); 172 173 for (String key : theSource.getMetadata().keySet()) { 174 Object val = theSource.getMetadata().get(key); 175 message.setObjectProperty(key, val); 176 } 177 } catch (JMSException e) { 178 throw new TransportException(e); 179 } 180 181 return message; 182 } 183 184 /** 185 * Copies data from the given Message into a Transportable. The default 186 * implementation expects a TextMessage, but this can be overridden. 187 * 188 * @param theMessage a JMS Message from which to obtain data 189 * @return a Transportable containing data from the given Message 190 */ 191 protected Transportable toTransportable(Message theMessage) throws TransportException { 192 if ( !(theMessage instanceof TextMessage)) { 193 throw new TransportException("This implementation expects getMessage() to return " 194 + " a TextMessage. Override this method if another message type is to be used"); 195 } 196 197 Transportable result; 198 try { 199 String text = ((TextMessage) theMessage).getText(); 200 result = new TransportableImpl(text); 201 result.getMetadata().putAll(getCommonMetadata()); 202 } catch (JMSException e) { 203 throw new TransportException(e); 204 } 205 206 return result; 207 } 208 209 /** 210 * @see AbstractTransport#doReceive() 211 */ 212 public Transportable doReceive() throws TransportException { 213 Transportable result; 214 try { 215 Message message = receiveJMS(); 216 result = toTransportable(message); 217 } catch (JMSException e) { 218 throw new TransportException(e); 219 } 220 return result; 221 } 222 223 /** 224 * Returns metadata under the static keys defined by this class. 225 * 226 * @see ca.uhn.hl7v2.protocol.TransportLayer#getCommonMetadata() 227 */ 228 public Map<String, Object> getCommonMetadata() { 229 return myMetadata; 230 } 231 232}