I know it's possible to define <env-entry> elements in web.xml for environment properties of a web application. But how do you access <env-entry> elements from classes that do not exist within a web application? In other words, this is my problem:
I have an EAR file, enterprise.ear, which contains the following...
The employee.jar file is a utility archive that contains common classes, one of which is a class called LogManager. LogManager needs to read environment properties in order to initialize itself. Currently, LogManager reads these properties from a property file, and therefore it needs to know the name of the property file in order to access it. I dont like this approach because I'm forced to hard-code the name of the property file in LogManager. I would rather leverage the power of J2EE deployment descriptors and use the <env-entry> element to define the environment properties for the LogManager class. Then, I would simply use JNDI to get the values of the <env-entry> elements without ever having to specify their location aside from the default JNDI context; java:comp/env, which is standard anyway.
The problem I'm having is that you can define the <env-entry> element in only 2 types of deployment descriptors; namely, web.xml and ejb-jar.xml, neither of which are available to LogManager because LogManager exists at the EAR root level. I was hoping application.xml supported the <env-entry> element, but it doesn't.
I can see a couple of options available to you. The first one is to change the way you read the property file. Check out ClassLoader.getResourceAsStream(), this allows you to load any sequential file using the class loader rather than direct IO. Whats the difference? You specify a file name based on its package location. For example, I have a class that uses a properties file. The class name is sjdavies.db.isolation.Demo. During initialization it executes Demo.class.getClassLoader().getResourceAsStream("sjdavies/db/isolation/db.properties") which returns an opened InputStream. The file db.properties is in the same directory as the class Demo. This loading method works regardless of actual platform, note that the file is relative to the class location. This will work from inside a JAR.
If you insist on using one of the XML files I suggest that you use web.xml and create a InitLogging servlet that loads on startup and only really contains code in the init() method. Have this servlet use its environment settings as parameters into logging initialization calls.
Ok, but where did the file name db.properties come from? I'm assuming you made that up, but when the class first loads, are you saying the following line is automatically executed?