|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 4
Once in the classpath, the property files and the properties they contain can be retrieved within any of the applications contained in the Mule ESB project. The code in Listing 12 will load all properties accessible from the classpath.
String fileName = “<YOUR PROPERTY FILE NAME>”;
InputStream propertyFile = null;
Properties properties = new Properties();
try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL propertyFileUrl = classLoader.getResource(fileName);
if (propertyFileUrl != null)
{
propertyFile = propertyFileUrl.openStream();
if (propertyFile != null)
{
if (fileName.toUpperCase().endsWith(".XML"))
{
properties.loadFromXML(propertyFile);
}
else
{
properties.load(propertyFile);
}
}
else
{
System.err.println("The property file [" + fileName + "] could not " +
"be opened for reading.");
}
}
else
{
System.err.println("The property file [" + fileName + "] could not " +
"be found in the classpath.");
}
}
catch (IOException e)
{
System.err.println ("The property file [" + fileName + "] could not " +
"be loaded. The error is: " + e.toString());
}
finally
{
if (propertyFile != null)
{
try
{
propertyFile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
In this article we've walked through the steps to develop an environment-aware Maven build process. The resulting Maven project contains all of the properties and property-file templates for all of your Java applications, and is useable by all your Java applications for any target environment. The benefits to using this architecture are as follows:
–Denvironment.name=test.
This last feature of the environment-aware build process is particularly useful. For example, your development environment might not require a failover server, but your test environment might. If your code handled the failover process, you could clone your primary server’s property file and simply modify the property values to reflect the redundant server. That cloned property file would then be used to connect to the redundant server when conditions were appropriate for failover.
Since your development environment does not need this extra property file but your test environment does, you can simply clone the correct property file template in the App-Config project, but only in the test directory tree, and configure it with the failover server connection information.
There are some downsides to this architecture, which are also worth noting:
target/conf.
target/alternate-resources directory. In practice, this has not been shown to cause any significant performance degradation, but it's still not ideal.
I believe the benefits of the environment-aware Maven build process (that is, having a uniform strategy and solution for managing all properties across environments for every Java project) outweigh its few downsides. You are welcome to download the source files that come along with this project and use them to set up your own environment-aware build process.
Paul Spinelli is currently CTO of the San Diego-based startup Tactical Edge. Since graduating from the University of Virginia with a bachelor's degree in computer science, Paul has worked as a software developer for over 13 years. His experience has brought him an in-depth knowledge of various programming languages, OOP techniques, and security technologies.
Read more about Tools & Methods in JavaWorld's Tools & Methods section.
Demo app setup
More about Maven and SDLC
More from JavaWorld