001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.sun.portal.providers.jsp.jasper3.jasper.servlet;
057:
058: import java.io.FileInputStream;
059: import java.io.ByteArrayOutputStream;
060: import java.io.File;
061: import java.io.FileNotFoundException;
062: import java.io.InputStream;
063: import java.io.IOException;
064:
065: import java.util.Hashtable;
066: import java.util.Vector;
067: import java.util.zip.ZipFile;
068: import java.util.zip.ZipEntry;
069: import java.net.URL;
070:
071: import java.security.*;
072:
073: import com.sun.portal.providers.jsp.jasper3.jasper.JasperException;
074: import com.sun.portal.providers.jsp.jasper3.jasper.Constants;
075: import com.sun.portal.providers.jsp.jasper3.jasper.JspCompilationContext;
076: import com.sun.portal.providers.jsp.jasper3.jasper.JspEngineContext;
077: import com.sun.portal.providers.jsp.jasper3.jasper.Options;
078: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.Compiler;
079:
080: import com.sun.portal.providers.jsp.jasper3.tomcat.logging.Logger;
081: import javax.servlet.http.*;
082:
083: /**
084: * This is a class loader that loads JSP files as though they were
085: * Java classes. It calls the compiler to compile the JSP file into a
086: * servlet and then loads the generated class.
087: *
088: * This code is quite fragile and needs careful
089: * treatment/handling/revisiting. I know this doesn't work very well
090: * right now for:
091: *
092: * (a) inner classes
093: * (b) does not work at all for tag handlers that have inner
094: * classes; but that is likely to change with the new JSP PR2
095: * spec.
096: *
097: * @author Anil K. Vijendran
098: * @author Harish Prabandham
099: */
100: public class JasperLoader extends
101: com.sun.portal.providers.jsp.jasper3.jasper.runtime.JspLoader {
102: // ClassLoader parent;
103: // Options options;
104: Object pd;
105:
106: /*
107: * This should be factoried out
108: */
109: public JasperLoader() {
110: super ();
111: }
112:
113: // public void setParentClassLoader( ClassLoader cl)
114: // {
115: // this.parent = cl;
116: // }
117:
118: // public void setOptions( Options options) {
119: // this.options = options;
120: // }
121:
122: public void setProtectionDomain(Object pd) {
123: this .pd = pd;
124: }
125:
126: protected synchronized Class loadClass(String name, boolean resolve)
127: throws ClassNotFoundException {
128: if (debug > 0)
129: log("load " + name);
130: // First, check if the class has already been loaded
131: Class c = findLoadedClass(name);
132: if (c == null) {
133: try {
134: if (parent != null) {
135: if (debug > 0)
136: log("load from parent " + name);
137: c = parent.loadClass(name);
138: } else {
139: if (debug > 0)
140: log("load from system " + name);
141: c = findSystemClass(name);
142: }
143: } catch (ClassNotFoundException e) {
144: // If still not found, then call findClass in order
145: // to find the class.
146: try {
147: if (debug > 0)
148: log("local jsp loading " + name);
149: c = findClass(name);
150: } catch (ClassNotFoundException ex) {
151: throw ex;
152: }
153: }
154: }
155: if (resolve) {
156: resolveClass(c);
157: }
158: return c;
159: }
160:
161: public InputStream getResourceAsStream(String name) {
162: if (debug > 0)
163: log("getResourcesAsStream()" + name);
164: URL url = getResource(name);
165: try {
166: return url != null ? url.openStream() : null;
167: } catch (IOException e) {
168: return null;
169: }
170: }
171:
172: public URL getResource(String name) {
173: if (debug > 0)
174: log("getResource() " + name);
175: if (parent != null)
176: return parent.getResource(name);
177: return super .getResource(name);
178: }
179:
180: private static final int debug = 0;
181:
182: private void log(String s) {
183: System.out.println("JspLoader: " + s);
184: }
185:
186: protected Class findClass(String className)
187: throws ClassNotFoundException {
188: try {
189: if (!className.startsWith(Constants.JSP_DIR)) {
190: // this is a class that the JSP file depends on
191: // (example: class in a tag library)
192: byte[] classBytes = loadClassDataFromJar(className);
193: if (classBytes == null)
194: throw new ClassNotFoundException(className);
195: return defClass(className, classBytes);
196: } else {
197: String fileName = null;
198: String outputDir = options.getScratchDir().getPath();
199:
200: fileName = outputDir + File.separatorChar
201: + className.replace('.', File.separatorChar)
202: + ".class";
203:
204: byte[] classBytes = null;
205: /**
206: * When using a SecurityManager and a JSP page itself triggers
207: * another JSP due to an errorPage or from a jsp:include,
208: * the loadClass must be performed with the Permissions of
209: * this class using doPriviledged because the parent JSP
210: * may not have sufficient Permissions.
211: */
212: classBytes = loadClassDataFromFile(fileName);
213: if (classBytes == null) {
214: throw new ClassNotFoundException(Constants
215: .getString("jsp.error.unable.loadclass",
216: new Object[] { className }));
217: }
218: return defClass(className, classBytes);
219: }
220: } catch (Exception ex) {
221: throw new ClassNotFoundException(Constants.getString(
222: "jsp.error.unable.loadclass",
223: new Object[] { className }));
224: }
225: }
226:
227: /**
228: * Just a short hand for defineClass now... I suspect we might need to
229: * make this public at some point of time.
230: */
231: protected Class defClass(String className, byte[] classData) {
232: return defineClass(className, classData, 0, classData.length);
233: }
234:
235: /**
236: * Load JSP class data from file, method may be called from
237: * within a doPriviledged if a SecurityManager is installed.
238: */
239: protected byte[] loadClassDataFromFile(String fileName) {
240: return doLoadClassDataFromFile(fileName);
241: }
242:
243: /**
244: * Load JSP class data from file, method may be called from
245: * within a doPriviledged if a SecurityManager is installed.
246: */
247: protected byte[] doLoadClassDataFromFile(String fileName) {
248: byte[] classBytes = null;
249: try {
250: FileInputStream fin = new FileInputStream(fileName);
251: ByteArrayOutputStream baos = new ByteArrayOutputStream();
252: byte buf[] = new byte[1024];
253: for (int i = 0; (i = fin.read(buf)) != -1;)
254: baos.write(buf, 0, i);
255: fin.close();
256: baos.close();
257: classBytes = baos.toByteArray();
258: } catch (Exception ex) {
259: return null;
260: }
261: return classBytes;
262: }
263:
264: // private Vector jars = new Vector();
265:
266: private byte[] loadClassDataFromJar(String className) {
267: String entryName = className.replace('.', '/') + ".class";
268: InputStream classStream = null;
269: //System.out.println("Loading " + className);
270:
271: for (int i = 0; i < jars.size(); i++) {
272: File this File = new File((String) jars.elementAt(i));
273: try {
274: //System.out.println(" - trying " + thisFile.getAbsolutePath());
275: // check if it exists...
276: if (!this File.exists()) {
277: continue;
278: }
279: ;
280:
281: if (this File.isFile()) {
282: ZipFile zip = new ZipFile(this File);
283: ZipEntry entry = zip.getEntry(entryName);
284: if (entry != null) {
285: classStream = zip.getInputStream(entry);
286: byte[] classData = getClassData(classStream);
287: zip.close();
288: return classData;
289: } else {
290: zip.close();
291: }
292: } else { // its a directory...
293: File classFile = new File(this File, entryName
294: .replace('/', File.separatorChar));
295: if (classFile.exists()) {
296: classStream = new FileInputStream(classFile);
297: byte[] classData = getClassData(classStream);
298: classStream.close();
299: return classData;
300: }
301: }
302: } catch (IOException ioe) {
303: return null;
304: }
305: }
306:
307: return null;
308: }
309:
310: private byte[] getClassData(InputStream istream) throws IOException {
311: byte[] buf = new byte[1024];
312: ByteArrayOutputStream bout = new ByteArrayOutputStream();
313: int num = 0;
314: while ((num = istream.read(buf)) != -1) {
315: bout.write(buf, 0, num);
316: }
317:
318: return bout.toByteArray();
319: }
320:
321: public String toString() {
322: return "JspLoader( " + options.getScratchDir() + " ) / "
323: + parent;
324: }
325:
326: }
|