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