Search in sources :

Example 6 with FactoryFinder

use of org.apache.camel.spi.FactoryFinder in project camel by apache.

the class DefaultCamelContext method getFactoryFinder.

public FactoryFinder getFactoryFinder(String path) throws NoFactoryAvailableException {
    synchronized (factories) {
        FactoryFinder answer = factories.get(path);
        if (answer == null) {
            answer = factoryFinderResolver.resolveFactoryFinder(getClassResolver(), path);
            factories.put(path, answer);
        }
        return answer;
    }
}
Also used : FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 7 with FactoryFinder

use of org.apache.camel.spi.FactoryFinder in project camel by apache.

the class RestEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    RestProducerFactory apiDocFactory = null;
    RestProducerFactory factory = null;
    if (apiDoc != null) {
        LOG.debug("Discovering camel-swagger-java on classpath for using api-doc: {}", apiDoc);
        // lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
        try {
            FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
            Object instance = finder.newInstance(DEFAULT_API_COMPONENT_NAME);
            if (instance instanceof RestProducerFactory) {
                // this factory from camel-swagger-java will facade the http component in use
                apiDocFactory = (RestProducerFactory) instance;
            }
            parameters.put("apiDoc", apiDoc);
        } catch (NoFactoryAvailableException e) {
            throw new IllegalStateException("Cannot find camel-swagger-java on classpath to use with api-doc: " + apiDoc);
        }
    }
    String cname = getComponentName();
    if (cname != null) {
        Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
        if (comp != null && comp instanceof RestProducerFactory) {
            factory = (RestProducerFactory) comp;
        } else {
            comp = getCamelContext().getComponent(getComponentName());
            if (comp != null && comp instanceof RestProducerFactory) {
                factory = (RestProducerFactory) comp;
            }
        }
        if (factory == null) {
            if (comp != null) {
                throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestProducerFactory");
            } else {
                throw new NoSuchBeanException(getComponentName(), RestProducerFactory.class.getName());
            }
        }
        cname = getComponentName();
    }
    // try all components
    if (factory == null) {
        for (String name : getCamelContext().getComponentNames()) {
            Component comp = getCamelContext().getComponent(name);
            if (comp != null && comp instanceof RestProducerFactory) {
                factory = (RestProducerFactory) comp;
                cname = name;
                break;
            }
        }
    }
    parameters.put("componentName", cname);
    // lookup in registry
    if (factory == null) {
        Set<RestProducerFactory> factories = getCamelContext().getRegistry().findByType(RestProducerFactory.class);
        if (factories != null && factories.size() == 1) {
            factory = factories.iterator().next();
        }
    }
    // and there must only be exactly one so we safely can pick this one
    if (factory == null) {
        RestProducerFactory found = null;
        String foundName = null;
        for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
            Object comp = getCamelContext().getComponent(name, true);
            if (comp != null && comp instanceof RestProducerFactory) {
                if (found == null) {
                    found = (RestProducerFactory) comp;
                    foundName = name;
                } else {
                    throw new IllegalArgumentException("Multiple RestProducerFactory found on classpath. Configure explicit which component to use");
                }
            }
        }
        if (found != null) {
            LOG.debug("Auto discovered {} as RestProducerFactory", foundName);
            factory = found;
        }
    }
    if (factory != null) {
        LOG.debug("Using RestProducerFactory: {}", factory);
        Producer producer;
        if (apiDocFactory != null) {
            // wrap the factory using the api doc factory which will use the factory
            parameters.put("restProducerFactory", factory);
            producer = apiDocFactory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
        } else {
            producer = factory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
        }
        RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
        RestProducer answer = new RestProducer(this, producer, config);
        answer.setOutType(outType);
        answer.setType(inType);
        return answer;
    } else {
        throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
    }
}
Also used : NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestProducerFactory(org.apache.camel.spi.RestProducerFactory) Producer(org.apache.camel.Producer) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 8 with FactoryFinder

use of org.apache.camel.spi.FactoryFinder in project camel by apache.

the class OsgiSpringCamelContext method createTypeConverter.

@Override
protected TypeConverter createTypeConverter() {
    // CAMEL-3614: make sure we use a bundle context which imports org.apache.camel.impl.converter package
    BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
    if (ctx == null) {
        ctx = bundleContext;
    }
    FactoryFinder finder = new OsgiFactoryFinderResolver(bundleContext).resolveDefaultFactoryFinder(getClassResolver());
    return new OsgiTypeConverter(ctx, this, getInjector(), finder);
}
Also used : OsgiFactoryFinderResolver(org.apache.camel.core.osgi.OsgiFactoryFinderResolver) OsgiTypeConverter(org.apache.camel.core.osgi.OsgiTypeConverter) FactoryFinder(org.apache.camel.spi.FactoryFinder) BundleContext(org.osgi.framework.BundleContext)

Example 9 with FactoryFinder

use of org.apache.camel.spi.FactoryFinder in project camel by apache.

the class CamelReactiveStreams method resolveServiceUsingFactory.

@SuppressWarnings("unchecked")
private static CamelReactiveStreamsService resolveServiceUsingFactory(CamelContext context, String name) {
    if (name == null) {
        name = "default-service";
    }
    String path = "META-INF/services/org/apache/camel/reactive-streams/";
    Class<? extends CamelReactiveStreamsService> serviceClass = null;
    try {
        FactoryFinder finder = context.getFactoryFinder(path);
        LOG.trace("Using FactoryFinder: {}", finder);
        serviceClass = (Class<? extends CamelReactiveStreamsService>) finder.findClass(name);
        return serviceClass.newInstance();
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Class referenced in '" + path + name + "' not found", e);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to create the reactive stream service defined in '" + path + name + "'", e);
    }
}
Also used : FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 10 with FactoryFinder

use of org.apache.camel.spi.FactoryFinder in project camel by apache.

the class JuelExpression method getExpressionFactory.

public synchronized ExpressionFactory getExpressionFactory(CamelContext context) {
    if (expressionFactory == null && context != null) {
        try {
            FactoryFinder finder = context.getFactoryFinder("META-INF/services/org/apache/camel/language/");
            Class<?> clazz = finder.findClass("el", "impl.", ExpressionFactory.class);
            if (clazz != null) {
                expressionFactory = (ExpressionFactory) clazz.newInstance();
            }
        } catch (ClassNotFoundException e) {
            LOG.debug("'impl.class' not found", e);
        } catch (IOException e) {
            LOG.debug("No impl class for juel ExpressionFactory defined in 'META-INF/services/org/apache/camel/language/el'", e);
        } catch (InstantiationException e) {
            LOG.debug("Failed to instantiate juel ExpressionFactory implementation class.", e);
        } catch (IllegalAccessException e) {
            LOG.debug("Failed to instantiate juel ExpressionFactory implementation class.", e);
        }
    }
    return getExpressionFactory();
}
Also used : IOException(java.io.IOException) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Aggregations

FactoryFinder (org.apache.camel.spi.FactoryFinder)14 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)7 InputStream (java.io.InputStream)3 ClassResolver (org.apache.camel.spi.ClassResolver)3 PackageScanClassResolver (org.apache.camel.spi.PackageScanClassResolver)3 BundleContext (org.osgi.framework.BundleContext)3 IOException (java.io.IOException)2 Component (org.apache.camel.Component)2 OsgiFactoryFinderResolver (org.apache.camel.core.osgi.OsgiFactoryFinderResolver)2 OsgiTypeConverter (org.apache.camel.core.osgi.OsgiTypeConverter)2 ProcessorFactory (org.apache.camel.spi.ProcessorFactory)2 RestConfiguration (org.apache.camel.spi.RestConfiguration)2 Method (java.lang.reflect.Method)1 CamelContext (org.apache.camel.CamelContext)1 ExpressionIllegalSyntaxException (org.apache.camel.ExpressionIllegalSyntaxException)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 Processor (org.apache.camel.Processor)1 Producer (org.apache.camel.Producer)1 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)1 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)1