Search in sources :

Example 1 with ClassResolver

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

the class OsgiClassResolverTest method testResolveClass.

@Test
public void testResolveClass() {
    ClassResolver classResolver = getClassResolver();
    Class<?> routeBuilder = classResolver.resolveClass("org.apache.camel.core.osgi.test.MyRouteBuilder");
    assertNotNull("The class of routeBuilder should not be null.", routeBuilder);
}
Also used : ClassResolver(org.apache.camel.spi.ClassResolver) Test(org.junit.Test)

Example 2 with ClassResolver

use of org.apache.camel.spi.ClassResolver 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 3 with ClassResolver

use of org.apache.camel.spi.ClassResolver 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 4 with ClassResolver

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

Example 5 with ClassResolver

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

the class XsltEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    final CamelContext ctx = getCamelContext();
    final ClassResolver resolver = ctx.getClassResolver();
    final Injector injector = ctx.getInjector();
    LOG.debug("{} using schema resource: {}", this, resourceUri);
    this.xslt = injector.newInstance(XsltBuilder.class);
    if (converter != null) {
        xslt.setConverter(converter);
    }
    boolean useSaxon = false;
    if (transformerFactoryClass == null && (saxon || saxonExtensionFunctions != null)) {
        useSaxon = true;
        transformerFactoryClass = SAXON_TRANSFORMER_FACTORY_CLASS_NAME;
    }
    TransformerFactory factory = transformerFactory;
    if (factory == null && transformerFactoryClass != null) {
        // provide the class loader of this component to work in OSGi environments
        Class<TransformerFactory> factoryClass = resolver.resolveMandatoryClass(transformerFactoryClass, TransformerFactory.class, XsltComponent.class.getClassLoader());
        LOG.debug("Using TransformerFactoryClass {}", factoryClass);
        factory = injector.newInstance(factoryClass);
        if (useSaxon) {
            XsltHelper.registerSaxonConfiguration(ctx, factoryClass, factory, saxonConfiguration);
            XsltHelper.registerSaxonConfigurationProperties(ctx, factoryClass, factory, saxonConfigurationProperties);
            XsltHelper.registerSaxonExtensionFunctions(ctx, factoryClass, factory, saxonExtensionFunctions);
        }
    }
    if (factory != null) {
        LOG.debug("Using TransformerFactory {}", factory);
        xslt.getConverter().setTransformerFactory(factory);
    }
    if (resultHandlerFactory != null) {
        xslt.setResultHandlerFactory(resultHandlerFactory);
    }
    if (errorListener != null) {
        xslt.errorListener(errorListener);
    }
    xslt.setFailOnNullBody(failOnNullBody);
    xslt.transformerCacheSize(transformerCacheSize);
    xslt.setUriResolver(uriResolver);
    xslt.setEntityResolver(entityResolver);
    xslt.setAllowStAX(allowStAX);
    xslt.setDeleteOutputFile(deleteOutputFile);
    configureOutput(xslt, output.name());
    // any additional transformer parameters then make a copy to avoid side-effects
    if (parameters != null) {
        Map<String, Object> copy = new HashMap<String, Object>(parameters);
        xslt.setParameters(copy);
    }
    // must load resource first which sets a template and do a stylesheet compilation to catch errors early
    loadResource(resourceUri);
    // the processor is the xslt builder
    setProcessor(xslt);
}
Also used : CamelContext(org.apache.camel.CamelContext) TransformerFactory(javax.xml.transform.TransformerFactory) HashMap(java.util.HashMap) Injector(org.apache.camel.spi.Injector) XsltBuilder(org.apache.camel.builder.xml.XsltBuilder) ClassResolver(org.apache.camel.spi.ClassResolver)

Aggregations

ClassResolver (org.apache.camel.spi.ClassResolver)13 InputStream (java.io.InputStream)6 PackageScanClassResolver (org.apache.camel.spi.PackageScanClassResolver)5 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)3 FactoryFinder (org.apache.camel.spi.FactoryFinder)3 Test (org.junit.Test)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Properties (java.util.Properties)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 CamelContext (org.apache.camel.CamelContext)1 Component (org.apache.camel.Component)1 XsltBuilder (org.apache.camel.builder.xml.XsltBuilder)1 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)1