Quadcap Embeddable Server

com/quadcap/http/servlets/jsp/JspObject.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.jsp; 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.io.BufferedInputStream; 00042 import java.io.File; 00043 import java.io.InputStream; 00044 import java.io.InputStreamReader; 00045 import java.io.IOException; 00046 import java.io.Reader; 00047 00048 import javax.servlet.ServletContext; 00049 00050 import com.quadcap.util.Debug; 00051 00052 /** 00053 * This is the information we keep track of for a JSP page, once we've 00054 * compiled it. 00055 * 00056 * @author Stan Bailes 00057 */ 00058 public class JspObject { 00059 static final String JSP_PACKAGE = "__jsp"; 00060 00061 ServletContext context; 00062 String name; 00063 String className; 00064 String packageName; 00065 File packageDir; 00066 File javaFile; 00067 File classFile; 00068 File jspFile = null; 00069 long lastCheck = -1; 00070 JspPage jspPage; 00071 00072 public JspObject(ServletContext context, 00073 File repository, String name) throws IOException { 00074 this.context = context; 00075 this.name = name; 00076 00077 String realPath = context.getRealPath(name); 00078 InputStream is = null; 00079 if (realPath != null) { 00080 this.jspFile = new File(realPath); 00081 } 00082 00083 String java = name; 00084 if (java.endsWith(".jsp")) java = java.substring(0, java.length()-4); 00085 if (java.indexOf('/') == 0) java = java.substring(1); 00086 00087 this.className = java; 00088 String pDir = JSP_PACKAGE; 00089 this.packageName = JSP_PACKAGE; 00090 int idx = java.lastIndexOf('/'); 00091 if (idx > 0) { 00092 this.className = java.substring(idx+1); 00093 this.packageName = JSP_PACKAGE + '.' + 00094 java.substring(0, idx).replace('/', '.'); 00095 pDir = java.substring(0, idx); 00096 } 00097 00098 File jspRoot = new File(repository, JSP_PACKAGE); 00099 this.packageDir = new File(jspRoot, pDir); 00100 if (!packageDir.isDirectory() && !packageDir.mkdirs()) { 00101 throw new IOException("Can't create directory: " + 00102 packageDir); 00103 } 00104 00105 this.javaFile = new File(jspRoot, java + ".java"); 00106 this.classFile = new File(jspRoot, java + ".class"); 00107 } 00108 00109 public String getClassName() { return className; } 00110 public String getPackageName() { return packageName; } 00111 public String getName() { return name; } 00112 public File getJavaFile() { return javaFile; } 00113 public File getClassFile() { return classFile; } 00114 00115 public Reader getJspReader() { 00116 InputStream is = context.getResourceAsStream(name); 00117 BufferedInputStream bis = new BufferedInputStream(is); 00118 InputStreamReader r = new InputStreamReader(bis); 00119 return r; 00120 } 00121 00122 public String getFullClassName() { return packageName + "." + className; } 00123 00124 public boolean needRecompile() { 00125 long now = System.currentTimeMillis(); 00126 long since = now - lastCheck; 00127 if (since < 2000) return false; 00128 00129 if (!classFile.exists()) return true; 00130 if (jspFile != null && 00131 classFile.lastModified() < jspFile.lastModified()) return true; 00132 lastCheck = now; 00133 return false; 00134 } 00135 00136 public JspPage getJspPage() { return jspPage; } 00137 public void setJspPage(JspPage page) { 00138 jspPage = page; 00139 } 00140 }