Search in sources :

Example 1 with NoFactoryAvailableException

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

the class OsgiFactoryFinderTest method testFindClass.

@Test
public void testFindClass() throws Exception {
    OsgiFactoryFinder finder = new OsgiFactoryFinder(getBundleContext(), new DefaultClassResolver(), "META-INF/services/org/apache/camel/component/");
    Class<?> clazz = finder.findClass("file_test", "strategy.factory.");
    assertNotNull("We should get the file strategy factory here", clazz);
    try {
        clazz = finder.findClass("nofile", "strategy.factory.");
        fail("We should get exception here");
    } catch (Exception ex) {
        assertTrue("Should get NoFactoryAvailableException", ex instanceof NoFactoryAvailableException);
    }
    try {
        clazz = finder.findClass("file_test", "nostrategy.factory.");
        fail("We should get exception here");
    } catch (Exception ex) {
        assertTrue("Should get IOException", ex instanceof IOException);
    }
}
Also used : DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) IOException(java.io.IOException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with NoFactoryAvailableException

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

the class ServiceCallExpressionConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public Expression newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "Expression factoryKey");
    Expression answer;
    // First try to find the factory from the registry.
    ServiceExpressionFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceExpressionFactory.class);
    if (factory != null) {
        // If a factory is found in the registry do not re-configure it as
        // it should be pre-configured.
        answer = factory.newInstance(camelContext);
    } else {
        Class<?> type;
        try {
            // Then use Service factory.
            type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
        } catch (Exception e) {
            throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
        }
        if (type != null) {
            if (ServiceExpressionFactory.class.isAssignableFrom(type)) {
                factory = (ServiceExpressionFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new IllegalArgumentException("Resolving Expression: " + factoryKey + " detected type conflict: Not a ExpressionFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            postProcessFactoryParameters(camelContext, parameters);
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : Expression(org.apache.camel.Expression) HashMap(java.util.HashMap) ServiceExpressionFactory(org.apache.camel.cloud.ServiceExpressionFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException)

Example 3 with NoFactoryAvailableException

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

the class ServiceCallLoadBalancerConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public LoadBalancer newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "LoadBalancer factoryKey");
    LoadBalancer answer;
    // First try to find the factory from the registry.
    LoadBalancerFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, LoadBalancerFactory.class);
    if (factory != null) {
        // If a factory is found in the registry do not re-configure it as
        // it should be pre-configured.
        answer = factory.newInstance(camelContext);
    } else {
        Class<?> type;
        try {
            // Then use Service factory.
            type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
        } catch (Exception e) {
            throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
        }
        if (type != null) {
            if (LoadBalancerFactory.class.isAssignableFrom(type)) {
                factory = (LoadBalancerFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new IllegalArgumentException("Resolving LoadBalancer: " + factoryKey + " detected type conflict: Not a LoadBalancerFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            postProcessFactoryParameters(camelContext, parameters);
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : HashMap(java.util.HashMap) LoadBalancer(org.apache.camel.cloud.LoadBalancer) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) LoadBalancerFactory(org.apache.camel.cloud.LoadBalancerFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException)

Example 4 with NoFactoryAvailableException

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

the class ServiceCallServiceFilterConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public ServiceFilter newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "ServiceFilter factoryKey");
    ServiceFilter answer;
    // First try to find the factory from the registry.
    ServiceFilterFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceFilterFactory.class);
    if (factory != null) {
        // If a factory is found in the registry do not re-configure it as
        // it should be pre-configured.
        answer = factory.newInstance(camelContext);
    } else {
        Class<?> type;
        try {
            // Then use Service factory.
            type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
        } catch (Exception e) {
            throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
        }
        if (type != null) {
            if (ServiceFilterFactory.class.isAssignableFrom(type)) {
                factory = (ServiceFilterFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new NoFactoryAvailableException("Resolving ServiceFilter: " + factoryKey + " detected type conflict: Not a ServiceFilterFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            postProcessFactoryParameters(camelContext, parameters);
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : ServiceFilter(org.apache.camel.cloud.ServiceFilter) ServiceFilterFactory(org.apache.camel.cloud.ServiceFilterFactory) HashMap(java.util.HashMap) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException)

Example 5 with NoFactoryAvailableException

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

the class DefaultProcessorFactory method createChildProcessor.

@Override
public Processor createChildProcessor(RouteContext routeContext, ProcessorDefinition<?> definition, boolean mandatory) throws Exception {
    String name = definition.getClass().getSimpleName();
    FactoryFinder finder = routeContext.getCamelContext().getFactoryFinder(RESOURCE_PATH);
    try {
        if (finder != null) {
            Object object = finder.newInstance(name);
            if (object != null && object instanceof ProcessorFactory) {
                ProcessorFactory pc = (ProcessorFactory) object;
                return pc.createChildProcessor(routeContext, definition, mandatory);
            }
        }
    } catch (NoFactoryAvailableException e) {
    // ignore there is no custom factory
    }
    return null;
}
Also used : NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) FactoryFinder(org.apache.camel.spi.FactoryFinder) ProcessorFactory(org.apache.camel.spi.ProcessorFactory)

Aggregations

NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)18 FactoryFinder (org.apache.camel.spi.FactoryFinder)7 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 IOException (java.io.IOException)3 Component (org.apache.camel.Component)3 ClassResolver (org.apache.camel.spi.ClassResolver)3 PackageScanClassResolver (org.apache.camel.spi.PackageScanClassResolver)3 BufferedInputStream (java.io.BufferedInputStream)2 Properties (java.util.Properties)2 ProcessorFactory (org.apache.camel.spi.ProcessorFactory)2 RestConfiguration (org.apache.camel.spi.RestConfiguration)2 URL (java.net.URL)1 Expression (org.apache.camel.Expression)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 NoSuchLanguageException (org.apache.camel.NoSuchLanguageException)1 Processor (org.apache.camel.Processor)1 Producer (org.apache.camel.Producer)1 LoadBalancer (org.apache.camel.cloud.LoadBalancer)1 LoadBalancerFactory (org.apache.camel.cloud.LoadBalancerFactory)1