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.NoTypeConversionAvailableException;
022 import org.apache.camel.Processor;
023 import org.apache.camel.spi.Registry;
024 import org.apache.camel.util.CamelContextHelper;
025 import org.apache.camel.util.ObjectHelper;
026
027 /**
028 * An implementation of a {@link BeanHolder} which will look up a bean from the registry and act as a cache of its metadata
029 *
030 * @version $Revision: 749533 $
031 */
032 public class RegistryBean implements BeanHolder {
033 private final CamelContext context;
034 private final String name;
035 private final Registry registry;
036 private Processor processor;
037 private BeanInfo beanInfo;
038 private Object bean;
039 private ParameterMappingStrategy parameterMappingStrategy;
040
041 public RegistryBean(CamelContext context, String name) {
042 this.context = context;
043 this.name = name;
044 this.registry = context.getRegistry();
045 }
046
047 public RegistryBean(CamelContext context, String name, ParameterMappingStrategy parameterMappingStrategy) {
048 this(context, name);
049 this.parameterMappingStrategy = parameterMappingStrategy;
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 Object getBean() throws NoSuchBeanException {
062 Object value = lookupBean();
063 if (value == null) {
064 throw new NoSuchBeanException(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() {
128 return registry.lookup(name);
129 }
130 }