Search in sources :

Example 11 with NoSuchBeanException

use of org.apache.camel.NoSuchBeanException in project camel by apache.

the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldNameUnknown.

public void testEndpointInjectProducerTemplateFieldNameUnknown() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyEndpointInjectProducerTemplateNameUnknown bean = new MyEndpointInjectProducerTemplateNameUnknown();
    Field field = bean.getClass().getField("producer");
    EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
    Class<?> type = field.getType();
    String propertyName = "producer";
    try {
        helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
        fail("Should throw exception");
    } catch (NoSuchBeanException e) {
        assertEquals("No bean could be found in the registry for: unknown of type: org.apache.camel.Endpoint", e.getMessage());
    }
}
Also used : EndpointInject(org.apache.camel.EndpointInject) Field(java.lang.reflect.Field) NoSuchBeanException(org.apache.camel.NoSuchBeanException)

Example 12 with NoSuchBeanException

use of org.apache.camel.NoSuchBeanException in project camel by apache.

the class CamelPostProcessorHelperTest method testBeanInjectNotFound.

public void testBeanInjectNotFound() throws Exception {
    CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
    MyBeanInjectBean bean = new MyBeanInjectBean();
    Field field = bean.getClass().getField("foo");
    Class<?> type = field.getType();
    try {
        helper.getInjectionBeanValue(type, "bar");
        fail("Should have thrown exception");
    } catch (NoSuchBeanException e) {
        assertEquals("No bean could be found in the registry for: bar of type: org.apache.camel.impl.FooBar", e.getMessage());
        assertEquals("bar", e.getName());
    }
}
Also used : Field(java.lang.reflect.Field) NoSuchBeanException(org.apache.camel.NoSuchBeanException)

Example 13 with NoSuchBeanException

use of org.apache.camel.NoSuchBeanException in project camel by apache.

the class TransactedDefinition method doResolvePolicy.

protected static Policy doResolvePolicy(RouteContext routeContext, String ref, Class<? extends Policy> type) {
    // explicit ref given so lookup by it
    if (ObjectHelper.isNotEmpty(ref)) {
        return CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), ref, Policy.class);
    }
    // no explicit reference given from user so we can use some convention over configuration here
    // try to lookup by scoped type
    Policy answer = null;
    if (type != null) {
        // try find by type, note that this method is not supported by all registry
        Map<String, ?> types = routeContext.lookupByType(type);
        if (types.size() == 1) {
            // only one policy defined so use it
            Object found = types.values().iterator().next();
            if (type.isInstance(found)) {
                return type.cast(found);
            }
        }
    }
    // for transacted routing try the default REQUIRED name
    if (type == TransactedPolicy.class) {
        // still not found try with the default name PROPAGATION_REQUIRED
        answer = routeContext.lookup(PROPAGATION_REQUIRED, TransactedPolicy.class);
    }
    // still no policy found then try lookup the platform transaction manager and use it as policy
    if (answer == null && type == TransactedPolicy.class) {
        Class<?> tmClazz = routeContext.getCamelContext().getClassResolver().resolveClass("org.springframework.transaction.PlatformTransactionManager");
        if (tmClazz != null) {
            // see if we can find the platform transaction manager in the registry
            Map<String, ?> maps = routeContext.lookupByType(tmClazz);
            if (maps.size() == 1) {
                // only one platform manager then use it as default and create a transacted
                // policy with it and default to required
                // as we do not want dependency on spring jars in the camel-core we use
                // reflection to lookup classes and create new objects and call methods
                // as this is only done during route building it does not matter that we
                // use reflection as performance is no a concern during route building
                Object transactionManager = maps.values().iterator().next();
                LOG.debug("One instance of PlatformTransactionManager found in registry: {}", transactionManager);
                Class<?> txClazz = routeContext.getCamelContext().getClassResolver().resolveClass("org.apache.camel.spring.spi.SpringTransactionPolicy");
                if (txClazz != null) {
                    LOG.debug("Creating a new temporary SpringTransactionPolicy using the PlatformTransactionManager: {}", transactionManager);
                    TransactedPolicy txPolicy = ObjectHelper.newInstance(txClazz, TransactedPolicy.class);
                    Method method;
                    try {
                        method = txClazz.getMethod("setTransactionManager", tmClazz);
                    } catch (NoSuchMethodException e) {
                        throw new RuntimeCamelException("Cannot get method setTransactionManager(PlatformTransactionManager) on class: " + txClazz);
                    }
                    ObjectHelper.invokeMethod(method, txPolicy, transactionManager);
                    return txPolicy;
                } else {
                    // camel-spring is missing on the classpath
                    throw new RuntimeCamelException("Cannot create a transacted policy as camel-spring.jar is not on the classpath!");
                }
            } else {
                if (maps.isEmpty()) {
                    throw new NoSuchBeanException(null, "PlatformTransactionManager");
                } else {
                    throw new IllegalArgumentException("Found " + maps.size() + " PlatformTransactionManager in registry. " + "Cannot determine which one to use. Please configure a TransactionTemplate on the transacted policy.");
                }
            }
        }
    }
    return answer;
}
Also used : TransactedPolicy(org.apache.camel.spi.TransactedPolicy) Policy(org.apache.camel.spi.Policy) TransactedPolicy(org.apache.camel.spi.TransactedPolicy) NoSuchBeanException(org.apache.camel.NoSuchBeanException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Method(java.lang.reflect.Method)

Aggregations

NoSuchBeanException (org.apache.camel.NoSuchBeanException)13 Component (org.apache.camel.Component)3 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 RestConfiguration (org.apache.camel.spi.RestConfiguration)3 Field (java.lang.reflect.Field)2 Consumer (org.apache.camel.Consumer)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 SessionClient (com.orbitz.consul.SessionClient)1 SessionCreatedResponse (com.orbitz.consul.model.session.SessionCreatedResponse)1 Method (java.lang.reflect.Method)1 CamelContext (org.apache.camel.CamelContext)1 EndpointInject (org.apache.camel.EndpointInject)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 Producer (org.apache.camel.Producer)1 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)1 SimpleLanguage (org.apache.camel.language.simple.SimpleLanguage)1 FactoryFinder (org.apache.camel.spi.FactoryFinder)1 Language (org.apache.camel.spi.Language)1 Policy (org.apache.camel.spi.Policy)1