1 18 package org.apache.tools.ant.util; 19 20 import java.io.File ; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.launch.Locator; 23 24 26 30 public class LoaderUtils { 31 32 33 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 34 35 41 public static void setContextClassLoader(ClassLoader loader) { 42 Thread currentThread = Thread.currentThread(); 43 currentThread.setContextClassLoader(loader); 44 } 45 46 47 53 public static ClassLoader getContextClassLoader() { 54 Thread currentThread = Thread.currentThread(); 55 return currentThread.getContextClassLoader(); 56 } 57 58 64 public static boolean isContextLoaderAvailable() { 65 return true; 66 } 67 68 75 private static File normalizeSource(File source) { 76 if (source != null) { 77 try { 78 source = FILE_UTILS.normalize(source.getAbsolutePath()); 79 } catch (BuildException e) { 80 } 82 } 83 84 return source; 85 } 86 87 96 public static File getClassSource(Class c) { 97 return normalizeSource(Locator.getClassSource(c)); 98 } 99 100 111 public static File getResourceSource(ClassLoader c, String resource) { 112 if (c == null) { 113 c = LoaderUtils.class.getClassLoader(); 114 } 115 return normalizeSource(Locator.getResourceSource(c, resource)); 116 } 117 118 124 public static String classNameToResource(String className) { 125 return className.replace('.', '/') + ".class"; 126 } 127 128 135 public static boolean classExists(ClassLoader loader, String className) { 136 return loader.getResource(classNameToResource(className)) != null; 137 } 138 } 139 140 | Popular Tags |