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.model.language;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.Expression;
026    import org.apache.camel.Predicate;
027    import org.apache.camel.language.bean.BeanExpression;
028    import org.apache.camel.spi.RouteContext;
029    import org.apache.camel.util.ObjectHelper;
030    
031    /**
032     * For expressions and predicates using the
033     * <a href="http://camel.apache.org/bean-language.html">bean language</a>
034     *
035     * @version $Revision: 775155 $
036     */
037    @XmlRootElement(name = "method")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class MethodCallExpression extends ExpressionDefinition {
040        @XmlAttribute(required = false)
041        private String bean;
042        @XmlAttribute(required = false)
043        private String method;
044        @XmlTransient
045        // we don't need to support the beanType class in Spring
046        private Class beanType;
047        
048    
049        public MethodCallExpression() {
050        }
051    
052        public MethodCallExpression(String beanName) {
053            super(beanName);
054        }
055    
056        public MethodCallExpression(String beanName, String method) {
057            super(beanName);
058            this.method = method;
059        }
060        
061        public MethodCallExpression(Class type) {
062            super(type.toString());
063            this.beanType = type;        
064        }
065        
066        public MethodCallExpression(Class type, String method) {
067            super(type.toString());
068            this.beanType = type;
069            this.method = method;
070        }
071    
072        public String getLanguage() {
073            return "bean";
074        }
075    
076        public String getMethod() {
077            return method;
078        }
079    
080        public void setMethod(String method) {
081            this.method = method;
082        }
083    
084        @SuppressWarnings("unchecked")
085        @Override
086        public Expression createExpression(RouteContext routeContext) {
087            if (beanType != null) {            
088                return new BeanExpression(ObjectHelper.newInstance(beanType), getMethod());
089            } else {
090                return new BeanExpression(beanName(), getMethod());   
091            }
092        }
093    
094        @SuppressWarnings("unchecked")
095        @Override
096        public Predicate createPredicate(RouteContext routeContext) {
097            if (beanType != null) {
098                return new BeanExpression(ObjectHelper.newInstance(beanType), getMethod());
099            } else {
100                return new BeanExpression(beanName(), getMethod());
101            }
102        }
103    
104        protected String beanName() {
105            if (bean != null) {
106                return bean;
107            }
108            return getExpression();
109        }
110    
111        @Override
112        public String toString() {
113            return "bean{" + beanName() + (method != null ? ", method=" + method : "") + "}";
114        }
115    }