1 14 15 package org.quickserver.util; 16 17 import java.util.*; 18 import java.io.*; 19 import java.net.*; 20 import org.quickserver.util.io.*; 21 import java.util.logging.*; 22 23 28 public class ClassUtil { 29 private static Logger logger = Logger.getLogger(ClassUtil.class.getName()); 30 31 39 public static Map loadClass(ClassLoader classLoader, String classNames[]) 40 throws Exception { 41 Class classloded = null; 42 HashMap classHash = new HashMap(); 43 for(int i=0;i<classNames.length;i++) { 44 try { 45 classloded = classLoader.loadClass(classNames[i]); 46 classHash.put(classNames[i], classloded); 47 } catch(Exception e) { 48 logger.warning("Could not load classes : "+e); 49 } 50 } 51 return classHash; 52 } 53 54 59 public static ClassLoader getClassLoaderFromJars(String jarDir) 60 throws Exception { 61 logger.fine("Getting ClassLoader for jars in "+jarDir); 62 File file = new File(jarDir); 63 ArrayList list = new ArrayList(); 64 65 File jars[] =file.listFiles(new JarFileList()); 66 for(int j=0; j<jars.length;j++){ 67 list.add(jars[j].toURL()); 68 } 69 70 Object array[] = list.toArray(); 71 URL jarurl[] = new URL[array.length]; 72 for(int i=0;i<array.length;i++) { 73 jarurl[i] = (URL)array[i]; 74 } 75 76 URLClassLoader classLoader = URLClassLoader.newInstance(jarurl); 77 return classLoader; 78 } 79 80 84 public static ClassLoader getClassLoaderFromJar(String jarPath) 85 throws Exception { 86 File file = new File(jarPath); 87 logger.fine("Getting ClassLoader for "+file.getCanonicalPath()); 88 URL jarurl[] = {file.toURL()}; 89 URLClassLoader classLoader = URLClassLoader.newInstance(jarurl); 90 return classLoader; 91 } 92 93 97 public static ClassLoader getClassLoader(String path) throws Exception { 98 File file = new File(path); 99 if(file.canRead()==false) { 100 logger.warning("Could not read path: "+path); 101 return null; 102 } 103 if(file.isDirectory()) 104 return getClassLoaderFromJars(path); 105 else 106 return getClassLoaderFromJar(path); 107 } 108 } 109 | Popular Tags |