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