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 java.util.Map;
020    
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Processor;
023    import org.apache.camel.converter.ObjectConverter;
024    import org.apache.camel.impl.DefaultComponent;
025    import org.apache.camel.impl.ProcessorEndpoint;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * An alternative to the <a href="http://activemq.apache.org/pojo.html">POJO Component</a>
031     * which implements the <a href="http://activemq.apache.org/bean.html">Bean Component</a>
032     * which will look up the URI in the Spring ApplicationContext and use that to handle message dispatching.
033     *
034     * @version $Revision: 585 $
035     */
036    public class BeanComponent extends DefaultComponent {
037        private static final transient Log LOG = LogFactory.getLog(BeanComponent.class);
038        private ParameterMappingStrategy parameterMappingStrategy;
039    
040        public BeanComponent() {
041        }
042    
043        /**
044         * A helper method to create a new endpoint from a bean with a generated URI
045         */
046        public ProcessorEndpoint createEndpoint(Object bean) {
047            String uri = "bean:generated:" + bean;
048            return createEndpoint(bean, uri);
049        }
050    
051        /**
052         * A helper method to create a new endpoint from a bean with a given URI
053         */
054        public ProcessorEndpoint createEndpoint(Object bean, String uri) {
055            BeanProcessor processor = new BeanProcessor(bean, getCamelContext(), getParameterMappingStrategy());
056            return createEndpoint(uri, processor);
057        }
058    
059        public ParameterMappingStrategy getParameterMappingStrategy() {
060            if (parameterMappingStrategy == null) {
061                parameterMappingStrategy = createParameterMappingStrategy();
062            }
063            return parameterMappingStrategy;
064        }
065    
066        public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) {
067            this.parameterMappingStrategy = parameterMappingStrategy;
068        }
069    
070        // Implementation methods
071        //-----------------------------------------------------------------------
072        protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
073            BeanEndpoint endpoint = new BeanEndpoint(uri, this);
074            endpoint.setBeanName(remaining);
075            endpoint.setCache(ObjectConverter.toBool(parameters.remove("cache")));
076            Processor processor = endpoint.getProcessor();
077            setProperties(processor, parameters);
078            return endpoint;
079        }
080    
081        protected BeanEndpoint createEndpoint(String uri, BeanProcessor processor) {
082            return new BeanEndpoint(uri, this, processor);
083        }
084    
085        protected ParameterMappingStrategy createParameterMappingStrategy() {
086            return BeanInfo.createParameterMappingStrategy(getCamelContext());
087        }
088    }