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.component.bean.BeanHolder;
022    import org.apache.camel.component.bean.BeanProcessor;
023    import org.apache.camel.component.bean.ConstantBeanHolder;
024    import org.apache.camel.component.bean.RegistryBean;
025    import org.apache.camel.impl.ExpressionSupport;
026    
027    /**
028     * Evaluates an expression using a bean method invocation
029     *
030     * @version $Revision: 47154 $
031     */
032    public class BeanExpression<E extends Exchange> extends ExpressionSupport<E> {
033        private String beanName;
034        private String method;
035        private Object bean;
036    
037        public BeanExpression(Object bean, String method) {
038            this.bean = bean;
039            this.method = method;
040        }
041    
042        public BeanExpression(String beanName, String method) {
043            this.beanName = beanName;
044            this.method = method;
045        }
046    
047        @Override
048        public String toString() {
049            return "BeanExpression[bean:" + (bean == null ? beanName : bean) + " method: " + method + "]";
050        }
051    
052        protected String assertionFailureMessage(E exchange) {
053            return "bean: " + beanName + " method: " + method;
054        }
055    
056        public Object evaluate(E exchange) {
057            // either use registry lookup or a constant bean
058            BeanHolder holder;
059            if (bean == null) {
060                holder = new RegistryBean(exchange.getContext(), beanName);
061            } else {
062                holder = new ConstantBeanHolder(bean, exchange.getContext());
063            }
064    
065            BeanProcessor processor = new BeanProcessor(holder);
066            if (method != null) {
067                processor.setMethod(method);
068            }
069            try {
070                Exchange newExchange = exchange.copy();
071                // The BeanExperession always has a result regardless of the ExchangePattern,
072                // so I add a checker here to make sure we can get the result.
073                if (!newExchange.getPattern().isOutCapable()) {
074                    newExchange.setPattern(ExchangePattern.InOut);
075                }
076                processor.process(newExchange);
077                return newExchange.getOut(true).getBody();
078            } catch (Exception e) {
079                throw new RuntimeBeanExpressionException(exchange, beanName, method, e);
080            }
081        }
082    }