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.component.bean;
018
019import java.util.Map;
020
021import org.apache.camel.Component;
022import org.apache.camel.Consumer;
023import org.apache.camel.ExchangePattern;
024import org.apache.camel.Processor;
025import org.apache.camel.Producer;
026import org.apache.camel.impl.DefaultEndpoint;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.UriEndpoint;
029import org.apache.camel.spi.UriParam;
030import org.apache.camel.spi.UriPath;
031
032/**
033 * Endpoint for the bean component.
034 *
035 * @version 
036 */
037@UriEndpoint(scheme = "bean", title = "Bean", syntax = "bean:beanName", producerOnly = true, label = "core,java")
038public class BeanEndpoint extends DefaultEndpoint {
039    private transient BeanHolder beanHolder;
040    private transient BeanProcessor processor;
041    @UriPath(description = "Sets the name of the bean to invoke") @Metadata(required = "true")
042    private String beanName;
043    @UriParam(description = "Sets the name of the method to invoke on the bean")
044    private String method;
045    @UriParam(label = "advanced", description = "If enabled, Camel will cache the result of the first Registry look-up."
046            + " Cache can be enabled if the bean in the Registry is defined as a singleton scope.")
047    private boolean cache;
048    @UriParam(label = "advanced", description = "How to treat the parameters which are passed from the message body."
049            + "true means the message body should be an array of parameters. Note: This option is used internally by Camel, and is not intended for end users to use.")
050    @Deprecated
051    private boolean multiParameterArray;
052    @UriParam(label = "advanced", description = "Used for configuring additional properties on the bean")
053    private Map<String, Object> parameters;
054
055    public BeanEndpoint() {
056        setExchangePattern(ExchangePattern.InOut);
057    }
058
059    public BeanEndpoint(String endpointUri, Component component, BeanProcessor processor) {
060        super(endpointUri, component);
061        this.processor = processor;
062        setExchangePattern(ExchangePattern.InOut);
063    }
064
065    public BeanEndpoint(String endpointUri, Component component) {
066        super(endpointUri, component);
067        setExchangePattern(ExchangePattern.InOut);
068    }
069
070    @Override
071    public Producer createProducer() throws Exception {
072        return new BeanProducer(this, processor);
073    }
074
075    @Override
076    public Consumer createConsumer(Processor processor) throws Exception {
077        throw new UnsupportedOperationException("You cannot consume from a bean endpoint");
078    }
079
080    @Override
081    public boolean isSingleton() {
082        return true;
083    }
084
085    public BeanProcessor getProcessor() {
086        return processor;
087    }
088
089    @Override
090    protected void doStart() throws Exception {
091        super.doStart();
092
093        if (processor == null) {
094            BeanHolder holder = getBeanHolder();
095            if (holder == null) {
096                RegistryBean registryBean = new RegistryBean(getCamelContext(), beanName);
097                if (cache) {
098                    holder = registryBean.createCacheHolder();
099                } else {
100                    holder = registryBean;
101                }
102            }
103            processor = new BeanProcessor(holder);
104            if (method != null) {
105                processor.setMethod(method);
106            }
107            processor.setMultiParameterArray(isMultiParameterArray());
108            if (parameters != null) {
109                setProperties(processor, parameters);
110            }
111        }
112    }
113
114    @Override
115    protected void doStop() throws Exception {
116        super.doStop();
117        // noop
118    }
119
120    // Properties
121    //-------------------------------------------------------------------------
122
123    public String getBeanName() {
124        return beanName;
125    }
126
127    /**
128     * Sets the name of the bean to invoke
129     */
130    public void setBeanName(String beanName) {
131        this.beanName = beanName;
132    }
133
134    public boolean isMultiParameterArray() {
135        return multiParameterArray;
136    }
137
138    /**
139     * How to treat the parameters which are passed from the message body;
140     * if it is true, the message body should be an array of parameters.
141     * <p/>
142     * Note: This option is used internally by Camel, and is not intended for end users to use.
143     *
144     * @deprecated this option is used internally by Camel, and is not intended for end users to use
145     */
146    @Deprecated
147    public void setMultiParameterArray(boolean mpArray) {
148        multiParameterArray = mpArray;
149    }
150
151    public boolean isCache() {
152        return cache;
153    }
154
155    /**
156     * If enabled, Camel will cache the result of the first Registry look-up.
157     * Cache can be enabled if the bean in the Registry is defined as a singleton scope.
158     */
159    public void setCache(boolean cache) {
160        this.cache = cache;
161    }
162
163    public String getMethod() {
164        return method;
165    }
166
167    /**
168     * Sets the name of the method to invoke on the bean
169     */
170    public void setMethod(String method) {
171        this.method = method;
172    }
173
174    public BeanHolder getBeanHolder() {
175        return beanHolder;
176    }
177
178    public void setBeanHolder(BeanHolder beanHolder) {
179        this.beanHolder = beanHolder;
180    }
181
182    public Map<String, Object> getParameters() {
183        return parameters;
184    }
185
186    /**
187     * Used for configuring additional properties on the bean
188     */
189    public void setParameters(Map<String, Object> parameters) {
190        this.parameters = parameters;
191    }
192
193    // Implementation methods
194    //-------------------------------------------------------------------------
195
196    @Override
197    protected String createEndpointUri() {
198        return "bean:" + getBeanName() + (method != null ? "?method=" + method : "");
199    }
200}