Asaf, hello. Good question and I don't know of any best practices. Others here may.
With the default security policy (running without "-security") I have not attempted to write data from a web application, excluding of course activities such as log messages or writing to sockets connected to a database. So my short answer is: you'll just have to try writing files to see what happens. Given that, you cannot or do not want to be in the business of attempting to modify the war file that held the original configuration. Obvious enough, but probably needs to be said. You also want to consider a saved-config file location in the file system that does not get overwritten when new war files are deployed, meaning someplace above WEB-INF/ (I believe that is possible). Wish I could help more - I just don't know first hand any more to tell. I'd start with FileOutputStream fos = new FileOutputStream("configfile"); fos.write("bytes".getBytes()); fos.close(); and see what happens. Mark On 5/17/06, Asaf Lahav <[EMAIL PROTECTED]> wrote:
Hi Mark, Is there a best practice for also writing/updating configuration file from within the application? Asaf Lahav VP R&D, Prima Grid LTD. Cellular: 972-54-4717955 Phone: 972-3-6540255 Fax: 972-3-6540254 -----Original Message----- From: Mark Petrovic [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 17, 2006 6:02 AM To: Tomcat Users List Subject: Application configuration: how to read files from a web application A few people in the last few days on the Tomcat Users have essentially asked: How do I read a file from disk from within my web application, and furthermore, how do I declaratively configure the name of that file so as to keep its path out of my code? It's a common question, and there is a good solution that applies to any Java application, not just a web application running in a container. Here's the frequently used scheme: bundle the file of interest in your appplication's war file, and from within your application, read its contents as a resource. Assume the application's context is named myapp, the file of interest is thisfile.dat, and that you can arrange for the file to be placed in your application's WEB-INF/classes directory. When the application is unbundled by Tomcat, you should see the file in a directory listing $ cd $CATALINA_HOME $ ls webapps/myapp/WEB-INF/classes/thisfile.dat webapps/myapp/WEB-INF/classes/thisfile.dat while this is clearly Unix-like syntax, the same ideas apply to Windows. What have we done so far? Strategically place the file along the application's classpath. This is essential to the scheme: the file must be along the application's classpath - and nowhere else. This is probably worth a close read Class Loader HOW-TO: http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html Futhermore, to declaratively configure your application, you'd like to put this file path in your application configuration, which keeps it hardcoded out of your code. In other words, in the application's web.xmlfile. So among other application configuration, you put this in your web.xml <servlet> <init-param> <param-name>thefile</param-name> <param-value>/thisfile.dat</param-value> </init-param> </servlet> Finally, somewhere in your servlet, you set about retrieving the file contents as follows: String thefile = this.getInitParameter("thefile"); InputStream is = this.getClass().getResourceAsStream(thefile); That's all there is to it. Of course, all of this assumes you have a method of using an InputStream to parse or process the file, but that is generally a good assumption. -- Mark AE6RT --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- Mark AE6RT