Search in sources :

Example 81 with PropertiesComponent

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

the class RouteBuilder method propertyInject.

/**
     * Injects a property placeholder value with the given key converted to the given type.
     *
     * @param key  the property key
     * @param type the type to convert the value as
     * @return the value, or <tt>null</tt> if value is empty
     * @throws Exception is thrown if property with key not found or error converting to the given type.
     */
public <T> T propertyInject(String key, Class<T> type) throws Exception {
    ObjectHelper.notEmpty(key, "key");
    ObjectHelper.notNull(type, "Class type");
    // the properties component is mandatory
    Component component = 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 = getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
    // enclose key with {{ }} to force parsing
    Object value = pc.parseUri(pc.getPrefixToken() + key + pc.getSuffixToken());
    if (value != null) {
        return getContext().getTypeConverter().mandatoryConvertTo(type, value);
    } else {
        return null;
    }
}
Also used : Component(org.apache.camel.Component) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 82 with PropertiesComponent

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

the class CamelContextFactoryBean method initPropertyPlaceholder.

@Override
protected void initPropertyPlaceholder() throws Exception {
    super.initPropertyPlaceholder();
    // if blueprint property resolver is enabled on CamelContext then bridge PropertiesComponent to blueprint
    if (isUseBlueprintPropertyResolver()) {
        // lookup existing configured properties component
        PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
        BlueprintPropertiesParser parser = new BlueprintPropertiesParser(pc, blueprintContainer, pc.getPropertiesParser());
        BlueprintPropertiesResolver resolver = new BlueprintPropertiesResolver(pc.getPropertiesResolver(), parser);
        // any extra properties
        ServiceReference<?> ref = bundleContext.getServiceReference(PropertiesComponent.OVERRIDE_PROPERTIES);
        if (ref != null) {
            Properties extra = (Properties) bundleContext.getService(ref);
            if (extra != null) {
                pc.setOverrideProperties(extra);
            }
        }
        // no locations has been set, so its a default component
        if (pc.getLocations() == null) {
            String[] ids = parser.lookupPropertyPlaceholderIds();
            for (int i = 0; i < ids.length; i++) {
                if (!ids[i].startsWith("blueprint:")) {
                    ids[i] = "blueprint:" + ids[i];
                }
            }
            if (ids.length > 0) {
                // location supports multiple separated by comma
                pc.setLocations(ids);
            }
        }
        if (pc.getLocations() != null) {
            // bridge camel properties with blueprint
            pc.setPropertiesParser(parser);
            pc.setPropertiesResolver(resolver);
        }
    }
}
Also used : Properties(java.util.Properties) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 83 with PropertiesComponent

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

the class IgniteCamelStreamerTest method testUserSpecifiedCamelContextWithPropertyPlaceholders.

/**
     * @throws Exception
     */
public void testUserSpecifiedCamelContextWithPropertyPlaceholders() throws Exception {
    // Create a CamelContext with a custom property placeholder.
    CamelContext context = new DefaultCamelContext();
    PropertiesComponent pc = new PropertiesComponent("camel.test.properties");
    context.addComponent("properties", pc);
    // Replace the context path in the test URL with the property placeholder.
    url = url.replaceAll("/ignite", "{{test.contextPath}}");
    // Recreate the Camel streamer with the new URL.
    streamer = createCamelStreamer(dataStreamer);
    streamer.setSingleTupleExtractor(singleTupleExtractor());
    streamer.setCamelContext(context);
    // Subscribe to cache PUT events.
    CountDownLatch latch = subscribeToPutEvents(50);
    // Action time.
    streamer.start();
    // Before sending the messages, get the actual URL after the property placeholder was resolved,
    // stripping the jetty: prefix from it.
    url = streamer.getCamelContext().getEndpoints().iterator().next().getEndpointUri().replaceAll("jetty:", "");
    // Send messages.
    sendMessages(0, 50, false);
    // Assertions.
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertCacheEntriesLoaded(50);
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) CountDownLatch(java.util.concurrent.CountDownLatch) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

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