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: 820990 $
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        public Object evaluate(Exchange exchange) {
055            // either use registry lookup or a constant bean
056            BeanHolder holder;
057            if (bean == null) {
058                holder = new RegistryBean(exchange.getContext(), beanName);
059            } else {
060                holder = new ConstantBeanHolder(bean, exchange.getContext());
061            }
062    
063            BeanProcessor processor = new BeanProcessor(holder);
064            if (method != null) {
065                processor.setMethod(method);
066            }
067            try {
068                Exchange newExchange = exchange.copy();
069                // The BeanExperession always has a result regardless of the ExchangePattern,
070                // so I add a checker here to make sure we can get the result.
071                if (!newExchange.getPattern().isOutCapable()) {
072                    newExchange.setPattern(ExchangePattern.InOut);
073                }
074                processor.process(newExchange);
075                return newExchange.getOut().getBody();
076            } catch (Exception e) {
077                throw new RuntimeBeanExpressionException(exchange, beanName, method, e);
078            }
079        }
080    
081        public <T> T evaluate(Exchange exchange, Class<T> type) {
082            Object result = evaluate(exchange);
083            return exchange.getContext().getTypeConverter().convertTo(type, result);
084        }
085    
086        public boolean matches(Exchange exchange) {
087            Object value = evaluate(exchange);
088            return ObjectHelper.evaluateValuePredicate(value);
089        }
090    
091    }