Prioritizing Properties Files when Bridging Camel and Spring

Last week I blogged about prioritizing properties files as they are loaded by Apache Camel in the camel-context.xml. But this will not work if you use the BridgePropertyPlaceholderConfigurer to bridge Camel and Spring’s properties placeholder abilities.

We can get the same behaviour when bridging, but we have to go about it a bit differently. This time, our camel-context looks like this:

<bean id="bridgePropertyPlaceholder" name="properties" class="somepackage.PriorityBridgePropPlaceholderConfigurer">
    <property name="locations">
      	<list>
            <value>file:/etc/configmap/app.properties</value>
       	    <value>file:d:/etc/configmap/app.properties</value>
   	    <value>classpath:app.properties</value>
      	</list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

And the subclassed Java code looks a bit different. Because the PropertiesLoaderSupport class has most of its properties private, we have to cache the values locally in our subclass and then pass them on to the PropertiesLoaderSupport. You can specify the file encoding either in the camel-context or in the Java code. I have chosen the latter to make re-use easier.

Here’s the rest of the code. You will probably want to add your own logging.

public class PriorityBridgePropPlaceholderConfigurer extends BridgePropertyPlaceholderConfigurer {

    private Resource[] locations;
    private boolean ignoreResourceNotFound;
    private String fileEncoding = "ISO-8859-1";

    @Override
    public void setLocations(Resource... locations) {
	this.locations = locations;
	super.setLocations(locations);
    }
	
    @Override
    public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound){
	this.ignoreResourceNotFound = ignoreResourceNotFound;
	super.setIgnoreResourceNotFound(ignoreResourceNotFound);
    }
	
    @Override
    public void setFileEncoding(String fileEncoding) {
	this.fileEncoding = fileEncoding;
	super.setFileEncoding(fileEncoding);
    }
	
    @Override
    protected void loadProperties(Properties props) throws IOException {
	if (this.locations != null) {
	    for (Resource location : this.locations) {
		try {
		    PropertiesLoaderUtils.fillProperties(props, new EncodedResource(location, this.fileEncoding));
		    return;
		}
		catch (IOException ex) {
		    // Resource not found when trying to open it
		    if (this.ignoreResourceNotFound && (ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
		    }
		    else {
			throw ex;
	            }
		}
            }
        }
    }
	
}

Leave a Reply

Your email address will not be published. Required fields are marked *