My question is rather complicated. I'll do my best to elucidate the issue.
I have written my own web framework based on
servlets. Obviously, my framework has it's own XML config file (i.e.: /WEB-INF/myconfig.xml). Now, as I was developing the framework (and a few applications which run on it), I was using NetBeans as my
IDE, and used the bundled
Tomcat server. Essentially, I let NetBeans take care of all the details of deploying the application. It output the exploded application into a directory, and Tomcat ran it from there. It also output a .war file, but Tomcat wasn't loading it.
Now I've finished the apps and I went through the process of setting up Tomcat, mod_jk, and Apache on the production server. I got it all set up and stuck the .war file which NetBeans made into the proper folder. However, the application didn't work. I checked the log files and determined that Tomcat/mod_jk/Apache are all configured correctly because log messages from my framework are showing up in the Tomcat logs, followed by some revealing exceptions/stack traces: my framework can't find it's configuration file.
I was using the following code to get the absolute path of the root of the web application:
then I could append "/WEB-INF/myconfig.xml", and build it with JDOM. However, the servlet API docs say that ServletContext.getRealPath() returns null if the application is deployed from a .war file. That's why my framework couldn't find its config file. It was looking for "null/WEB-INF/myconfig.xml", which obviously is not valid.
I also explored Class.getResource(). However, as I understand it, this technique will fail when the servlet container doesn't expand .war files (Tomcat does by default, but can be configured to not do so).
So, I guess there are two options, neither of which I know how to do.
1) Build the .war file with the configuration file inside of it, and then somehow load the configuration file using a path relative to the root of the .war file.
2) Or, even better, have the configuration file outside of the .war file, and load it. Somehow. The challenge is in getting the location of the file to my framework. I'm fresh out of ideas.
I would really like to do number two, but I could live with number one.
I really don't know how to explain this dilemma without being so verbose, so I've had limited success googling for solutions. Hence this post.
Any ideas?
Thanks a lot,
benjamin