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.spel;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.ExpressionEvaluationException;
021    import org.apache.camel.impl.ExpressionSupport;
022    import org.apache.camel.spring.SpringCamelContext;
023    import org.springframework.context.ApplicationContext;
024    import org.springframework.context.expression.BeanFactoryResolver;
025    import org.springframework.expression.EvaluationContext;
026    import org.springframework.expression.Expression;
027    import org.springframework.expression.ParserContext;
028    import org.springframework.expression.common.TemplateParserContext;
029    import org.springframework.expression.spel.standard.SpelExpressionParser;
030    import org.springframework.expression.spel.support.StandardEvaluationContext;
031    
032    /**
033     * Class responsible for evaluating <a href="http://static.springsource.org
034     * /spring/docs/current/spring-framework-reference/html/expressions.html">
035     * Spring Expression Language</a> in the context of Camel.
036     */
037    public class SpelExpression extends ExpressionSupport {
038    
039        private final String expressionString;
040        private final Class<?> type;
041    
042        // SpelExpressionParser is thread-safe according to the docs
043        private final SpelExpressionParser expressionParser;
044    
045        public SpelExpression(String expressionString, Class<?> type) {
046            this.expressionString = expressionString;
047            this.type = type;
048            this.expressionParser = new SpelExpressionParser();
049        }
050    
051        public static SpelExpression spel(String expression) {
052            return new SpelExpression(expression, Object.class);
053        }
054    
055        public <T> T evaluate(Exchange exchange, Class<T> tClass) {
056            try {
057                Expression expression = parseExpression();
058                EvaluationContext evaluationContext = createEvaluationContext(exchange);
059                Object value = expression.getValue(evaluationContext);
060                // Let Camel handle the type conversion
061                return exchange.getContext().getTypeConverter().convertTo(tClass, value);
062            } catch (Exception e) {
063                throw new ExpressionEvaluationException(this, exchange, e);
064            }
065        }
066    
067        private EvaluationContext createEvaluationContext(Exchange exchange) {
068            StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
069            if (exchange.getContext() instanceof SpringCamelContext) {
070                // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
071                ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext()).getApplicationContext();
072                evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
073            }
074            return evaluationContext;
075        }
076    
077        private Expression parseExpression() {
078            // Support template parsing with #{ } delimiters
079            ParserContext parserContext = new TemplateParserContext();
080            Expression expression = expressionParser.parseExpression(expressionString, parserContext);
081            return expression;
082        }
083    
084        public Class<?> getType() {
085            return type;
086        }
087    
088        protected String assertionFailureMessage(Exchange exchange) {
089            return expressionString;
090        }
091    
092        @Override
093        public String toString() {
094            return "SpelExpression[" + expressionString + "]";
095        }
096    }