1 12 package org.eclipse.ant.internal.core; 13 14 15 import java.io.IOException ; 16 import java.net.URL ; 17 import java.net.URLClassLoader ; 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.List ; 22 23 public class AntClassLoader extends URLClassLoader { 24 25 private static final String ANT_PACKAGES_PREFIX = "org.apache.tools"; private static final String ANT_URL_PREFIX = "org/apache/tools"; 28 private boolean fAllowPluginLoading = false; 29 30 protected ClassLoader [] fPluginLoaders; 31 32 private ClassLoader fContextClassloader = null; 33 34 public AntClassLoader(URL [] urls, ClassLoader [] pluginLoaders) { 35 super(urls, ClassLoader.getSystemClassLoader()); 36 fPluginLoaders = pluginLoaders; 37 } 38 39 42 protected Class findClass(String name) throws ClassNotFoundException { 43 Class result = null; 44 if (fAllowPluginLoading || !(name.startsWith(ANT_PACKAGES_PREFIX))) { 47 result = loadClassPlugins(name); 48 } 49 50 if (result != null) { 51 return result; 52 } 53 54 return super.findClass(name); 55 } 56 57 protected Class loadClassPlugins(String name) { 58 ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); 61 if (fContextClassloader != null) { 62 Thread.currentThread().setContextClassLoader(fContextClassloader); 63 } 64 try { 65 Class result = null; 66 if (fPluginLoaders != null) { 67 for (int i = 0; (i < fPluginLoaders.length) && (result == null); i++) { 68 try { 69 result = fPluginLoaders[i].loadClass(name); 70 } catch (ClassNotFoundException e) { 71 } 74 } 75 } 76 return result; 77 } finally { 78 Thread.currentThread().setContextClassLoader(originalClassLoader); 79 } 80 } 81 82 85 public URL findResource(String name) { 86 if (fAllowPluginLoading || !(name.startsWith(ANT_URL_PREFIX))) { 87 URL result = findResourcePlugins(name); 88 if (result != null) { 89 return result; 90 } 91 } 92 93 return super.findResource(name); 94 } 95 96 private URL findResourcePlugins(String name) { 97 ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); 100 if (fContextClassloader != null) { 101 Thread.currentThread().setContextClassLoader(fContextClassloader); 102 } 103 try { 104 URL result = null; 105 if (fPluginLoaders != null) { 106 for (int i = 0; i < fPluginLoaders.length; i++) { 107 result = fPluginLoaders[i].getResource(name); 108 if (result != null) { 109 return result; 110 } 111 } 112 } 113 } finally { 114 Thread.currentThread().setContextClassLoader(originalClassLoader); 115 } 116 return null; 117 } 118 119 122 public Enumeration findResources(String name) throws IOException { 123 ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); 124 if (fContextClassloader != null) { 125 Thread.currentThread().setContextClassLoader(fContextClassloader); 126 } 127 List all = new ArrayList (); 128 try { 129 if (fAllowPluginLoading || !(name.startsWith(ANT_URL_PREFIX) || name.startsWith(ANT_URL_PREFIX, 1))) { 130 if (fPluginLoaders != null) { 131 Enumeration result = null; 132 for (int i = 0; i < fPluginLoaders.length; i++) { 133 result = fPluginLoaders[i].getResources(name); 134 while (result.hasMoreElements()) { 135 all.add(result.nextElement()); 136 } 137 } 138 } 139 } 140 141 Enumeration superResources = super.findResources(name); 142 if (all.isEmpty()) { 143 return superResources; 144 } 145 146 while (superResources.hasMoreElements()) { 147 all.add(superResources.nextElement()); 148 } 149 return Collections.enumeration(all); 150 } finally { 151 Thread.currentThread().setContextClassLoader(originalClassLoader); 152 } 153 } 154 155 162 public void allowPluginClassLoadersToLoadAnt(boolean allowLoading) { 163 fAllowPluginLoading = allowLoading; 164 } 165 166 public void setPluginContextClassloader(ClassLoader classLoader) { 167 fContextClassloader = classLoader; 168 } 169 } | Popular Tags |