Quadcap Embeddable Database

com/quadcap/io/dir/ClassLoader.java

Go to the documentation of this file.
00001 package com.quadcap.io.dir; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Enumeration; 00042 import java.util.Hashtable; 00043 import java.util.Vector; 00044 00045 import java.io.File; 00046 import java.io.FileOutputStream; 00047 import java.io.InputStream; 00048 import java.io.IOException; 00049 00050 import java.net.URL; 00051 00052 import com.quadcap.io.IO; 00053 00054 00055 /** 00056 * This class implements a JSP class loader 00057 * 00058 * @author Stan Bailes 00059 */ 00060 public class ClassLoader extends java.lang.ClassLoader { 00061 Directory root; 00062 Vector jars = new Vector(); 00063 00064 public ClassLoader(Directory root) { 00065 super(); 00066 this.root = root; 00067 try { 00068 init(); 00069 } catch (IOException ex) { 00070 } 00071 } 00072 00073 00074 public Class loadClass(String name) throws ClassNotFoundException { 00075 return loadClass(name, true); 00076 } 00077 00078 // reverse the normal order.... 00079 public synchronized Class loadClass(String name, boolean resolve) 00080 throws ClassNotFoundException 00081 { 00082 // First, check if the class has already been loaded 00083 Class c = findLoadedClass(name); 00084 if (c == null) { 00085 try { 00086 c = findClass(name); 00087 } catch (ClassNotFoundException e) { 00088 c = super.loadClass(name, resolve); 00089 } 00090 } 00091 if (resolve) { 00092 resolveClass(c); 00093 } 00094 return c; 00095 } 00096 00097 public Class findClass(String name) throws ClassNotFoundException { 00098 byte[] b = loadClassData(name); 00099 return defineClass(name, b, 0, b.length, 00100 this.getClass().getProtectionDomain()); 00101 } 00102 00103 public URL findResource(String name) { 00104 URL url = null; 00105 try { 00106 String fileName = "META-INF" + name; 00107 for (int i = 0; url == null && i < jars.size(); i++) { 00108 Directory d = (Directory)jars.elementAt(i); 00109 url = d.getURL(name); 00110 } 00111 } catch (Throwable t) { 00112 url = null; 00113 } 00114 return url; 00115 } 00116 00117 private byte[] loadClassData(String name) throws ClassNotFoundException { 00118 try { 00119 Entry classFile = locateClassFile(name); 00120 byte[] b = new byte[(int)(classFile.getSize())]; 00121 InputStream f = classFile.getInputStream(); 00122 IO.readFully(f, b); 00123 f.close(); 00124 return b; 00125 } catch (IOException e) { 00126 throw new ClassNotFoundException("error loading class: " + 00127 e.toString()); 00128 } 00129 } 00130 00131 final Entry locateClassFile(String name) throws ClassNotFoundException { 00132 String className = name.replace('.', '/') + ".class"; 00133 Entry classFile = null; 00134 for (int i = 0; classFile == null && i < jars.size(); i++) { 00135 Directory d = (Directory)jars.elementAt(i); 00136 classFile = d.getEntry(className); 00137 } 00138 if (classFile == null) { 00139 throw new ClassNotFoundException("not found: " + name); 00140 } 00141 return classFile; 00142 } 00143 00144 static int tmpCount = 0; 00145 00146 public String getClassPath() { 00147 StringBuffer sb = new StringBuffer(); 00148 String delim = System.getProperty("path.separator"); 00149 for (int i = 0; i < jars.size(); i++) { 00150 Directory d = (Directory)jars.elementAt(i); 00151 if (i > 0) sb.append(delim); 00152 sb.append(d.getRootPath()); 00153 } 00154 return sb.toString(); 00155 } 00156 00157 final void init() throws IOException { 00158 Enumeration e = root.entries(); 00159 Directory classes = null; 00160 File tmpClasses = null; 00161 while (e.hasMoreElements()) { 00162 String path = e.nextElement().toString(); 00163 if (path.endsWith(".jar")) { 00164 Directory d = null; 00165 String realPath = root.getRealPath(path); 00166 if (realPath == null) { 00167 throw new IOException("No Path: " + path); 00168 } 00169 d = Directory.getDirectory(new File(realPath)); 00170 jars.addElement(d); 00171 } 00172 } 00173 } 00174 } 00175