1 22 package org.jboss.mq.selectors; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 27 import javax.jms.DeliveryMode ; 28 import javax.jms.InvalidSelectorException ; 29 import javax.jms.JMSException ; 30 31 import org.jboss.logging.Logger; 32 import org.jboss.mq.SpyMessage; 33 import org.jboss.util.NestedRuntimeException; 34 35 45 public class Selector 46 { 47 48 static Logger cat = Logger.getLogger(Selector.class); 49 50 51 private static Class parserClass = SelectorParser.class; 52 53 54 public static final String USE_NUMERIC_DELIVERY_MODE = "org.jboss.mq.selectors.useNumericDeliveryMode"; 55 56 57 private static boolean useNumericDeliveryMode; 58 59 public String selector; 60 61 public HashMap identifiers; 62 63 public Object result; 64 65 private Class resultType; 66 67 static 68 { 69 try 70 { 71 String property = System.getProperty(USE_NUMERIC_DELIVERY_MODE, "false"); 72 useNumericDeliveryMode = Boolean.valueOf(property).booleanValue(); 73 } 74 catch (Exception ignored) 75 { 76 cat.trace("Cannot get property " + USE_NUMERIC_DELIVERY_MODE, ignored); 77 } 78 } 79 80 84 public static Class getSelectorParserClass() 85 { 86 return Selector.parserClass; 87 } 88 89 96 public static void setSelectorParserClass(Class parserClass) 97 { 98 Selector.parserClass = parserClass; 99 } 100 101 public Selector(String sel) throws InvalidSelectorException 102 { 103 selector = sel; 104 identifiers = new HashMap (); 105 106 try 107 { 108 ISelectorParser bob = (ISelectorParser) parserClass.newInstance(); 109 result = bob.parse(sel, identifiers); 110 resultType = result.getClass(); 111 } 112 catch (Exception e) 113 { 114 InvalidSelectorException exception = new InvalidSelectorException ("The selector is invalid: " + sel); 115 exception.setLinkedException(e); 116 throw exception; 117 } 118 catch (Error e) 119 { 120 InvalidSelectorException exception = new InvalidSelectorException ("The selector is invalid: " + sel); 121 exception.setLinkedException(new NestedRuntimeException(e)); 122 throw exception; 123 } 124 } 125 126 public synchronized boolean test(SpyMessage.Header mes) throws JMSException 127 { 128 try 129 { 130 Iterator i = identifiers.values().iterator(); 132 133 while (i.hasNext()) 134 { 135 Identifier id = (Identifier) i.next(); 136 Object find = mes.jmsProperties.get(id.name); 137 138 if (find == null) 139 find = getHeaderFieldReferences(mes, id.name); 140 141 if (find == null) 142 id.value = null; 143 else 144 { 145 Class type = find.getClass(); 146 if (type.equals(Boolean .class) || 147 type.equals(String .class) || 148 type.equals(Double .class) || 149 type.equals(Float .class) || 150 type.equals(Integer .class) || 151 type.equals(Long .class) || 152 type.equals(Short .class) || 153 type.equals(Byte .class)) 154 id.value = find; 155 else 156 throw new Exception ("Bad property '" + id.name + "' type: " + type); 157 } 158 } 159 160 Object res; 162 163 if (resultType.equals(Identifier.class)) 164 res = ((Identifier)result).value; 165 else if (resultType.equals(Operator.class)) 166 { 167 Operator op = (Operator) result; 168 res = op.apply(); 169 } 170 else 171 res = result; 172 173 if (res == null) 174 return false; 175 176 if (!(res.getClass().equals(Boolean .class))) 177 throw new Exception ("Bad object type: " + res); 178 179 return ((Boolean ) res).booleanValue(); 180 } 181 catch (Exception e) 182 { 183 cat.warn("Invalid selector: " + selector, e); 184 return false; 185 } 186 } 187 188 public boolean test(SpyMessage msg) throws JMSException 189 { 190 return test(msg.header); 191 } 192 193 private Object getHeaderFieldReferences(SpyMessage.Header header, String idName) 195 throws JMSException 196 { 197 if (idName.equals("JMSDeliveryMode")) 202 { 203 if (useNumericDeliveryMode) 204 return new Integer (header.jmsDeliveryMode); 205 else if (header.jmsDeliveryMode == DeliveryMode.NON_PERSISTENT) 207 return "NON_PERSISTENT"; 208 else 209 return "PERSISTENT"; 210 } 211 else if (idName.equals("JMSPriority")) 212 return new Integer (header.jmsPriority); 213 else if (idName.equals("JMSMessageID")) 214 return header.jmsMessageID; 215 else if (idName.equals("JMSTimestamp")) 216 return new Long (header.jmsTimeStamp); 217 else if (idName.equals("JMSCorrelationID")) 218 return header.jmsCorrelationIDString; 219 else if (idName.equals("JMSType")) 220 return header.jmsType; 221 else 222 return null; 223 } 224 } 225 | Popular Tags |