You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Roytman, Alex" <ro...@peacetech.com> on 2000/11/21 16:59:50 UTC

Thread.currentThread().getContextClassLoader() in Tomcat 3.2

I would like to use Thread.currentThread().getContextClassLoader() with
Tomcat 3.2 to resolve one common problem when class from system classpath
needs to call something loaded by tomcat context's loader. As far as I
understand Tomcat 3.2 suppose to run under jdk1.1 so it does not do
Thread.currentThread().setContextClassLoader() before it hands control to a
Servlet.

Below are my interceptors I use for
Thread.currentThread().setContextClassLoader() and setting scope of jndi to
make it tomcat context specific.

I wonder if someone with more knowledge on tomcat internal could possibly
comment on this approach. Is it right approach? is it safe? What if someone
in another interceptor will do something else to
Thread.currentThread().contextClassLoader?    

Your feedback is greatly appreciated

Alex



Request Interceptor:

  public int preService(Request request, Response response) {
 
Thread.currentThread().setContextClassLoader(request.getContext().getServlet
Loader().getClassLoader());
    HierInitCtxFactory.selectScope(request.getContext().getPath());
    return 0;
  }

  public int postService(Request request, Response response) {
    HierInitCtxFactory.selectScope(null);
 
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    return 0;
  }


ContextInterceptor:

  public void preServletInit(Context ctx, ServletWrapper sw) throws
TomcatException {
 
Thread.currentThread().setContextClassLoader(ctx.getServletLoader().getClass
Loader());
    HierInitCtxFactory.selectScope(ctx.getPath());
  }

  public void postServletInit(Context ctx, ServletWrapper sw) throws
TomcatException {
    HierInitCtxFactory.selectScope(null);
 
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
  }

Re: Thread.currentThread().getContextClassLoader() in Tomcat 3.2

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Roytman, Alex" wrote:

> I would like to use Thread.currentThread().getContextClassLoader() with
> Tomcat 3.2 to resolve one common problem when class from system classpath
> needs to call something loaded by tomcat context's loader. As far as I
> understand Tomcat 3.2 suppose to run under jdk1.1 so it does not do
> Thread.currentThread().setContextClassLoader() before it hands control to a
> Servlet.
>

If Tomcat 3.2 runs in a Java2 environment, it does in fact call
setContextClassLoader(), assigning the webapp class loader for the currently
selected web application to the current request thread.  Tomcat 4.0 does this
under all circumstances, because it requires a Java2 environment.  However, the
fact that this occurs has nothing to do with your issue.

Under any version of Tomcat, what you are asking for does not work.  More
importantly, it *should not* work.  Class loaders are only allowed to look *up*
the class loader hierarchy (and, of course, such a hierarchy exists only in a
Java2 environment).  Classes that are loaded from the system class path *do not*
have any access to classes loaded only by a webapp's class loader.  If they did,
it would violate the whole concept of a web application being self contained.

If you have a class that needs access to classes that are loaded from
WEB-INF/classes and WEB-INF/lib, that class itself must be loaded from one of
those two places (and therefore loaded by the webapp class loader).

Craig McClanahan