• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to configure a JNDI URL resource

 
Rohit David
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

CONTEXT.XML

code:
--------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?><Context> <Resource name="url/wfresource" auth="Container" type="java.net.URL" url="http://localhost:8080/ibi_html/wfresource.properties"/></Context>

--------------------------------------------------------------------------------



WEB.XML

code:
--------------------------------------------------------------------------------

<resource-env-ref> <description>Object factory for MyBean instances.</description> <resource-env-ref-name>url/wfresource</resource-env-ref-name> <resource-env-ref-type>java.net.URL</resource-env-ref-type></resource-env-ref>

--------------------------------------------------------------------------------



JNDI Lookup code snippet


code:
--------------------------------------------------------------------------------

//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");

--------------------------------------------------------------------------------



Its throwing an exception.

My question is if URL resource configuration is possible in Tomcat 5.5, and if so how.

thanks
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rohit,

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:

mysql.jndiName=jdbc/mysql

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.

Hope this helps...
Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!