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/12/13 05:46:36 UTC

Thread.currentThread().setContextClassLoader() [was: ServletCont ext.getResourceAsStream() in 3.2?]

Tomcat developers, please correct me if I am wrong but as far as I know
Tomcat 3.2 does not set ContextClassLoader for its threads. So you are using
main class loader which knows nothing about your WEB-INF/lib
I had a different problem - when code loaded by main classloader need to
load classes from WEB-INF/lib. I solved it by writing request and context
interceptors for tomcat to call
Thread.currentThread().setContextClassLoader() for each thread before it
touches a servlet (doing init/destroy or serving request) 

-----Original Message-----
From: Sean Dowd [mailto:sdowd@arcmail.com]
Sent: Tuesday, December 12, 2000 10:12 AM
To: tomcat-user@jakarta.apache.org
Subject: Re: ServletContext.getResourceAsStream() in 3.2?


> If you want to load a file that is under WEB-INF, you don't need to
involve the
> class loader at all.  Simply use:
>
>    InputStream is =
>
getServletContext().getResourceAsStream("/WEB-INF/myprops.properties");

Cool.  That works (missed the part about relative to the context
directory
in the javadoc).

But now I'm back to the classloading issue.  I'm trying to use JNDI and
the 
call to load the factory class is failing.  The JNDI code is doing
something
like this to load the factory:

    String className = "com.netscape.jndi.ldap.LdapContextFactory";
    try
      {
      // this works
//       Class c = Class.forName( className );
      // this does not
      ClassLoader loader =
(java.lang.ClassLoader)java.security.AccessController.doPrivileged(new
java.security.PrivilegedAction() {
      
      public java.lang.Object run()
        {
        return java.lang.Thread.currentThread().getContextClassLoader();
        }
      
      });
      Class c = java.lang.Class.forName(className, true, loader);
      log( "Class " + className + " loaded." );
      }
    catch ( Exception e )
      {
      log( "Error loading class " + className + ": " + e );
      return;
      }

I'm seeing a ClassNotFoundException but the jar is definitely in
WEB-INF/lib
and definitely contains com.netscape.jndi.ldap.LdapContextFactory.

Complete Installation steps ..

Posted by Sunil Kumar K <su...@majoris.com>.
Hi Guys,

    Can u ppl help me out....
    can anyone send me a complete installation script for tomcat 4 on Red
Hat Linux 6.2
    I have apache 1.3.14
    apache Jserv 1.1.2

cheers
Sunil


----- Original Message -----
From: "Craig R. McClanahan" <Cr...@eng.sun.com>
To: <to...@jakarta.apache.org>
Sent: Wednesday, December 13, 2000 10:29 AM
Subject: Re: Thread.currentThread().setContextClassLoader() [was:
ServletContext.getResourceAsStream() in 3.2?]


>
>
> "Roytman, Alex" wrote:
>
> > Tomcat developers, please correct me if I am wrong but as far as I know
> > Tomcat 3.2 does not set ContextClassLoader for its threads.
>
> The context class loader is a Java2 thing.  Since Tomcat 3.2 runs under
JDK 1.1
> as well, it cannot do this by default.  However, you can enable it by
adding the
> following in your server.xml file:
>
>     <RequestInterceptor
>      className="org.apache.tomcat.request.Jdk12Interceptor />
>
> Note -- any web application that depends on this (setting the context
class
> loader) is ***not*** guaranteed to be portable, because it is ***not***
required
> by the servlet specification.
>
> > So you are using
> > main class loader which knows nothing about your WEB-INF/lib
>
> This is not true.
>
> You are using a class loader that is aware of JAR files in WEB-INF/lib,
and
> unpacked classes in WEB-INF/classes, even on a JDK 1.1 system.  It just
doesn't
> call the Thread.setContextClassLoader() method by default.
>
> >
> > I had a different problem - when code loaded by main classloader need to
> > load classes from WEB-INF/lib. I solved it by writing request and
context
> > interceptors for tomcat to call
> > Thread.currentThread().setContextClassLoader() for each thread before it
> > touches a servlet (doing init/destroy or serving request)
> >
>
> See above.
>
> Craig McClanahan
>
> PS:  Note that Tomcat 4.0, because it is guaranteed a Java2 platform as a
> prerequisite, calls Thread.setContextClassLoader() on every request by
default.
>
>


Re: Thread.currentThread().setContextClassLoader() [was: ServletContext.getResourceAsStream() in 3.2?]

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.

"Roytman, Alex" wrote:

> Tomcat developers, please correct me if I am wrong but as far as I know
> Tomcat 3.2 does not set ContextClassLoader for its threads.

The context class loader is a Java2 thing.  Since Tomcat 3.2 runs under JDK 1.1
as well, it cannot do this by default.  However, you can enable it by adding the
following in your server.xml file:

    <RequestInterceptor
     className="org.apache.tomcat.request.Jdk12Interceptor />

Note -- any web application that depends on this (setting the context class
loader) is ***not*** guaranteed to be portable, because it is ***not*** required
by the servlet specification.

> So you are using
> main class loader which knows nothing about your WEB-INF/lib

This is not true.

You are using a class loader that is aware of JAR files in WEB-INF/lib, and
unpacked classes in WEB-INF/classes, even on a JDK 1.1 system.  It just doesn't
call the Thread.setContextClassLoader() method by default.

>
> I had a different problem - when code loaded by main classloader need to
> load classes from WEB-INF/lib. I solved it by writing request and context
> interceptors for tomcat to call
> Thread.currentThread().setContextClassLoader() for each thread before it
> touches a servlet (doing init/destroy or serving request)
>

See above.

Craig McClanahan

PS:  Note that Tomcat 4.0, because it is guaranteed a Java2 platform as a
prerequisite, calls Thread.setContextClassLoader() on every request by default.