Read XML Properties in Java

Once in a while you need to externalize some configuration without the overhead of a complete framework, here’s a simple method to read an XML formatted property file in java. In most cases, it’s a performance advantage to wrap this up in a Singleton pattern, but that’s a different topic altogether.

private getAttributes() {
final String filename = "";
final InputStream input = getClass().getClassLoader().getResourceAsStream(filename);
System.err.println("Cannot find properties:"+ filename);
final java.util.Properties props = new java.util.Properties();
try {
hostprop = props.getProperty("hostname",null);
userprop = props.getProperty("username",null);
pswdprop = props.getProperty("password",null);
catch(final Exception e){
System.err.println("Error occurred while reading properties file:"+ input);
try {
catch(final ex){

The matching file would resemble…

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE properties SYSTEM "">
<entry key="hostname">localhost</entry>
<entry key="username">example</entry>
<entry key="password">example</entry>

Maven/Ant echoproperties at build time

Once you have started using automation tools for continuous builds, you often find edge cases where your builds have minor variations due to the environments on which the projects have been built. To isolate these, it is often useful to have the build tool output a snapshot of it’s properties at the time the project was built. Thankfully, Ant and Maven make this easy to implement, required additions to your config files are below for each tool.

Maven: (pom.xml)

<echoproperties />

NOTE: ‘target’ is preferred over ‘tasks’ in newer versions of the plugin, it was deprecated in 1.5.

Ant: (build.xml)

<echoproperties />


MySQL useConfigs=maxPerformance

I noticed this while updating to SonarQube 4.5. The documentation and references to this parameter lead me to believe that it is a useful shortcut to optimizing server resources.

In /opt/sonar/


NOTE: In some instances you may need to escape the ampersands!

I’ll change this on a few projects and watch for any issues changes.


Improve Apache Tomcat logging performance

Logging is often an overlooked performance drain on systems requiring high throughput. Here’s a simple change to the default Tomcat logging configuration to implement. It works on all operating systems.

In the file:

.handlers =, java.util.logging.ConsoleHandler

.handlers =


Avoid CSS Expressions

MSIE5 added support for CSS expressions or “Dynamic Properties”, however MSIE8 ‘deprecated’ their use and prevents their use in Standards Mode.

While powerful because this allowed you to script your CSS dynamically, there were two primary problems.

  1. Performance – the expression could fire literally hundreds or thousands of times on a page when scrolled or resized.
  2. Security – this represented an attack vector and exposed XSS


Internationalizing JSP with ResourceBundles

Adding multi-language support to JSP based applications is very simple. In this post we will investigate the method that you can use to externalize your text based content.

NOTE: Additional work is required to establish the Locale, format Dates and Numbers or to support other differences such as text-direction.


<%@ taglib prefix="fmt" uri="" %>
<fmt:setLocale value="en_US" />
<fmt:setBundle basename="ResourceBundles.TestBundle" scope="request" var="rb" />
<fmt:message bundle="${rb}" key="label.test" />







You can also specify some default Locale information in web.xml if you do not wish to use the in your JSPs.




Some explanation… in this case we’ve told our JSP that the resources are in the TestBundle properties. As the Locale is set to ‘en_US’ it will first look in the file, if not found it will then look in and finally in If not found there, the output will generally be in the form ‘???key???‘, in this example: ‘???label.test???‘, my understanding is that this can be suppressed by setting ‘allowNull=true‘ somewhere, but I have never found that setting to date.