DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5188>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5188

AdaptiveClassLoader : Finding resources with getResources(). (jndi.properties).

           Summary: AdaptiveClassLoader : Finding resources with
                    getResources(). (jndi.properties).
           Product: Tomcat 4
           Version: 4.0.1 Final
          Platform: PC
        OS/Version: Windows NT/2K
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Catalina
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


Hi,

When trying to create a InitialContext() for a JNDI lookup, the InitialContext
() attempts to locate a jndi.properties file.
This files does exist in a jar file in the WEB-INF/classes directory, but 
Tomcat doesn't locate it.

I can see, that others have had the same problem, but I can't find any bugs 
submitted on it. If this is a duplicate entry, send a heated flame and delete 
the entry *S*.

I've pasted in an entry I found on the net describing the problem, and a fix 
for the Tomcat 3.2 - hope it's of any help. I'm currently assigned to a 25-
hours-per-day project, so I haven't even taken the time to check, if this code 
have been merged into the released code. I've taken the easy (ugly) way out and 
placed the JAR file containing my jndi.properties file in the global class path.

----------------------- Paste in - start 
From: Christopher Audley <[EMAIL PROTECTED]> 

The following discussion applies to tomcat 3.2.1 running under Sun JDK 1.3

I spent this afternoon tracking down why a call to new InitialContext() 
from a web application did not appear to be finding the jndi.properties 
located under WEB-INF/classes.  I am using the Jdk12Interceptor and 
verified that Thread.currentThread().getClassLoader() was the tomcat 
AdaptiveClassLoader before the call to new InitialContext().  After some 
investigation I determined that the JNDI implementation uses the 
classloader method getResources to find all occurances of the file 
jndi.properties in the classpath.  AdaptiveClassLoader does not have 
this method implemented, the default implementation calls findResources 
which simply returns an empty Enumeration unless overriden.

Attached are patches to AdaptiveClassLoader and ClassRepository to 
implement findResources so that JNDI will behave correctly when a 
jndi.properties is placed in the web application classpath.  I have 
moved some code from AdaptiveClassLoader to ClassRepository to avoid 
code duplication and changed the implementation of getResource to 
findResource for consistency.

I hope that this can be incorporated into the sources before 3.2.2.

Cheers
Chris

diff -ubr jakarta-tomcat-3.2.1-
src.orig/src/share/org/apache/tomcat/loader/AdaptiveClassLoader.java jakarta-
tomcat-3.2.1-src/src/share/org/apache/tomcat/loader/AdaptiveClassLoader.java
--- jakarta-tomcat-3.2.1-
src.orig/src/share/org/apache/tomcat/loader/AdaptiveClassLoader.java    Sun Mar 
11 19:21:12 2001
+++ jakarta-tomcat-3.2.1-
src/src/share/org/apache/tomcat/loader/AdaptiveClassLoader.java Sun Mar 11 
18:40:46 2001
@@ -762,69 +762,45 @@
      * @param   name    the name of the resource, to be used as is.
      * @return  an URL on the resource, or null if not found.
      */
-    public URL getResource(String name) {
-        if( debug > 0 ) log( "getResource() " + name );
-        // First ask the parent class loader to fetch it, if possible
-        URL u = null;
-        if (parent != null) {
-            u = parent.getResource(name);
-            if (u != null)
-                return (u);
-        }
-        // Second ask the system class loader to fetch it from the classpath
-        u = getSystemResource(name);
-        if (u != null) {
-            return u;
-        }
-
+    protected URL findResource(String name) {
         if (name == null) {
             return null;
         }
 
-        // Third, check our own repositories
+        // check our own repositories
         Enumeration repEnum = repository.elements();
         while (repEnum.hasMoreElements()) {
             ClassRepository cp = (ClassRepository) repEnum.nextElement();
-            File file = cp.getFile();
-            // Construct a file://-URL if the repository is a directory
-            if (file.isDirectory()) {
-                String fileName = name.replace('/', File.separatorChar);
-                File resFile = new File(file, fileName);
-                if (resFile.exists()) {
-                    // Build a file:// URL form the file name
-                    try {
-                        return new URL("file", null, resFile.getAbsolutePath
());                            
-                    } catch(java.net.MalformedURLException badurl) {
-                        badurl.printStackTrace();
-                        return null;
-                    }
+            URL resourceURL = cp.findResource(name);
+            if ( resourceURL != null )
+                return resourceURL;
                 }
-            }
-            else {
-                // a jar:-URL *could* change even between minor releases, but
-                // didn't between JVM's 1.1.6 and 1.3beta. Tested on JVM's from
-                // IBM, Blackdown, Microsoft, Sun @ Windows and Sun @ Solaris
-                try {
-                    ZipFile zf = new ZipFile(file.getAbsolutePath());
-                    ZipEntry ze = zf.getEntry(name);
 
-                    if (ze != null) {
-                        try {
-                            return new URL("jar:file:" + file.getAbsolutePath
() + "!/" + name);
-                        } catch(java.net.MalformedURLException badurl) {
-                            badurl.printStackTrace();
-                            return null;
-                        }
-                    }
-                } catch (IOException ioe) {
-                    ioe.printStackTrace();
+        // Not found
                     return null;
                 }
+
+    /**
+     * Find all occurences of a given resource in the class loaders
+     * repositories.
+     *
+     * @param   name    the name of the resource, to be used as is.
+     * @return  an Enumeration of URLs of to this resource.
+     */
+    protected Enumeration findResources(String name) {
+        if( debug > 0 ) log( "findResources() " + name );
+
+        Vector urls = new Vector( repository.size() );
+        if ( name != null ) {
+            Enumeration repEnum = repository.elements();
+            while( repEnum.hasMoreElements() ) {
+                ClassRepository cp = (ClassRepository) repEnum.nextElement();
+                URL resourceURL = cp.findResource(name);
+                if( resourceURL != null )
+                    urls.add( resourceURL );
             }   
         }
-
-        // Not found
-        return null;
+        return urls.elements();
     }
 
     public String toString() {
diff -ubr jakarta-tomcat-3.2.1-
src.orig/src/share/org/apache/tomcat/loader/ClassRepository.java jakarta-tomcat-
3.2.1-src/src/share/org/apache/tomcat/loader/ClassRepository.java
--- jakarta-tomcat-3.2.1-
src.orig/src/share/org/apache/tomcat/loader/ClassRepository.java        Sun Mar 
11 19:22:31 2001
+++ jakarta-tomcat-3.2.1-
src/src/share/org/apache/tomcat/loader/ClassRepository.java     Sun Mar 11 
19:01:44 2001
@@ -61,6 +61,8 @@
 import java.io.*;
 import java.lang.*;
 import java.util.*;
+import java.util.zip.*;
+import java.net.*;
 
 public class ClassRepository {
     private File file;
@@ -77,5 +79,43 @@
 
     public Object getProtectionDomain() {
        return protectionDomain;
+    }
+
+    public URL findResource(String name) {
+        if (file.isDirectory()) {
+            String fileName = name.replace('/', File.separatorChar);
+            File resFile = new File(file, fileName);
+            if (resFile.exists()) {
+                // Build a file:// URL form the file name
+                try {
+                    return new URL("file", null, resFile.getAbsolutePath
());                            
+                } catch(java.net.MalformedURLException badurl) {
+                    badurl.printStackTrace();
+                    return null;
+                }
+            }
+        }
+        else {
+            // a jar:-URL *could* change even between minor releases, but
+            // didn't between JVM's 1.1.6 and 1.3beta. Tested on JVM's from
+            // IBM, Blackdown, Microsoft, Sun @ Windows and Sun @ Solaris
+            try {
+                ZipFile zf = new ZipFile(file.getAbsolutePath());
+                ZipEntry ze = zf.getEntry(name);
+
+                if (ze != null) {
+                    try {
+                        return new URL("jar:file:" + file.getAbsolutePath() 
+ "!/" + name);
+                    } catch(java.net.MalformedURLException badurl) {
+                        badurl.printStackTrace();
+                        return null;
+                    }
+                }
+            } catch (IOException ioe) {
+                ioe.printStackTrace();
+                return null;
+            }
+        }   
+        return null;
     }
 }



----------------------- Paste in - end

Apart from that, a greeting to all you guys - great work bringing all this 
stuff to us.

/Sverre Eplov

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to