@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
BeanProcessor answer;
Class<?> clazz = bean != null ? bean.getClass() : null;
BeanHolder beanHolder;
if (ObjectHelper.isNotEmpty(ref)) {
// lets cache by default
if (isCacheBean()) {
// cache the registry lookup which avoids repeat lookup in the registry
beanHolder = new RegistryBean(routeContext.getCamelContext(), ref).createCacheHolder();
// bean holder will check if the bean exists
bean = beanHolder.getBean();
} else {
// we do not cache so we invoke on-demand
beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
}
answer = new BeanProcessor(beanHolder);
} else {
if (bean == null) {
if (beanType == null && beanClass == null) {
throw new IllegalArgumentException("bean, ref or beanType must be provided");
}
// the clazz is either from beanType or beanClass
if (beanType != null) {
try {
clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(beanType);
} catch (ClassNotFoundException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
} else {
clazz = beanClass;
}
// create a bean if there is a default public no-arg constructor
if (isCacheBean() && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz);
ObjectHelper.notNull(bean, "bean", this);
}
}
// validate the bean type is not from java so you by mistake think its a reference
// to a bean name but the String is being invoke instead
if (bean instanceof String) {
throw new IllegalArgumentException("The bean instance is a java.lang.String type: " + bean
+ ". We suppose you want to refer to a bean instance by its id instead. Please use beanRef.");
}
// the holder should either be bean or type based
if (bean != null) {
beanHolder = new ConstantBeanHolder(bean, routeContext.getCamelContext());
} else {
if (isCacheBean() && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
// we can only cache if we can create an instance of the bean, and for that we need a public constructor
beanHolder = new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext()).createCacheHolder();
} else {
if (ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
beanHolder = new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
} else {
// this is only for invoking static methods on the bean
beanHolder = new ConstantStaticTypeBeanHolder(clazz, routeContext.getCamelContext());
}
}
}
answer = new BeanProcessor(beanHolder);
}
// check for multiParameterArray setting
if (multiParameterArray != null) {
answer.setMultiParameterArray(multiParameterArray);
}
// check for method exists
if (method != null) {
answer.setMethod(method);
// check there is a method with the given name, and leverage BeanInfo for that
// which we only do if we are caching the bean as otherwise we will create a bean instance for this check
// which we only want to do if we cache the bean
if (isCacheBean()) {
BeanInfo beanInfo = beanHolder.getBeanInfo();
if (bean != null) {
// there is a bean instance, so check for any methods
if (!beanInfo.hasMethod(method)) {
throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method));
}