Search in sources :

Example 1 with MethodNotFoundException

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

the class BeanLanguageInvalidOGNLTest method testBeanLanguageInvalidOGNL.

public void testBeanLanguageInvalidOGNL() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").transform().method(MyReallyCoolBean.class, "getOther[xx");
        }
    });
    try {
        context.start();
        fail("Should have thrown exception");
    } catch (FailedToCreateRouteException e) {
        RuntimeCamelException rce = assertIsInstanceOf(RuntimeCamelException.class, e.getCause());
        MethodNotFoundException mnfe = assertIsInstanceOf(MethodNotFoundException.class, rce.getCause());
        assertEquals("getOther[xx", mnfe.getMethodName());
        ExpressionIllegalSyntaxException cause = assertIsInstanceOf(ExpressionIllegalSyntaxException.class, mnfe.getCause());
        assertEquals("Illegal syntax: getOther[xx", cause.getMessage());
        assertEquals("getOther[xx", cause.getExpression());
    }
}
Also used : FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) RouteBuilder(org.apache.camel.builder.RouteBuilder) ExpressionIllegalSyntaxException(org.apache.camel.ExpressionIllegalSyntaxException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MethodNotFoundException(org.apache.camel.component.bean.MethodNotFoundException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) ExpressionIllegalSyntaxException(org.apache.camel.ExpressionIllegalSyntaxException) MethodNotFoundException(org.apache.camel.component.bean.MethodNotFoundException)

Example 2 with MethodNotFoundException

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

the class BeanTest method testNoMethodBeanLookup.

public void testNoMethodBeanLookup() throws Exception {
    Expression exp = BeanLanguage.bean("foo.cake");
    Exchange exchange = createExchangeWithBody("Claus");
    Object result = exp.evaluate(exchange, Object.class);
    assertNull(result);
    assertNotNull(exchange.getException());
    MethodNotFoundException e = assertIsInstanceOf(MethodNotFoundException.class, exchange.getException());
    assertSame(context.getRegistry().lookupByName("foo"), e.getBean());
    assertEquals("cake", e.getMethodName());
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) MethodNotFoundException(org.apache.camel.component.bean.MethodNotFoundException)

Example 3 with MethodNotFoundException

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

the class BeanTest method testNoMethod.

public void testNoMethod() throws Exception {
    MyUser user = new MyUser();
    Expression exp = BeanLanguage.bean(user, "unknown");
    Exchange exchange = createExchangeWithBody("Claus");
    Object result = exp.evaluate(exchange, Object.class);
    assertNull(result);
    assertNotNull(exchange.getException());
    MethodNotFoundException e = assertIsInstanceOf(MethodNotFoundException.class, exchange.getException());
    assertSame(user, e.getBean());
    assertEquals("unknown", e.getMethodName());
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) MethodNotFoundException(org.apache.camel.component.bean.MethodNotFoundException)

Example 4 with MethodNotFoundException

use of org.apache.camel.component.bean.MethodNotFoundException 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)

Aggregations

MethodNotFoundException (org.apache.camel.component.bean.MethodNotFoundException)4 Exchange (org.apache.camel.Exchange)2 Expression (org.apache.camel.Expression)2 ExpressionIllegalSyntaxException (org.apache.camel.ExpressionIllegalSyntaxException)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 BeanHolder (org.apache.camel.component.bean.BeanHolder)1 BeanInfo (org.apache.camel.component.bean.BeanInfo)1 BeanProcessor (org.apache.camel.component.bean.BeanProcessor)1 ConstantBeanHolder (org.apache.camel.component.bean.ConstantBeanHolder)1 ConstantStaticTypeBeanHolder (org.apache.camel.component.bean.ConstantStaticTypeBeanHolder)1 ConstantTypeBeanHolder (org.apache.camel.component.bean.ConstantTypeBeanHolder)1 RegistryBean (org.apache.camel.component.bean.RegistryBean)1