Search in sources :

Example 1 with BeanHolder

use of org.apache.camel.component.bean.BeanHolder in project camel by apache.

the class EjbComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    EjbEndpoint answer = new EjbEndpoint(uri, this);
    answer.setBeanName(remaining);
    // plugin registry to lookup in jndi for the EJBs
    Registry registry = new JndiRegistry(getContext());
    // and register the bean as a holder on the endpoint
    BeanHolder holder = new EjbRegistryBean(registry, getCamelContext(), answer.getBeanName());
    answer.setBeanHolder(holder);
    return answer;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) BeanHolder(org.apache.camel.component.bean.BeanHolder) Registry(org.apache.camel.spi.Registry) JndiRegistry(org.apache.camel.impl.JndiRegistry)

Example 2 with BeanHolder

use of org.apache.camel.component.bean.BeanHolder in project camel by apache.

the class ClassComponent method createEndpoint.

protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    ClassEndpoint endpoint = new ClassEndpoint(uri, this);
    endpoint.setBeanName(remaining);
    // bean name is the FQN
    String name = endpoint.getBeanName();
    Class<?> clazz = getCamelContext().getClassResolver().resolveMandatoryClass(name);
    // the bean.xxx options is for the bean
    Map<String, Object> options = IntrospectionSupport.extractProperties(parameters, "bean.");
    endpoint.setParameters(options);
    BeanHolder holder;
    // if there is options then we need to create a bean instance
    if (!options.isEmpty()) {
        // create bean
        Object bean = getCamelContext().getInjector().newInstance(clazz);
        // now set additional properties on it
        setProperties(bean, options);
        holder = new ConstantBeanHolder(bean, getCamelContext());
    } else {
        // otherwise refer to the type
        holder = new ConstantTypeBeanHolder(clazz, getCamelContext());
    }
    validateParameters(uri, options, null);
    // and register the bean as a holder on the endpoint
    endpoint.setBeanHolder(holder);
    return endpoint;
}
Also used : ConstantTypeBeanHolder(org.apache.camel.component.bean.ConstantTypeBeanHolder) ConstantBeanHolder(org.apache.camel.component.bean.ConstantBeanHolder) ConstantTypeBeanHolder(org.apache.camel.component.bean.ConstantTypeBeanHolder) BeanHolder(org.apache.camel.component.bean.BeanHolder) ConstantBeanHolder(org.apache.camel.component.bean.ConstantBeanHolder)

Example 3 with BeanHolder

use of org.apache.camel.component.bean.BeanHolder in project camel by apache.

the class BeanDefinition method createProcessor.

@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);
            }
        }
        // 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 ref.");
        }
        // 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);
        // 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));
                }
            } else if (clazz != null) {
                // there is no bean instance, so check for static methods only
                if (!beanInfo.hasStaticMethod(method)) {
                    throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, clazz, method, true));
                }
            }
        }
    }
    return answer;
}
Also used : ConstantTypeBeanHolder(org.apache.camel.component.bean.ConstantTypeBeanHolder) ConstantBeanHolder(org.apache.camel.component.bean.ConstantBeanHolder) ConstantStaticTypeBeanHolder(org.apache.camel.component.bean.ConstantStaticTypeBeanHolder) ConstantTypeBeanHolder(org.apache.camel.component.bean.ConstantTypeBeanHolder) BeanHolder(org.apache.camel.component.bean.BeanHolder) BeanProcessor(org.apache.camel.component.bean.BeanProcessor) RegistryBean(org.apache.camel.component.bean.RegistryBean) BeanInfo(org.apache.camel.component.bean.BeanInfo) ConstantBeanHolder(org.apache.camel.component.bean.ConstantBeanHolder) ConstantStaticTypeBeanHolder(org.apache.camel.component.bean.ConstantStaticTypeBeanHolder) MethodNotFoundException(org.apache.camel.component.bean.MethodNotFoundException)

Example 4 with BeanHolder

use of org.apache.camel.component.bean.BeanHolder in project camel by apache.

the class MethodCallExpression method createExpression.

@Override
public Expression createExpression(CamelContext camelContext) {
    Expression answer;
    if (beanType == null && beanTypeName != null) {
        try {
            beanType = camelContext.getClassResolver().resolveMandatoryClass(beanTypeName);
        } catch (ClassNotFoundException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    BeanHolder holder;
    if (beanType != null) {
        // create a bean if there is a default public no-arg constructor
        if (ObjectHelper.hasDefaultPublicNoArgConstructor(beanType)) {
            instance = camelContext.getInjector().newInstance(beanType);
            holder = new ConstantBeanHolder(instance, camelContext);
        } else {
            holder = new ConstantStaticTypeBeanHolder(beanType, camelContext);
        }
    } else if (instance != null) {
        holder = new ConstantBeanHolder(instance, camelContext);
    } else {
        String ref = beanName();
        // if its a ref then check that the ref exists
        BeanHolder regHolder = new RegistryBean(camelContext, ref);
        // get the bean which will check that it exists
        instance = regHolder.getBean();
        holder = new ConstantBeanHolder(instance, camelContext);
    }
    // create answer using the holder
    answer = new BeanExpression(holder, getMethod());
    // and do sanity check that if a method name was given, that it exists
    validateHasMethod(camelContext, instance, beanType, getMethod());
    return answer;
}
Also used : BeanExpression(org.apache.camel.language.bean.BeanExpression) Expression(org.apache.camel.Expression) ConstantBeanHolder(org.apache.camel.component.bean.ConstantBeanHolder) ConstantStaticTypeBeanHolder(org.apache.camel.component.bean.ConstantStaticTypeBeanHolder) BeanHolder(org.apache.camel.component.bean.BeanHolder) RegistryBean(org.apache.camel.component.bean.RegistryBean) ConstantBeanHolder(org.apache.camel.component.bean.ConstantBeanHolder) ConstantStaticTypeBeanHolder(org.apache.camel.component.bean.ConstantStaticTypeBeanHolder) BeanExpression(org.apache.camel.language.bean.BeanExpression)

Aggregations

BeanHolder (org.apache.camel.component.bean.BeanHolder)4 ConstantBeanHolder (org.apache.camel.component.bean.ConstantBeanHolder)3 ConstantStaticTypeBeanHolder (org.apache.camel.component.bean.ConstantStaticTypeBeanHolder)2 ConstantTypeBeanHolder (org.apache.camel.component.bean.ConstantTypeBeanHolder)2 RegistryBean (org.apache.camel.component.bean.RegistryBean)2 Expression (org.apache.camel.Expression)1 BeanInfo (org.apache.camel.component.bean.BeanInfo)1 BeanProcessor (org.apache.camel.component.bean.BeanProcessor)1 MethodNotFoundException (org.apache.camel.component.bean.MethodNotFoundException)1 JndiRegistry (org.apache.camel.impl.JndiRegistry)1 BeanExpression (org.apache.camel.language.bean.BeanExpression)1 Registry (org.apache.camel.spi.Registry)1