|
| 1 | +package com.sandwich.util.io; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.net.URL; |
| 8 | +import java.net.URLConnection; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Map.Entry; |
| 12 | + |
| 13 | +public class DynamicClassLoader extends ClassLoader { |
| 14 | + |
| 15 | + private static Map<URL, Class<?>> classesByLocation = new HashMap<URL, Class<?>>(); |
| 16 | + private static Map<Class<?>, URL> locationByClass = new HashMap<Class<?>, URL>(); |
| 17 | + |
| 18 | + private final FileMonitor fileMonitor; |
| 19 | + private final String binDir, sourceDir; |
| 20 | + private final String[] classPath; |
| 21 | + |
| 22 | + public DynamicClassLoader(String binDir, String sourceDir, String[] classPath, FileMonitor fileMonitor){ |
| 23 | + this(binDir, sourceDir, classPath, fileMonitor, ClassLoader.getSystemClassLoader()); |
| 24 | + } |
| 25 | + |
| 26 | + public DynamicClassLoader(String binDir, String sourceDir, String[] classPath, |
| 27 | + FileMonitor fileMonitor, ClassLoader parent) { |
| 28 | + super(parent); |
| 29 | + this.fileMonitor = fileMonitor; |
| 30 | + this.binDir = binDir; |
| 31 | + this.sourceDir = sourceDir; |
| 32 | + this.classPath = classPath; |
| 33 | + } |
| 34 | + |
| 35 | + public static void remove(URL url){ |
| 36 | + String urlToString = url.toString().replace(FileCompiler.CLASS_SUFFIX, "").replace(FileCompiler.JAVA_SUFFIX, ""); |
| 37 | + for(Entry<URL, Class<?>> entry : classesByLocation.entrySet()){ |
| 38 | + if(entry.getKey().toString().contains(urlToString)){ |
| 39 | + locationByClass.remove(entry.getValue()); |
| 40 | + entry.setValue(null); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public static void remove(Class<?> clas){ |
| 46 | + for(Entry<Class<?>, URL> entry : locationByClass.entrySet()){ |
| 47 | + if(entry.getKey().getName().contains(clas.getName())){ |
| 48 | + classesByLocation.remove(entry.getValue()); |
| 49 | + entry.setValue(null); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public Class<?> loadClass(String className){ |
| 55 | + return loadClass(className, FileCompilerAction.LOGGING_HANDLER); |
| 56 | + } |
| 57 | + |
| 58 | + public Class<?> loadClass(String className, CompilationListener listener){ |
| 59 | + String fileSeparator = System.getProperty("file.separator"); |
| 60 | + String fileName = binDir + fileSeparator |
| 61 | + + className.replace(".", fileSeparator) |
| 62 | + + FileCompiler.CLASS_SUFFIX; |
| 63 | + File classFile = new File(fileName); |
| 64 | + try { |
| 65 | + // file may have never been compiled, go ahead and compile it now |
| 66 | + File sourceFile = FileCompiler.classToSource(binDir, sourceDir,classFile); |
| 67 | + if(classFile.exists()){ |
| 68 | + String absolutePath = classFile.getAbsolutePath(); |
| 69 | + boolean isAnonymous = absolutePath.contains("$"); |
| 70 | + if(fileMonitor.isFileModifiedSinceLastPoll(sourceFile.getAbsolutePath(), sourceFile.lastModified())){ |
| 71 | + if(!isAnonymous){ |
| 72 | + compile(className, fileName, sourceFile, listener); |
| 73 | + } |
| 74 | + } |
| 75 | + return loadClass(classFile.toURI().toURL(), className); |
| 76 | + } |
| 77 | + try{ |
| 78 | + return super.loadClass(className); |
| 79 | + }catch(ClassNotFoundException x){ |
| 80 | + compile(className, fileName, sourceFile, listener); |
| 81 | + classFile = new File(fileName); |
| 82 | + return loadClass(classFile.toURI().toURL(), className); |
| 83 | + } |
| 84 | + } catch (Exception e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void compile(String className, String fileName, File sourceFile, CompilationListener listener) |
| 90 | + throws IOException { |
| 91 | + FileCompiler.compile(sourceFile, new File(binDir), listener, classPath); |
| 92 | + fileMonitor.updateFileSaveTime(sourceFile); |
| 93 | + } |
| 94 | + |
| 95 | + public Class<?> loadClass(URL url, String className){ |
| 96 | + Class<?> clazz = classesByLocation.get(url); |
| 97 | + if(clazz != null){ |
| 98 | + return clazz; |
| 99 | + } |
| 100 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 101 | + try { |
| 102 | + URLConnection connection = url.openConnection(); |
| 103 | + InputStream input = connection.getInputStream(); |
| 104 | + int data = input.read(); |
| 105 | + while(data != -1){ |
| 106 | + buffer.write(data); |
| 107 | + data = input.read(); |
| 108 | + } |
| 109 | + input.close(); |
| 110 | + } catch (IOException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } |
| 113 | + |
| 114 | + byte[] classData = buffer.toByteArray(); |
| 115 | + clazz = defineClass(className, classData, 0, classData.length); |
| 116 | + classesByLocation.put(url, clazz); |
| 117 | + locationByClass.put(clazz, url); |
| 118 | + return clazz; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public DynamicClassLoader clone(){ |
| 123 | + return new DynamicClassLoader(binDir, sourceDir, classPath, fileMonitor); |
| 124 | + } |
| 125 | +} |
0 commit comments