On Fri, 15 Nov 2002, Sri Sankaran wrote:

> Date: Fri, 15 Nov 2002 14:38:40 -0500
> From: Sri Sankaran <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts-User <[EMAIL PROTECTED]>
> Subject: [OT] Tomcat class loading
>
>
> It seems as if the class-loader that loaded a particular class maintains some kind 
>of reference to the class.  Any dependent class can be loaded only by this loader or 
>any one up the Tomcat hierarchy
>
>              Bootstrap
>                    |
>                System
>                    |
>               Common
>         /                      \
>  Catalina                 Shared
>                            /              \
>                      Webapp-1  Webapp-2
>
> So in other words, if Class-A is loaded by the Common classloader and
> Class-A requires Class-B the search will only be conducted by the
> Bootstrap classloader, the System classloader and the Common classloader
> (in that order).
>

If you are just doing standard Java style instantiations:

  Foo f = new Foo();

then you are correct -- Java looks up but not down.  In fact, there is no
way to go down, because a java.lang.ClassLoader instance has no knowledge
of other class loaders that consider it to be a parent.

However, there is a workaround for the very common scenario that something
in the Common class loader needs to instantiate a class from a webapp (for
example, you want to put struts.jar into common).  There is a Java thing
called a "context class loader" attached to each request processing
thread, and the app server will generally (required in J2EE 1.4, but
commonly implemented in current servers) store the webapp class loader for
the current webapp there.  Thus, if you want to load class
"com.mypackage.MyFoo" out of the webapp class loader, you could say:

  // Get reference to the class loader for this webapp
  ClassLoader wacl = Thread.currentThread().getContextClassLoader();

  // Load the desired class from this class loader
  Class fooClass = wacl.loadClass("com.mypackage.MyFoo");

  // Instantiate an instance of this class
  Foo foo = (Foo) fooClass.newInstance();

If you look into the Struts 1.1 method RequestUtils.applicationClass(),
you'll see that it uses this technique.  Because of that, you should -- in
principle -- be able to put struts.jar into the common class loader in
Tomcat, and things should still work.  (There may be some corner cases we
haven't caught yet, but it *should* be possible).

Note that this won't work at all for Struts 1.0, because that code doesn't
use the context class loader.

> Is this correct?
>
> Sri
>

Craig



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@;jakarta.apache.org>

Reply via email to