Wes
Wes

Reputation: 1259

Set Tomcat 7 working directory per webapp?

So I determined that the working directory for Tomcat 7 is by default $CATALINA_BASE/bin

I was provided with a jar (runs within a deployed webapp) that runs on code that does not read external properties files the recommended way for Tomcat 7. The code was initially intended for Weblogic, and is still used over there.

It opens files in this way:

FileInputStream fis = new FileInputStream("AXFLogger.properties");

I know the recommended way for Tomcat would be something like:

InputStream is = getClass().getClassLoader().getResourceAsStream("AXFLogger.properties");

Anyway the issue is not what the code should be, the issue is changing the code could be really problematic because it is old and I may need a team offshore to rewrite it for me.

I'm familiar with the workDir Tomcat attribute but this is appears to be a global Tomcat setting which is a problem. I want it to apply only to an individual webapp. Obviously storing the webapp's properties files in Tomcat's bin directory is a bad idea.

So I'm wondering if anyone has any ideas for a workaround to this? I need this to help mitigate the risk that changing the code might not be feasible. Thanks

Upvotes: 0

Views: 1260

Answers (1)

Christopher Schultz
Christopher Schultz

Reputation: 20862

It is not possible to change the working directory for each web application. The process has a "working directory", and changing that might be problematic, depending upon the server software you are using (Tomcat itself doesn't care, but other app servers such as Weblogic might).

Each application has its own "work" directory when running under Tomcat: if you fetch the value of the context attribute javax.servlet.context.tempdir, you'll find a java.io.File instance that points to a context-specific working directory.

Unfortunately, when simply opening a file like this:

FileInputStream in = new FileInputStream("myfile");

Only the first of the two concepts above is in play: the file will be loaded from the current working directory of the process.

You can try setting the working directory when launching Tomcat, but it will only work for a single application (if you have multiple applications behaving as described above). Here's how you'd do that:

$ cd path/to/web/application
$ $CATALINA_HOME/bin/startup.sh

That will set the working directory to path/to/web/application. Tomcat will do nothing to change that working directory.

If you have multiple applications that all behave this way, one way to get around that would be to simply launch multiple Tomcat processes -- one for each web application -- living in their own working directories. Of course, you'll need a reverse-proxy out front to proxy client requests to the right Tomcat back-end based upon the URL pattern (or whatever), but you should probably have a reverse-proxy anyway for stability and fault-tolerance, anyway.

If you have access to the source code, it would in fact be best to modify it as you suggested in your original posting (albeit using a leading / just to be precise). Even better, perhaps use a servlet or context <init-param> to specify the location of the file, in case you want to move it around somewhere else. Modifying the source code for this purpose shouldn't require any long engagement with some off-shore company... just go edit the file and fix it in 30 seconds.

Upvotes: 2

Related Questions