471,306 Members | 829 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

How to locate external configuration file

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
2 8485
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
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.

Similar topics

4 posts views Thread by Andrew Poulos | last post: by
3 posts views Thread by bowser | last post: by
reply views Thread by Andrea Anastasescu | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.