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.impl.DefaultComponent;
023    import org.apache.camel.impl.ProcessorEndpoint;
024    import org.apache.camel.spi.Registry;
025    import org.apache.camel.util.IntrospectionSupport;
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: 1.1 $
035     */
036    public class BeanComponent extends DefaultComponent {
037        private static final Log LOG = LogFactory.getLog(BeanComponent.class);
038        private ParameterMappingStrategy parameterMappingStrategy;
039    
040        public BeanComponent() {
041        }
042    
043        public ParameterMappingStrategy getParameterMappingStrategy() {
044            if (parameterMappingStrategy == null) {
045                parameterMappingStrategy = createParameterMappingStrategy();
046            }
047            return parameterMappingStrategy;
048        }
049    
050        public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) {
051            this.parameterMappingStrategy = parameterMappingStrategy;
052        }
053    
054        // Implementation methods
055        //-----------------------------------------------------------------------
056    
057        protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
058            Object bean = getBean(remaining);
059            BeanProcessor processor = new BeanProcessor(bean, getParameterMappingStrategy());
060            IntrospectionSupport.setProperties(processor, parameters);
061            return new ProcessorEndpoint(uri, this, processor);
062        }
063    
064        public Object getBean(String remaining) throws NoBeanAvailableException {
065            Registry registry = getCamelContext().getRegistry();
066            Object bean = registry.lookup(remaining);
067            if (bean == null) {
068                throw new NoBeanAvailableException(remaining);
069            }
070            return bean;
071        }
072    
073        protected ParameterMappingStrategy createParameterMappingStrategy() {
074            return BeanProcessor.createParameterMappingStrategy(getCamelContext());
075        }
076    }