1 57 58 package org.apache.wsif.util.jms; 59 60 import java.lang.reflect.InvocationTargetException; 61 import java.lang.reflect.Method; 62 import java.util.ArrayList; 63 import java.util.Arrays; 64 import java.util.HashMap; 65 import java.util.Iterator; 66 import java.util.Enumeration; 67 68 import javax.jms.Destination; 69 import javax.jms.JMSException; 70 import javax.jms.Message; 71 import javax.jms.MessageProducer; 72 import org.apache.wsif.WSIFException; 73 import org.apache.wsif.logging.Trc; 74 75 84 public class WSIFJMSProperties extends HashMap { 85 private static final long serialVersionUID = 1L; 86 public static final String IN = "in"; 87 public static final String OUT = "out"; 88 private static final ArrayList allDirections = 89 new ArrayList(Arrays.asList(new Object[] { IN, OUT })); 90 91 private static final String CORRELATIONID = "JMSCorrelationID"; 92 private static final String DELIVERYMODE = "JMSDeliveryMode"; 93 private static final String DESTINATION = "JMSDestination"; 94 private static final String EXPIRATION = "JMSExpiration"; 95 private static final String MESSAGEID = "JMSMessageID"; 96 private static final String PRIORITY = "JMSPriority"; 97 private static final String REDELIVERED = "JMSRedelivered"; 98 private static final String REPLYTO = "JMSReplyTo"; 99 private static final String TIMESTAMP = "JMSTimestamp"; 100 private static final String TIMETOLIVE = "JMSTimeToLive"; 101 private static final String TYPE = "JMSType"; 102 103 private static final ArrayList predefinedProps = 104 new ArrayList(Arrays.asList(new Object[]{ 105 CORRELATIONID,DELIVERYMODE,DESTINATION,EXPIRATION,PRIORITY, 106 REDELIVERED,REPLYTO,TIMESTAMP,TIMETOLIVE, TYPE})); 107 108 private String direction; 109 110 113 public WSIFJMSProperties(String direction) throws WSIFException { 114 super(); 115 Trc.entry(this, direction); 116 117 if (!allDirections.contains(direction)) 118 throw new WSIFException("Invalid direction " + direction); 119 this.direction = direction; 120 Trc.exit(deep()); 121 } 122 123 126 public WSIFJMSProperties(WSIFJMSProperties props) { 127 super(props); 128 Trc.entry(this, props); 129 direction = props.direction; 130 Trc.exit(deep()); 131 } 132 133 137 public boolean set(MessageProducer producer, Message message) 138 throws WSIFException { 139 Trc.entry(this, producer, message); 140 if (direction != IN) 141 throw new WSIFException("Only input properties can be set on a MessageProducer"); 142 143 if (isEmpty()) { 144 Trc.exit(false); 145 return false; 146 } 147 148 for (Iterator it = keySet().iterator(); it.hasNext();) 149 try { 150 String prop = (String) (it.next()); 151 Object value = get(prop); 152 Class type = value.getClass(); 153 154 if (predefinedProps.contains(prop)) 155 try { 156 if (prop.equals(CORRELATIONID)) { 157 message.setJMSCorrelationID((String) value); 158 } else if (prop.equals(DELIVERYMODE)) { 159 message.setJMSDeliveryMode( 160 ((Integer) value).intValue()); 161 producer.setDeliveryMode(((Integer) value).intValue()); 162 } else if (prop.equals(DESTINATION)) { 163 message.setJMSDestination((Destination) value); 164 } else if (prop.equals(EXPIRATION)) { 165 message.setJMSExpiration(((Long) value).longValue()); 166 } else if (prop.equals(PRIORITY)) { 167 message.setJMSPriority(((Integer) value).intValue()); 168 producer.setPriority(((Integer) value).intValue()); 169 } else if (prop.equals(REDELIVERED)) { 170 message.setJMSRedelivered( 171 ((Boolean) value).booleanValue()); 172 } else if (prop.equals(REPLYTO)) { 173 message.setJMSReplyTo((Destination) value); 174 } else if (prop.equals(TIMESTAMP)) { 175 message.setJMSTimestamp(((Long) value).longValue()); 176 } else if (prop.equals(TIMETOLIVE)) { 177 producer.setTimeToLive(((Long) value).longValue()); 178 } else if(prop.equals(TYPE)) { 179 message.setJMSType((String)value); 180 } 181 } catch (ClassCastException ce) { 182 Trc.exception(ce); 183 throw new WSIFException( 184 "Unexpected type " 185 + type 186 + " for JMS property " 187 + prop 188 + "."); 189 } 190 191 else { 193 if (type.equals(String.class)) 194 message.setStringProperty(prop, value.toString()); 195 else if (type.equals(Integer.class)) 196 message.setIntProperty(prop, ((Integer) value).intValue()); 197 else if (type.equals(Boolean.class)) 198 message.setBooleanProperty( 199 prop, 200 ((Boolean) value).booleanValue()); 201 else if (type.equals(Byte.class)) 202 message.setByteProperty(prop, ((Byte) value).byteValue()); 203 else if (type.equals(Double.class)) 204 message.setDoubleProperty( 205 prop, 206 ((Double) value).doubleValue()); 207 else if (type.equals(Float.class)) 208 message.setFloatProperty( 209 prop, 210 ((Float) value).floatValue()); 211 else if (type.equals(Long.class)) 212 message.setLongProperty(prop, ((Long) value).longValue()); 213 else if (type.equals(Short.class)) 214 message.setShortProperty( 215 prop, 216 ((Short) value).shortValue()); 217 else 218 message.setObjectProperty(prop, value); 219 } 220 } catch (JMSException je) { 221 Trc.exception(je); 222 throw WSIFJMSConstants.ToWsifException(je); 223 } 224 225 Trc.exit(true); 226 return true; 227 } 228 229 232 public void getPropertiesFromMessage(Message message) throws WSIFException { 233 Trc.entry(this, message); 234 if (direction != OUT) 235 throw new WSIFException("Only output properties can be got from a message"); 236 237 clear(); 238 try { 239 put(CORRELATIONID, message.getJMSCorrelationID()); 240 put(DELIVERYMODE, new Integer(message.getJMSDeliveryMode())); 241 put(DESTINATION, message.getJMSDestination()); 242 put(EXPIRATION, new Long(message.getJMSExpiration())); 243 put(MESSAGEID, message.getJMSMessageID()); 244 put(PRIORITY, new Integer(message.getJMSPriority())); 245 put(REDELIVERED, new Boolean(message.getJMSRedelivered())); 246 put(REPLYTO, message.getJMSReplyTo()); 247 put(TIMESTAMP, new Long(message.getJMSTimestamp())); 248 put(TYPE, message.getJMSType()); 249 250 Enumeration enum = message.getPropertyNames(); 251 while (enum.hasMoreElements()) { 252 String name = (String) enum.nextElement(); 253 put(name, message.getObjectProperty(name)); 254 } 255 } catch (JMSException je) { 256 Trc.exception(je); 257 throw WSIFJMSConstants.ToWsifException(je); 258 } 259 260 if (Trc.ON) 261 Trc.exit(deep()); 262 } 263 264 public void clear() { 265 Trc.entry(this); 266 super.clear(); 267 Trc.exit(); 268 } 269 270 public Object get(Object o1) { 271 Trc.entry(this, o1); 272 Object o2 = super.get(o1); 273 Trc.exit(o2); 274 return o2; 275 } 276 277 public Object put(Object o1, Object o2) { 278 Trc.entry(this, o1, o2); 279 Object o3 = super.put(o1, o2); 280 Trc.exit(o3); 281 return o3; 282 } 283 284 public void putAll(HashMap hm) { 285 Trc.entry(this, hm); 286 super.putAll(hm); 287 Trc.exit(); 288 } 289 290 public String toString() { 291 return "WSIFJMSProperties(" + size() + "," + hashCode() + ")"; 292 } 293 294 public String deep() { 295 String buff = ""; 296 try { 297 buff = new String(super.toString() + "\n"); 298 buff += "direction:" 299 + (direction.equals(IN) ? "in" : direction.equals(OUT) ? "out" : "unknown"); 300 301 } catch (Exception e) { 302 Trc.exceptionInTrace(e); 303 } 304 return buff; 305 } 306 } | Popular Tags |