1 57 58 package org.apache.commons.discovery.resource; 59 60 import java.util.Vector ; 61 62 import org.apache.commons.discovery.jdk.JDKHooks; 63 64 65 75 public class ClassLoaders 76 { 77 protected Vector classLoaders = new Vector (); 78 79 81 public ClassLoaders() { 82 } 83 84 public int size() { 85 return classLoaders.size(); 86 } 87 88 public ClassLoader get(int idx) { 89 return (ClassLoader )classLoaders.elementAt(idx); 90 } 91 92 97 public void put(ClassLoader classLoader) { 98 if (classLoader != null) { 99 classLoaders.addElement(classLoader); 100 } 101 } 102 103 104 113 public void put(ClassLoader classLoader, boolean prune) { 114 if (classLoader != null && !(prune && isAncestor(classLoader))) { 115 classLoaders.addElement(classLoader); 116 } 117 } 118 119 120 133 public boolean isAncestor(final ClassLoader classLoader) { 134 135 if (classLoader == null) 136 return true; 137 138 for (int idx = 0; idx < size(); idx++) { 139 for(ClassLoader walker = get(idx); 140 walker != null; 141 walker = walker.getParent()) 142 { 143 if (walker == classLoader) { 144 return true; 145 } 146 } 147 } 148 return false; 149 } 150 151 152 171 public static ClassLoaders getLibLoaders(Class spi, Class factory, boolean prune) { 172 ClassLoaders loaders = new ClassLoaders(); 173 174 loaders.put(spi.getClassLoader()); 175 loaders.put(factory.getClassLoader(), prune); 176 loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(), prune); 177 178 return loaders; 179 } 180 181 201 public static ClassLoaders getAppLoaders(Class spi, Class factory, boolean prune) { 202 ClassLoaders loaders = new ClassLoaders(); 203 204 loaders.put(JDKHooks.getJDKHooks().getThreadContextClassLoader()); 205 loaders.put(spi.getClassLoader(), prune); 206 loaders.put(factory.getClassLoader(), prune); 207 loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(), prune); 208 209 return loaders; 210 } 211 } 212 | Popular Tags |