File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Putting a resources file outside the application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Putting a resources file outside the application" Watch "Putting a resources file outside the application" New topic

Putting a resources file outside the application

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155

I am sure this question has cropped up before, and although I have looked around for a suitable answer, I have not found anything that helps.

In my JSF 2.0 application I have a couple of resources files. One is used to specify the paths and file names to files that are read from folders that outside the application, and one is used to initialize a number of variables. At the moment both resources files are in the same folder as the Java classes, as is usually the case.

For example I have code such as:

Which is all well and good, and works without any problems.

However, I need to deploy the war file on another server, and I want to make it easy to edit the properties files and not have them inside the war file. Is there an easy way of putting the properties files in an extenal directory, so that they can be edited, and at the same time my application can know where they are located?

I am using the Glassfish server, but want to avoid, if possible, changing the server, and somehow make the application aware of where the properties files are.

Some advice would be most appreciated - many thanks in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

The whole resource bundle subsystem is basically interface-based, so you can select from a number of resource providers, including ones that you design yourself.

However, the easiest way to include resources from an externally-located file is to define the resource filename path as a web application resource and use JNDI to look it up. Once you have the filename path, you can use the regular bundle load-from-file mechanism.

For Tomcat, you would do this by adding a resource definition to the webapp's WEB-INF/web.xml file to define its nature and default value. Tomcat can then override the default in the webapp's Context element. Glassfish would use the web.xml part but employ a GlassFish-specific mechanism to set the override. I haven't worked with GlassFish recently enough to know specifically what that is (check with the appropriate Ranch forum if you need Glassfish help).

In the webapp, as part of the core initialization code, you'd do a JNDI lookup to get the value of the resource path. You'd get a JNDI context and use it as the control point to lookup something like "java:comp/env/myresources", where "myresources" is the name you defined in web.xml.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Putting a resources file outside the application
It's not a secret anymore!