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.spi.Metadata;
026import org.apache.camel.util.ObjectHelper;
027
028/**
029 * Calls a java bean
030 */
031@Metadata(label = "eip,endpoint")
032@XmlRootElement(name = "bean")
033@XmlAccessorType(XmlAccessType.FIELD)
034public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
035    @XmlAttribute
036    private String ref;
037    @XmlAttribute
038    private String method;
039    @XmlAttribute
040    private String beanType;
041    @XmlAttribute
042    @Metadata(defaultValue = "true")
043    private Boolean cache;
044    @XmlTransient
045    private Class<?> beanClass;
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[" + description() + "]";
064    }
065
066    public String description() {
067        if (ref != null) {
068            String methodText = "";
069            if (method != null) {
070                methodText = " method:" + method;
071            }
072            return "ref:" + ref + methodText;
073        } else if (bean != null) {
074            return ObjectHelper.className(bean);
075        } else if (beanClass != null) {
076            return beanClass.getName();
077        } else if (beanType != null) {
078            return beanType;
079        } else {
080            return "";
081        }
082    }
083
084    @Override
085    public String getShortName() {
086        return "bean";
087    }
088
089    @Override
090    public String getLabel() {
091        return "bean[" + description() + "]";
092    }
093
094    public String getRef() {
095        return ref;
096    }
097
098    /**
099     * Sets a reference to a bean to use
100     */
101    public void setRef(String ref) {
102        this.ref = ref;
103    }
104
105    public String getMethod() {
106        return method;
107    }
108
109    /**
110     * Sets the method name on the bean to use
111     */
112    public void setMethod(String method) {
113        this.method = method;
114    }
115
116    /**
117     * Sets an instance of the bean to use
118     */
119    public void setBean(Object bean) {
120        this.bean = bean;
121    }
122
123    public Object getBean() {
124        return bean;
125    }
126
127    public String getBeanType() {
128        return beanType;
129    }
130
131    /**
132     * Sets the Class of the bean
133     */
134    public void setBeanType(String beanType) {
135        this.beanType = beanType;
136    }
137
138    public Class<?> getBeanClass() {
139        return beanClass;
140    }
141
142    /**
143     * Sets the Class of the bean
144     */
145    public void setBeanType(Class<?> beanType) {
146        this.beanClass = beanType;
147    }
148
149    public Boolean getCache() {
150        return cache;
151    }
152
153    /**
154     * Caches the bean lookup, to avoid lookup up bean on every usage.
155     */
156    public void setCache(Boolean cache) {
157        this.cache = cache;
158    }
159
160    // Fluent API
161    // -------------------------------------------------------------------------
162
163}