Search in sources :

Example 41 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class BaseNettyTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    CamelContext context = super.createCamelContext();
    context.addComponent("properties", new PropertiesComponent("ref:prop"));
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 42 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class CamelServletContextListener method initPropertyPlaceholder.

/**
     * Initializes the property placeholders by registering the {@link PropertiesComponent} with
     * the configuration from the given init parameters.
     */
private void initPropertyPlaceholder(ServletCamelContext camelContext, Map<String, Object> parameters) throws Exception {
    // setup property placeholder first
    Map<String, Object> properties = IntrospectionSupport.extractProperties(parameters, "propertyPlaceholder.");
    if (properties != null && !properties.isEmpty()) {
        PropertiesComponent pc = new PropertiesComponent();
        IntrospectionSupport.setProperties(pc, properties);
        // validate we could set all parameters
        if (!properties.isEmpty()) {
            throw new IllegalArgumentException("Error setting propertyPlaceholder parameters on CamelContext." + " There are " + properties.size() + " unknown parameters. [" + properties + "]");
        }
        // register the properties component
        camelContext.addComponent("properties", pc);
    }
}
Also used : PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 43 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class CamelAnnotationsHandler method handleUseOverridePropertiesWithPropertiesComponent.

/**
     * Handles override this method to include and override properties with the Camel {@link org.apache.camel.component.properties.PropertiesComponent}.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
public static void handleUseOverridePropertiesWithPropertiesComponent(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
    Collection<Method> methods = getAllMethods(testClass);
    final List<Properties> properties = new LinkedList<Properties>();
    for (Method method : methods) {
        if (AnnotationUtils.findAnnotation(method, UseOverridePropertiesWithPropertiesComponent.class) != null) {
            Class<?>[] argTypes = method.getParameterTypes();
            if (argTypes.length > 0) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not a no-argument method.");
            } else if (!Properties.class.isAssignableFrom(method.getReturnType())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but does not return a java.util.Properties.");
            } else if (!Modifier.isStatic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not static.");
            } else if (!Modifier.isPublic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not public.");
            }
            try {
                properties.add((Properties) method.invoke(null));
            } catch (Exception e) {
                throw new RuntimeException("Method [" + method.getName() + "] threw exception during evaluation.", e);
            }
        }
    }
    if (properties.size() != 0) {
        CamelSpringTestHelper.doToSpringCamelContexts(context, new CamelSpringTestHelper.DoToSpringCamelContextsStrategy() {

            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class);
                Properties extra = new Properties();
                for (Properties prop : properties) {
                    extra.putAll(prop);
                }
                if (!extra.isEmpty()) {
                    LOGGER.info("Using {} properties to override any existing properties on the PropertiesComponent on CamelContext with name [{}].", extra.size(), contextName);
                    pc.setOverrideProperties(extra);
                }
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) Method(java.lang.reflect.Method) Properties(java.util.Properties) LinkedList(java.util.LinkedList) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 44 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class CamelSpringTestContextLoader method handleUseOverridePropertiesWithPropertiesComponent.

/**
     * Handles override this method to include and override properties with the Camel {@link org.apache.camel.component.properties.PropertiesComponent}.
     *
     * @param context the initialized Spring context
     * @param testClass the test class being executed
     */
protected void handleUseOverridePropertiesWithPropertiesComponent(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
    Collection<Method> methods = getAllMethods(testClass);
    final List<Properties> properties = new LinkedList<Properties>();
    for (Method method : methods) {
        if (AnnotationUtils.findAnnotation(method, UseOverridePropertiesWithPropertiesComponent.class) != null) {
            Class<?>[] argTypes = method.getParameterTypes();
            if (argTypes.length > 0) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not a no-argument method.");
            } else if (!Properties.class.isAssignableFrom(method.getReturnType())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but does not return a java.util.Properties.");
            } else if (!Modifier.isStatic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not static.");
            } else if (!Modifier.isPublic(method.getModifiers())) {
                throw new IllegalArgumentException("Method [" + method.getName() + "] is annotated with UseOverridePropertiesWithPropertiesComponent but is not public.");
            }
            try {
                properties.add((Properties) method.invoke(null));
            } catch (Exception e) {
                throw new RuntimeException("Method [" + method.getName() + "] threw exception during evaluation.", e);
            }
        }
    }
    if (properties.size() != 0) {
        CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

            public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
                PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class);
                Properties extra = new Properties();
                for (Properties prop : properties) {
                    extra.putAll(prop);
                }
                if (!extra.isEmpty()) {
                    LOG.info("Using {} properties to override any existing properties on the PropertiesComponent on CamelContext with name [{}].", extra.size(), contextName);
                    pc.setOverrideProperties(extra);
                }
            }
        });
    }
}
Also used : SpringCamelContext(org.apache.camel.spring.SpringCamelContext) Method(java.lang.reflect.Method) Properties(java.util.Properties) LinkedList(java.util.LinkedList) DoToSpringCamelContextsStrategy(org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 45 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class BlueprintPropertiesLocationElementImplicitTest method testPropertiesLocationElement.

@Test
public void testPropertiesLocationElement() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedHeaderReceived("property-1", "property-value-1");
    mock.expectedHeaderReceived("property-2", "property-value-2");
    mock.expectedHeaderReceived("cm", "cm-value");
    PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
    assertNotNull("Properties component not defined", pc);
    List<PropertiesLocation> locations = pc.getLocations();
    assertNotNull(locations);
    assertEquals("Properties locations", 2, locations.size());
    template.sendBody("direct:start", null);
    mock.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) PropertiesLocation(org.apache.camel.component.properties.PropertiesLocation) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Test(org.junit.Test)

Aggregations

PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)83 CamelContext (org.apache.camel.CamelContext)35 Properties (java.util.Properties)17 ApplicationScoped (javax.enterprise.context.ApplicationScoped)12 Produces (javax.enterprise.inject.Produces)12 Named (javax.inject.Named)12 RouteBuilder (org.apache.camel.builder.RouteBuilder)11 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)8 PropertiesLocation (org.apache.camel.component.properties.PropertiesLocation)7 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)3 Component (org.apache.camel.Component)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Exchange (org.apache.camel.Exchange)2