1 10 11 package org.mule.providers.jms; 12 13 import java.util.Enumeration ; 14 15 import javax.jms.Destination ; 16 import javax.jms.JMSException ; 17 import javax.jms.Message ; 18 19 import org.mule.config.MuleProperties; 20 import org.mule.providers.AbstractMessageAdapter; 21 import org.mule.umo.MessagingException; 22 import org.mule.umo.provider.MessageTypeNotSupportedException; 23 24 31 public class JmsMessageAdapter extends AbstractMessageAdapter 32 { 33 36 private static final long serialVersionUID = -8151716840620558143L; 37 38 private String jmsSpec; 39 private Message jmsMessage; 40 41 public JmsMessageAdapter(Object message) throws MessagingException 42 { 43 super(); 44 this.setMessage(message); 45 } 46 47 public void setSpecification(String newSpec) 48 { 49 if (JmsConstants.JMS_SPECIFICATION_11.equals(newSpec) 50 || (JmsConstants.JMS_SPECIFICATION_102B.equals(newSpec))) 51 { 52 this.jmsSpec = newSpec; 53 } 54 else 55 56 { 57 throw new IllegalArgumentException ( 58 "JMS specification needs to be one of the defined values in JmsConstants but was: " + newSpec); 59 } 60 } 61 62 70 public String getPayloadAsString(String encoding) throws Exception 71 { 72 return new String (getPayloadAsBytes(), encoding); 73 } 74 75 81 public byte[] getPayloadAsBytes() throws Exception 82 { 83 return JmsMessageUtils.toByteArray(jmsMessage, jmsSpec); 84 } 85 86 89 public Object getPayload() 90 { 91 return jmsMessage; 92 } 93 94 97 private void setMessage(Object message) throws MessagingException 98 { 99 if (message instanceof Message ) 100 { 101 this.jmsMessage = (Message )message; 102 } 103 else 104 { 105 throw new MessageTypeNotSupportedException(message, getClass()); 106 } 107 108 try 109 { 110 String value = this.jmsMessage.getJMSCorrelationID(); 111 if (value != null) 112 { 113 setProperty(JmsConstants.JMS_CORRELATION_ID, value); 114 } 115 } 116 catch (JMSException e) 117 { 118 } 120 121 try 122 { 123 int value = this.jmsMessage.getJMSDeliveryMode(); 124 setProperty(JmsConstants.JMS_DELIVERY_MODE, new Integer (value)); 125 } 126 catch (JMSException e) 127 { 128 } 130 131 try 132 { 133 Destination value = this.jmsMessage.getJMSDestination(); 134 if (value != null) 135 { 136 setProperty(JmsConstants.JMS_DESTINATION, value); 137 } 138 } 139 catch (JMSException e) 140 { 141 } 143 144 try 145 { 146 long value = this.jmsMessage.getJMSExpiration(); 147 setProperty(JmsConstants.JMS_EXPIRATION, new Long (value)); 148 } 149 catch (JMSException e) 150 { 151 } 153 154 try 155 { 156 String value = this.jmsMessage.getJMSMessageID(); 157 if (value != null) 158 { 159 setProperty(JmsConstants.JMS_MESSAGE_ID, value); 160 } 161 } 162 catch (JMSException e) 163 { 164 } 166 167 try 168 { 169 int value = this.jmsMessage.getJMSPriority(); 170 setProperty(JmsConstants.JMS_PRIORITY, new Integer (value)); 171 } 172 catch (JMSException e) 173 { 174 } 176 177 try 178 { 179 boolean value = this.jmsMessage.getJMSRedelivered(); 180 setProperty(JmsConstants.JMS_REDELIVERED, Boolean.valueOf(value)); 181 } 182 catch (JMSException e) 183 { 184 } 186 187 try 188 { 189 Destination value = this.jmsMessage.getJMSReplyTo(); 190 if (value != null) 191 { 192 setProperty(JmsConstants.JMS_REPLY_TO, value); 193 } 194 } 195 catch (JMSException e) 196 { 197 } 199 200 try 201 { 202 long value = this.jmsMessage.getJMSTimestamp(); 203 setProperty(JmsConstants.JMS_TIMESTAMP, new Long (value)); 204 } 205 catch (JMSException e) 206 { 207 } 209 210 try 211 { 212 String value = this.jmsMessage.getJMSType(); 213 if (value != null) 214 { 215 setProperty(JmsConstants.JMS_TYPE, value); 216 } 217 } 218 catch (JMSException e) 219 { 220 } 222 223 try 224 { 225 Enumeration e = this.jmsMessage.getPropertyNames(); 226 while (e.hasMoreElements()) 227 { 228 String key = (String )e.nextElement(); 229 try 230 { 231 Object value = this.jmsMessage.getObjectProperty(key); 232 if (value != null) 233 { 234 setProperty(key, value); 235 } 236 } 237 catch (JMSException e1) 238 { 239 } 241 } 242 } 243 catch (JMSException e1) 244 { 245 } 247 } 248 249 public String getUniqueId() 250 { 251 return (String )getProperty(JmsConstants.JMS_MESSAGE_ID); 252 } 253 254 265 public void setCorrelationId(String id) 266 { 267 setProperty(JmsConstants.JMS_CORRELATION_ID, id); 268 } 269 270 282 public String getCorrelationId() 283 { 284 return (String )getProperty(JmsConstants.JMS_CORRELATION_ID); 285 } 286 287 295 public void setReplyTo(Object replyTo) 296 { 297 if (replyTo instanceof Destination ) 298 { 299 setProperty(JmsConstants.JMS_REPLY_TO, replyTo); 300 } 301 else 302 { 303 super.setReplyTo(replyTo); 304 } 305 } 306 307 315 public Object getReplyTo() 316 { 317 Object replyTo = getProperty(JmsConstants.JMS_REPLY_TO); 318 if (replyTo == null) 319 { 320 replyTo = getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); 321 } 322 return replyTo; 323 } 324 325 } 326 | Popular Tags |