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 025 /** 026 * An implementation of a {@link BeanHolder} which will look up a bean from the registry and act as a cache of its metadata 027 * 028 * @version 029 */ 030 public class RegistryBean implements BeanHolder { 031 private final Object lock = new Object(); 032 private final CamelContext context; 033 private final String name; 034 private final Registry registry; 035 private volatile Processor processor; 036 private volatile BeanInfo beanInfo; 037 private volatile 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 Object bean = getBean(); 059 BeanInfo info = createBeanInfo(bean); 060 return new ConstantBeanHolder(bean, info); 061 } 062 063 public Object getBean() throws NoSuchBeanException { 064 // must always lookup bean first 065 Object value = lookupBean(); 066 067 if (value != null) { 068 // could be a class then create an instance of it 069 if (value instanceof Class) { 070 // bean is a class so create an instance of it 071 value = context.getInjector().newInstance((Class<?>)value); 072 } 073 bean = value; 074 return value; 075 } 076 077 // okay bean is not in registry, so try to resolve if its a class name and create a shared instance 078 synchronized (lock) { 079 if (bean != null) { 080 return bean; 081 } 082 083 // maybe its a class 084 bean = context.getClassResolver().resolveClass(name); 085 if (bean == null) { 086 // no its not a class then we cannot find the bean 087 throw new NoSuchBeanException(name); 088 } 089 // could be a class then create an instance of it 090 if (bean instanceof Class) { 091 // bean is a class so create an instance of it 092 bean = context.getInjector().newInstance((Class<?>)bean); 093 } 094 } 095 096 return bean; 097 } 098 099 public Processor getProcessor() { 100 if (processor == null && bean != null) { 101 processor = CamelContextHelper.convertTo(context, Processor.class, bean); 102 } 103 return processor; 104 } 105 106 public BeanInfo getBeanInfo() { 107 if (beanInfo == null && bean != null) { 108 this.beanInfo = createBeanInfo(bean); 109 } 110 return beanInfo; 111 } 112 113 public BeanInfo getBeanInfo(Object bean) { 114 if (this.bean == bean) { 115 return getBeanInfo(); 116 } else { 117 return createBeanInfo(bean); 118 } 119 } 120 121 public String getName() { 122 return name; 123 } 124 125 public Registry getRegistry() { 126 return registry; 127 } 128 129 public CamelContext getContext() { 130 return context; 131 } 132 133 public ParameterMappingStrategy getParameterMappingStrategy() { 134 if (parameterMappingStrategy == null) { 135 parameterMappingStrategy = createParameterMappingStrategy(); 136 } 137 return parameterMappingStrategy; 138 } 139 140 public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) { 141 this.parameterMappingStrategy = parameterMappingStrategy; 142 } 143 144 // Implementation methods 145 //------------------------------------------------------------------------- 146 protected BeanInfo createBeanInfo(Object bean) { 147 return new BeanInfo(context, bean.getClass(), getParameterMappingStrategy()); 148 } 149 150 protected ParameterMappingStrategy createParameterMappingStrategy() { 151 return BeanInfo.createParameterMappingStrategy(context); 152 } 153 154 protected Object lookupBean() { 155 return registry.lookupByName(name); 156 } 157 }