tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bradley Neuberg <bneub...@randomwalk.com>
Subject Re-Do [PATCH] Bug and Bugfix for NetworkClassLoader
Date Wed, 29 Dec 1999 23:23:54 GMT
I'm sorry; my last bugfix for the getResource code in
org.apache.util.NetworkClassLoader had a bug itself!

Here's a description of the bug: getResource() is not implemented in
org.apache.tomcat.core.ServletClassLoader nor in
org.apache.tomcat.util.NetworkClassLoader.  If called on an instance of
ServletClassLoader or NetworkClassLoader then getResource() cascades up
to the superclass ClassLoader which returns the wrong resource.  It is
strange that getResource() is not implemented, as getResourceAsStream()
is implemented in NetworkClassLoader.

Here is the correct code:

    /** @return The URL that can retrieve the requested resource
        if such a resource exists, otherwise returns null */
    public URL getResource(String name) {
      URL resource;
      // Algorithm:
      //
      // 1. first check the system path for the resource
      // 2. next  check the  delegate/parent class loader for the
resource
      // 3. then attempt to get the resource from the url set.
      //

      // Lets check the system path for the resource.
      resource = getSystemResource(name);

      // Lets check the parent/delegate class loader for the resource.
      if(parent != null) {
          resource = parent.getResource(name);
          if(resource != null)
              return resource;
      }

      // Lets load it ourselves.
      for(Enumeration e = urlset.keys(); e.hasMoreElements();) {
        URL url = (URL) e.nextElement();
        URLResourceReader urr = (URLResourceReader) urlset.get(url);
        if(urr != null) {
          // append the name of the resource to the end of this url
          if (name.startsWith("/") && url.getFile().endsWith("/"))
            name = name.substring(1, name.length());
          try {
            url = new URL(url.getProtocol(), url.getHost(),
                          url.getPort(), url.getFile() + name);
          }
          catch (MalformedURLException ex) {
            ex.printStackTrace();
            // should never happend
          }
          return url;
        }
      }

      // nothing found
      return null;
    }


Mime
View raw message