KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > XPathExpression


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.filter;
19
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24 import javax.jms.JMSException JavaDoc;
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 /**
32  * Used to evaluate an XPath Expression in a JMS selector.
33  */

34 public final class XPathExpression implements BooleanExpression {
35
36     private static final Log log = LogFactory.getLog(XPathExpression.class);
37     private static final String JavaDoc EVALUATOR_SYSTEM_PROPERTY = "org.apache.activemq.XPathEvaluatorClassName";
38     private static final String JavaDoc DEFAULT_EVALUATOR_CLASS_NAME=XalanXPathEvaluator.class.getName();
39     
40     private static final Constructor JavaDoc EVALUATOR_CONSTRUCTOR;
41     
42     static {
43         String JavaDoc cn = System.getProperty(EVALUATOR_SYSTEM_PROPERTY, DEFAULT_EVALUATOR_CLASS_NAME);
44         Constructor JavaDoc m = null;
45         try {
46             try {
47                 m = getXPathEvaluatorConstructor(cn);
48             } catch (Throwable JavaDoc 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 JavaDoc 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 JavaDoc getXPathEvaluatorConstructor(String JavaDoc cn) throws ClassNotFoundException JavaDoc, SecurityException JavaDoc, NoSuchMethodException JavaDoc {
63         Class JavaDoc c = XPathExpression.class.getClassLoader().loadClass(cn);
64         if( !XPathEvaluator.class.isAssignableFrom(c) ) {
65             throw new ClassCastException JavaDoc(""+c+" is not an instance of "+XPathEvaluator.class);
66         }
67         return c.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
68     }
69     
70     private final String JavaDoc xpath;
71     private final XPathEvaluator evaluator;
72     
73     static public interface XPathEvaluator {
74         public boolean evaluate(Message message) throws JMSException JavaDoc;
75     }
76     
77     XPathExpression(String JavaDoc xpath) {
78         this.xpath = xpath;
79         this.evaluator = createEvaluator(xpath);
80     }
81
82     private XPathEvaluator createEvaluator(String JavaDoc xpath2) {
83         try {
84             return (XPathEvaluator)EVALUATOR_CONSTRUCTOR.newInstance(new Object JavaDoc[]{xpath});
85         } catch (InvocationTargetException JavaDoc e) {
86             Throwable JavaDoc cause = e.getCause();
87             if( cause instanceof RuntimeException JavaDoc ) {
88                 throw (RuntimeException JavaDoc)cause;
89             }
90             throw new RuntimeException JavaDoc("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e);
91         } catch (Throwable JavaDoc e) {
92             throw new RuntimeException JavaDoc("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e);
93         }
94     }
95
96     public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
97         try {
98             if( message.isDropped() )
99                 return null;
100             return evaluator.evaluate(message.getMessage()) ? Boolean.TRUE : Boolean.FALSE;
101         } catch (IOException JavaDoc e) {
102             throw JMSExceptionSupport.create(e);
103         }
104
105     }
106
107     public String JavaDoc toString() {
108         return "XPATH "+ConstantExpression.encodeString(xpath);
109     }
110     
111     /**
112      * @param message
113      * @return true if the expression evaluates to Boolean.TRUE.
114      * @throws JMSException
115      */

116     public boolean matches(MessageEvaluationContext message) throws JMSException JavaDoc {
117         Object JavaDoc object = evaluate(message);
118         return object!=null && object==Boolean.TRUE;
119     }
120
121 }
122
Popular Tags