I am trying to replicate the Websphere's URL Resource provider functionality in Tomcat, but haven't been really successful. So what I would like to do is access a properties file from a URL. This provides a convenient approach to modifying prop files as apposed to residing within WAR file. Anyway back to my question, I have taken a stab at this and below is how my meta-inf\context.xml, web.xml, and jndi code snippet looks.
//Get a handle to the JNDI environment naming contextContext env = (Context)new InitialContext().lookup("java:comp/env");//Get a single valuejava.net.URL destURL = (java.net.URL)env.lookup("url/wfresource");
We are doing it that way where I work, but it is a little different than how you describe.
One way is to use the Tomcat admin page and create a new datasource. That takes care of part of the picture. I had to do quite a bit of hacking to get it to work from beginning to end. I'll describe the steps, and hopefully you can retrofit them for your use.
Part of the configuration is in a properties file: configuration.properties. Say we want to setup a MySQL connection. In the file, add a line like:
To access this value, do something like this:
After loading the properties file, you can call the getJndiName method passing dbName as "mysql". This method is useful if you are setting up several database resources. Of course, you'll need to add them to the properties file as well.
To establish a connection, create a method similar to this:
Now, you're ready to make the query code:
The fun part is getting the connection to work. I ended up using the application context. So in $CATALINA_HOME/conf/Catalina/localhost, I have my file. In your case it would be something like myapp.xml. Create the file as follows:
Important: MAKE A COPY OF THIS FILE. If you redeploy your web app, it's very possible Tomcat will nuke it.
There's something the Tomcat admin util is *supposed* to configure. With 5.5, I had to beat it into submission with a ball peen hammer to get it to work. If you use it, in the <GlobalNamingResources> section, look for:
It should reflect the settings you added through the admin page. You can use this as a reference if you prefer not to use it though.