Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

Tomcat JNDI - mapping a String

Ondra Nekola

2004-02-23

Replies:

I have developed a small web-based aplication. I use an ant build script
to compile it and to generate a war. The application is used in several
servers with different settings (it uses different databases, connects to
different xml-rpc resources...) so it has to somehow read the
configuration from the server environment.
I have used the tomcat Web server administration tool to set Resources - a
data source and several Environment Entries. The tool regenerates
server.xml file in such a manner:

<Server>
...
<GlobalNamingResources>
  <Environment description="" name="BLAH_USER" type="java.lang.String"
value="MrSpock"/>
...
</GlobalNamingResources>
...
</Srver>

Then I change my deployment descriptor get access to this environment:
<web-app>
...
<resource-env-ref>
  <resource-env-ref-name>BLAh_USER</resource-env-ref-name>
  <resource-env-ref-type>java.lang.String</resource-env-ref-type>
</resource-env-ref>
...
</web-app>

The code, that tryes to read this environmnt looks like this:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
String res_user = (String) envCtx.lookup("BLAH_USER");

The problem is, that I get this exception:
javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance (ResourceEnvFactory.java:146)
    at javax.naming.spi.NamingManager.getObjectInstance (NamingManager.java:301)
    at org.apache.naming.NamingContext.lookup (NamingContext.java:837)
    at org.apache.naming.NamingContext.lookup (NamingContext.java:197)
    at myapp.servlets.MyAppListener.contextInitialized(MyAppListener.java:123)

When I try to access a mapped JDBC resource everything seems to be OK.
When I list (NamingEnumeration enum = initCtx.list("java:comp/env");) the
keys for mapped resources BLAH_USER appears.

I have tried look this situation in the Tomcat documentation and to google
it, but I haven't get any reasonable idea to solve it.
--
 S pozdravem
    Ondrej Nekola
    ondra@(protected)
    http://www.matfyz.cz/ondra
    ICQ# 160692888

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)



©2008 junlu.com - Jax Systems, LLC, U.S.A.