Search in sources :

Example 21 with Component

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

the class RestEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    RestProducerFactory apiDocFactory = null;
    RestProducerFactory factory = null;
    if (apiDoc != null) {
        LOG.debug("Discovering camel-swagger-java on classpath for using api-doc: {}", apiDoc);
        // lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
        try {
            FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
            Object instance = finder.newInstance(DEFAULT_API_COMPONENT_NAME);
            if (instance instanceof RestProducerFactory) {
                // this factory from camel-swagger-java will facade the http component in use
                apiDocFactory = (RestProducerFactory) instance;
            }
            parameters.put("apiDoc", apiDoc);
        } catch (NoFactoryAvailableException e) {
            throw new IllegalStateException("Cannot find camel-swagger-java on classpath to use with api-doc: " + apiDoc);
        }
    }
    String cname = getComponentName();
    if (cname != null) {
        Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
        if (comp != null && comp instanceof RestProducerFactory) {
            factory = (RestProducerFactory) comp;
        } else {
            comp = getCamelContext().getComponent(getComponentName());
            if (comp != null && comp instanceof RestProducerFactory) {
                factory = (RestProducerFactory) comp;
            }
        }
        if (factory == null) {
            if (comp != null) {
                throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestProducerFactory");
            } else {
                throw new NoSuchBeanException(getComponentName(), RestProducerFactory.class.getName());
            }
        }
        cname = getComponentName();
    }
    // try all components
    if (factory == null) {
        for (String name : getCamelContext().getComponentNames()) {
            Component comp = getCamelContext().getComponent(name);
            if (comp != null && comp instanceof RestProducerFactory) {
                factory = (RestProducerFactory) comp;
                cname = name;
                break;
            }
        }
    }
    parameters.put("componentName", cname);
    // lookup in registry
    if (factory == null) {
        Set<RestProducerFactory> factories = getCamelContext().getRegistry().findByType(RestProducerFactory.class);
        if (factories != null && factories.size() == 1) {
            factory = factories.iterator().next();
        }
    }
    // and there must only be exactly one so we safely can pick this one
    if (factory == null) {
        RestProducerFactory found = null;
        String foundName = null;
        for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
            Object comp = getCamelContext().getComponent(name, true);
            if (comp != null && comp instanceof RestProducerFactory) {
                if (found == null) {
                    found = (RestProducerFactory) comp;
                    foundName = name;
                } else {
                    throw new IllegalArgumentException("Multiple RestProducerFactory found on classpath. Configure explicit which component to use");
                }
            }
        }
        if (found != null) {
            LOG.debug("Auto discovered {} as RestProducerFactory", foundName);
            factory = found;
        }
    }
    if (factory != null) {
        LOG.debug("Using RestProducerFactory: {}", factory);
        Producer producer;
        if (apiDocFactory != null) {
            // wrap the factory using the api doc factory which will use the factory
            parameters.put("restProducerFactory", factory);
            producer = apiDocFactory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
        } else {
            producer = factory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
        }
        RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
        RestProducer answer = new RestProducer(this, producer, config);
        answer.setOutType(outType);
        answer.setType(inType);
        return answer;
    } else {
        throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
    }
}
Also used : NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestProducerFactory(org.apache.camel.spi.RestProducerFactory) Producer(org.apache.camel.Producer) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 22 with Component

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

the class ManagedCamelContext method completeEndpointPath.

public List<String> completeEndpointPath(String componentName, Map<String, Object> endpointParameters, String completionText) throws Exception {
    if (completionText == null) {
        completionText = "";
    }
    Component component = context.getComponent(componentName, false);
    if (component != null) {
        ComponentConfiguration configuration = component.createComponentConfiguration();
        configuration.setParameters(endpointParameters);
        return configuration.completeEndpointPath(completionText);
    } else {
        return new ArrayList<String>();
    }
}
Also used : ComponentConfiguration(org.apache.camel.ComponentConfiguration) ArrayList(java.util.ArrayList) Component(org.apache.camel.Component)

Example 23 with Component

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

the class EndpointConfigurationTest method createContext.

@BeforeClass
public static void createContext() throws Exception {
    context = new DefaultCamelContext();
    Component component = new ConfiguredComponent();
    context.addComponent(MAPPED_SCHEME, component);
    // so that TypeConverters are available
    context.start();
}
Also used : Component(org.apache.camel.Component) BeforeClass(org.junit.BeforeClass)

Example 24 with Component

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

the class ComponentFoundInRegistryTest method testGuice.

@Test
public void testGuice() throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.PROVIDER_URL, GuiceInitialContextFactory.class.getName());
    env.put(Injectors.MODULE_CLASS_NAMES, MyModule.class.getName());
    InitialContext context = new InitialContext(env);
    Injector injector = (Injector) context.lookup(Injector.class.getName());
    assertNotNull("Found injector", injector);
    Object value = context.lookup("foo");
    assertNotNull("Should have found a value for foo!", value);
    CamelContext camelContext = injector.getInstance(CamelContext.class);
    Component component = camelContext.getComponent("foo");
    assertThat(component, is(MockComponent.class));
    Endpoint endpoint = camelContext.getEndpoint("foo:cheese");
    assertThat(endpoint, is(MockEndpoint.class));
}
Also used : CamelContext(org.apache.camel.CamelContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Hashtable(java.util.Hashtable) MockComponent(org.apache.camel.component.mock.MockComponent) InitialContext(javax.naming.InitialContext) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Injector(com.google.inject.Injector) MockComponent(org.apache.camel.component.mock.MockComponent) Component(org.apache.camel.Component) GuiceInitialContextFactory(org.apache.camel.guice.jndi.GuiceInitialContextFactory) Test(org.junit.Test)

Example 25 with Component

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

the class HttpCustomComponentNameTest method testCustomName.

@Test
public void testCustomName() throws Exception {
    context.start();
    int port = AvailablePortFinder.getNextAvailable(24400);
    Component custom = new HttpComponent();
    context.addComponent("http-foo", custom);
    ServiceHelper.startService(custom);
    String uri = "http-foo://www.somewhere.com:" + port + "?q=Camel";
    Endpoint endpoint = context.getEndpoint(uri);
    assertNotNull(endpoint);
    // the endpoint uri should use the custom component name as scheme
    assertEquals(uri, endpoint.getEndpointUri());
    context.stop();
}
Also used : Endpoint(org.apache.camel.Endpoint) Component(org.apache.camel.Component) Endpoint(org.apache.camel.Endpoint) Test(org.junit.Test)

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