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;
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.BeanScope;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.util.ObjectHelper;
028
029/**
030 * Calls a java bean
031 */
032@Metadata(label = "eip,endpoint")
033@XmlRootElement(name = "bean")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
036    @XmlAttribute
037    private String ref;
038    @XmlAttribute
039    private String method;
040    @XmlAttribute
041    private String beanType;
042    @XmlAttribute
043    @Metadata(defaultValue = "true", javaType = "java.lang.Boolean")
044    @Deprecated
045    private String cache;
046    @XmlAttribute
047    @Metadata(defaultValue = "Singleton", enums = "Singleton,Request,Prototype")
048    private String scope;
049    @XmlTransient
050    private Class<?> beanClass;
051    @XmlTransient
052    private Object bean;
053
054    public BeanDefinition() {
055    }
056
057    public BeanDefinition(String ref) {
058        this.ref = ref;
059    }
060
061    public BeanDefinition(String ref, String method) {
062        this.ref = ref;
063        this.method = method;
064    }
065
066    @Override
067    public String toString() {
068        return "Bean[" + description() + "]";
069    }
070
071    public String description() {
072        if (ref != null) {
073            String methodText = "";
074            if (method != null) {
075                methodText = " method:" + method;
076            }
077            return "ref:" + ref + methodText;
078        } else if (bean != null) {
079            return ObjectHelper.className(bean);
080        } else if (beanClass != null) {
081            return beanClass.getName();
082        } else if (beanType != null) {
083            return beanType;
084        } else {
085            return "";
086        }
087    }
088
089    @Override
090    public String getShortName() {
091        return "bean";
092    }
093
094    @Override
095    public String getLabel() {
096        return "bean[" + description() + "]";
097    }
098
099    public String getRef() {
100        return ref;
101    }
102
103    /**
104     * Sets a reference to a bean to use
105     */
106    public void setRef(String ref) {
107        this.ref = ref;
108    }
109
110    public String getMethod() {
111        return method;
112    }
113
114    /**
115     * Sets the method name on the bean to use
116     */
117    public void setMethod(String method) {
118        this.method = method;
119    }
120
121    /**
122     * Sets an instance of the bean to use
123     */
124    public void setBean(Object bean) {
125        this.bean = bean;
126    }
127
128    public Object getBean() {
129        return bean;
130    }
131
132    public String getBeanType() {
133        return beanType;
134    }
135
136    /**
137     * Sets the Class of the bean
138     */
139    public void setBeanType(String beanType) {
140        this.beanType = beanType;
141    }
142
143    public Class<?> getBeanClass() {
144        return beanClass;
145    }
146
147    /**
148     * Sets the Class of the bean
149     */
150    public void setBeanType(Class<?> beanType) {
151        this.beanClass = beanType;
152    }
153
154    public String getCache() {
155        if (scope == null || BeanScope.Singleton.name().equals(scope)) {
156            return "true";
157        } else {
158            return "false";
159        }
160    }
161
162    /**
163     * Use singleton option instead
164     */
165    public void setCache(String cache) {
166        if ("true".equals(cache)) {
167            scope = BeanScope.Singleton.name();
168        } else {
169            scope = BeanScope.Prototype.name();
170        }
171    }
172
173    public String getScope() {
174        return scope;
175    }
176
177    public void setScope(String scope) {
178        this.scope = scope;
179    }
180
181    /**
182     * Scope of bean.
183     *
184     * When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint.
185     * The bean should be thread-safe in case concurrent threads is calling the bean at the same time.
186     * When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean
187     * while processing a request and you want to call the same bean instance multiple times while processing the request.
188     * The bean does not have to be thread-safe as the instance is only called from the same request.
189     * When using delegate scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated
190     * to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope.
191     * so when using delegate then this depends on the delegated registry.
192     */
193    public void setScope(BeanScope scope) {
194        this.scope = scope.name();
195    }
196
197}