tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chefo <che...@gmail.com>
Subject Re: Access StandardContext in WebappClassLoader
Date Thu, 26 Aug 2010 15:24:52 GMT
Hi again

and thanks for the quick responses.

What bothers me in the WebappClassLoader is the fact that before it checks
its repositories (lib folder and classes) and asks its parent class loader,
it will first attempt to load a class from the system class loader - from
the jvm. This is not standard classloader logic but sth done on purpose in
the webapp class loader. I want to prevent that for certain packages and I
want it to be configurable for each web application. That's why I wrote my
WebappClassLoader and configured its usage in the default context.xml
(<Loader loaderClass="org.chefo.OSGiWebappClassLoader"/> in <catalina
home>/conf/context/xml). I figured the easiest way to configure a list of
packages that should not be loaded through the system class loader is with a
parameter in the context of the web application, thus I need to access a
context parameter in my webapp class loader. I thought it would be normal
for the webapp class loader to be able to access the context that is
associated with it...

Hope that makes it a bit clearer...

Thanks,
Chefo


On Thu, Aug 26, 2010 at 5:43 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Chefo,
>
> On 8/26/2010 9:38 AM, Chefo wrote:
> > I'm using tomcat 6.0.18 adapted to osgi environment and I would like to
> make
> > the WebappClassLoader configurable so that it does not load certain
> packages
> > directly through the system class loader but use the class loader
> hierarchy
> > instead.
>
> What ClassLoader hierarchy?
>
> > In order to do that I have extended the standard webapp class
> > loader and configured the container to use the new one in the base
> > context.xml.
>
> Ok.
>
> > I want to configure the packages through a context parameter
> > (separately for each webapp) or eventually through an env-entry. However
> I
> > cannot find a way to access the StandardContext instance in my
> > WebappClassLoader.
>
> That's because these two classes have no relationship to one another.
>
> > This is probably a very simple thing to do but I'm not
> > really well familiar with catalina API and this is kinda driving me
> crazy.
> > Any help/suggestion is more than welcome.
>
> The WebappClassLoader is just a ClassLoader that loads "down" instead of
> "up" like traditional ClassLoaders. It's not really webapp or
> context-specific in it's code... it's just that the servlet spec says
> that webapps need to load classes locally before looking up the
> ClassLoader tree, so this class was written to do just that.
>
> The WebappClassLoader isn't tied to the webapp/context other than it's
> the one that's been assigned to the context as it's ClassLoader.
>
> Instead of trying to reach-down from the ClassLoader to the
> StandardContext, why not do the reverse: register a
> ServletContextListener that configures the (already-created)
> ClassLoader. Something like this:
>
> public class CrazyClassLoaderConfigrationListener
>  implements ServletContextListener
> {
>  public void contextInitialized(ServletContextEvent e)
>  {
>    ClassLoader cl = this.getClass().getClassLoader();
>
>    if(!(cl instanceof CrazyClassLoader))
>      cl = Thread.currentThread().getContextClassLoader();
>
>    if(cl instanceof CrazyClassLoader)
>    {
>      ((CrazyClassLoader)cl)
>           .configure(e.getServletContext()
>                       .getInitParameter("packages"));
>    }
>    else
>    {
>      e.getServletContext().log("Could not configure CrazyClassLoader");
>    }
>  }
> }
>
> That's a little late to be setting the ClassLoader configuration, but it
> will happen before your servlets are loaded.
>
> Of course, you could also just use a .properties file to configure your
> ClassLoader.
>
> The real question is: what is it that you're really trying to do?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkx2fYgACgkQ9CaO5/Lv0PAs0ACgtV8J5d+y8n+CbxlNZpYJZndA
> qckAn2P0c45ioh8O7SyUsKfiYQfK3TPU
> =4TUr
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message