Bug 32713 - resource-env-ref in webapp not registered in JNDI
resource-env-ref in webapp not registered in JNDI
Product: Tomcat 5
Classification: Unclassified
Component: Catalina
Nightly Build
PC Windows XP
: P2 normal (vote)
: ---
Assigned To: Tomcat Developers Mailing List
Depends on:
  Show dependency tree
Reported: 2004-12-15 05:55 UTC by Simon S.
Modified: 2004-12-15 04:02 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Simon S. 2004-12-15 05:55:38 UTC
In my conf/server.xml I define:

  <Environment name="test" type="java.lang.String" value="hello world"/>

Then in my webapp/WEB-INF/web.xml I add:


Then from a servlet I use code such as:

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

I tried all manner of variations to try to make it work and eventually ended up 
downloading the source, debugging tomcat and seeing that the ResourceEnvRef is 
registered incorrectly as follows:

15:02:06,809 DEBUG [NamingContextListener]   Adding resource env 
ref /Catalina/localhost/router-web

Reviewing the source (HEAD as of 15-dec-2004), it looks like the code is in 
fact using the wrong name to register the resource:

NamingContextListener, line 855:
     * Set the specified resources in the naming context.
    public void addResourceEnvRef(ContextResourceEnvRef resourceEnvRef) {

        // Create a reference to the resource env.
        Reference ref = new ResourceEnvRef(resourceEnvRef.getType());
        // FIXME: Adding the additional parameters, if any
        Iterator params = resourceEnvRef.listProperties();
        while (params.hasNext()) {
            String paramName = (String) params.next();
            String paramValue = (String) resourceEnvRef.getProperty(paramName);
            StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
        try {
            if (logger.isDebugEnabled())
                log.debug("  Adding resource env ref " + name);
            createSubcontexts(envCtx, name);
            envCtx.bind(name, ref);
"name" is an attribute of the containing class, NamingContextListener. This 
seems wrong.  It should be registered under a name obtained from the resource-
env-entry (resourceEnvRef object).

Hope this helps somebody out there.
Comment 1 Remy Maucherat 2004-12-15 13:02:43 UTC
I suppose this was caused by a cut & paste error. Thanks.