Using Jboss System Properties

So if I have a jboss application set up on different environments, is there an easy way for me to load environment specific properties on a per instance basis?

Yes there is, read on.

So normally, in most applications you might end up with some property values you’ll want to override based on the environment. For example, login credentials to some third party service, environment specific file locations, environment specific jnp servers, or properties that flag the application’s mode as test or production environments.

It’s generally a good idea for an application to have properties bundled within the deployable artifact so that during deployment you can avoid running into “property not found” type of exceptions. Defaults are always a good idea, and since a deployable artifact should be completely self contained, bundling properties during a deploy is a naturally good practice. But what if you want to override these properties with an environmentally dependent set of values?

We can use the jboss “conf” directory. Because jboss uses an instance style of configuration, you can stick your properties in the “/jboss-install-dir/server/configured-instance/conf” directory, and access them easily from within your code. In a nutshell you can ask the System.properties object for the URL location of the “conf” directory, then load your properties file into a local Properties object as a resource. If your property file is not in the “conf” directory, you can then choose to load from the bundled properties (deployed with your archive) the regular way.

Lets take a look at some of the properties jboss loads into System.properties – on startup if you have jboss set up to output debugging during boot, you will eventually see a system property dump like this:



06:34:04,070 INFO [ServerInfo] Java version: 1.6.0_0,Sun Microsystems Inc.
06:34:04,071 INFO [ServerInfo] Java Runtime: OpenJDK Runtime Environment (build 1.6.0_0-b14)
06:34:04,072 INFO [ServerInfo] Java VM: OpenJDK Client VM 14.0-b08,Sun Microsystems Inc.
06:34:04,072 INFO [ServerInfo] OS-System: Linux 2.6.29.4-167.fc11.i686.PAE,i386
06:34:04,074 DEBUG [ServerInfo] Full System Properties Dump
06:34:04,075 DEBUG [ServerInfo] java.vendor: Sun Microsystems Inc.
06:34:04,076 DEBUG [ServerInfo] sun.java.launcher: SUN_STANDARD
06:34:04,076 DEBUG [ServerInfo] sun.management.compiler: HotSpot Client Compiler
06:34:04,076 DEBUG [ServerInfo] os.name: Linux
06:34:04,076 DEBUG [ServerInfo] user.name: jboss
06:34:04,076 DEBUG [ServerInfo] jboss.bind.address: 192.168.1.252
06:34:04,076 DEBUG [ServerInfo] jboss.home.dir: /jboss/jboss-5.1.0.GA
06:34:04,076 DEBUG [ServerInfo] jboss.home.url: file:/jboss/jboss-5.1.0.GA/

06:34:04,076 DEBUG [ServerInfo] java.version: 1.6.0_0
06:34:04,076 DEBUG [ServerInfo] jboss.server.home.dir: /jboss/jboss-5.1.0.GA/server/standard
06:34:04,077 DEBUG [ServerInfo] jboss.server.home.url: file:/jboss/jboss-5.1.0.GA/server/standard/
06:34:04,077 DEBUG [ServerInfo] jboss.server.config.url: file:/jboss/jboss-5.1.0.GA/server/standard/conf/

06:34:04,077 DEBUG [ServerInfo] jboss.common.lib.url: file:/jboss/jboss-5.1.0.GA/common/lib/
06:34:04,078 DEBUG [ServerInfo] java.home: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre
06:34:04,078 DEBUG [ServerInfo] java.endorsed.dirs: /jboss/jboss-5.1.0.GA/lib/endorsed*
06:34:04,079 DEBUG [ServerInfo] jboss.lib.url: file:/jboss/jboss-5.1.0.GA/lib/
06:34:04,079 DEBUG [ServerInfo] bind.address: 192.168.1.252
06:34:04,080 DEBUG [ServerInfo] jboss.server.temp.dir: /jboss/jboss-5.1.0.GA/server/standard/tmp
06:34:04,080 DEBUG [ServerInfo] user.home: /home/jboss
06:34:04,080 DEBUG [ServerInfo] jboss.common.base.url: file:/jboss/jboss-5.1.0.GA/common/
06:34:04,080 DEBUG [ServerInfo] jboss.server.log.dir: /jboss/jboss-5.1.0.GA/server/standard/log
06:34:04,081 DEBUG [ServerInfo] java.io.tmpdir: /tmp
06:34:04,081 DEBUG [ServerInfo] jboss.server.data.dir: /jboss/jboss-5.1.0.GA/server/standard/data
06:34:04,081 DEBUG [ServerInfo] java.rmi.server.hostname: 192.168.1.252
06:34:04,082 DEBUG [ServerInfo] user.dir: /jboss/jboss-5.1.0.GA/bin
06:34:04,082 DEBUG [ServerInfo] java.vm.name: OpenJDK Client VM
06:34:04,082 DEBUG [ServerInfo] jboss.server.base.dir: /jboss/jboss-5.1.0.GA/server
06:34:04,082 DEBUG [ServerInfo] jboss.server.base.url: file:/jboss/jboss-5.1.0.GA/server/
06:34:04,082 DEBUG [ServerInfo] file.encoding: UTF-8
06:34:04,082 DEBUG [ServerInfo] jboss.server.name: standard


Note the bolded entries. You can access these properties by saying something like this:

String serverConfUrl = System.getProperty("jboss.server.config.url")

A more complete solution that would check the server conf directory before loading the bundled properties could look something like this:

String propertiesFileName = "application.properties";

Properties props = new Properties();
String path = System.getProperty("jboss.server.config.url")+propertiesFileName;

if(new File(path).exists()) {
	props.load(new URL(path).openStream());
	log.info("loaded application properties from file: " + path);
} else {
	props.load(MyClass.class.getResourceAsStream("/" + propertiesFileName));
	log.info("loaded application properties: /"+propertiesFileName);
}

There you have it. An easy to use environment specific properties definition strategy with bundled fail over defaults. Try saying that three times.

 
    Twitter
  • del.icio.us
  • Reddit
  • Technorati
  • Google Bookmarks
  • Blogplay
  • Yahoo! Buzz
  • LinkedIn
  • Facebook
  • Digg

Related posts:

  1. Using Jboss Datasource files
  2. External Jboss deploy directories
  3. XML, Xalan, Endorsed dirs and &..
Posted on January 11, 2010 at 8:47 pm by Ant · Permalink
In: Java, Jboss · Tagged with: , ,

2 Responses

  1. Bharat Kumar Patel Written by Bharat Kumar Patel
    on February 19, 2010 at 3:24 am
    Permalink

    Hi

    I have facing a problem with a webapplication, I am using JBoss 5.0. In my web application I want to load a xsl file to make some conversion, however, when I place the file in JBoss/default/conf dir I am unable to create a File object. I am using Maven. I am able to access the file if I place the file in jboss home. but not anywhere else. could you provide me a solution. I have follow the steps that you have given above but unfortunately it does not work. After some RD I think there is some security issue with JBoss and Java that the file is gettting locked in conf dir.

    Thanks in advance

    [Reply]

    Ant

    Ant Reply:

    Hi Bharat,
    Thanks for the question. In order to be able to write a file to any directory you must have the appropriate permissions. It sounds from your description that you might be running on linux and as the jboss user. This would mean you would automatically have “write” permissions on any files that jboss owns, like the jboss home directory. Check the permissions on your jboss install, if the jboss user is not the owner of the entire tree structure, you will run into file write permissions. One common thing is if jboss was installed as root and then run as jboss, jboss won’t have write permissions on the directory since the jboss user wont have as many permissions as root.
    Actually, I think i misunderstood your question. If you are trying to “read” the file, but not letting you it sounds like its still some kind of OS/permissions related kind of thing. The same concept I just mentioned above still applies. In order for jboss to act on a file structure it needs to have the right permissions. In the “read” case, it needs “read”. If you’re on linux, it might help to try and set chmod on the tree structure to 0777/ugo=rwx. If you’re on windows, try setting read/write permissions to allow everyone. Then go back and limit permissions as necessary.
    Usually, its not very useful to write to the conf directory anyway. You might want to create a separate directory unrelated to the jboss install in case you end up upgrading to a different version of jboss in the future. I would usually create a directory in the base of the disk drive with a path something like this:

    /public/jboss/files

    This way when you upgrade jboss, you wont have to reference a file structure tied to an outdated version of jboss.

    Hope this helps.

    [Reply]

Subscribe to comments via RSS

Leave a Reply