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