001: /*
002: GNUJSP - a free JSP implementation
003: Copyright (C) 1998-1999, Vincent Partington <vinny@klomp.org>
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.gjt.jsp;
021:
022: import java.io.DataInputStream;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.IOException;
026: import java.util.Hashtable;
027:
028: import javax.servlet.ServletConfig;
029:
030: public class JSPClassLoader extends ClassLoader {
031: private File repository;
032: private ServletConfig config;
033: private boolean debug;
034: private Hashtable classCache;
035:
036: public JSPClassLoader(File repository, ServletConfig config,
037: boolean debug) {
038: this .repository = repository;
039: this .config = config;
040: this .debug = debug;
041: this .classCache = new Hashtable();
042: }
043:
044: protected Class loadClass(String className, boolean resolve)
045: throws ClassNotFoundException {
046: Class c = (Class) classCache.get(className);
047:
048: if (c == null) {
049: byte[] classData = loadClassData(className);
050: if (classData != null) {
051: c = defineClass(className, classData, 0,
052: classData.length);
053: if (debug) {
054: config.getServletContext().log(
055: className + " loaded by JSP class loader");
056: }
057: } else {
058: ClassLoader cl = JSPServlet.class.getClassLoader();
059: if (cl == null) {
060: c = findSystemClass(className);
061: if (debug) {
062: config
063: .getServletContext()
064: .log(
065: className
066: + " loaded by system class loader");
067: }
068: } else {
069: c = cl.loadClass(className);
070: if (debug) {
071: if (c.getClassLoader() == null) {
072: config
073: .getServletContext()
074: .log(
075: className
076: + " loaded by system class loader (through servlet class loader)");
077: } else {
078: config
079: .getServletContext()
080: .log(
081: className
082: + " loaded by servlet class loader");
083: }
084: }
085: }
086:
087: if (c == null) {
088: if (debug) {
089: config.getServletContext().log(
090: "failed to load " + className);
091: }
092: return null;
093: }
094: }
095: classCache.put(className, c);
096: }
097:
098: if (resolve) {
099: resolveClass(c);
100: }
101:
102: return c;
103: }
104:
105: private byte[] loadClassData(String className) {
106: File classFile;
107: DataInputStream in;
108: byte[] classData;
109:
110: if (!className.startsWith("_jsp.")) {
111: return null;
112: }
113:
114: classFile = JSPCompiler.getFileForClass(repository, className,
115: ".class");
116: try {
117: in = new DataInputStream(new FileInputStream(classFile));
118: try {
119: classData = new byte[(int) classFile.length()];
120: in.readFully(classData, 0, classData.length);
121: return classData;
122: } finally {
123: in.close();
124: }
125: } catch (IOException ioexc) {
126: ;
127: }
128:
129: return null;
130: }
131: }
|