You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Don Asper <Do...@sas.com> on 2014/06/04 17:46:14 UTC

Using custom classloader

Hi:  I want my web apps running on Tomcat 7.0.35 to use a custom classloader.  The reason is that I want each web app classloader instance to do some processing to set up the classpath for the web app it belongs to.  I have the following questions:  1) Is org.apache.catalina.loader.WebappClassLoader  a good class to derive my custom classloader from?  Is it a good choice going forward for later versions of Tomcat?  2) Is there a recommended way for my custom classloader to identify which web app it belongs to?  It needs to identify which web app it is for so that it will know what jars to put on its classpath.  Thanks!

Re: Using custom classloader

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Don,

On 6/4/14, 11:46 AM, Don Asper wrote:
> Hi:  I want my web apps running on Tomcat 7.0.35 to use a custom 
> classloader.  The reason is that I want each web app classloader 
> instance to do some processing to set up the classpath for the web
>  app it belongs to.

Can you be more specific? Perhaps you don't actually need to write a new
ClassLoader... you may be able to configure the existing ClassLoader to
behave the way you want, or possibly use a non-ClassLoader-related
solution.

> I have the following questions:  1) Is 
> org.apache.catalina.loader.WebappClassLoader  a good class to
> derive my custom classloader from?

Yes.

> Is it a good choice going forward for later versions of Tomcat?

Probably. I don't think that class (name) has changed significantly in a
very long time. If you hook-into the constructor or lifecycle methods
you should be good. Remember: always test with new versions :)

> 2) Is there a recommended way for my custom classloader to identify
>  which web app it belongs to? It needs to identify which web app it
> is for so that it will know what jars to put on its classpath.

If you are using WebapClassLoader as your parent, you can call
super.getContextName(), but that will just get you the name of the
context, not the location of the deployment artifact. What information
do you actually need?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJTj7f0AAoJEBzwKT+lPKRYG3AP/3gJCphgLXN8hZvojSz760CO
JRzw+YR3qubhFPH+K2diwveA4tLwIpmL7rMiCljXIVZz+q5illbYtrMR6Ou9nCmt
k7L9R2rjX5Fyr+Eew4qevXQHL+0V0MbVsuRd9Gs9D4LVvMgNpOb1ArhIkweZEi2A
67GTPKENU+o29yJrxXU/19EMbyItu6O/w26boPeX6+7MQXfvSycSeJZdytNA3Sws
psiEf+5/5th2jppDB2aoS/lpZbAoODLUPkeOryGZPjAL5KFCqVAUvhtV/CmfHqKw
VSbaa6XCvhFlijXETArWiSgjdaWm5dBEWK+XdKhAV9PWt10zBHeL4+vXdsan3K6Z
3tXy9ceMTeHC9oXAJDOcdXIjiL8wIJlPOH0P05/cbnMaGL2ItKu1H+V6y9NqvKXw
hr07SypVdWzjez+1s3u0Uv7hzyoRXVtfbA5vpCAOgo0jL04bLLfzlJ/JE+yZwdk1
Y37XBxRWvRpUfeax0+U/q5X+8Io3bBHwBzUW9JPJnQE1iLxej/H4Db1Fp+kXnVaz
GtwLYEAechGXgAnZF+0msocTdLb7OjecCcKmVDmwHayUqHPAQNrh0Bn2vaw4EWef
HkV6XH3Rt3bcvgw+CUQoiDa+k02BTtBOCLRlCCYc6L8MTL9wh2uVUWIrijBNMTjn
jETFSqYlIC2H6O5mGiJ2
=Lc35
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Using custom classloader

Posted by Konstantin Kolinko <kn...@gmail.com>.
2014-06-04 19:46 GMT+04:00 Don Asper <Do...@sas.com>:
> Hi:  I want my web apps running on Tomcat 7.0.35 to use a custom classloader.  The reason is that I want each web app classloader instance to do some processing to set up the classpath for the web app it belongs to.  I have the following questions:  1) Is org.apache.catalina.loader.WebappClassLoader  a good class to derive my custom classloader from?  Is it a good choice going forward for later versions of Tomcat?  2) Is there a recommended way for my custom classloader to identify which web app it belongs to?  It needs to identify which web app it is for so that it will know what jars to put on its classpath.  Thanks!

Look at VirtualWebappLoader. Maybe it suits your needs as is.

Also the same trick that it does can be performed from a <Listener>.
There is no need to extend WebappLoader just for that.

A Listener and a WebappLoader have access to Tomcat's Context class
and can get the paths.

The VirtualWebappLoader is not available in Tomcat 8, replaced by new
Pre/Post/... resources APIs.

Beware that webapp's classloader should be able to produce class path
that is used by JSP compiler (Eclipse JCP or by external javac
depending on how your Tomcat is configured). Tomcat WebappClassLoader
has API for that.

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org