views:

201

answers:

3

I need to access some files on servers from servlet. They have different paths on my development machine and on the deployment server. I would like to put some config file (with paths) somewhere (like shared dir in capistrano deployment) so application could read it. Or maybe set some property on the application server or anything like that. I can use Tomcat, Jetty or Glassfish.

Currently I made a config file in WEB-INF with config for local machine. Deployment script that copies .war on the server, modifies the war file by putting server config file inside. It works, but is not too nice, and if something changes with paths on the server I would have to change local file and redeploy application.

Can you suggest better solution?

+1  A: 

The usual way to do this is to configure your dependencies inside the servlet container, and expose them via JNDI. In your case, just exposing a String with the path would probably be sufficient.

The JNDI tutorial and Tomcat's JNDI docs should help you get started. JNDI also nicely integrates with Spring, in case you use the Spring IOC container.

Henning
+2  A: 

There are a couple of similar questions here in SO. This is one I've found, I remember there are more.

Anyway, this link from the above answer could be helpful.

A summary of your options are:

  • If you have a database, store your options there.
  • You can also use JNDI for either storing your properties directly or the path of the configuration file.
  • You can use an environmental variable to store the path of the configuration file.
kgiannakakis
+1  A: 

I agree with kgiannakakis, but there are some limitations to that as well - Storing properties/xml configurations in database is definately an option but not always feasible specially if it is an non property resource like XML. - JNDI look up's are performance hit and you can't store xml's there as well - If you have multiple copies of application deployed on same server, environment variables can't be used. - All of above cannot be used to do something like MyClass.getResourceAsStream()

.

If above solution is ok then fine else you can use the concept of shared libraries. I am not sure of how it works with above app servers you mentioned but given below are links for Geronimo, WASCE, IBM Websphere. Concept is that during deployment you specify additional classpath for your EAR.

Geronimo http://cwiki.apache.org/GMOxDOC21/configuring-geronimo-eclipse-plugin-to-publish-maven-dependencies-as-shared-library.html

WASCE 2.1.0 http://publib.boulder.ibm.com/wasce/V2.1.0/en/shared-library.html

IBM Websphere http://publib.boulder.ibm.com/infocenter/wasinfo/v5r1//index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/rxml_library.html

Gladwin Burboz