6 Replies Latest reply on Oct 4, 2002 4:21 PM by adrian.brock

    Bug (?) wrt. getResourceAsStream() from webapp component of

    adrians

      The JBoss book and lost diagram - http://www.jboss.org/servlet/JiveServlet/download/67-17765-3728702-607/UCLArch.gif, seem to imply that the UCL offers a flat namespace that would make the classes and resources of all deployment units of an application visible in all the units. Am I already misinterpreting here?

      Our situation is as follows:

      ear (exploded)
      -- EJB jar 1, .... EJB jar n
      -- war 1 (exploded), ... war n (exploded)
      -- common jar 1, ... common jar n

      In one of the web apps, there is a need to load an XML file from the root of one of the common jars. This common jar is referenced in the Class-Path of the EJB jar manifests, but is not referenced at all by the web apps. Under Weblogic 6.1, this arrangement still lets us load a file in the common jar since the web apps see everything seen by the EJB jar loader.

      With JBoss 3.0.1, Thread.currentThread().getContextClassLoader().getResourceAsStream("/resource.xml") returns null, although Thread.currentThread().getContextClassLoader().loadClass("a.class.in.the.common.jar") succeeds and loads the named class. Shouldn't getResourceAsStream() use the same loading mechanism to find resources? I tried adding the common jar to the Class-Path in the web app's manifest, but still had no luck in loading the resource.

      Any input on this issue (or on how class/resource loading actually works in JBoss 3.x) would be appreciate,

      Adrian

        • 1. Re: Bug (?) wrt. getResourceAsStream() from webapp component
          chgrimm

          could you send me ear with a simple example ( war file + common.jar containing resources )

          i have the source distro of jboss 3.0.2. may be i can figure out via debug

          • 2. Re: Bug (?) wrt. getResourceAsStream() from webapp component

            getResourceAsStream("resource.xml");

            Regards,
            Adrian

            • 3. Re: Bug (?) wrt. getResourceAsStream() from webapp component
              adrians

              > getResourceAsStream("resource.xml");
              >
              > Regards,
              > Adrian

              Thanks, this seems to work. Why is it that by prefixing the resource with a "/" it fails to work, though? I thought the convention for looking from the top of the classloader's namespace was to prefix a resource in this manner. As a point of note, Weblogic succeeds in loading a resource from the root of the namespace with or without the "/" through the getResourceAsStream() call on a classloader. Someone might consider changing this behaviour in order to make life easier for people switching to JBoss from Weblogic.

              Adrian

              • 4. Re: Bug (?) wrt. getResourceAsStream() from webapp component

                Which convention?

                I think this is a "feature" of the weblogic classloaders.

                Try it from a simple java class with a resource inside
                a jar on your classpath.
                Doesn't work then either.

                Regards,
                Adrian

                • 5. Re: Bug (?) wrt. getResourceAsStream() from webapp component
                  adrians

                  Adrian,

                  From the javadoc for Class getResourceAsStream:

                  ---
                  Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
                  This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResourceAsStream.
                  ---

                  The ClassLoader getResourceAsStream() refers to getResource() for details and that method saysthe following:

                  ---
                  This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will call findResource to find the resource.
                  ---

                  The way I read it, the ClassLoader getResourceAsStream() should be able to handle resource names prefixed with "/" as referring to resources at the root of the namespace since the Class method will delegate to it. Also, resources could possibly end up being loaded through either the Class or ClassLoader methods in a given app and my feeling is you shouldn't need to have two different string defined depending on the expected use. What I mean (this answers your second point) is that if you have a resource that exists at the root but whose path is defined without the leading "/" and you try to load it through the Class getResourceAsStream() it would not be found since it would be prefixed with the respective class' package. Anyhow, that's my take on it...

                  Adrian2

                  • 6. Re: Bug (?) wrt. getResourceAsStream() from webapp component

                    Yes,
                    getClass().getResourceAsStream("/resource.xml");
                    works as described in the javadoc.
                    If you look at the code for java.lang.Class
                    it passes "resource.xml" to the classloader.

                    Regards,
                    Adrian