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.bean; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.ExchangePattern; 021 import org.apache.camel.Expression; 022 import org.apache.camel.Predicate; 023 import org.apache.camel.component.bean.BeanHolder; 024 import org.apache.camel.component.bean.BeanProcessor; 025 import org.apache.camel.component.bean.ConstantBeanHolder; 026 import org.apache.camel.component.bean.RegistryBean; 027 import org.apache.camel.util.ObjectHelper; 028 029 /** 030 * Evaluates an expression using a bean method invocation 031 * 032 * @version $Revision: 751450 $ 033 */ 034 public class BeanExpression implements Expression, Predicate { 035 private String beanName; 036 private String method; 037 private Object bean; 038 039 public BeanExpression(Object bean, String method) { 040 this.bean = bean; 041 this.method = method; 042 } 043 044 public BeanExpression(String beanName, String method) { 045 this.beanName = beanName; 046 this.method = method; 047 } 048 049 @Override 050 public String toString() { 051 return "BeanExpression[bean:" + (bean == null ? beanName : bean) + " method: " + method + "]"; 052 } 053 054 protected String assertionFailureMessage(Exchange exchange) { 055 return "bean: " + beanName + " method: " + method; 056 } 057 058 public Object evaluate(Exchange exchange) { 059 // either use registry lookup or a constant bean 060 BeanHolder holder; 061 if (bean == null) { 062 holder = new RegistryBean(exchange.getContext(), beanName); 063 } else { 064 holder = new ConstantBeanHolder(bean, exchange.getContext()); 065 } 066 067 BeanProcessor processor = new BeanProcessor(holder); 068 if (method != null) { 069 processor.setMethod(method); 070 } 071 try { 072 Exchange newExchange = exchange.copy(); 073 // The BeanExperession always has a result regardless of the ExchangePattern, 074 // so I add a checker here to make sure we can get the result. 075 if (!newExchange.getPattern().isOutCapable()) { 076 newExchange.setPattern(ExchangePattern.InOut); 077 } 078 processor.process(newExchange); 079 return newExchange.getOut(true).getBody(); 080 } catch (Exception e) { 081 throw new RuntimeBeanExpressionException(exchange, beanName, method, e); 082 } 083 } 084 085 public <T> T evaluate(Exchange exchange, Class<T> type) { 086 Object result = evaluate(exchange); 087 return exchange.getContext().getTypeConverter().convertTo(type, result); 088 } 089 090 public boolean matches(Exchange exchange) { 091 Object value = evaluate(exchange); 092 return ObjectHelper.evaluateValuePredicate(value); 093 } 094 095 public void assertMatches(String text, Exchange exchange) throws AssertionError { 096 } 097 }