Madura Resource Loader

I’ve just released a new Java project to GitHub. Actually this is version 1.1 so I guess it isn’t so very new. But version 1.0 is only a month or so old and I did not get round to blogging about it then.

The reason for 1.1 I will come to but 1.0 is doing a great job and I can tell you about that first.

If you are using Java and Spring you often load up strings into a message source. They get managed there for I18n issues and I’ve always used Spring’s ResourceMessageBundle class for this. It allows you to list several properties files which it loads, and merges the result appropriately.

This is good, until you find there are half a dozen properties files buried in dependent jar files you maybe don’t even know about. I wanted a way to make this easier and more transparent.

The solution is really simple, almost trivial (which suggests it is a great idea, right?). All I did was extend ResourceMessageBundle and add some extra code. The extra code scans for beans annotated with MessageResource (actually nz.co.senanque.resourceloader.MessageResource). That annotation takes a name and I use that to specify the name of the properties file.

So, say you have a properties file named MyProperties.properties buried in a dependent jar file. You also have beans loaded from that jar file. One of those beans is annotated @MessageResource(“MyProperties”). That tells the extended ResourceMessageBundle to load a properties file from the same package. Easy, eh?

The second thing this library does is similar. It just declares a class I use for supplying the version number from the jar file’s manifest. I have beans in each library like this:

@Component(“MaduraObjectVersion”)
public class LocalVersion 

             extends nz.co.senanque.version.Version {

    public LocalVersion() {
    }
    public String getVersion() {
        return this.getClass()
          .getPackage().getImplementationVersion();
    }
}

I use some code to search the beans for ones of type nz.co.senanque.version.Version. For each one I query the version and it ends up in the about box. I use the component string as the name of the thing I’m versioning so the above would get me something like “MaduraObjectVersion: 3.0.0” and it is easy to build a list of jar files I am dependent on each with a version.

This is all open source and published to maven central. It doesn’t depend on the rest of the Madura stuff I’ve built.

Leave a Reply

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