By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,873 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,873 IT Pros & Developers. It's quick & easy.

How to locate external configuration file

P: n/a
I'm new to Java programming. I'm developing a Servlet for tomcat which needs
an external configuration file. With external I mean a file (in XML format)
which
is customizable and not contained in my package. I don't want to hardcode
any
path and I would like to be independent of the application server (tomcat,
websphere,...).
Until now, I put the file in my package and open it using
obj.getClass.getResourceAsStream
but when I put my whole package in a .jar file, it prevents that the file is
customizable
(or the .jar file would have to be unpacked on each installation).
Putting any path in the web.xml has the same problem.
The article @ javaworld
(http://www.javaworld.com/javaworld/j...cooltools.html)
didn't help because the JDBC connection string actually belongs into the
configuration file.
Also defining a system property when starting tomcat (-Dparam=value) is not
a very good
option as it complicates work for the IT manager (already complicate with
JAAS,
required changes in tomcat's catalina.policy,...).
Someone got any experience how to easily configure a servlet, keep the
configuration
customizable (the configuration itself as well as the location of the
configuration file) and
how to locate the configuration file in a file-system- and
application-server-independent way?

Many thanks
Alex
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alex,

Every Appserver has some sort of common area where all applications
installed on the app server can look for classes common to all applications.
In Tomcat it's shared/classes. In WebSphere it's
WebSphere/Appserver/lib/ext, or configurable as a classpath variable in the
admin console. It sounds like putting your XML file in one of these common
areas would be the least amount of work for an installer.

Merrill

"Alexander Kienzle" <no****@nomail.com> wrote in message
news:cd**********@atlas.ip-plus.net...
I'm new to Java programming. I'm developing a Servlet for tomcat which needs an external configuration file. With external I mean a file (in XML format) which
is customizable and not contained in my package. I don't want to hardcode
any
path and I would like to be independent of the application server (tomcat,
websphere,...).
Until now, I put the file in my package and open it using
obj.getClass.getResourceAsStream
but when I put my whole package in a .jar file, it prevents that the file is customizable
(or the .jar file would have to be unpacked on each installation).
Putting any path in the web.xml has the same problem.
The article @ javaworld
(http://www.javaworld.com/javaworld/j...cooltools.html)
didn't help because the JDBC connection string actually belongs into the
configuration file.
Also defining a system property when starting tomcat (-Dparam=value) is not a very good
option as it complicates work for the IT manager (already complicate with
JAAS,
required changes in tomcat's catalina.policy,...).
Someone got any experience how to easily configure a servlet, keep the
configuration
customizable (the configuration itself as well as the location of the
configuration file) and
how to locate the configuration file in a file-system- and
application-server-independent way?

Many thanks
Alex

Jul 17 '05 #2

P: n/a
Hi Merril

Thanks for the answer, but how do I find that directory?
I assume I cannot use getResourceAsStream in those cases but I'm not
sure because the ClassLoader's getResourceAsStream method will
look in many places (the order which directories are searched is not
described in my Java docs!).

Using System.GetProperties("catalina.home") + "shared/..." would not
work if the application server is websphere (and vice versa).
Could I just try 'catalina.home' and if it fails, assume that the
application server is websphere and use the websphere's
system property to get to the path or is there a more generic way
to find the actual application server?

I also have another problem, that is, when I have multiple versions of
my package (using different context-path). They would most probably
require different configuration files. Is it possible to put the
configuration
file in the same directory as the .jar file? Will getResourceAsStream
work then? If not, how can I find out in which directory the .jar file is
(maybe ServletContext.getRealPath("/"))?

Thanks in advance

Alex

"Merrill" <mh*********@socal.rr.com> wrote in message
news:gZ*******************@twister.socal.rr.com...
Alex,

Every Appserver has some sort of common area where all applications
installed on the app server can look for classes common to all applications. In Tomcat it's shared/classes. In WebSphere it's
WebSphere/Appserver/lib/ext, or configurable as a classpath variable in the admin console. It sounds like putting your XML file in one of these common areas would be the least amount of work for an installer.

Merrill

"Alexander Kienzle" <no****@nomail.com> wrote in message
news:cd**********@atlas.ip-plus.net...
I'm new to Java programming. I'm developing a Servlet for tomcat which needs
an external configuration file. With external I mean a file (in XML

format)
which
is customizable and not contained in my package. I don't want to hardcode any
path and I would like to be independent of the application server (tomcat, websphere,...).
Until now, I put the file in my package and open it using
obj.getClass.getResourceAsStream
but when I put my whole package in a .jar file, it prevents that the file is
customizable
(or the .jar file would have to be unpacked on each installation).
Putting any path in the web.xml has the same problem.
The article @ javaworld
(http://www.javaworld.com/javaworld/j...cooltools.html)
didn't help because the JDBC connection string actually belongs into the
configuration file.
Also defining a system property when starting tomcat (-Dparam=value) is

not
a very good
option as it complicates work for the IT manager (already complicate

with JAAS,
required changes in tomcat's catalina.policy,...).
Someone got any experience how to easily configure a servlet, keep the
configuration
customizable (the configuration itself as well as the location of the
configuration file) and
how to locate the configuration file in a file-system- and
application-server-independent way?

Many thanks
Alex


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.