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 = "example.properties";
final InputStream input = getClass().getClassLoader().getResourceAsStream(filename);
if(input==null){
System.err.println("Cannot find properties:"+ filename);
}
final java.util.Properties props = new java.util.Properties();
try {
props.loadFromXML(input);
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);
e.printStackTrace();
}
finally{
try {
input.close();
}
catch(final java.io.IOException ex){
ex.printStackTrace();
}
}
}

The matching file would resemble…

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="hostname">localhost</entry>
<entry key="username">example</entry>
<entry key="password">example</entry>
</properties>

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)

....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<echoproperties />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

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

Ant: (build.xml)

...
<echoproperties />
...

REFERENCES:

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/sonar.properties


sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

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.

REFERENCES:

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:
$TOMCAT_HOME/conf/logging.properties

Change:
.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

to
.handlers = 1catalina.org.apache.juli.FileHandler

REFERENCES:

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

REFERENCES:

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.

JSP:

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

/src/ResourceBundles/TestBundle.properties:

label.test=test(default)

/src/ResourceBundles/TestBundle_en.properties

label.test=test(en)

/src/ResourceBundles/TestBundle_en_US.properties

label.test=test(en_US)

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

/WEB-INF/web.xml


<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.locale</param-name>
<param-value>en</param-value>
</context-param>

<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale</param-name>
<param-value>en</param-value>
</context-param>

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 TestBundle_en_US.properties file, if not found it will then look in TestBundle_en.properties and finally in TestBundle.properties. 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.

REFERENCES: