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 */ 017package org.apache.camel.model.language; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.spi.Metadata; 026import org.apache.camel.util.ObjectHelper; 027 028/** 029 * Call a method of the specified Java bean passing the Exchange, Body or specific headers to it. 030 */ 031@Metadata(firstVersion = "1.3.0", label = "language,core,java", title = "Bean method") 032@XmlRootElement(name = "method") 033@XmlAccessorType(XmlAccessType.FIELD) 034public class MethodCallExpression extends ExpressionDefinition { 035 @XmlAttribute 036 private String ref; 037 @XmlAttribute 038 private String method; 039 @XmlAttribute(name = "beanType") 040 private String beanTypeName; 041 @XmlTransient 042 private Class<?> beanType; 043 @XmlTransient 044 private Object instance; 045 046 public MethodCallExpression() { 047 } 048 049 public MethodCallExpression(String beanName) { 050 this(beanName, null); 051 } 052 053 public MethodCallExpression(String beanName, String method) { 054 super((String)null); // we dont use @XmlValue but the attributes instead 055 if (beanName != null && beanName.startsWith("ref:")) { 056 beanName = beanName.substring(4); 057 } else if (beanName != null && beanName.startsWith("bean:")) { 058 beanName = beanName.substring(5); 059 } 060 setRef(beanName); 061 setMethod(method); 062 } 063 064 public MethodCallExpression(Object instance) { 065 this(instance, null); 066 } 067 068 public MethodCallExpression(Object instance, String method) { 069 super((String)null); // we dont use @XmlValue but the attributes instead 070 // must use setter as they have special logic 071 setInstance(instance); 072 setMethod(method); 073 } 074 075 public MethodCallExpression(Class<?> type) { 076 this(type, null); 077 } 078 079 public MethodCallExpression(Class<?> type, String method) { 080 super((String)null); // we dont use @XmlValue but the attributes instead 081 setBeanType(type); 082 setBeanTypeName(type.getName()); 083 setMethod(method); 084 } 085 086 @Override 087 public String getLanguage() { 088 return "bean"; 089 } 090 091 public String getRef() { 092 return ref; 093 } 094 095 /** 096 * Reference to bean to lookup in the registry 097 */ 098 public void setRef(String ref) { 099 this.ref = ref; 100 } 101 102 public String getMethod() { 103 return method; 104 } 105 106 /** 107 * Name of method to call 108 */ 109 public void setMethod(String method) { 110 this.method = method; 111 } 112 113 public Class<?> getBeanType() { 114 return beanType; 115 } 116 117 public void setBeanType(Class<?> beanType) { 118 this.beanType = beanType; 119 this.instance = null; 120 } 121 122 public String getBeanTypeName() { 123 return beanTypeName; 124 } 125 126 /** 127 * Class name of the bean to use 128 */ 129 public void setBeanTypeName(String beanTypeName) { 130 this.beanTypeName = beanTypeName; 131 } 132 133 public Object getInstance() { 134 return instance; 135 } 136 137 public void setInstance(Object instance) { 138 // people may by mistake pass in a class type as the instance 139 if (instance instanceof Class) { 140 this.beanType = (Class<?>)instance; 141 this.instance = null; 142 } else { 143 this.beanType = null; 144 this.instance = instance; 145 } 146 } 147 148 private String beanName() { 149 if (ref != null) { 150 return ref; 151 } else if (instance != null) { 152 return ObjectHelper.className(instance); 153 } 154 return getExpression(); 155 } 156 157 @Override 158 public String toString() { 159 boolean isRef = ref != null; 160 return "bean[" + (isRef ? "ref:" : "") + beanName() + (method != null ? " method:" + method : "") + "]"; 161 } 162 163}