I'd like to store connection URLs in a JNDI binding for my Tomcat application. Since Tomcat uses context.xml
for JNDI resource defining, I need to figure out the propert way to store a String (or multiple strings for multiple connections) in context.xml
.
My reason for doing this is so that I can define different strings for different environments, and load them through JNDI.
Usually, I see entries like so:
<Context ...>
<Resource name="someName" auth="Container"
type="someFullyQualifiedClassName"
description="Some description."/>
</Context>
Is it really just as simple as:
<Context ...>
<Resource name="myConnectionURL" auth="Container"
type="java.lang.String"
description="A connection URL string."/>
</Context>
If so, where do I actually store the String value?!?! And if it's not correct, then what is the proper way for me to store, for instance, "amqp:5272//blah.example.com¶m1=4
" in context.xml
so I could then look it up like so:
Context ctx = new InitialContext();
String connectionURL = (String)ctx.lookup("myConnectionURL");
Thanks in advance!
You can use an Environment
tag:
<Context>
<Environment name="myConnectionURL" value="amqp:5272//blah.example.com¶m1=4" type="java.lang.String"/>
</Context>
And you can read it almost as you specified in the question:
InitialContext initialContext = new InitialContext();
Context environmentContext = (Context) initialContext.lookup("java:/comp/env");
String connectionURL = (String) environmentContext.lookup("myConnectionURL");
This is much the same as using a Parameter
tag, but without the need for a ServletContext
.