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.component.bean.BeanProcessor;
021 import org.apache.camel.component.bean.RegistryBean;
022 import org.apache.camel.impl.ExpressionSupport;
023
024 /**
025 * Evaluates an expression using a bean method invocation
026 *
027 * @version $Revision: 36321 $
028 */
029 public class BeanExpression<E extends Exchange> extends ExpressionSupport<E> {
030 private String beanName;
031 private String method;
032
033 public BeanExpression(String beanName, String method) {
034 this.beanName = beanName;
035 this.method = method;
036 }
037
038 @Override
039 public String toString() {
040 return "BeanExpression[bean: " + beanName + " method: " + method + "]";
041 }
042
043 protected String assertionFailureMessage(E exchange) {
044 return "bean: " + beanName + " method: " + method;
045 }
046
047 public Object evaluate(E exchange) {
048 BeanProcessor processor = new BeanProcessor(new RegistryBean(exchange.getContext(), beanName));
049 if (method != null) {
050 processor.setMethod(method);
051 }
052 try {
053 Exchange newExchange = exchange.copy();
054 processor.process(newExchange);
055 return newExchange.getOut(true).getBody();
056 } catch (Exception e) {
057 throw new RuntimeBeanExpressionException(exchange, beanName, method, e);
058 }
059 }
060 }