Search in sources :

Example 1 with FactoryFinder

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

the class GenericFileEndpoint method createGenericFileStrategy.

/**
     * A strategy method to lazily create the file strategy
     */
@SuppressWarnings("unchecked")
protected GenericFileProcessStrategy<T> createGenericFileStrategy() {
    Class<?> factory = null;
    try {
        FactoryFinder finder = getCamelContext().getFactoryFinder("META-INF/services/org/apache/camel/component/");
        log.trace("Using FactoryFinder: {}", finder);
        factory = finder.findClass(getScheme(), "strategy.factory.", CamelContext.class);
    } catch (ClassNotFoundException e) {
        log.trace("'strategy.factory.class' not found", e);
    } catch (IOException e) {
        log.trace("No strategy factory defined in 'META-INF/services/org/apache/camel/component/'", e);
    }
    if (factory == null) {
        // use default
        try {
            log.trace("Using ClassResolver to resolve class: {}", DEFAULT_STRATEGYFACTORY_CLASS);
            factory = this.getCamelContext().getClassResolver().resolveClass(DEFAULT_STRATEGYFACTORY_CLASS);
        } catch (Exception e) {
            log.trace("Cannot load class: {}", DEFAULT_STRATEGYFACTORY_CLASS, e);
        }
        // fallback and us this class loader
        try {
            if (log.isTraceEnabled()) {
                log.trace("Using classloader: {} to resolve class: {}", this.getClass().getClassLoader(), DEFAULT_STRATEGYFACTORY_CLASS);
            }
            factory = this.getCamelContext().getClassResolver().resolveClass(DEFAULT_STRATEGYFACTORY_CLASS, this.getClass().getClassLoader());
        } catch (Exception e) {
            if (log.isTraceEnabled()) {
                log.trace("Cannot load class: {} using classloader: " + this.getClass().getClassLoader(), DEFAULT_STRATEGYFACTORY_CLASS, e);
            }
        }
        if (factory == null) {
            throw new TypeNotPresentException(DEFAULT_STRATEGYFACTORY_CLASS + " class not found", null);
        }
    }
    try {
        Method factoryMethod = factory.getMethod("createGenericFileProcessStrategy", CamelContext.class, Map.class);
        Map<String, Object> params = getParamsAsMap();
        log.debug("Parameters for Generic file process strategy {}", params);
        return (GenericFileProcessStrategy<T>) ObjectHelper.invokeMethod(factoryMethod, null, getCamelContext(), params);
    } catch (NoSuchMethodException e) {
        throw new TypeNotPresentException(factory.getSimpleName() + ".createGenericFileProcessStrategy method not found", e);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) IOException(java.io.IOException) Method(java.lang.reflect.Method) ExpressionIllegalSyntaxException(org.apache.camel.ExpressionIllegalSyntaxException) IOException(java.io.IOException) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 2 with FactoryFinder

use of org.apache.camel.spi.FactoryFinder 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)

Example 3 with FactoryFinder

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

the class DefaultCamelContext method getComponentParameterJsonSchema.

public String getComponentParameterJsonSchema(String componentName) throws IOException {
    // use the component factory finder to find the package name of the component class, which is the location
    // where the documentation exists as well
    FactoryFinder finder = getFactoryFinder(DefaultComponentResolver.RESOURCE_PATH);
    try {
        Class<?> clazz = null;
        try {
            clazz = finder.findClass(componentName);
        } catch (NoFactoryAvailableException e) {
        // ignore, i.e. if a component is an auto-configured spring-boot
        // component
        }
        if (clazz == null) {
            // fallback and find existing component
            Component existing = hasComponent(componentName);
            if (existing != null) {
                clazz = existing.getClass();
            } else {
                return null;
            }
        }
        String packageName = clazz.getPackage().getName();
        packageName = packageName.replace('.', '/');
        String path = packageName + "/" + componentName + ".json";
        ClassResolver resolver = getClassResolver();
        InputStream inputStream = resolver.loadResourceAsStream(path);
        log.debug("Loading component JSON Schema for: {} using class resolver: {} -> {}", new Object[] { componentName, resolver, inputStream });
        if (inputStream != null) {
            try {
                return IOHelper.loadText(inputStream);
            } finally {
                IOHelper.close(inputStream);
            }
        }
        // special for ActiveMQ as it is really just JMS
        if ("ActiveMQComponent".equals(clazz.getSimpleName())) {
            return getComponentParameterJsonSchema("jms");
        } else {
            return null;
        }
    } catch (ClassNotFoundException e) {
        return null;
    }
}
Also used : InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Component(org.apache.camel.Component) PackageScanClassResolver(org.apache.camel.spi.PackageScanClassResolver) ClassResolver(org.apache.camel.spi.ClassResolver) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 4 with FactoryFinder

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

the class DefaultCamelContext method getDataFormatParameterJsonSchema.

public String getDataFormatParameterJsonSchema(String dataFormatName) throws IOException {
    // use the dataformat factory finder to find the package name of the dataformat class, which is the location
    // where the documentation exists as well
    FactoryFinder finder = getFactoryFinder(DefaultDataFormatResolver.DATAFORMAT_RESOURCE_PATH);
    try {
        Class<?> clazz = null;
        try {
            clazz = finder.findClass(dataFormatName);
        } catch (NoFactoryAvailableException e) {
        // ignore, i.e. if a component is an auto-configured spring-boot
        // data-formats
        }
        if (clazz == null) {
            return null;
        }
        String packageName = clazz.getPackage().getName();
        packageName = packageName.replace('.', '/');
        String path = packageName + "/" + dataFormatName + ".json";
        ClassResolver resolver = getClassResolver();
        InputStream inputStream = resolver.loadResourceAsStream(path);
        log.debug("Loading dataformat JSON Schema for: {} using class resolver: {} -> {}", new Object[] { dataFormatName, resolver, inputStream });
        if (inputStream != null) {
            try {
                return IOHelper.loadText(inputStream);
            } finally {
                IOHelper.close(inputStream);
            }
        }
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    }
}
Also used : InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) PackageScanClassResolver(org.apache.camel.spi.PackageScanClassResolver) ClassResolver(org.apache.camel.spi.ClassResolver) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 5 with FactoryFinder

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

the class DefaultCamelContext method getLanguageParameterJsonSchema.

public String getLanguageParameterJsonSchema(String languageName) throws IOException {
    // use the language factory finder to find the package name of the language class, which is the location
    // where the documentation exists as well
    FactoryFinder finder = getFactoryFinder(DefaultLanguageResolver.LANGUAGE_RESOURCE_PATH);
    try {
        Class<?> clazz = null;
        try {
            clazz = finder.findClass(languageName);
        } catch (NoFactoryAvailableException e) {
        // ignore, i.e. if a component is an auto-configured spring-boot
        // languages
        }
        if (clazz == null) {
            return null;
        }
        String packageName = clazz.getPackage().getName();
        packageName = packageName.replace('.', '/');
        String path = packageName + "/" + languageName + ".json";
        ClassResolver resolver = getClassResolver();
        InputStream inputStream = resolver.loadResourceAsStream(path);
        log.debug("Loading language JSON Schema for: {} using class resolver: {} -> {}", new Object[] { languageName, resolver, inputStream });
        if (inputStream != null) {
            try {
                return IOHelper.loadText(inputStream);
            } finally {
                IOHelper.close(inputStream);
            }
        }
        return null;
    } catch (ClassNotFoundException e) {
        return null;
    }
}
Also used : InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) PackageScanClassResolver(org.apache.camel.spi.PackageScanClassResolver) ClassResolver(org.apache.camel.spi.ClassResolver) 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