1 18 package org.apache.activemq.filter; 19 20 import java.io.IOException ; 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.InvocationTargetException ; 23 24 import javax.jms.JMSException ; 25 26 import org.apache.activemq.command.Message; 27 import org.apache.activemq.util.JMSExceptionSupport; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 34 public final class XPathExpression implements BooleanExpression { 35 36 private static final Log log = LogFactory.getLog(XPathExpression.class); 37 private static final String EVALUATOR_SYSTEM_PROPERTY = "org.apache.activemq.XPathEvaluatorClassName"; 38 private static final String DEFAULT_EVALUATOR_CLASS_NAME=XalanXPathEvaluator.class.getName(); 39 40 private static final Constructor EVALUATOR_CONSTRUCTOR; 41 42 static { 43 String cn = System.getProperty(EVALUATOR_SYSTEM_PROPERTY, DEFAULT_EVALUATOR_CLASS_NAME); 44 Constructor m = null; 45 try { 46 try { 47 m = getXPathEvaluatorConstructor(cn); 48 } catch (Throwable e) { 49 log.warn("Invalid "+XPathEvaluator.class.getName()+" implementation: "+cn+", reason: "+e,e); 50 cn = DEFAULT_EVALUATOR_CLASS_NAME; 51 try { 52 m = getXPathEvaluatorConstructor(cn); 53 } catch (Throwable e2) { 54 log.error("Default XPath evaluator could not be loaded",e); 55 } 56 } 57 } finally { 58 EVALUATOR_CONSTRUCTOR = m; 59 } 60 } 61 62 private static Constructor getXPathEvaluatorConstructor(String cn) throws ClassNotFoundException , SecurityException , NoSuchMethodException { 63 Class c = XPathExpression.class.getClassLoader().loadClass(cn); 64 if( !XPathEvaluator.class.isAssignableFrom(c) ) { 65 throw new ClassCastException (""+c+" is not an instance of "+XPathEvaluator.class); 66 } 67 return c.getConstructor(new Class []{String .class}); 68 } 69 70 private final String xpath; 71 private final XPathEvaluator evaluator; 72 73 static public interface XPathEvaluator { 74 public boolean evaluate(Message message) throws JMSException ; 75 } 76 77 XPathExpression(String xpath) { 78 this.xpath = xpath; 79 this.evaluator = createEvaluator(xpath); 80 } 81 82 private XPathEvaluator createEvaluator(String xpath2) { 83 try { 84 return (XPathEvaluator)EVALUATOR_CONSTRUCTOR.newInstance(new Object []{xpath}); 85 } catch (InvocationTargetException e) { 86 Throwable cause = e.getCause(); 87 if( cause instanceof RuntimeException ) { 88 throw (RuntimeException )cause; 89 } 90 throw new RuntimeException ("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e); 91 } catch (Throwable e) { 92 throw new RuntimeException ("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e); 93 } 94 } 95 96 public Object evaluate(MessageEvaluationContext message) throws JMSException { 97 try { 98 if( message.isDropped() ) 99 return null; 100 return evaluator.evaluate(message.getMessage()) ? Boolean.TRUE : Boolean.FALSE; 101 } catch (IOException e) { 102 throw JMSExceptionSupport.create(e); 103 } 104 105 } 106 107 public String toString() { 108 return "XPATH "+ConstantExpression.encodeString(xpath); 109 } 110 111 116 public boolean matches(MessageEvaluationContext message) throws JMSException { 117 Object object = evaluate(message); 118 return object!=null && object==Boolean.TRUE; 119 } 120 121 } 122 | Popular Tags |