Deep Dive into Java Resource Strings

struts

I need to make at least one string, probably more –that was *never* going to be changed into a configurable resource– to be set by a configuration property.

The first lesson is right here: when I hear “That’s something that will never change don’t spend time making it configurable.” from someone outside of engineering I need to apply the aspirational axiom. “If something is said as an emphatic announcement of fact, it is what the speaker wants to be true. If they want it to be true, it is not true. Probably do the opposite, quietly.

It is all over the application: the class packages, class names, logo, UI, image paths and names. Not just replace- make configurable so that this is not a patch for one project, but an enhancement to the main trunk- a “bump in the roadmap” since it was not planned.

The first question is where to store the configurable string? The main properties file for the application is going to be where we start, since it is a global change.

The next question is how to find all the places the string is hiding in the application?

The easy grind part is searching the many .jsp and .java files for instances that follow a specific pattern and replacing with resource getter code (more on that later). Sadly, there was no pattern that excluded commented code in my IDE, so this was truly a grind-scan.

Most properties files contained helpful pointers in comments to the target files where the resource bundle was used, which were mostly .jsp files with struts framework. I add the following just above any resource bundle and below includes and tld directives:

<bean:define id=”identifier”>

	<%= EConfigFactory.getSingleton().getStringProperty(EConfig.STRING_NAME, "Default")%>

Note that the “Default” value is a string that would be displayed if not value is returned by the EConfigFactory method call to EConfig.STRING.NAME, and can be left as an empty string.

Using in a JSP

To have JSP insert the property so that the string is displayed in the rendered HTML page, use this:

<%= identifier %>

Using in .java Class

String identifier = EConfigFactory.getSingleton().getStringProperty(EConfig.SERVICE_NAME, 'Platform')

Using with a Resourced String

<fmt:message key="users.description">

Where a resource bundle supplies a string such as this:

jsp.index.users.description = Provides a tool to find and view users who can access {0}.

The fmt:param tags supply the value that fills {0} in the property. Multiple properties are added in the order the parameters are listed.

There are two significant problems with this method:

  • When viewing the property file in isolation (as a translator, for example) the numbers in brace provide no contextual meaning
  • Locating what files use the property file requires a commenting convention that leaves a breadcrumb in it or you must do a search for the key through the entire project to find all uses.

 

You may also like...

Leave a Reply

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

Captcha loading...

This site uses Akismet to reduce spam. Learn how your comment data is processed.