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 + ")";
}
};
}
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;
}
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;
}
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;
}
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();
}
Aggregations