001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.platform.extension; 005 006 import graphlab.platform.StaticUtils; 007 008 import java.io.File; 009 import java.io.FileInputStream; 010 import java.io.FileNotFoundException; 011 import java.io.IOException; 012 import java.net.URL; 013 import java.net.URLClassLoader; 014 import java.util.*; 015 016 /** 017 * @author azin azadi 018 019 */ 020 public class ExtensionClassLoader extends ClassLoader { 021 public Map<String, byte[]> classesData = new HashMap<String, byte[]>(); 022 Map<String, Class> classes = new HashMap<String, Class>(); 023 Vector<File> unknownFiles = new Vector<File>(); 024 public static URLClassLoader classLoader; 025 public static ClassLoader cl; 026 027 public ExtensionClassLoader(String dirPath) { 028 loadClasses(dirPath); 029 } 030 031 public Collection<Class> getLoadedClasses() { 032 return classes.values(); 033 } 034 035 public Vector<File> getUnknownFilesFound() { 036 return unknownFiles; 037 } 038 039 040 Vector<URL> urls; 041 042 private void loadClassFiles(String dir, String pack) { 043 urls = new Vector<URL>(); 044 File file = new File(dir); 045 if (!file.exists()) 046 return; 047 File files[] = file.listFiles(); 048 for (File file1 : files) 049 if (file1.isDirectory()) 050 loadClassFiles(dir + "/" + file1.getName(), pack + "." + file1.getName()); 051 else if (file1.getName().endsWith(".class")) 052 try { 053 String name; 054 if (pack.length() > 0) 055 name = pack.substring(1) + "."; 056 else 057 name = ""; 058 name = name + file1.getName().substring(0, file1.getName().length() - 6); 059 byte data[] = new byte[(int) file1.length()]; 060 FileInputStream fis = new FileInputStream(file1); 061 fis.read(data); 062 classesData.put(name, data); 063 urls.add(file1.toURL()); 064 } 065 catch (FileNotFoundException e) { 066 e.printStackTrace(); 067 // ExceptionHandler.handleException(e); 068 } 069 catch (IOException e) { 070 e.printStackTrace(); 071 // ExceptionHandler.handleException(e); 072 } 073 else { 074 unknownFiles.add(file1); 075 } 076 077 } 078 079 private void loadClasses(String dirPath) { 080 loadClassFiles(dirPath, ""); 081 // defineClasses(); 082 } 083 084 public Class findClass(String name) throws ClassNotFoundException { 085 Class ret = null; 086 if (!classesData.containsKey(name)) 087 ret = getParent().loadClass(name); 088 if (ret == null && !classes.containsKey(name)) { 089 byte data[] = (byte[]) classesData.get(name); 090 Class c = defineClass(name, data, 0, data.length); 091 classes.put(name, c); 092 } 093 ret = classes.get(name); 094 if (ret == null) 095 return classLoader.loadClass(name); 096 else 097 return ret; 098 } 099 100 public Collection getClassesImplementing(Class cl) { 101 Collection col = new Vector(); 102 for (Map.Entry<String, Class> entry1 : classes.entrySet()) { 103 Map.Entry entry = (Map.Entry) entry1; 104 Class c = (Class) entry.getValue(); 105 if (StaticUtils.isImplementing(c, cl)) 106 col.add(c); 107 } 108 109 return col; 110 } 111 112 }