summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/filter/XPathExpression.java
blob: 85402e0781714b55de26fe47116b46db3bf50521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.qpid.server.filter;
//
// Based on like named file from r450141 of the Apache ActiveMQ project <http://www.activemq.org/site/home.html>
//

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import javax.jms.JMSException;

//import org.apache.activemq.command.Message;
//import org.apache.activemq.util.JMSExceptionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.qpid.server.queue.AMQMessage;
import org.apache.qpid.AMQException;

/**
 * Used to evaluate an XPath Expression in a JMS selector.
 */
public final class XPathExpression implements BooleanExpression {

    private static final Log log = LogFactory.getLog(XPathExpression.class);
    private static final String EVALUATOR_SYSTEM_PROPERTY = "org.apache.qpid.server.filter.XPathEvaluatorClassName";
    private static final String DEFAULT_EVALUATOR_CLASS_NAME=XalanXPathEvaluator.class.getName();
    
    private static final Constructor EVALUATOR_CONSTRUCTOR;
    
    static {
        String cn = System.getProperty(EVALUATOR_SYSTEM_PROPERTY, DEFAULT_EVALUATOR_CLASS_NAME);        
        Constructor m = null;
        try {
            try {
                m = getXPathEvaluatorConstructor(cn);
            } catch (Throwable e) {
                log.warn("Invalid "+XPathEvaluator.class.getName()+" implementation: "+cn+", reason: "+e,e);
                cn = DEFAULT_EVALUATOR_CLASS_NAME;
                try {
                    m = getXPathEvaluatorConstructor(cn);
                } catch (Throwable e2) {
                    log.error("Default XPath evaluator could not be loaded",e);
                }
            }
        } finally {
            EVALUATOR_CONSTRUCTOR = m;
        }
    }

    private static Constructor getXPathEvaluatorConstructor(String cn) throws ClassNotFoundException, SecurityException, NoSuchMethodException {
        Class c = XPathExpression.class.getClassLoader().loadClass(cn);
        if( !XPathEvaluator.class.isAssignableFrom(c) ) {
            throw new ClassCastException(""+c+" is not an instance of "+XPathEvaluator.class);
        }
        return c.getConstructor(new Class[]{String.class});
    }
    
    private final String xpath;
    private final XPathEvaluator evaluator;
    
    static public interface XPathEvaluator {
        public boolean evaluate(AMQMessage message) throws AMQException;
    }    
    
    XPathExpression(String xpath) {
        this.xpath = xpath;
        this.evaluator = createEvaluator(xpath);
    }

    private XPathEvaluator createEvaluator(String xpath2) {
        try {
            return (XPathEvaluator)EVALUATOR_CONSTRUCTOR.newInstance(new Object[]{xpath});
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if( cause instanceof RuntimeException ) {
                throw (RuntimeException)cause;
            }
            throw new RuntimeException("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e);
        } catch (Throwable e) {
            throw new RuntimeException("Invalid XPath Expression: "+xpath+" reason: "+e.getMessage(), e);
        }
    }

    public Object evaluate(AMQMessage message) throws AMQException {
//        try {
//FIXME this is flow to disk work
//            if( message.isDropped() )
//                return null;
            return evaluator.evaluate(message) ? Boolean.TRUE : Boolean.FALSE;
//        } catch (IOException e) {
//
//            JMSException exception = new JMSException(e.getMessage());
//            exception.initCause(e);
//            throw exception;
//
//        }

    }

    public String toString() {
        return "XPATH "+ConstantExpression.encodeString(xpath);
    }
    
    /**
     * @param message
     * @return true if the expression evaluates to Boolean.TRUE.
     * @throws JMSException
     */
    public boolean matches(AMQMessage message) throws AMQException
    {
        Object object = evaluate(message);
        return object!=null && object==Boolean.TRUE;            
    }

}