001: package com.quadcap.io.dir;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Enumeration;
042: import java.util.Hashtable;
043: import java.util.Vector;
044:
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.InputStream;
048: import java.io.IOException;
049:
050: import java.net.URL;
051:
052: import com.quadcap.io.IO;
053:
054: /**
055: * This class implements a JSP class loader
056: *
057: * @author Stan Bailes
058: */
059: public class ClassLoader extends java.lang.ClassLoader {
060: Directory root;
061: Vector jars = new Vector();
062:
063: public ClassLoader(Directory root) {
064: super ();
065: this .root = root;
066: try {
067: init();
068: } catch (IOException ex) {
069: }
070: }
071:
072: public Class loadClass(String name) throws ClassNotFoundException {
073: return loadClass(name, true);
074: }
075:
076: // reverse the normal order....
077: public synchronized Class loadClass(String name, boolean resolve)
078: throws ClassNotFoundException {
079: // First, check if the class has already been loaded
080: Class c = findLoadedClass(name);
081: if (c == null) {
082: try {
083: c = findClass(name);
084: } catch (ClassNotFoundException e) {
085: c = super .loadClass(name, resolve);
086: }
087: }
088: if (resolve) {
089: resolveClass(c);
090: }
091: return c;
092: }
093:
094: public Class findClass(String name) throws ClassNotFoundException {
095: byte[] b = loadClassData(name);
096: return defineClass(name, b, 0, b.length, this .getClass()
097: .getProtectionDomain());
098: }
099:
100: public URL findResource(String name) {
101: URL url = null;
102: try {
103: String fileName = "META-INF" + name;
104: for (int i = 0; url == null && i < jars.size(); i++) {
105: Directory d = (Directory) jars.elementAt(i);
106: url = d.getURL(name);
107: }
108: } catch (Throwable t) {
109: url = null;
110: }
111: return url;
112: }
113:
114: private byte[] loadClassData(String name)
115: throws ClassNotFoundException {
116: try {
117: Entry classFile = locateClassFile(name);
118: byte[] b = new byte[(int) (classFile.getSize())];
119: InputStream f = classFile.getInputStream();
120: IO.readFully(f, b);
121: f.close();
122: return b;
123: } catch (IOException e) {
124: throw new ClassNotFoundException("error loading class: "
125: + e.toString());
126: }
127: }
128:
129: final Entry locateClassFile(String name)
130: throws ClassNotFoundException {
131: String className = name.replace('.', '/') + ".class";
132: Entry classFile = null;
133: for (int i = 0; classFile == null && i < jars.size(); i++) {
134: Directory d = (Directory) jars.elementAt(i);
135: classFile = d.getEntry(className);
136: }
137: if (classFile == null) {
138: throw new ClassNotFoundException("not found: " + name);
139: }
140: return classFile;
141: }
142:
143: static int tmpCount = 0;
144:
145: public String getClassPath() {
146: StringBuffer sb = new StringBuffer();
147: String delim = System.getProperty("path.separator");
148: for (int i = 0; i < jars.size(); i++) {
149: Directory d = (Directory) jars.elementAt(i);
150: if (i > 0)
151: sb.append(delim);
152: sb.append(d.getRootPath());
153: }
154: return sb.toString();
155: }
156:
157: final void init() throws IOException {
158: Enumeration e = root.entries();
159: Directory classes = null;
160: File tmpClasses = null;
161: while (e.hasMoreElements()) {
162: String path = e.nextElement().toString();
163: if (path.endsWith(".jar")) {
164: Directory d = null;
165: String realPath = root.getRealPath(path);
166: if (realPath == null) {
167: throw new IOException("No Path: " + path);
168: }
169: d = Directory.getDirectory(new File(realPath));
170: jars.addElement(d);
171: }
172: }
173: }
174: }
|