001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.filter;
018
019import java.io.IOException;
020import java.lang.reflect.Constructor;
021import java.lang.reflect.InvocationTargetException;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025import java.util.Properties;
026
027import javax.jms.JMSException;
028import javax.xml.parsers.DocumentBuilder;
029import javax.xml.parsers.DocumentBuilderFactory;
030import javax.xml.parsers.ParserConfigurationException;
031
032import org.apache.activemq.command.Message;
033import org.apache.activemq.util.JMSExceptionSupport;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Used to evaluate an XPath Expression in a JMS selector.
039 */
040public final class XPathExpression implements BooleanExpression {
041
042    private static final Logger LOG = LoggerFactory.getLogger(XPathExpression.class);
043    private static final String EVALUATOR_SYSTEM_PROPERTY = "org.apache.activemq.XPathEvaluatorClassName";
044    private static final String DEFAULT_EVALUATOR_CLASS_NAME = "org.apache.activemq.filter.XalanXPathEvaluator";
045    public static final String DOCUMENT_BUILDER_FACTORY_FEATURE = "org.apache.activemq.documentBuilderFactory.feature";
046
047    private static final Constructor EVALUATOR_CONSTRUCTOR;
048    private static DocumentBuilder builder = null;
049
050    static {
051        String cn = System.getProperty(EVALUATOR_SYSTEM_PROPERTY, DEFAULT_EVALUATOR_CLASS_NAME);
052        Constructor m = null;
053        try {
054            try {
055                m = getXPathEvaluatorConstructor(cn);
056                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
057                builderFactory.setNamespaceAware(true);
058                builderFactory.setIgnoringElementContentWhitespace(true);
059                builderFactory.setIgnoringComments(true);
060                try {
061                    // set some reasonable defaults
062                    builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
063                    builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
064                    builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
065                } catch (ParserConfigurationException e) {
066                    LOG.warn("Error setting document builder factory feature", e);
067                }
068                // setup the feature from the system property
069                setupFeatures(builderFactory);
070                builder = builderFactory.newDocumentBuilder();
071            } catch (Throwable e) {
072                LOG.warn("Invalid " + XPathEvaluator.class.getName() + " implementation: " + cn + ", reason: " + e, e);
073                cn = DEFAULT_EVALUATOR_CLASS_NAME;
074                try {
075                    m = getXPathEvaluatorConstructor(cn);
076                } catch (Throwable e2) {
077                    LOG.error("Default XPath evaluator could not be loaded", e);
078                }
079            }
080        } finally {
081            EVALUATOR_CONSTRUCTOR = m;
082        }
083    }
084
085    private final String xpath;
086    private final XPathEvaluator evaluator;
087
088    public static interface XPathEvaluator {
089        boolean evaluate(Message message) throws JMSException;
090    }
091
092    XPathExpression(String xpath) {
093        this.xpath = xpath;
094        this.evaluator = createEvaluator(xpath);
095    }
096
097    private static Constructor getXPathEvaluatorConstructor(String cn) throws ClassNotFoundException, SecurityException, NoSuchMethodException {
098        Class c = XPathExpression.class.getClassLoader().loadClass(cn);
099        if (!XPathEvaluator.class.isAssignableFrom(c)) {
100            throw new ClassCastException("" + c + " is not an instance of " + XPathEvaluator.class);
101        }
102        return c.getConstructor(new Class[] {String.class, DocumentBuilder.class});
103    }
104
105    protected static void setupFeatures(DocumentBuilderFactory factory) {
106        Properties properties = System.getProperties();
107        List<String> features = new ArrayList<String>();
108        for (Map.Entry<Object, Object> prop : properties.entrySet()) {
109            String key = (String) prop.getKey();
110            if (key.startsWith(DOCUMENT_BUILDER_FACTORY_FEATURE)) {
111                String uri = key.split(DOCUMENT_BUILDER_FACTORY_FEATURE + ":")[1];
112                Boolean value = Boolean.valueOf((String)prop.getValue());
113                try {
114                    factory.setFeature(uri, value);
115                    features.add("feature " + uri + " value " + value);
116                } catch (ParserConfigurationException e) {
117                    LOG.warn("DocumentBuilderFactory doesn't support the feature {} with value {}, due to {}.", new Object[]{uri, value, e});
118                }
119            }
120        }
121        if (features.size() > 0) {
122            StringBuffer featureString = new StringBuffer();
123            // just log the configured feature
124            for (String feature : features) {
125                if (featureString.length() != 0) {
126                    featureString.append(", ");
127                }
128                featureString.append(feature);
129            }
130        }
131
132    }
133
134    private XPathEvaluator createEvaluator(String xpath2) {
135        try {
136            return (XPathEvaluator)EVALUATOR_CONSTRUCTOR.newInstance(new Object[] {xpath, builder});
137        } catch (InvocationTargetException e) {
138            Throwable cause = e.getCause();
139            if (cause instanceof RuntimeException) {
140                throw (RuntimeException)cause;
141            }
142            throw new RuntimeException("Invalid XPath Expression: " + xpath + " reason: " + e.getMessage(), e);
143        } catch (Throwable e) {
144            throw new RuntimeException("Invalid XPath Expression: " + xpath + " reason: " + e.getMessage(), e);
145        }
146    }
147
148    public Object evaluate(MessageEvaluationContext message) throws JMSException {
149        try {
150            if (message.isDropped()) {
151                return null;
152            }
153            return evaluator.evaluate(message.getMessage()) ? Boolean.TRUE : Boolean.FALSE;
154        } catch (IOException e) {
155            throw JMSExceptionSupport.create(e);
156        }
157
158    }
159
160    public String toString() {
161        return "XPATH " + ConstantExpression.encodeString(xpath);
162    }
163
164    /**
165     * @param message
166     * @return true if the expression evaluates to Boolean.TRUE.
167     * @throws JMSException
168     */
169    public boolean matches(MessageEvaluationContext message) throws JMSException {
170        Object object = evaluate(message);
171        return object != null && object == Boolean.TRUE;
172    }
173
174}