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.component.bean;
018    
019    import org.apache.camel.Component;
020    import org.apache.camel.ExchangePattern;
021    import org.apache.camel.Processor;
022    import org.apache.camel.impl.ProcessorEndpoint;
023    import org.apache.camel.spi.UriEndpoint;
024    import org.apache.camel.spi.UriParam;
025    
026    /**
027     * Endpoint for the bean component.
028     *
029     * @version 
030     */
031    @UriEndpoint(scheme = "bean")
032    public class BeanEndpoint extends ProcessorEndpoint {
033        private BeanHolder beanHolder;
034        @UriParam
035        private boolean cache;
036        @UriParam
037        private boolean multiParameterArray;
038        @UriParam
039        private String beanName;
040        @UriParam
041        private String method;
042    
043        public BeanEndpoint() {
044            init();
045        }
046    
047        public BeanEndpoint(String endpointUri, Component component, BeanProcessor processor) {
048            super(endpointUri, component, processor);
049            init();
050        }
051    
052        public BeanEndpoint(String endpointUri, Component component) {
053            super(endpointUri, component);
054            init();
055        }
056    
057        // Properties
058        //-------------------------------------------------------------------------
059    
060        public String getBeanName() {
061            return beanName;
062        }
063    
064        /**
065         * Sets the name of the bean to invoke
066         */
067        public void setBeanName(String beanName) {
068            this.beanName = beanName;
069        }
070    
071        public boolean isMultiParameterArray() {
072            return multiParameterArray;
073        }
074    
075        public void setMultiParameterArray(boolean mpArray) {
076            multiParameterArray = mpArray;
077        }
078    
079        public boolean isCache() {
080            return cache;
081        }
082    
083        public void setCache(boolean cache) {
084            this.cache = cache;
085        }
086    
087        public String getMethod() {
088            return method;
089        }
090    
091        /**
092         * Sets the name of the method to invoke on the bean
093         */
094        public void setMethod(String method) {
095            this.method = method;
096        }
097    
098        public BeanHolder getBeanHolder() {
099            return beanHolder;
100        }
101    
102        public void setBeanHolder(BeanHolder beanHolder) {
103            this.beanHolder = beanHolder;
104        }
105    
106        // Implementation methods
107        //-------------------------------------------------------------------------
108    
109        @Override
110        protected String createEndpointUri() {
111            return "bean:" + getBeanName() + (method != null ? "?method=" + method : "");
112        }
113    
114        private void init() {
115            setExchangePattern(ExchangePattern.InOut);
116        }
117    
118        @Override
119        protected Processor createProcessor() throws Exception {
120            BeanHolder holder = getBeanHolder();
121            if (holder == null) {
122                RegistryBean registryBean = new RegistryBean(getCamelContext(), beanName);
123                if (cache) {
124                    holder = registryBean.createCacheHolder();
125                } else {
126                    holder = registryBean;
127                }
128            }
129            BeanProcessor processor = new BeanProcessor(holder);
130            if (method != null) {
131                processor.setMethod(method);
132            }
133            processor.setMultiParameterArray(isMultiParameterArray());
134    
135            return processor;
136        }
137    }