Coverage Report - org.apache.camel.language.ognl.OgnlExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
OgnlExpression
70% 
N/A 
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.ognl;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import ognl.Ognl;
 23  
 import ognl.OgnlContext;
 24  
 import ognl.OgnlException;
 25  
 
 26  
 import org.apache.camel.Exchange;
 27  
 import org.apache.camel.impl.ExpressionSupport;
 28  
 import org.apache.camel.language.ExpressionEvaluationException;
 29  
 import org.apache.camel.language.IllegalSyntaxException;
 30  
 
 31  
 /**
 32  
  * @version $Revision: $
 33  
  */
 34  
 public class OgnlExpression extends ExpressionSupport<Exchange> {
 35  
 
 36  
     private final String expressionString;
 37  
     private final Class<?> type;
 38  
     private Object expression;
 39  
 
 40  6
     public OgnlExpression(OgnlLanguage language, String expressionString, Class<?> type) {
 41  6
         this.expressionString = expressionString;
 42  6
         this.type = type;
 43  
         try {
 44  6
             this.expression = Ognl.parseExpression(expressionString);
 45  0
         } catch (OgnlException e) {
 46  0
             throw new IllegalSyntaxException(language, expressionString);
 47  6
         }
 48  6
     }
 49  
 
 50  
     public static OgnlExpression ognl(String expression) {
 51  0
         return new OgnlExpression(new OgnlLanguage(), expression, Object.class);
 52  
     }
 53  
 
 54  
     public Object evaluate(Exchange exchange) {
 55  
         // TODO we could use caching here but then we'd have possible
 56  
         // concurrency issues
 57  
         // so lets assume that the provider caches
 58  6
         Map values = new HashMap();
 59  6
         populateContext(values, exchange);
 60  6
         OgnlContext oglContext = new OgnlContext();
 61  
         try {
 62  6
             return Ognl.getValue(expression, oglContext, new RootObject(exchange));
 63  0
         } catch (OgnlException e) {
 64  0
             throw new ExpressionEvaluationException(this, exchange, e);
 65  
         }
 66  
     }
 67  
 
 68  
     protected void populateContext(Map map, Exchange exchange) {
 69  6
         map.put("exchange", exchange);
 70  6
         map.put("in", exchange.getIn());
 71  6
         map.put("out", exchange.getOut());
 72  6
     }
 73  
 
 74  
     protected String assertionFailureMessage(Exchange exchange) {
 75  0
         return expressionString;
 76  
     }
 77  
 }