Coverage Report - org.apache.camel.language.juel.JuelExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
JuelExpression
83% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.language.juel;
 18  
 
 19  
 import javax.el.ELContext;
 20  
 import javax.el.ExpressionFactory;
 21  
 import javax.el.ValueExpression;
 22  
 
 23  
 import de.odysseus.el.util.SimpleContext;
 24  
 
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Message;
 27  
 import org.apache.camel.impl.ExpressionSupport;
 28  
 
 29  
 /**
 30  
  * @version $Revision: $
 31  
  */
 32  
 public class JuelExpression extends ExpressionSupport<Exchange> {
 33  
 
 34  
     private final String expression;
 35  
     private final Class<?> type;
 36  
     private ExpressionFactory expressionFactory;
 37  
 
 38  3
     public JuelExpression(String expression, Class<?> type) {
 39  3
         this.expression = expression;
 40  3
         this.type = type;
 41  3
     }
 42  
 
 43  
     public static JuelExpression el(String expression) {
 44  0
         return new JuelExpression(expression, Object.class);
 45  
     }
 46  
 
 47  
     public Object evaluate(Exchange exchange) {
 48  
         // TODO we could use caching here but then we'd have possible concurrency issues
 49  
         // so lets assume that the provider caches
 50  3
         ELContext context = populateContext(createContext(), exchange);
 51  3
         ValueExpression valueExpression = getExpressionFactory().createValueExpression(context, expression, type);
 52  3
         return valueExpression.getValue(context);
 53  
     }
 54  
 
 55  
     public ExpressionFactory getExpressionFactory() {
 56  12
         if (expressionFactory == null) {
 57  3
             expressionFactory = ExpressionFactory.newInstance();
 58  
         }
 59  12
         return expressionFactory;
 60  
     }
 61  
 
 62  
     public void setExpressionFactory(ExpressionFactory expressionFactory) {
 63  0
         this.expressionFactory = expressionFactory;
 64  0
     }
 65  
 
 66  
     protected ELContext populateContext(ELContext context, Exchange exchange) {
 67  3
         setVariable(context, "exchange", exchange, Exchange.class);
 68  3
         setVariable(context, "in", exchange.getIn(), Message.class);
 69  3
         setVariable(context, "out", exchange.getOut(), Message.class);
 70  3
         return context;
 71  
     }
 72  
 
 73  
     protected void setVariable(ELContext context, String name, Object value, Class<?> type) {
 74  9
         ValueExpression valueExpression = getExpressionFactory().createValueExpression(value, type);
 75  9
         SimpleContext simpleContext = (SimpleContext) context;
 76  9
         simpleContext.setVariable(name, valueExpression);
 77  9
     }
 78  
 
 79  
     /**
 80  
      * Factory method to create the EL context
 81  
      */
 82  
     protected ELContext createContext() {
 83  3
         return new SimpleContext();
 84  
     }
 85  
 
 86  
     protected String assertionFailureMessage(Exchange exchange) {
 87  0
         return expression;
 88  
     }
 89  
 }