FAQ

On 4/17/07, Christopher Schultz wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Craig,

Craig McClanahan wrote:
The naive way for a library to do this is:

String className = ...; // Calculate the name of the class you want
Class clazz = Class.forName(className);

* If commons-foo.jar is stored in a shared classloader provided by your
container, you'll get a ClassNotFoundException. That happens because
Class.forName() and friends start from the classloader that loaded the
calling class itself (i.e. the commons-foo class doing this work) ... but
your application class is not visible because you can only look *up* a
classloader hierarchy, not down.
Can you briefly explain how struts even works when loaded from a shared
ClassLoader? My reading of this suggests that shared struts would not be
able to load actions found in the webapp's ClassLoader(s), but it sounds
like the OP has done this successfully.
Yep ... it's magic :-).

Actually, what happens is that the servlet container provides a
mechanism to acquire the ClassLoader instance for the web application
itself, from which you can load application classes no matter where
the caller is located. For instance, when Struts loads an action
class, it does something like this (plus some exception catching, of
course):

String actionClassName = ...;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class actionClass = cl.loadClass(actionClassName);

The container guarantees that the context class loader for a thread is
set correctly, before it enters your servlet, filter, or listener.
I'm just curious how it is done because you say above that this is a
naive implementation... how does one do this in a smart way? It's not
too relevant to the question... just thought I'd take the opportunity to
find out since I've never heard of a good way to search "down" the
ClassLoader hierarchy.
There is indeed no way to search down the hierarchy, which is why the
context class loader convention was created. As you examine libraries
for potentially putting them into a shared class loader, look for code
that does this kind of thing.
Thanks,
- -chris
Craig

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGJMBM9CaO5/Lv0PARAqcPAJ9vnk8pj9El1gWtmqPv2smLYkSyxgCbBjOx
1nEpmu62YlqqDaqjHYLEkN8=
=HxZg
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 9 | next ›
Discussion Overview
groupuser @
categoriesstruts
postedApr 17, '07 at 2:12a
activeApr 20, '07 at 3:30p
posts9
users4
websitestruts.apache.org
irc#struts

People

Translate

site design / logo © 2017 Grokbase