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.juel;
018    
019    import javax.el.ELContext;
020    import javax.el.ExpressionFactory;
021    import javax.el.ValueExpression;
022    
023    import de.odysseus.el.util.SimpleContext;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Message;
027    import org.apache.camel.impl.ExpressionSupport;
028    
029    /**
030     * @version $Revision: $
031     */
032    public class JuelExpression extends ExpressionSupport<Exchange> {
033    
034        private final String expression;
035        private final Class<?> type;
036        private ExpressionFactory expressionFactory;
037    
038        public JuelExpression(String expression, Class<?> type) {
039            this.expression = expression;
040            this.type = type;
041        }
042    
043        public static JuelExpression el(String expression) {
044            return new JuelExpression(expression, Object.class);
045        }
046    
047        public Object evaluate(Exchange exchange) {
048            // TODO we could use caching here but then we'd have possible concurrency issues
049            // so lets assume that the provider caches
050            ELContext context = populateContext(createContext(), exchange);
051            ValueExpression valueExpression = getExpressionFactory().createValueExpression(context, expression, type);
052            return valueExpression.getValue(context);
053        }
054    
055        public ExpressionFactory getExpressionFactory() {
056            if (expressionFactory == null) {
057                expressionFactory = ExpressionFactory.newInstance();
058            }
059            return expressionFactory;
060        }
061    
062        public void setExpressionFactory(ExpressionFactory expressionFactory) {
063            this.expressionFactory = expressionFactory;
064        }
065    
066        protected ELContext populateContext(ELContext context, Exchange exchange) {
067            setVariable(context, "exchange", exchange, Exchange.class);
068            setVariable(context, "in", exchange.getIn(), Message.class);
069            setVariable(context, "out", exchange.getOut(), Message.class);
070            return context;
071        }
072    
073        protected void setVariable(ELContext context, String name, Object value, Class<?> type) {
074            ValueExpression valueExpression = getExpressionFactory().createValueExpression(value, type);
075            SimpleContext simpleContext = (SimpleContext) context;
076            simpleContext.setVariable(name, valueExpression);
077        }
078    
079        /**
080         * Factory method to create the EL context
081         */
082        protected ELContext createContext() {
083            return new SimpleContext();
084        }
085    
086        protected String assertionFailureMessage(Exchange exchange) {
087            return expression;
088        }
089    }