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.NoTypeConversionAvailableException;
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: 1290 $
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    
057        public ConstantBeanHolder createCacheHolder() throws Exception {
058            return new ConstantBeanHolder(getBean(), getBeanInfo());
059        }
060    
061        public Object getBean() throws Exception {
062            Object value = lookupBean();
063            if (value == null) {
064                throw new NoBeanAvailableException(name);
065            }
066            if (value != bean) {
067                bean = value;
068                processor = null;
069                if (!ObjectHelper.equal(ObjectHelper.type(bean), ObjectHelper.type(value))) {
070                    beanInfo = null;
071                }
072            }
073            return value;
074        }
075    
076        public Processor getProcessor() {
077            if (processor == null && bean != null) {
078                try {
079                    processor = CamelContextHelper.convertTo(context, Processor.class, bean);
080                } catch (NoTypeConversionAvailableException ex) {
081                    // ignore
082                }
083            }
084            return processor;
085        }
086    
087        public BeanInfo getBeanInfo() {
088            if (beanInfo == null && bean != null) {
089                this.beanInfo = createBeanInfo();
090            }
091            return beanInfo;
092        }
093    
094        public String getName() {
095            return name;
096        }
097    
098        public Registry getRegistry() {
099            return registry;
100        }
101    
102        public CamelContext getContext() {
103            return context;
104        }
105    
106        public ParameterMappingStrategy getParameterMappingStrategy() {
107            if (parameterMappingStrategy == null) {
108                parameterMappingStrategy = createParameterMappingStrategy();
109            }
110            return parameterMappingStrategy;
111        }
112    
113        public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) {
114            this.parameterMappingStrategy = parameterMappingStrategy;
115        }
116    
117        // Implementation methods
118        //-------------------------------------------------------------------------
119        protected BeanInfo createBeanInfo() {
120            return new BeanInfo(context, bean.getClass(), getParameterMappingStrategy());
121        }
122    
123        protected ParameterMappingStrategy createParameterMappingStrategy() {
124            return BeanInfo.createParameterMappingStrategy(context);
125        }
126    
127        protected Object lookupBean() throws Exception {
128            return registry.lookup(name);
129        }
130    }