Search in sources :

Example 1 with PropertiesLocation

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

the class AbstractCamelContextFactoryBean method initPropertyPlaceholder.

protected void initPropertyPlaceholder() throws Exception {
    if (getCamelPropertyPlaceholder() != null) {
        CamelPropertyPlaceholderDefinition def = getCamelPropertyPlaceholder();
        List<PropertiesLocation> locations = new ArrayList<>();
        if (def.getLocation() != null) {
            ObjectHelper.createIterable(def.getLocation()).forEach(location -> locations.add(new PropertiesLocation((String) location)));
        }
        if (def.getLocations() != null) {
            def.getLocations().forEach(definition -> locations.add(definition.toLocation()));
        }
        PropertiesComponent pc = new PropertiesComponent();
        pc.setLocations(locations);
        pc.setEncoding(def.getEncoding());
        if (def.isCache() != null) {
            pc.setCache(def.isCache());
        }
        if (def.isIgnoreMissingLocation() != null) {
            pc.setIgnoreMissingLocation(def.isIgnoreMissingLocation());
        }
        // if using a custom resolver
        if (ObjectHelper.isNotEmpty(def.getPropertiesResolverRef())) {
            PropertiesResolver resolver = CamelContextHelper.mandatoryLookup(getContext(), def.getPropertiesResolverRef(), PropertiesResolver.class);
            pc.setPropertiesResolver(resolver);
        }
        // if using a custom parser
        if (ObjectHelper.isNotEmpty(def.getPropertiesParserRef())) {
            PropertiesParser parser = CamelContextHelper.mandatoryLookup(getContext(), def.getPropertiesParserRef(), PropertiesParser.class);
            pc.setPropertiesParser(parser);
        }
        pc.setPropertyPrefix(def.getPropertyPrefix());
        pc.setPropertySuffix(def.getPropertySuffix());
        if (def.isFallbackToUnaugmentedProperty() != null) {
            pc.setFallbackToUnaugmentedProperty(def.isFallbackToUnaugmentedProperty());
        }
        if (def.getDefaultFallbackEnabled() != null) {
            pc.setDefaultFallbackEnabled(def.getDefaultFallbackEnabled());
        }
        pc.setPrefixToken(def.getPrefixToken());
        pc.setSuffixToken(def.getSuffixToken());
        if (def.getFunctions() != null && !def.getFunctions().isEmpty()) {
            for (CamelPropertyPlaceholderFunctionDefinition function : def.getFunctions()) {
                String ref = function.getRef();
                PropertiesFunction pf = CamelContextHelper.mandatoryLookup(getContext(), ref, PropertiesFunction.class);
                pc.addFunction(pf);
            }
        }
        // register the properties component
        getContext().addComponent("properties", pc);
    }
}
Also used : PropertiesParser(org.apache.camel.component.properties.PropertiesParser) PropertiesResolver(org.apache.camel.component.properties.PropertiesResolver) ArrayList(java.util.ArrayList) PropertiesLocation(org.apache.camel.component.properties.PropertiesLocation) PropertiesFunction(org.apache.camel.component.properties.PropertiesFunction) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 2 with PropertiesLocation

use of org.apache.camel.component.properties.PropertiesLocation 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 3 with PropertiesLocation

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

the class BlueprintPropertiesResolver method resolveProperties.

@Override
public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
    Properties answer = new Properties();
    boolean explicit = false;
    for (PropertiesLocation location : locations) {
        if ("blueprint".equals(location.getResolver())) {
            blueprint.addPropertyPlaceholder(location.getPath());
            // indicate an explicit blueprint id was configured
            explicit = true;
        } else {
            // delegate the url
            answer.putAll(delegate.resolveProperties(context, ignoreMissingLocation, Collections.singletonList(location)));
        }
    }
    if (!explicit) {
        // this is convention over configuration
        for (String id : blueprint.lookupPropertyPlaceholderIds()) {
            blueprint.addPropertyPlaceholder(id);
        }
    }
    return answer;
}
Also used : PropertiesLocation(org.apache.camel.component.properties.PropertiesLocation) Properties(java.util.Properties)

Example 4 with PropertiesLocation

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

the class CamelSpringPropertiesLocationElementTest method testPropertiesLocationElement.

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

Example 5 with PropertiesLocation

use of org.apache.camel.component.properties.PropertiesLocation 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

PropertiesLocation (org.apache.camel.component.properties.PropertiesLocation)8 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)7 Test (org.junit.Test)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 PropertiesResolver (org.apache.camel.component.properties.PropertiesResolver)2 CamelContext (org.apache.camel.CamelContext)1 PropertiesFunction (org.apache.camel.component.properties.PropertiesFunction)1 PropertiesParser (org.apache.camel.component.properties.PropertiesParser)1 BridgePropertyPlaceholderConfigurer (org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer)1