1 18 19 package org.apache.activemq.filter; 20 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 24 import javax.jms.DeliveryMode ; 25 import javax.jms.JMSException ; 26 27 import org.apache.activemq.command.ActiveMQDestination; 28 import org.apache.activemq.command.Message; 29 import org.apache.activemq.command.TransactionId; 30 import org.apache.activemq.util.JMSExceptionSupport; 31 32 37 public class PropertyExpression implements Expression { 38 39 interface SubExpression { 40 public Object evaluate(Message message); 41 } 42 43 static final private HashMap JMS_PROPERTY_EXPRESSIONS = new HashMap (); 44 static { 45 JMS_PROPERTY_EXPRESSIONS.put("JMSDestination", new SubExpression() { 46 public Object evaluate(Message message) { 47 ActiveMQDestination dest = message.getOriginalDestination(); 48 if( dest == null ) 49 dest = message.getDestination(); 50 if( dest == null ) 51 return null; 52 return dest.toString(); 53 } 54 }); 55 JMS_PROPERTY_EXPRESSIONS.put("JMSReplyTo", new SubExpression() { 56 public Object evaluate(Message message) { 57 if( message.getReplyTo() == null ) 58 return null; 59 return message.getReplyTo().toString(); 60 } 61 }); 62 JMS_PROPERTY_EXPRESSIONS.put("JMSType", new SubExpression() { 63 public Object evaluate(Message message) { 64 return message.getType(); 65 } 66 }); 67 JMS_PROPERTY_EXPRESSIONS.put("JMSDeliveryMode", new SubExpression() { 68 public Object evaluate(Message message) { 69 return new Integer (message.isPersistent() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT ); 70 } 71 }); 72 JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new SubExpression() { 73 public Object evaluate(Message message) { 74 return new Integer (message.getPriority()); 75 } 76 }); 77 JMS_PROPERTY_EXPRESSIONS.put("JMSMessageID", new SubExpression() { 78 public Object evaluate(Message message) { 79 if( message.getMessageId() == null ) 80 return null; 81 return message.getMessageId().toString(); 82 } 83 }); 84 JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new SubExpression() { 85 public Object evaluate(Message message) { 86 return new Long (message.getTimestamp()); 87 } 88 }); 89 JMS_PROPERTY_EXPRESSIONS.put("JMSCorrelationID", new SubExpression() { 90 public Object evaluate(Message message) { 91 return message.getCorrelationId(); 92 } 93 }); 94 JMS_PROPERTY_EXPRESSIONS.put("JMSExpiration", new SubExpression() { 95 public Object evaluate(Message message) { 96 return new Long (message.getExpiration()); 97 } 98 }); 99 JMS_PROPERTY_EXPRESSIONS.put("JMSPriority", new SubExpression() { 100 public Object evaluate(Message message) { 101 return new Integer (message.getPriority()); 102 } 103 }); 104 JMS_PROPERTY_EXPRESSIONS.put("JMSTimestamp", new SubExpression() { 105 public Object evaluate(Message message) { 106 return new Long (message.getTimestamp()); 107 } 108 }); 109 JMS_PROPERTY_EXPRESSIONS.put("JMSRedelivered", new SubExpression() { 110 public Object evaluate(Message message) { 111 return new Boolean (message.isRedelivered()); 112 } 113 }); 114 115 JMS_PROPERTY_EXPRESSIONS.put("JMSXDeliveryCount", new SubExpression() { 116 public Object evaluate(Message message) { 117 return new Integer (message.getRedeliveryCounter()+1); 118 } 119 }); 120 JMS_PROPERTY_EXPRESSIONS.put("JMSXGroupID", new SubExpression() { 121 public Object evaluate(Message message) { 122 return message.getGroupID(); 123 } 124 }); 125 JMS_PROPERTY_EXPRESSIONS.put("JMSXGroupSeq", new SubExpression() { 126 public Object evaluate(Message message) { 127 return new Integer (message.getGroupSequence()); 128 } 129 }); 130 JMS_PROPERTY_EXPRESSIONS.put("JMSXProducerTXID", new SubExpression() { 131 public Object evaluate(Message message) { 132 TransactionId txId = message.getOriginalTransactionId(); 133 if( txId==null) 134 txId = message.getTransactionId(); 135 if( txId==null ) 136 return null; 137 return new Integer (txId.toString()); 138 } 139 }); 140 } 141 142 private final String name; 143 private final SubExpression jmsPropertyExpression; 144 145 public PropertyExpression(String name) { 146 this.name = name; 147 jmsPropertyExpression = (SubExpression) JMS_PROPERTY_EXPRESSIONS.get(name); 148 } 149 150 public Object evaluate(MessageEvaluationContext message) throws JMSException { 151 try { 152 if( message.isDropped() ) 153 return null; 154 155 if( jmsPropertyExpression!=null ) 156 return jmsPropertyExpression.evaluate(message.getMessage()); 157 try { 158 return message.getMessage().getProperty(name); 159 } catch (IOException ioe) { 160 throw JMSExceptionSupport.create("Could not get property: "+name+" reason: "+ioe.getMessage(), ioe); 161 } 162 } catch (IOException e) { 163 throw JMSExceptionSupport.create(e); 164 } 165 166 } 167 168 public Object evaluate(Message message) throws JMSException { 169 if( jmsPropertyExpression!=null ) 170 return jmsPropertyExpression.evaluate(message); 171 try { 172 return message.getProperty(name); 173 } catch (IOException ioe) { 174 throw JMSExceptionSupport.create(ioe); 175 } 176 } 177 178 public String getName() { 179 return name; 180 } 181 182 183 186 public String toString() { 187 return name; 188 } 189 190 193 public int hashCode() { 194 return name.hashCode(); 195 } 196 197 200 public boolean equals(Object o) { 201 202 if (o == null || !this.getClass().equals(o.getClass())) { 203 return false; 204 } 205 return name.equals(((PropertyExpression) o).name); 206 207 } 208 209 } 210 | Popular Tags |