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