tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: Design question: common/lib --> WEB-INF/lib
Date Fri, 05 Apr 2002 23:45:57 GMT


On Fri, 5 Apr 2002, Mark Shaw wrote:

> Date: Fri, 5 Apr 2002 15:29:04 -0800
> From: Mark Shaw <MShaw@centricasoftware.com>
> Reply-To: Tomcat Users List <tomcat-user@jakarta.apache.org>
> To: "'tomcat-user@jakarta.apache.org'" <tomcat-user@jakarta.apache.org>
> Subject: Design question: common/lib --> WEB-INF/lib
>
> I have a design question I'm guessing someone has already tackled:
>
> Description:
> I'm developing an application that runs within Tomcat.  All the JARs for
> that application are in it's webapp/WEB-INF/lib, except for a few classes
> that implement a Realm which referenced in server.xml.  Because these
> classes are referenced in server.xml, they need to be in
> {catalina_home}/common/lib.  The problem I have is that my Realm need to use
> my database code, but my database JARs are defined in my webapp lib.  I
> could put everything in common/lib except that I'd like to be able to
> control (start/stop/re-load) my application using the Tomcat manager.
>
> Question:
> How can my code in common/lib reference objects in webapp/lib?  Do I have to
> do this using JNDI by defining common interfaces in common/lib and returning
> webapp/lib objects from my factories?  Any guidances is appreciated.
>

Using JNDI is one way, but it can be cumbersome.

Another way is to rely on a requirement in J2EE 1.3 (and therefore in
Servlet 2.3) that requires containers to expose the web application's
class loader:

  ClassLoader webappLoader =
    Thread.currentThread().getContextClassLoader();
  Class webappClass = webappLoader.loadClass("com.mycompany.MyClass");

This works because the container (Tomcat in this case) always sets the
thread context class loader to the one for the webapp being executed on
the current request thread -- therefore, the classes in /WEB-INF/classes
and /WEB-INF/lib are visible via this class loader, even though the class
containing the above code is loaded from somewhere else.

WARNING - This only works for request processing threads managed by the
container.  If you start your own threads, you are on your own for saving
a reference to the webapp class loader you need.

> Thanks,
> -Mark
>

Craig



--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message