Accessing custom Properties from JBoss AS

Blog Moove-it
moove-it
Published in
2 min readOct 7, 2011

Hi everyone !

I have this issue / trouble several times. I would love share this tips with the JBoss coomunity … particularity JBoss Application Server (AS) developers.

Problem:

I have a scenario to access certain key values from a config file. I need Access to this properties file from a Java application (WAR or JAR) deployed in a JBoss AS

Solutions

1- Hard Code the path (no please !!)

Hard Code the path and create a Properties instance.

InputStream inStream = new FileInputStream(PATH_CONFIG+"config.properties");
Properties instance = new Properties();
instance.load(inStream);
//access to a property
String value = instance.getProperty(key);

2- Using JBoss System Properties (almost)

Avoid to Hard Code … use a tool from JBoss. System.getProperty()

JBoss allows access to a standard set of properties. For instance: jboss home dir (“jboss.server.home.dir). Here I share some piece of code to make an instance of this properties file.

String path = System.getProperty("jboss.server.home.dir")+"/conf/config.properties";
InputStream inStream = new FileInputStream(path);
Properties instance = new Properties();
instance.load(inStream);
//access to a property
String value = instance.getProperty(key);

3- Using Resource bundle (the best !)

JBoss AS add to the classpath a lot of folders and jars. One of them is the “config” folder into the server domain. eg. “server/default/conf”

ResourceBundle objects contain an array of key-value pairs. You specify the key, which must be a String, when you want to retrieve the value from the ResourceBundle. The value is the locale-specific object.

When you create a ResourceBundle this properties file must be in the classpath.

Then we could:

ResourceBundle instance = ResourceBundle.getBundle("project.config");//access to a property
String value = instance.getString(key);

In this example the file “config.properties” must be inside a “project” folder declared in the classpath, for instance: “config”

That’s all folk !

--

--