11.4.05

Loading Java properties file inside EJB component (OC4J)

Loading properties file inside Java is common way to parametrize the application. Under single JVM you would use following code to load the properties file:


Properties prop = new Properties();
try
{
prop.load(new FileInputStream("hello.properties"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}


Under OC4J you might want to keep all the properties files under the same directory e.g. $OC4J_HOME/properties. For this you should edit the container application.xml file under $OC4J_HOME/config -directory. Add following line to the application.xml:

<library path="../properties"/>


In the J2EE application you can trust the class loader to find the properties file under this directory. For this you should code your application as:


Properties prop = new Properties();
try
{
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("hello.properties"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

No comments: