Tuesday, August 4, 2015

Externalizing system properties or truststore

The need, aka Why: 

Set system properties based on environment. While there are many ways to externalize properties in java world (JNDI), spring profile, or as Stack Overflow search reveals, JBoss System Properties; there aren't many ways to externalize system properties - other than setting them up in .profile or similar file. However, if you need a consistent and more clear way of setting them up, there aren't as many options (or I am not aware of).

A solution, aka What:

A clear, clean and intuitive ( and I'd say smart) solution is to let Spring handle it for you. Yep, it can be done, and pretty easily too!

The following code sets the system property javax.net.ssl.truststore to specify location of truststore, defined in a standard property file that is easily configured per-environment. It is searchable in editor and consistent across operating systems, so is kinda neat.


Of course, if you are setting the truststore, you might need to set the trust store password as well - which would not be plain text property, and hence not such a simple task, but thats for another day ;)

How:

Well, the code is pretty self-explanatory, Spring uses Map / property bean titled "systemProperties" to store the system properties. The above code uses Spring's MethodInvokingFactoryBean to modify this bean to add additional properties to System properties. At least that's what I believe is happening.

Challenges:

The above solution worked for me when the truststore was in classpath, otherwise on Windows 8, it gave me a weird InvalidAlgorithmParameterException. I looked at SO but that doesnt help me, though it looks like a generally correct response. Well, again I'll leave that for another day!