Search in sources :

Example 11 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 12 with NoFactoryAvailableException

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

the class OsgiFactoryFinder method findClass.

@Override
public Class<?> findClass(String key, String propertyPrefix, Class<?> checkClass) throws ClassNotFoundException, IOException {
    final String prefix = propertyPrefix != null ? propertyPrefix : "";
    final String classKey = propertyPrefix + key;
    return addToClassMap(classKey, () -> {
        BundleEntry entry = getResource(key, checkClass);
        if (entry != null) {
            URL url = entry.url;
            InputStream in = url.openStream();
            // lets load the file
            BufferedInputStream reader = null;
            try {
                reader = IOHelper.buffered(in);
                Properties properties = new Properties();
                properties.load(reader);
                String className = properties.getProperty(prefix + "class");
                if (className == null) {
                    throw new IOException("Expected property is missing: " + prefix + "class");
                }
                return entry.bundle.loadClass(className);
            } finally {
                IOHelper.close(reader, key, null);
                IOHelper.close(in, key, null);
            }
        } else {
            throw new NoFactoryAvailableException(classKey);
        }
    });
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Example 13 with NoFactoryAvailableException

use of org.apache.camel.NoFactoryAvailableException 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 14 with NoFactoryAvailableException

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

the class DefaultFactoryFinder method doFindFactoryProperties.

private Properties doFindFactoryProperties(String key) throws IOException {
    String uri = path + key;
    InputStream in = classResolver.loadResourceAsStream(uri);
    if (in == null) {
        throw new NoFactoryAvailableException(uri);
    }
    // lets load the file
    BufferedInputStream reader = null;
    try {
        reader = IOHelper.buffered(in);
        Properties properties = new Properties();
        properties.load(reader);
        return properties;
    } finally {
        IOHelper.close(reader, key, null);
        IOHelper.close(in, key, null);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) Properties(java.util.Properties)

Example 15 with NoFactoryAvailableException

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

the class DefaultLanguageResolver method resolveLanguage.

public Language resolveLanguage(String name, CamelContext context) {
    // lookup in registry first
    Language languageReg = ResolverHelper.lookupLanguageInRegistryWithFallback(context, name);
    if (languageReg != null) {
        return languageReg;
    }
    Class<?> type = null;
    try {
        type = findLanguage(name, context);
    } catch (NoFactoryAvailableException e) {
    // ignore
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid URI, no Language registered for scheme: " + name, e);
    }
    if (type != null) {
        if (Language.class.isAssignableFrom(type)) {
            return (Language) context.getInjector().newInstance(type);
        } else {
            throw new IllegalArgumentException("Resolving language: " + name + " detected type conflict: Not a Language implementation. Found: " + type.getName());
        }
    } else {
        // no specific language found then try fallback
        return noSpecificLanguageFound(name, context);
    }
}
Also used : Language(org.apache.camel.spi.Language) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException)

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