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.mvel;
018    
019    import java.io.Serializable;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.ExpressionEvaluationException;
023    import org.apache.camel.ExpressionIllegalSyntaxException;
024    import org.apache.camel.impl.ExpressionSupport;
025    
026    public class MvelExpression extends ExpressionSupport {
027    
028        private final String expressionString;
029        private final Class<?> type;
030        private final Serializable compiled;
031    
032        public MvelExpression(MvelLanguage language, String expressionString, Class<?> type) {
033            this.expressionString = expressionString;
034            this.type = type;
035            try {
036                this.compiled = org.mvel2.MVEL.compileExpression(expressionString);
037            } catch (Exception e) {
038                throw new ExpressionIllegalSyntaxException(expressionString, e);
039            }
040        }
041    
042        public static MvelExpression mvel(String expression) {
043            return new MvelExpression(new MvelLanguage(), expression, Object.class);
044        }
045    
046        public <T> T evaluate(Exchange exchange, Class<T> tClass) {
047            try {
048                Object value = org.mvel2.MVEL.executeExpression(compiled, new RootObject(exchange));
049                return exchange.getContext().getTypeConverter().convertTo(tClass, value);
050            } catch (Exception e) {
051                throw new ExpressionEvaluationException(this, exchange, e);
052            }
053        }
054    
055        public Object evaluate(Exchange exchange) {
056            try {
057                return org.mvel2.MVEL.executeExpression(compiled, new RootObject(exchange));
058            } catch (Exception e) {
059                throw new ExpressionEvaluationException(this, exchange, e);
060            }
061        }
062        
063        public Class<?> getType() {
064            return type;
065        }
066    
067        protected String assertionFailureMessage(Exchange exchange) {
068            return expressionString;
069        }
070    
071        @Override
072        public String toString() {
073            return "Mvel[" + expressionString + "]";
074        }
075    }