aspose file tools*
The moose likes Websphere and the fly likes What is the best practice to add the application property files to the classpath? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Websphere
Bookmark "What is the best practice to add the application property files to the classpath?" Watch "What is the best practice to add the application property files to the classpath?" New topic
Author

What is the best practice to add the application property files to the classpath?

Srinivas Chalamalasetti
Greenhorn

Joined: Jun 18, 2002
Posts: 4
Hi All,
We are developing a J2EE application using WebSphere Studio 4.0.2. The application has some property files and resource bundle files, which have to be in the classpath. If we use “Java Build Path” to add these individual property files to the classpath, they cannot be found when the application is run on WebSphere. Using “Edit Module Dependencies …” (manifest.mf file) we can add either jar or ZIP files only. We would like to add the property files in the classpath such that they are visible both in compile time (in WSAD) and in run time (WAS). One solution that is not elegant is to create a jar/zip file containing property files and use “Edit Module Dependencies” to add this jar/zip file to the “manifest.mf” file. Please give any ideas/solutions for this problem.
Thanks and Regards,
Srinivas
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If the properties are really web-app properties, you can either put them in the web.xml as application properties and get them via the ServletContext, or (my preference) you can put the properties file in the web-inf/classes directory and use the Class.getResource() to find it.
eg if MyServlet is a servlet in the webapp, and the properties file is my.properties located at web-inf/classes/my.properties, you can always find the properties file regardless of where the application server is deployed using:
Srinivas Chalamalasetti
Greenhorn

Joined: Jun 18, 2002
Posts: 4
Hi David,
Thanks for the reply. The property files we have are NOT specific to web application or EJB application. The property files are read by the classes used by both web applications and EJB applications. IBM webSphere redbooks mention as a best practice that all the common jar file should be kept in the root directory of EAR file. Any module/application (EJB or WEB) that uses classes in the common jar file have to point to the jar file in "manifest.mf" file's classpath attribute. The documents do not mention about how to add property files in the classpath.
Regards,
Srinivas
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You can't really use the classpath in application servers, because the application servers hide it.
I didn't quite get the example correct above, it should have been:

What happens is, by using MyServlet.class, when you call .getResource() it look is in the 'area where the MyServlet class is'. ie it uses the ClassLoader that loaded the MyServlet class to find the required file.
You should be able to do the same thing at the EAR level. If the EAR contains a common library jar that contains a properties file, and it has a class called MyClass, calling

should look for a file called my.properties in the root directory of the same JAR (ie inside the jar)
I'm not so confident about this since I don't like having webapps and ejb apps relying on the same properties since you can get concurrency problems if you don't update everything at the same time.
Thoughts?
Srinivas Chalamalasetti
Greenhorn

Joined: Jun 18, 2002
Posts: 4
Hi David,
Currently we are following similar approach to what you have suggested. There is a file called “common.jar” file in the application, which is used by both EJB and WAR modules. The property files are also present in the common.jar file. The property reader classes use ‘ResourceBundle.getBundle(“propertyFileName” ’ to load and cache the properties. As the common.jar is present in class path (in manifest.mf file), the frameworks such as log4j can see the corresponding property files. This solution is working fine but I would like to take others views/ideas on this issue.
The classloader of WEB module can pickup the classes (or property files) present in the directories WEB-INF\classes, WEB-INF\lib and context root of web module. EJB classloader can pick the classes in the root directory of ejb jar file. However, the class files in root directory of EAR file are not picked up by any classloader.
I do not wish to put the property files in the WEB modules classpath (WEB-INF\classes or WEB-INF\lib) because we have to keep the property file loading logic in a servlet. If there is Swing front end to the application in addition to web front end, using servlets to load the properties may not be a good solution.
Even if both EJB and WEB modules access the property file through a class accessible by both modules, there won’t be any multi thread issues. The properties are read only, they would be loaded only once and cached into memory. After initial loading any module can request for the values of the properties.
Regards,
Srinivas
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Our current management of properties is an absolute mess and we are currently in the process of re-evaluating the whole thing.
One solution being looked at ( which I'm against ) is treating the EJB layer as a central properties manager and have all properties requests as EJB calls.
I still prefer having a generic properties manager and separating properties into either web or ejb properties so that distribution and concurrency aren't as big a problem.
Dunno
 
 
subject: What is the best practice to add the application property files to the classpath?