Search in sources :

Example 36 with Component

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

the class OsgiComponentResolverTest method testOsgiResolverFindLanguageDoubleFallbackTest.

@Test
public void testOsgiResolverFindLanguageDoubleFallbackTest() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("allstar", new SampleComponent(false));
    registry.put("allstar-component", new SampleComponent(true));
    CamelContext camelContext = new DefaultCamelContext(registry);
    OsgiComponentResolver resolver = new OsgiComponentResolver(getBundleContext());
    Component component = resolver.resolveComponent("allstar", camelContext);
    assertNotNull("We should find the super component", component);
    assertTrue("We should get the super component here", component instanceof SampleComponent);
    assertFalse("We should NOT find the fallback component", ((SampleComponent) component).isFallback());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) Component(org.apache.camel.Component) FileComponent(org.apache.camel.component.file.FileComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 37 with Component

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

the class OsgiComponentResolverTest method testOsgiResolverFindComponentTest.

@Test
public void testOsgiResolverFindComponentTest() throws Exception {
    CamelContext camelContext = new DefaultCamelContext();
    OsgiComponentResolver resolver = new OsgiComponentResolver(getBundleContext());
    Component component = resolver.resolveComponent("file_test", camelContext);
    assertNotNull("We should find file_test component", component);
    assertTrue("We should get the file component here", component instanceof FileComponent);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) FileComponent(org.apache.camel.component.file.FileComponent) Component(org.apache.camel.Component) FileComponent(org.apache.camel.component.file.FileComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 38 with Component

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

the class OsgiComponentResolverTest method testOsgiResolverFindComponentFallbackTest.

@Test
public void testOsgiResolverFindComponentFallbackTest() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("allstar-component", new SampleComponent(true));
    CamelContext camelContext = new DefaultCamelContext(registry);
    OsgiComponentResolver resolver = new OsgiComponentResolver(getBundleContext());
    Component component = resolver.resolveComponent("allstar", camelContext);
    assertNotNull("We should find the super component", component);
    assertTrue("We should get the super component here", component instanceof SampleComponent);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) SimpleRegistry(org.apache.camel.impl.SimpleRegistry) Component(org.apache.camel.Component) FileComponent(org.apache.camel.component.file.FileComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 39 with Component

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

the class ManagedCamelContext method componentParameterJsonSchema.

public String componentParameterJsonSchema(String componentName) throws Exception {
    // favor using pre generated schema if component has that
    String json = context.getComponentParameterJsonSchema(componentName);
    if (json == null) {
        // okay this requires having the component on the classpath and being instantiated
        Component component = context.getComponent(componentName);
        if (component != null) {
            ComponentConfiguration configuration = component.createComponentConfiguration();
            json = configuration.createParameterJsonSchema();
        }
    }
    return json;
}
Also used : ComponentConfiguration(org.apache.camel.ComponentConfiguration) Component(org.apache.camel.Component)

Example 40 with Component

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

the class CamelContextHelper method findComponents.

public static SortedMap<String, Properties> findComponents(CamelContext camelContext, Enumeration<URL> componentDescriptionIter) throws LoadPropertiesException {
    SortedMap<String, Properties> map = new TreeMap<String, Properties>();
    while (componentDescriptionIter != null && componentDescriptionIter.hasMoreElements()) {
        URL url = componentDescriptionIter.nextElement();
        LOG.trace("Finding components in url: {}", url);
        try {
            Properties properties = new Properties();
            properties.load(url.openStream());
            String names = properties.getProperty("components");
            if (names != null) {
                StringTokenizer tok = new StringTokenizer(names);
                while (tok.hasMoreTokens()) {
                    String name = tok.nextToken();
                    // try to find the class name for this component
                    String className = null;
                    InputStream is = null;
                    try {
                        // now load the component name resource so we can grab its properties and the class name
                        Enumeration<URL> urls = camelContext.getClassResolver().loadAllResourcesAsURL(COMPONENT_BASE + name);
                        if (urls != null && urls.hasMoreElements()) {
                            is = urls.nextElement().openStream();
                        }
                        if (is != null) {
                            Properties compProperties = new Properties();
                            compProperties.load(is);
                            if (!compProperties.isEmpty()) {
                                className = compProperties.getProperty("class");
                            }
                        }
                    } catch (Exception e) {
                    // ignore
                    } finally {
                        IOHelper.close(is);
                    }
                    // inherit properties we loaded first, as it has maven details
                    Properties prop = new Properties();
                    prop.putAll(properties);
                    if (camelContext.hasComponent(name) != null) {
                        prop.put("component", camelContext.getComponent(name));
                    }
                    if (className != null) {
                        prop.put("class", className);
                    }
                    prop.put("name", name);
                    map.put(name, prop);
                }
            }
        } catch (IOException e) {
            throw new LoadPropertiesException(url, e);
        }
    }
    // lets see what other components are registered on camel context
    List<String> names = camelContext.getComponentNames();
    for (String name : names) {
        if (!map.containsKey(name)) {
            Component component = camelContext.getComponent(name);
            if (component != null) {
                Properties properties = new Properties();
                properties.put("component", component);
                properties.put("class", component.getClass().getName());
                properties.put("name", name);
                // override default component if name clash
                map.put(name, properties);
            }
        }
    }
    // lets see what other components are in the registry
    Map<String, Component> beanMap = camelContext.getRegistry().findByTypeWithName(Component.class);
    Set<Map.Entry<String, Component>> entries = beanMap.entrySet();
    for (Map.Entry<String, Component> entry : entries) {
        String name = entry.getKey();
        if (!map.containsKey(name)) {
            Component component = entry.getValue();
            if (component != null) {
                Properties properties = new Properties();
                properties.put("component", component);
                properties.put("class", component.getClass().getName());
                properties.put("name", name);
                map.put(name, properties);
            }
        }
    }
    return map;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Properties(java.util.Properties) URL(java.net.URL) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) NoSuchBeanException(org.apache.camel.NoSuchBeanException) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) Component(org.apache.camel.Component) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

Component (org.apache.camel.Component)52 Test (org.junit.Test)24 ComponentConfiguration (org.apache.camel.ComponentConfiguration)14 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)9 SedaComponent (org.apache.camel.component.seda.SedaComponent)9 Map (java.util.Map)7 CamelContext (org.apache.camel.CamelContext)7 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)7 IOException (java.io.IOException)6 Endpoint (org.apache.camel.Endpoint)6 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)6 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)6 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)5 LinkedHashMap (java.util.LinkedHashMap)4 MalformedObjectNameException (javax.management.MalformedObjectNameException)4 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)4 NoSuchBeanException (org.apache.camel.NoSuchBeanException)4 RuntimeCamelException (org.apache.camel.RuntimeCamelException)4 VetoCamelContextStartException (org.apache.camel.VetoCamelContextStartException)4 DirectComponent (org.apache.camel.component.direct.DirectComponent)4