Package org.apache.camel.component.bean

Examples of org.apache.camel.component.bean.BeanHolder


        // now set additional properties on it
        setProperties(bean, parameters);

        // and register the bean as a holder on the endpoint
        BeanHolder holder = new ConstantBeanHolder(bean, getCamelContext());
        endpoint.setBeanHolder(holder);

        // create processor
        Processor processor = endpoint.getProcessor();
        setProperties(processor, parameters);
View Full Code Here


    @Override
    public Processor createProcessor(RouteContext routeContext) throws Exception {
        BeanProcessor answer;
        Class<?> clazz = bean != null ? bean.getClass() : null;
        BeanHolder beanHolder;

        if (ObjectHelper.isNotEmpty(ref)) {
            if (cache != null && cache) {
                // cache the registry lookup which avoids repeat lookup in the registry
                beanHolder = new RegistryBean(routeContext.getCamelContext(), ref).createCacheHolder();
            } else {
                beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
            }
            // bean holder will check if the bean exists
            bean = beanHolder.getBean();
            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 (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
            beanHolder = bean != null ? new ConstantBeanHolder(bean, routeContext.getCamelContext()) : new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
            answer = new BeanProcessor(beanHolder);
        }

        // check for method exists
        if (method != null) {
            answer.setMethod(method);

            // check there is a method with the given name, and leverage BeanInfo for that
            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));
                }
View Full Code Here

        return "bean: " + beanName + " method: " + method;
    }

    public Object evaluate(E exchange) {
        // either use registry lookup or a constant bean
        BeanHolder holder;
        if (bean == null) {
            holder = new RegistryBean(exchange.getContext(), beanName);
        } else {
            holder = new ConstantBeanHolder(bean, exchange.getContext());
        }
View Full Code Here

    private BeanProcessor beanProcessor;

    public RmiProducer(RmiEndpoint endpoint) throws RemoteException, NotBoundException {
        super(endpoint);
        BeanHolder holder = new RmiRegistryBean(endpoint.getCamelContext(), endpoint.getName(), endpoint.getRegistry());
        beanProcessor = new BeanProcessor(holder);
        String method = endpoint.getMethod();
        if (method != null) {
            beanProcessor.setMethod(method);
        }
View Full Code Here

    @Override
    public Processor createProcessor(RouteContext routeContext) {
        BeanProcessor answer;
        Class<?> clazz = bean != null ? bean.getClass() : null;
        BeanHolder beanHolder;

        if (ObjectHelper.isNotEmpty(ref)) {
            beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
            // bean holder will check if the bean exists
            bean = beanHolder.getBean();
            answer = new BeanProcessor(beanHolder);
        } else {
            if (bean == null) {
                ObjectHelper.notNull(beanType, "bean, ref or beanType", this);
                try {
                    clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(beanType);
                } catch (ClassNotFoundException e) {
                    throw ObjectHelper.wrapRuntimeCamelException(e);
                }
                // create a bean if there is a default public no-arg constructor
                if (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
            beanHolder = bean != null ? new ConstantBeanHolder(bean, routeContext.getCamelContext()) : new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
            answer = new BeanProcessor(beanHolder);
        }

        // check for method exists
        if (method != null) {
            answer.setMethod(method);

            // check there is a method with the given name, and leverage BeanInfo for that
            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));
                }
View Full Code Here

     * Optimize to create the bean holder once, so we can reuse it for further
     * evaluation, which is faster.
     */
    private synchronized BeanHolder createBeanHolder(CamelContext context) {
        // either use registry lookup or a constant bean
        BeanHolder holder;
        if (bean != null) {
            holder = new ConstantBeanHolder(bean, context);
        } else if (beanName != null) {
            holder = new RegistryBean(context, beanName);
        } else if (type != null) {
View Full Code Here

            // Split ognl except when this is not a Map, Array
            // and we would like to keep the dots within the key name
            List<String> methods = OgnlHelper.splitOgnl(ognl);

            for (String methodName : methods) {
                BeanHolder holder = new ConstantBeanHolder(beanToCall, exchange.getContext());

                // support the null safe operator
                boolean nullSafe = OgnlHelper.isNullSafeOperator(methodName);

                // keep up with how far are we doing
                ognlPath += methodName;

                // get rid of leading ?. or . as we only needed that to determine if null safe was enabled or not
                methodName = OgnlHelper.removeLeadingOperators(methodName);

                // are we doing an index lookup (eg in Map/List/array etc)?
                String key = null;
                KeyValueHolder<String, String> index = OgnlHelper.isOgnlIndex(methodName);
                if (index != null) {
                    methodName = index.getKey();
                    key = index.getValue();
                }

                // only invoke if we have a method name to use to invoke
                if (methodName != null) {
                    InvokeProcessor invoke = new InvokeProcessor(holder, methodName);
                    invoke.process(resultExchange);

                    // check for exception and rethrow if we failed
                    if (resultExchange.getException() != null) {
                        throw new RuntimeBeanExpressionException(exchange, beanName, methodName, resultExchange.getException());
                    }

                    result = invoke.getResult();
                }

                // if there was a key then we need to lookup using the key
                if (key != null) {
                    result = lookupResult(resultExchange, key, result, nullSafe, ognlPath, holder.getBean());
                }

                // check null safe for null results
                if (result == null && nullSafe) {
                    return;
View Full Code Here

        return "BeanExpression[bean:" + (bean == null ? beanName : bean) + " method: " + method + "]";
    }

    public Object evaluate(Exchange exchange) {
        // either use registry lookup or a constant bean
        BeanHolder holder;
        if (bean == null) {
            holder = new RegistryBean(exchange.getContext(), beanName);
        } else {
            holder = new ConstantBeanHolder(bean, exchange.getContext());
        }

        // invoking the bean can either be the easy way or using OGNL

        // validate OGNL
        if (OgnlHelper.isInvalidValidOgnlExpression(method)) {
            ExpressionIllegalSyntaxException cause = new ExpressionIllegalSyntaxException(method);
            throw new RuntimeBeanExpressionException(exchange, beanName, method, cause);
        }

        if (OgnlHelper.isValidOgnlExpression(method)) {
            // okay the method is an ognl expression
            Object beanToCall = holder.getBean();
            OgnlInvokeProcessor ognl = new OgnlInvokeProcessor(beanToCall, method);
            try {
                ognl.process(exchange);
                return ognl.getResult();
            } catch (Exception e) {
View Full Code Here

            // loop and invoke each method
            Object beanToCall = bean;
            List<String> methods = OgnlHelper.splitOgnl(ognl);
            for (String methodName : methods) {
                BeanHolder holder = new ConstantBeanHolder(beanToCall, exchange.getContext());

                // support the null safe operator
                boolean nullSafe = OgnlHelper.isNullSafeOperator(methodName);

                // keep up with how far are we doing
                ognlPath += methodName;

                // get rid of leading ?. or . as we only needed that to determine if null safe was enabled or not
                methodName = OgnlHelper.removeLeadingOperators(methodName);

                // are we doing an index lookup (eg in Map/List/array etc)?
                String key = null;
                KeyValueHolder<String, String> index = OgnlHelper.isOgnlIndex(methodName);
                if (index != null) {
                    methodName = index.getKey();
                    key = index.getValue();
                }

                // only invoke if we have a method name to use to invoke
                if (methodName != null) {
                    InvokeProcessor invoke = new InvokeProcessor(holder, methodName);
                    invoke.process(resultExchange);

                    // check for exception and rethrow if we failed
                    if (resultExchange.getException() != null) {
                        throw new RuntimeBeanExpressionException(exchange, beanName, methodName, resultExchange.getException());
                    }

                    result = invoke.getResult();
                }

                // if there was a key then we need to lookup using the key
                if (key != null) {
                    result = lookupResult(resultExchange, key, result, nullSafe, ognlPath, holder.getBean());
                }

                // check null safe for null results
                if (result == null && nullSafe) {
                    return;
View Full Code Here

    private BeanProcessor beanProcessor;

    public RmiProducer(RmiEndpoint endpoint) throws RemoteException, NotBoundException {
        super(endpoint);
        BeanHolder holder = new RmiRegistryBean(endpoint.getContext(), endpoint.getName(), endpoint.getRegistry());
        beanProcessor = new BeanProcessor(holder);
        String method = endpoint.getMethod();
        if (method != null) {
            beanProcessor.setMethod(method);
        }
View Full Code Here

TOP

Related Classes of org.apache.camel.component.bean.BeanHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.