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 
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(Registry registry, CamelContext context, String name) {
047            this.registry = registry;
048            this.context = context;
049            this.name = name;
050        }
051    
052        @Override
053        public String toString() {
054            return "bean: " + name;
055        }
056    
057        public ConstantBeanHolder createCacheHolder() throws Exception {
058            return new ConstantBeanHolder(getBean(), getBeanInfo());
059        }
060    
061        public synchronized Object getBean() throws NoSuchBeanException {
062            Object value = lookupBean();
063            if (value == null) {
064                // maybe its a class
065                value = context.getClassResolver().resolveClass(name);
066                if (value == null) {
067                    // no its not a class then we cannot find the bean
068                    throw new NoSuchBeanException(name);
069                }
070            }
071            if (value != bean) {
072                if (!ObjectHelper.equal(ObjectHelper.type(bean), ObjectHelper.type(value))) {
073                    beanInfo = null;
074                }
075                bean = value;
076                processor = null;
077    
078                // could be a class then create an instance of it
079                if (bean instanceof Class) {
080                    // bean is a class so create an instance of it
081                    bean = context.getInjector().newInstance((Class<?>)bean);
082                    value = bean;
083                }
084            }
085            return value;
086        }
087    
088        public Processor getProcessor() {
089            if (processor == null && bean != null) {
090                processor = CamelContextHelper.convertTo(context, Processor.class, bean);
091            }
092            return processor;
093        }
094    
095        public BeanInfo getBeanInfo() {
096            if (beanInfo == null && bean != null) {
097                this.beanInfo = createBeanInfo();
098            }
099            return beanInfo;
100        }
101    
102        public String getName() {
103            return name;
104        }
105    
106        public Registry getRegistry() {
107            return registry;
108        }
109    
110        public CamelContext getContext() {
111            return context;
112        }
113    
114        public ParameterMappingStrategy getParameterMappingStrategy() {
115            if (parameterMappingStrategy == null) {
116                parameterMappingStrategy = createParameterMappingStrategy();
117            }
118            return parameterMappingStrategy;
119        }
120    
121        public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) {
122            this.parameterMappingStrategy = parameterMappingStrategy;
123        }
124    
125        // Implementation methods
126        //-------------------------------------------------------------------------
127        protected BeanInfo createBeanInfo() {
128            return new BeanInfo(context, bean.getClass(), getParameterMappingStrategy());
129        }
130    
131        protected ParameterMappingStrategy createParameterMappingStrategy() {
132            return BeanInfo.createParameterMappingStrategy(context);
133        }
134    
135        protected Object lookupBean() {
136            return registry.lookup(name);
137        }
138    }