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;
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.Processor;
026 import org.apache.camel.component.bean.BeanProcessor;
027 import org.apache.camel.component.bean.RegistryBean;
028 import org.apache.camel.spi.RouteContext;
029 import org.apache.camel.util.CamelContextHelper;
030 import org.apache.camel.util.ObjectHelper;
031
032 /**
033 * Represents an XML <bean/> element
034 *
035 * @version $Revision: 751357 $
036 */
037 @XmlRootElement(name = "bean")
038 @XmlAccessorType(XmlAccessType.FIELD)
039 public class BeanDefinition extends OutputDefinition<ProcessorDefinition> {
040 @XmlAttribute(required = false)
041 private String ref;
042 @XmlAttribute(required = false)
043 private String method;
044 @XmlAttribute(required = false)
045 private Class<?> beanType;
046 @XmlTransient
047 private Object bean;
048
049 public BeanDefinition() {
050 }
051
052 public BeanDefinition(String ref) {
053 this.ref = ref;
054 }
055
056 public BeanDefinition(String ref, String method) {
057 this.ref = ref;
058 this.method = method;
059 }
060
061 @Override
062 public String toString() {
063 return "Bean[" + getLabel() + "]";
064 }
065
066 @Override
067 public String getShortName() {
068 return "bean";
069 }
070
071 public String getRef() {
072 return ref;
073 }
074
075 public void setRef(String ref) {
076 this.ref = ref;
077 }
078
079 public String getMethod() {
080 return method;
081 }
082
083 public void setMethod(String method) {
084 this.method = method;
085 }
086
087 public void setBean(Object bean) {
088 this.bean = bean;
089 }
090
091 public Class getBeanType() {
092 return beanType;
093 }
094
095 public void setBeanType(Class beanType) {
096 this.beanType = beanType;
097 }
098
099 // Fluent API
100 //-------------------------------------------------------------------------
101 /**
102 * Sets the ref String on camel bean
103 *
104 * @param ref the bean's id in the registry
105 * @return the builder
106 */
107 public BeanDefinition ref(String ref) {
108 setRef(ref);
109 return this;
110 }
111
112 /**
113 * Sets the calling method name of camel bean
114 *
115 * @param method the bean's method name which wants camel to call
116 * @return the builder
117 */
118 public BeanDefinition method(String method) {
119 setMethod(method);
120 return this;
121 }
122
123 /**
124 * Sets the bean's instance that camel to call
125 *
126 * @param bean the instance of the bean
127 * @return the builder
128 */
129 public BeanDefinition bean(Object bean) {
130 setBean(bean);
131 return this;
132 }
133
134 /**
135 * Sets the Class of the bean that camel will instantiation it for calling
136 *
137 * @param beanType the Class of the bean
138 * @return the builder
139 */
140 public BeanDefinition beanType(Class beanType) {
141 setBean(beanType);
142 return this;
143 }
144
145 @Override
146 @SuppressWarnings("unchecked")
147 public Processor createProcessor(RouteContext routeContext) {
148 BeanProcessor answer;
149 if (ref != null) {
150 answer = new BeanProcessor(new RegistryBean(routeContext.getCamelContext(), ref));
151 } else {
152 if (bean == null) {
153 ObjectHelper.notNull(beanType, "bean, ref or beanType", this);
154 bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), beanType);
155 }
156 answer = new BeanProcessor(bean, routeContext.getCamelContext());
157 }
158 if (method != null) {
159 answer.setMethod(method);
160 }
161 return answer;
162 }
163
164 @Override
165 public String getLabel() {
166 if (ref != null) {
167 String methodText = "";
168 if (method != null) {
169 methodText = " method: " + method;
170 }
171 return "ref: " + ref + methodText;
172 } else if (bean != null) {
173 return bean.toString();
174 } else if (beanType != null) {
175 return beanType.getName();
176 } else {
177 return "";
178 }
179 }
180 }