You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "jason.parr" <ja...@usa.net> on 2011/08/16 16:28:25 UTC
Dynamically configuring list of property file locations in context
I want to configure the list of property file locations dynamically.
Basically each of my environments (dev,stage,prod) have a particular
hierachy of property file overrides and the list has to be determined at
runtime.
Rather than as below statically listing them in my spring config file:
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations"
value="file:config/config.properties,file:config/config-dev.properties" />
</bean>
I want to calculate a dynamic list, I've worked out how todo this when using
CamelTestSupport by overriding createCamelContext.
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
// Load test specific properties
PropertiesComponent properties =
context.getComponent("properties",PropertiesComponent.class);
properties.setLocations(myDynamicLocations);
But how can I do this from my actual route that extends RouteBuilder?, I
tried overriding createContainer but this doesnt seemed to get called.
public class RadialRouteBuilder extends RouteBuilder {
/**
* A main() so we can easily run these routing rules in our IDE
*
* @param args command line arguments
* @throws Exception exception that terminated running of main thread
*/
public static void main(String... args) throws Exception {
Main.main(args);
}
@Override
protected CamelContext createContainer() {
CamelContext context = super.createContainer();
PropertiesComponent properties = new PropertiesComponent();
properties.setLocations(myDynamicLocations);
context.addComponent("properties", properties);
return context;
}
This just ends with me getting exception:
Caused by: java.lang.IllegalArgumentException: PropertiesComponent with name
properties must be defined in CamelContext to support property placeholders.
at
org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:969)
at
org.apache.camel.model.ProcessorDefinition.resolvePropertyPlaceholders(ProcessorDefinition.java:500)
at
org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:426)
at
org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:183)
at
org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:791)
... 26 more
I may be trying to use a sledge hammer to crack a nut, so there may be an
easier way of doing this. The main point is that the exact list of
properties files are not known until runtime, this may be unusual but is
just the way it is!
--
View this message in context: http://camel.465427.n5.nabble.com/Dynamically-configuring-list-of-property-file-locations-in-context-tp4704591p4704591.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Dynamically configuring list of property file locations in
context
Posted by "jason.parr" <ja...@usa.net>.
Worked out how todo this
Have my own PropertiesResolver, e.g.
public class EnvironmentPropertyResolver implements PropertiesResolver {
DefaultPropertiesResolver defaultPropertiesResolver = new
DefaultPropertiesResolver();
@Override
public Properties resolveProperties(CamelContext context, String... uri)
throws Exception {
ArrayList<String> locations = new ArrayList<String>();
for (String location : uri)
if ("dynamic".equals(location)) {
locations.addAll(getEnvironmentPropertyFilePaths());
} else {
locations.add(location);
}
return defaultPropertiesResolver.resolveProperties(context,
locations.toArray(new String[]{}));
}
public List<String> getEnvironmentPropertyFilePaths() {
// do real dynamic stuff
return
Arrays.asList("file:config/config.properties","file:config/config-dev.properties");
}
}
And spring/camel config something like:
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="propertiesResolver">
<bean
class="com.nomura.fiet.stp.core.camel.EnvironmentPropertyResolver" />
</property>
<property name="locations" value="dynamic" />
</bean>
--
View this message in context: http://camel.465427.n5.nabble.com/Dynamically-configuring-list-of-property-file-locations-in-context-tp4704591p4705011.html
Sent from the Camel - Users mailing list archive at Nabble.com.