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    @SuppressWarnings("deprecation")
038    public class SpelExpression extends ExpressionSupport {
039    
040        private final String expressionString;
041        private final Class<?> type;
042    
043        // SpelExpressionParser is thread-safe according to the docs
044        private final SpelExpressionParser expressionParser;
045    
046        public SpelExpression(String expressionString, Class<?> type) {
047            this.expressionString = expressionString;
048            this.type = type;
049            this.expressionParser = new SpelExpressionParser();
050        }
051    
052        public static SpelExpression spel(String expression) {
053            return new SpelExpression(expression, Object.class);
054        }
055    
056        public <T> T evaluate(Exchange exchange, Class<T> tClass) {
057            try {
058                Expression expression = parseExpression();
059                EvaluationContext evaluationContext = createEvaluationContext(exchange);
060                Object value = expression.getValue(evaluationContext);
061                // Let Camel handle the type conversion
062                return exchange.getContext().getTypeConverter().convertTo(tClass, value);
063            } catch (Exception e) {
064                throw new ExpressionEvaluationException(this, exchange, e);
065            }
066        }
067    
068        private EvaluationContext createEvaluationContext(Exchange exchange) {
069            StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
070            if (exchange.getContext() instanceof SpringCamelContext) {
071                // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
072                ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext()).getApplicationContext();
073                evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
074            }
075            return evaluationContext;
076        }
077    
078        private Expression parseExpression() {
079            // Support template parsing with #{ } delimiters
080            ParserContext parserContext = new TemplateParserContext();
081            Expression expression = expressionParser.parseExpression(expressionString, parserContext);
082            return expression;
083        }
084    
085        public Class<?> getType() {
086            return type;
087        }
088    
089        protected String assertionFailureMessage(Exchange exchange) {
090            return expressionString;
091        }
092    
093        @Override
094        public String toString() {
095            return "SpelExpression[" + expressionString + "]";
096        }
097    }