Search in sources :

Example 6 with PropertiesComponent

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

the class ExpressionBuilder method propertiesComponentExpression.

public static Expression propertiesComponentExpression(final String key, final String locations, final String defaultValue) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(key).evaluate(exchange, String.class);
            String text2 = simpleExpression(locations).evaluate(exchange, String.class);
            try {
                if (text2 != null) {
                    // the properties component is optional as we got locations
                    // getComponent will create a new component if none already exists
                    Component component = exchange.getContext().getComponent("properties");
                    PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
                    // enclose key with {{ }} to force parsing
                    String[] paths = text2.split(",");
                    return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken(), paths);
                } else {
                    // the properties component is mandatory if no locations provided
                    Component component = exchange.getContext().hasComponent("properties");
                    if (component == null) {
                        throw new IllegalArgumentException("PropertiesComponent with name properties must be defined" + " in CamelContext to support property placeholders in expressions");
                    }
                    PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
                    // enclose key with {{ }} to force parsing
                    return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken());
                }
            } catch (Exception e) {
                // property with key not found, use default value if provided
                if (defaultValue != null) {
                    return defaultValue;
                }
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }

        @Override
        public String toString() {
            return "properties(" + key + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Component(org.apache.camel.Component) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 7 with PropertiesComponent

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

the class CamelPostProcessorHelperTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    CamelContext context = super.createCamelContext();
    PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
    pc.setLocation("ref:myProp");
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 8 with PropertiesComponent

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

the class PropertiesAvailableEverywhereTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    CamelContext camelContext = super.createCamelContext();
    final Properties properties = new Properties();
    properties.put("foo", "bar");
    PropertiesComponent pc = camelContext.getComponent("properties", PropertiesComponent.class);
    pc.setLocations(new String[0]);
    pc.setPropertiesResolver(new PropertiesResolver() {

        @Override
        public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
            return properties;
        }
    });
    return camelContext;
}
Also used : CamelContext(org.apache.camel.CamelContext) PropertiesResolver(org.apache.camel.component.properties.PropertiesResolver) PropertiesLocation(org.apache.camel.component.properties.PropertiesLocation) Properties(java.util.Properties) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 9 with PropertiesComponent

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

the class ManagedCamelContextUpdateRoutesWithPropertyPlaceholdersFromXmlPTest method createCamelContext.

@Override
protected CamelContext createCamelContext() throws Exception {
    props = new Properties();
    props.put("somewhere", "mock:changed");
    props.put("theBar", "mock:bar");
    CamelContext context = super.createCamelContext();
    PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
    pc.setLocations(new String[0]);
    pc.setOverrideProperties(props);
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) Properties(java.util.Properties) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 10 with PropertiesComponent

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

the class RouteAutoStartupTest method testRouteAutoStartedUsingProperties.

public void testRouteAutoStartedUsingProperties() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            PropertiesComponent properties = new PropertiesComponent();
            properties.setLocation("classpath:org/apache/camel/processor/routeAutoStartupTest.properties");
            context.addComponent("properties", properties);
            from("direct:start").autoStartup("{{autoStartupProp}}").to("mock:result");
        }
    });
    context.start();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

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