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.CamelContext;
020    import org.apache.camel.Processor;
021    import org.apache.camel.spi.Registry;
022    import org.apache.camel.util.CamelContextHelper;
023    import org.apache.camel.util.ObjectHelper;
024    
025    /**
026     * An implementation of a {@link BeanHolder} which will
027     *
028     * @version $Revision: 36321 $
029     */
030    public class RegistryBean implements BeanHolder {
031        private final CamelContext context;
032        private final String name;
033        private final Registry registry;
034        private Processor processor;
035        private BeanInfo beanInfo;
036        private Object bean;
037        private ParameterMappingStrategy parameterMappingStrategy;
038    
039        public RegistryBean(CamelContext context, String name) {
040            this.context = context;
041            this.name = name;
042            this.registry = context.getRegistry();
043        }
044    
045        public RegistryBean(CamelContext context, String name, ParameterMappingStrategy parameterMappingStrategy) {
046            this(context, name);
047            this.parameterMappingStrategy = parameterMappingStrategy;
048        }
049    
050        @Override
051        public String toString() {
052            return "bean: " + name;
053        }
054    
055    
056        public ConstantBeanHolder createCacheHolder() throws Exception {
057            return new ConstantBeanHolder(getBean(), getBeanInfo());
058        }
059    
060        public Object getBean() throws Exception {
061            Object value = lookupBean();
062            if (value == null) {
063                throw new NoBeanAvailableException(name);
064            }
065            if (value != bean) {
066                bean = value;
067                processor = null;
068                if (!ObjectHelper.equal(ObjectHelper.type(bean), ObjectHelper.type(value))) {
069                    beanInfo = null;
070                }
071            }
072            return value;
073        }
074    
075        public Processor getProcessor() {
076            if (processor == null && bean != null) {
077                processor = CamelContextHelper.convertTo(context, Processor.class, bean);
078            }
079            return processor;
080        }
081    
082        public BeanInfo getBeanInfo() {
083            if (beanInfo == null && bean != null) {
084                this.beanInfo = createBeanInfo();
085            }
086            return beanInfo;
087        }
088    
089        public String getName() {
090            return name;
091        }
092    
093        public Registry getRegistry() {
094            return registry;
095        }
096    
097        public CamelContext getContext() {
098            return context;
099        }
100    
101        public ParameterMappingStrategy getParameterMappingStrategy() {
102            if (parameterMappingStrategy == null) {
103                parameterMappingStrategy = createParameterMappingStrategy();
104            }
105            return parameterMappingStrategy;
106        }
107    
108        public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) {
109            this.parameterMappingStrategy = parameterMappingStrategy;
110        }
111    
112        // Implementation methods
113        //-------------------------------------------------------------------------
114        protected BeanInfo createBeanInfo() {
115            return new BeanInfo(context, bean.getClass(), getParameterMappingStrategy());
116        }
117    
118        protected ParameterMappingStrategy createParameterMappingStrategy() {
119            return BeanInfo.createParameterMappingStrategy(context);
120        }
121    
122        protected Object lookupBean() throws Exception {
123            return registry.lookup(name);
124        }
125    }