Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I Locate an application properties file

Reply
Thread Tools

How do I Locate an application properties file

 
 
MattC
Guest
Posts: n/a
 
      09-16-2005
I am working in a Struts web based J2EE environment. My application is
packaged as a web app.

I have application level configuration information that needs to be
kept in a properties file. My problem is how to programmatically
determine the absolute pathname of that properties file.

It seems to me I have two basic options.

1.) I can create the properties file and put it in a "known" location.
For instance the WEB-INF\classes directory. In order to do this I will
need to determine the absolute path name to the WEB-INF directory. For
instance it might be C:\myproject\public_html\WEB-INF. How can I
determine the absolute pathname of the WEB-INF directory?

2.) I can store a relative pathname of the properties file in the
web.xml file. This is a more flexible implementation and so it is
preferred over option #1. In order to do this I need to determine the
physical location of the WEB-INF directory as in option #1. In addition
I need to gain access to the web.xml file.

Can someone tell me how to access the web.xml file from a Struts
application? In other words, I don't have access to the servlet.init()
method and its associated ServletConfig object.

Thanks,
Matt

 
Reply With Quote
 
 
 
 
Malte
Guest
Posts: n/a
 
      09-16-2005
MattC wrote:
> I am working in a Struts web based J2EE environment. My application is
> packaged as a web app.
>
> I have application level configuration information that needs to be
> kept in a properties file. My problem is how to programmatically
> determine the absolute pathname of that properties file.
>
> It seems to me I have two basic options.
>
> 1.) I can create the properties file and put it in a "known" location.
> For instance the WEB-INF\classes directory. In order to do this I will
> need to determine the absolute path name to the WEB-INF directory. For
> instance it might be C:\myproject\public_html\WEB-INF. How can I
> determine the absolute pathname of the WEB-INF directory?
>
> 2.) I can store a relative pathname of the properties file in the
> web.xml file. This is a more flexible implementation and so it is
> preferred over option #1. In order to do this I need to determine the
> physical location of the WEB-INF directory as in option #1. In addition
> I need to gain access to the web.xml file.
>
> Can someone tell me how to access the web.xml file from a Struts
> application? In other words, I don't have access to the servlet.init()
> method and its associated ServletConfig object.
>
> Thanks,
> Matt
>


Below is an outlined example. The idea is to use the webapp.xml file.
Put in a context-param tag, then in your Java code, use the servlet
context to retrieve the information. If you, like I did for the example,
stuff your properties file into /WEB-INF/data, getRealPath() will find
it for you nicely. What you don't want to do is hardcode c:\ or
something ugly. What if someone deploys your application to a real OS
when it would become something line /opt/mywebapps ?

<web-app>

<context-param>
<param-name>DATA_DIR</param-name>
<param-value>/WEB-INF/data</param-value>
</context-param>
...
</web-app>

private File getDataDir() throws ServletException {
ServletContext application = getServletContext();
String dataDir = application.getInitParameter(DATA_DIR_PARAM);

if (dataDir == null || dataDir.length() == 0)
error(FILE_COMP_ID, MISSING_DATA_DIR_PARAM_ERROR,
FacesMessage.SEVERITY_FATAL);

String realDir = application.getRealPath(dataDir);
if (realDir == null)
error(FILE_COMP_ID, CANNOT_ACCESS_DATA_DIR_ERROR,
FacesMessage.SEVERITY_FATAL);

return new File(realDir);
}
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-17-2005
On 16 Sep 2005 14:24:48 -0700, "MattC" <>
wrote or quoted :

>I have application level configuration information that needs to be
>kept in a properties file. My problem is how to programmatically
>determine the absolute pathname of that properties file.


Frustrating eh? especially if you want this to work muliplatform.

Look into the Preferences API for getting around that problem.
The other way is to use JAWS.

see http://mindprod.com/jgloss/preferences.html
http://mindprod.com/jgloss/avawebstart.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      09-17-2005
MattC wrote:
> I am working in a Struts web based J2EE environment. My application is
> packaged as a web app.
>
> I have application level configuration information that needs to be
> kept in a properties file. My problem is how to programmatically
> determine the absolute pathname of that properties file.


Why do you need the absolute pathname? The simplest solution is to put
the .properties file in the classpath and load it via ResourceBundle.
If you do not want a ResourceBundle you can use
ClassLoader.getResource() to get the URL.

>
> It seems to me I have two basic options.
>
> 1.) I can create the properties file and put it in a "known" location.
> For instance the WEB-INF\classes directory. In order to do this I will
> need to determine the absolute path name to the WEB-INF directory. For
> instance it might be C:\myproject\public_html\WEB-INF. How can I
> determine the absolute pathname of the WEB-INF directory?


ServletContext or ServletConfig has a getRealPath() method that can help
you. However, there is no real need to do it this way.

>
> 2.) I can store a relative pathname of the properties file in the
> web.xml file. This is a more flexible implementation and so it is
> preferred over option #1. In order to do this I need to determine the
> physical location of the WEB-INF directory as in option #1. In addition
> I need to gain access to the web.xml file.
>
> Can someone tell me how to access the web.xml file from a Struts
> application? In other words, I don't have access to the servlet.init()
> method and its associated ServletConfig object.
>


Look into just putting it in the classpath. It's the easiest way and it
works in every environment (web container, EJB, applet, etc.).


HTH,
Ray

--
XML is the programmer's duct tape.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-17-2005
On Sat, 17 Sep 2005 22:20:23 GMT, Raymond DeCampo
<> wrote or quoted :

>Why do you need the absolute pathname? The simplest solution is to put
>the .properties file in the classpath and load it via ResourceBundle.
>If you do not want a ResourceBundle you can use
>ClassLoader.getResource() to get the URL.


If it is a read-only properties file, you can put it in the jar. The
problem only comes when you have to create a properties file out of
thin air and it is not clear where a safe place to put it is.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Application cannot locate win32ui.pyd (or Python) (126) vsoler Python 4 08-04-2010 06:50 PM
Problem with asp.net application locate on network share sp1dEr ASP .Net Web Services 0 08-01-2005 08:43 PM
Locate File Diologe Reggie ASP .Net 2 06-18-2005 06:12 AM
Using Hosts file to locate schema for validation shawnk XML 0 04-29-2005 02:16 PM
Can aspx file use PATH variable to locate dlls iana_kosio@yahoo.com ASP .Net 1 04-08-2005 09:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57