一、前言
关于geronimo-kernel的包(2.2)的org.apache.geronimo.kernel.util.ClassLoaderRegistry源码,基于java.lang.ref.WeakReference软引用方式注入ClassLoader类加载器,最有效高效的方式使用jvm的性能来实现类加载器池。
二、源码说明
package org.apache.geronimo.kernel.util;@b@@b@import java.lang.ref.WeakReference;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@public class ClassLoaderRegistry@b@{@b@ private static final List<WeakReference> list = new ArrayList();@b@@b@ public static synchronized List getList()@b@ {@b@ List ret = new ArrayList();@b@ for (int i = 0; i < list.size(); ++i)@b@ if (list.get(i) != null) {@b@ ret.add((ClassLoader)((WeakReference)list.get(i)).get());@b@ }@b@ else@b@ list.remove(i);@b@@b@ return ret;@b@ }@b@@b@ public static synchronized boolean add(ClassLoader cloader) {@b@ if (contains(cloader))@b@ return false;@b@@b@ return list.add(new WeakReference(cloader));@b@ }@b@@b@ public static synchronized boolean contains(ClassLoader cloader) {@b@ for (int i = 0; i < list.size(); ++i) {@b@ WeakReference wk = (WeakReference)list.get(i);@b@ if (wk.get() == null) {@b@ list.remove(i);@b@ }@b@ else if (wk.get().equals(cloader))@b@ return true;@b@ }@b@ return false;@b@ }@b@@b@ public static synchronized boolean remove(ClassLoader cloader) {@b@ boolean result = false;@b@ for (int i = 0; i < list.size(); ++i) {@b@ WeakReference wk = (WeakReference)list.get(i);@b@ if (wk.get() == null) { list.remove(i);@b@ } else if (wk.get().equals(cloader)) {@b@ list.remove(i);@b@ result = true;@b@ }@b@ }@b@ return result;@b@ }@b@}