Search in sources :

Example 11 with FactoryFinder

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

the class OsgiDefaultCamelContext 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 : FactoryFinder(org.apache.camel.spi.FactoryFinder) BundleContext(org.osgi.framework.BundleContext)

Example 12 with FactoryFinder

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

the class RestApiEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    RestApiProcessorFactory factory = null;
    RestConfiguration config = getCamelContext().getRestConfiguration(componentName, true);
    // lookup in registry
    Set<RestApiProcessorFactory> factories = getCamelContext().getRegistry().findByType(RestApiProcessorFactory.class);
    if (factories != null && factories.size() == 1) {
        factory = factories.iterator().next();
    }
    // lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
    if (factory == null) {
        String name = apiComponentName != null ? apiComponentName : config.getApiComponent();
        if (name == null) {
            name = DEFAULT_API_COMPONENT_NAME;
        }
        try {
            FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
            Object instance = finder.newInstance(name);
            if (instance instanceof RestApiProcessorFactory) {
                factory = (RestApiProcessorFactory) instance;
            }
        } catch (NoFactoryAvailableException e) {
        // ignore
        }
    }
    if (factory != null) {
        // if no explicit port/host configured, then use port from rest configuration
        String host = "";
        int port = 80;
        if (config.getHost() != null) {
            host = config.getHost();
        }
        int num = config.getPort();
        if (num > 0) {
            port = num;
        }
        // if no explicit hostname set then resolve the hostname
        if (ObjectHelper.isEmpty(host)) {
            if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
                host = "0.0.0.0";
            } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
                host = HostUtils.getLocalHostName();
            } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
                host = HostUtils.getLocalIp();
            }
            // no host was configured so calculate a host to use
            // there should be no schema in the host (but only port)
            String targetHost = host + (port != 80 ? ":" + port : "");
            getParameters().put("host", targetHost);
        }
        // the base path should start with a leading slash
        String path = getPath();
        if (path != null && !path.startsWith("/")) {
            path = "/" + path;
        }
        // whether listing of the context id's is enabled or not
        boolean contextIdListing = config.isApiContextListing();
        Processor processor = factory.createApiProcessor(getCamelContext(), path, getContextIdPattern(), contextIdListing, config, getParameters());
        return new RestApiProducer(this, processor);
    } else {
        throw new IllegalStateException("Cannot find RestApiProcessorFactory in Registry or classpath (such as the camel-swagger-java component)");
    }
}
Also used : Processor(org.apache.camel.Processor) RestApiProcessorFactory(org.apache.camel.spi.RestApiProcessorFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) RestConfiguration(org.apache.camel.spi.RestConfiguration) FactoryFinder(org.apache.camel.spi.FactoryFinder) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint)

Example 13 with FactoryFinder

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

the class DefaultProcessorFactory method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext, ProcessorDefinition<?> definition) 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.createProcessor(routeContext, definition);
            }
        }
    } 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)

Example 14 with FactoryFinder

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

the class BlueprintCamelContext 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)

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