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     */
017    package org.apache.camel.language.jxpath;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Message;
022    import org.apache.camel.impl.ExpressionSupport;
023    import org.apache.camel.language.ExpressionEvaluationException;
024    import org.apache.commons.jxpath.CompiledExpression;
025    import org.apache.commons.jxpath.JXPathContext;
026    import org.apache.commons.jxpath.JXPathException;
027    
028    /**
029     * <a href="http://commons.apache.org/jxpath/">JXPath</a> {@link Expression} support 
030     */
031    public class JXPathExpression extends ExpressionSupport<Exchange> {
032    
033        private String expression;
034        private CompiledExpression compiledExpression;
035        private final Class<?> type;
036    
037        /**
038         * Creates a new JXPathExpression instance
039         * 
040         * @param expression the JXPath expression to be evaluated
041         * @param type the expected result type
042         */
043        public JXPathExpression(String expression, Class<?> type) {
044            super();
045            this.expression = expression;
046            this.type = type;
047        }
048    
049    
050        public Object evaluate(Exchange exchange) {
051            try {
052                JXPathContext context = JXPathContext.newContext(exchange);
053                Object result = getJXPathExpression().getValue(context, type);
054                assertResultType(exchange, result);
055                return result;
056            } catch (JXPathException e) {
057                throw new ExpressionEvaluationException(this, exchange, e);
058            }
059        }
060    
061        /*
062         * Check if the result is of the specified type
063         */
064        private void assertResultType(Exchange exchange, Object result) {
065            if (result != null && !type.isAssignableFrom(result.getClass())) {
066                throw new JXPathException("JXPath result type is " + result.getClass() + " instead of required type " + type);
067            }
068        }
069    
070        @Override
071        protected String assertionFailureMessage(Exchange exchange) {
072            return expression.toString();
073        }
074    
075        /*
076         * Get a compiled expression instance for better performance
077         */
078        private synchronized CompiledExpression getJXPathExpression() {
079            if (compiledExpression == null) {
080                compiledExpression = JXPathContext.compile(expression);
081            }
082            return compiledExpression;
083        }
084    }