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: 755890 $
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        public RegistryBean(CamelContext context, String name, ParameterMappingStrategy parameterMappingStrategy) {
047            this(context, name);
048            this.parameterMappingStrategy = parameterMappingStrategy;
049        }
050    
051        @Override
052        public String toString() {
053            return "bean: " + name;
054        }
055    
056        public ConstantBeanHolder createCacheHolder() throws Exception {
057            return new ConstantBeanHolder(getBean(), getBeanInfo());
058        }
059    
060        public Object getBean() throws NoSuchBeanException {
061            Object value = lookupBean();
062            if (value == null) {
063                throw new NoSuchBeanException(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() {
123            return registry.lookup(name);
124        }
125    }