1 23 24 package org.objectweb.medor.optim.lib; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.util.HashSet ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 33 43 public class DirsCompileClassLoader extends ClassLoader { 44 45 private long bytecount; 46 private Hashtable cache = new Hashtable (); 47 private HashSet pathList = new HashSet (); 48 49 public DirsCompileClassLoader(HashSet classpaths) { 50 super(); 51 pathList = classpaths; 52 } 53 54 private byte[] loadClassData(String name) throws ClassNotFoundException { 56 57 byte[] data = null; 58 try { 59 String path = getJavaPathClass(name); 60 File cf = new File (path + File.separatorChar + 61 name.replace('.', File.separatorChar) + ".class"); 62 FileInputStream in = new FileInputStream (cf); 63 bytecount = cf.length(); 64 if (bytecount > 0) { 65 data = new byte[(int) bytecount]; 66 in.read(data); 67 in.close(); 68 } 69 } catch (IOException ioexp) { 70 throw new ClassNotFoundException (name); 71 } 72 return (data); 73 } 74 75 public synchronized Class loadClass(String name, boolean resolve) 76 throws ClassNotFoundException { 77 Class c = (Class ) cache.get(name); 78 System.out.println("Loading ..." + name); 79 if (c == null) { 80 try { 84 System.out.println("First try default System loading class... "); 85 c = this.findSystemClass(name); } catch (ClassNotFoundException e) { 87 System.err.println("System loading... failed when loading " + name); 88 } 89 if (c == null) { 90 if ((!isClass(name)) && (isJavaSource(name))) { 92 Runtime rt = Runtime.getRuntime(); 93 Process p; 94 try { 96 System.out.println("Launching javac ..." + pathList); 97 String classpath = ""; 98 Iterator i = pathList.iterator(); 99 if (i.hasNext()) classpath = 100 classpath + ((String ) i.next()); 101 while (i.hasNext()) { 102 classpath = classpath + ";" + ((String ) i.next()); 104 } 105 106 String cmd = "javac -classpath " + classpath + " " + 107 getJavaSourcePath(name) + File.separatorChar + 108 name.replace('.', File.separatorChar) + ".java"; 109 System.out.println(cmd); 110 p = rt.exec(cmd); 111 p.waitFor(); 112 System.out.println("Compilation finisched ..."); 113 114 } catch (IOException e) { 115 System.err.println(" Compilation failed... " + e.getMessage()); 116 117 118 } catch (InterruptedException e) { 119 System.err.println(" Compilation failed... " + e.getMessage()); 120 } 121 122 c = loadClass(name, resolve); 123 } else if (isClass(name)) { 124 125 System.err.println("Try loading class data bytes... " + name); 126 byte[] data = loadClassData(name); 127 128 if (data != null) { 129 System.out.println("Byte class loaded...try to define class now"); 130 c = defineClass(name, data, 0, (int) bytecount); 131 if (resolve && c != null) { 132 resolveClass(c); 133 } 134 } 135 } else 136 throw new ClassNotFoundException (name); 137 } 139 } 140 141 142 if (c != null) 143 cache.put(name, c); 144 else 145 throw new ClassNotFoundException (name); 146 return c; 147 } 148 149 private boolean isClass(String name) { 150 boolean isClass = false; 151 Iterator it = pathList.iterator(); 152 String dir = null; 153 while ((it.hasNext()) & (!isClass)) { 154 dir = (String ) it.next(); 155 File cf = new File (dir + File.separatorChar + 157 name.replace('.', File.separatorChar) + ".class"); 158 if (cf.exists()) isClass = true; 159 160 } 161 return isClass; 162 } 163 164 private boolean isJavaSource(String name) { 165 boolean isSource = false; 166 Iterator it = pathList.iterator(); 167 String dir = null; 168 while (it.hasNext() && (!isSource)) { 169 dir = (String ) it.next(); 170 File cf = new File (dir + File.separator + 172 name.replace('.', File.separatorChar) + ".java"); 173 if (cf.exists()) isSource = true; 174 175 } 176 return isSource; 177 } 178 179 private String getJavaSourcePath(String name) { 180 Iterator it = pathList.iterator(); 181 String dir = null; 182 boolean trouv = false; 183 while ((it.hasNext()) && (!trouv)) { 184 dir = (String ) it.next(); 185 File sf = new File (dir + File.separatorChar + 187 name.replace('.', File.separatorChar) + 188 ".java"); 189 if (sf.exists()) trouv = true; 190 } 191 if (trouv) 192 return dir; 193 else 194 return null; 195 } 196 197 private String getJavaPathClass(String name) { 198 Iterator it = pathList.iterator(); 199 String dir = null; 200 boolean trouv = false; 201 while ((it.hasNext()) && (!trouv)) { 202 dir = (String ) it.next(); 203 File sf = new File (dir + File.separatorChar + 205 name.replace('.', File.separatorChar) + ".class"); 206 if (sf.exists()) trouv = true; 207 } 208 if (trouv) 209 return dir; 210 else 211 return null; 212 } 213 } 214 | Popular Tags |