1 18 19 package sync4j.syncclient.common; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 import java.util.zip.ZipEntry ; 28 import java.util.zip.ZipFile ; 29 30 import sync4j.syncclient.common.logging.Logger; 31 32 42 43 public class SimpleClassLoader extends ClassLoader { 44 45 47 48 private String workingDirectory = null; 49 50 private Logger logger = new Logger(); 51 52 54 60 public SimpleClassLoader(String workingDirectory) { 61 File file = new File (workingDirectory); 62 this.workingDirectory = workingDirectory; 63 64 if (logger.isLoggable(Logger.INFO)) { 65 logger.info("WorkingDirectory classloader: " + 66 file.getAbsolutePath() ); 67 68 } 69 70 } 71 72 74 75 public String getWorkingDirectory() { 76 return this.workingDirectory; 77 } 78 79 83 public Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 84 85 byte[] b = null; 86 87 Class classLoaded = null; 88 89 try { 90 b = loadClassData(name); 91 } catch (Exception ex) { 92 throw new ClassNotFoundException ("Class " + name + " not found: " + ex); 93 } 94 95 if (b==null) { 96 classLoaded = findSystemClass(name); 97 } else { 98 classLoaded = defineClass(name, b, 0, b.length); 99 } 100 101 return classLoaded; 102 } 103 104 106 116 private byte[] loadClassData(String name) throws Exception { 117 byte[] content = null; 118 119 int index = name.lastIndexOf("."); 120 121 if (index == -1) { 122 throw new Exception ("Java class name not recognized"); 123 } 124 125 String sPackage = name.substring(0, index); name = name.substring(index+1, name.length()); 128 content = loadInDirectory(workingDirectory, sPackage, name); 130 131 return content; 132 } 133 134 144 private byte[] loadInDirectory(String dirName, 145 String packageName, String javaClassName) 146 throws IOException { 147 byte[] content = null; 148 149 StringTokenizer stPackage = new StringTokenizer (packageName, "."); 151 152 while (stPackage.hasMoreTokens()) { 153 dirName = dirName + File.separator + stPackage.nextToken(); 154 } 155 156 File fileClass = new File (dirName + File.separator 158 + javaClassName + ".class"); 159 if (fileClass.isFile()) { 160 161 if (logger.isLoggable(Logger.INFO)) { 162 logger.info("Class found in: " + dirName); 163 } 164 165 int size = (int)fileClass.length(); 166 content = new byte[size]; 167 168 FileInputStream fIstream = new FileInputStream (fileClass); 169 fIstream.read(content); 170 fIstream.close(); 171 } 172 173 return content; 174 } 175 176 } | Popular Tags |