Hi guys,
I'd like to contribute two new classes to the JDK7 and add a new
package-visible field to the ClassLoader. These classes would be meant for the
framework and server developers to allow for greater control over the
references between classes in different class loaders. Specifically the goal
would be to prevent the notoriously common classloader leaks. The motivation is
partially described here:
http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/
That blog post also describes a way to backport it to older Java versions,
unfortunately we found that the described approach will not work in some
esoteric cases.
The pseudo-code follows. It's not really optimized or even tested and is only
meant to illustrate the functionality and complexity of the proposal. I'd like
to hear your feedback on this.
class ClassLoader {
Map localMap = new HashMap();
//...
}
public class ClassLoaderLocal {
private Object key = new Object();
public Object get(ClassLoader cl) {
cl.localMap.get(key);
}
public void set(ClassLoader cl, Object value) {
cl.localMap.put(key, value);
}
}
public class ClassLoaderWeakReference extends Reference {
private final WeakReference clRef;
private final ClassLoaderLocal cll = new ClassLoaderLocal();
public ClassLoaderWeakReference(Object target) {
clRef = new WeakReference(target.getClass().getClassLoader());
cll.set(target.getClass().getClassLoader(), target);
}
public Object get() {
if (clRef.get() == null)
return null;
return cll.get((ClassLoader) clRef.get());
}
}
Jevgeni Kabanov: Founder & CTO at ZeroTurnaround
[email protected] | Skype: ekabanov | http://twitter.com/ekabanov
"jrebel is r0x: http://bit.ly/6fRGji" - mschambeck