001: package com.quadcap.http.servlets.jsp;
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.io.BufferedInputStream;
042: import java.io.File;
043: import java.io.InputStream;
044: import java.io.InputStreamReader;
045: import java.io.IOException;
046: import java.io.Reader;
047:
048: import javax.servlet.ServletContext;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * This is the information we keep track of for a JSP page, once we've
054: * compiled it.
055: *
056: * @author Stan Bailes
057: */
058: public class JspObject {
059: static final String JSP_PACKAGE = "__jsp";
060:
061: ServletContext context;
062: String name;
063: String className;
064: String packageName;
065: File packageDir;
066: File javaFile;
067: File classFile;
068: File jspFile = null;
069: long lastCheck = -1;
070: JspPage jspPage;
071:
072: public JspObject(ServletContext context, File repository,
073: String name) throws IOException {
074: this .context = context;
075: this .name = name;
076:
077: String realPath = context.getRealPath(name);
078: InputStream is = null;
079: if (realPath != null) {
080: this .jspFile = new File(realPath);
081: }
082:
083: String java = name;
084: if (java.endsWith(".jsp"))
085: java = java.substring(0, java.length() - 4);
086: if (java.indexOf('/') == 0)
087: java = java.substring(1);
088:
089: this .className = java;
090: String pDir = JSP_PACKAGE;
091: this .packageName = JSP_PACKAGE;
092: int idx = java.lastIndexOf('/');
093: if (idx > 0) {
094: this .className = java.substring(idx + 1);
095: this .packageName = JSP_PACKAGE + '.'
096: + java.substring(0, idx).replace('/', '.');
097: pDir = java.substring(0, idx);
098: }
099:
100: File jspRoot = new File(repository, JSP_PACKAGE);
101: this .packageDir = new File(jspRoot, pDir);
102: if (!packageDir.isDirectory() && !packageDir.mkdirs()) {
103: throw new IOException("Can't create directory: "
104: + packageDir);
105: }
106:
107: this .javaFile = new File(jspRoot, java + ".java");
108: this .classFile = new File(jspRoot, java + ".class");
109: }
110:
111: public String getClassName() {
112: return className;
113: }
114:
115: public String getPackageName() {
116: return packageName;
117: }
118:
119: public String getName() {
120: return name;
121: }
122:
123: public File getJavaFile() {
124: return javaFile;
125: }
126:
127: public File getClassFile() {
128: return classFile;
129: }
130:
131: public Reader getJspReader() {
132: InputStream is = context.getResourceAsStream(name);
133: BufferedInputStream bis = new BufferedInputStream(is);
134: InputStreamReader r = new InputStreamReader(bis);
135: return r;
136: }
137:
138: public String getFullClassName() {
139: return packageName + "." + className;
140: }
141:
142: public boolean needRecompile() {
143: long now = System.currentTimeMillis();
144: long since = now - lastCheck;
145: if (since < 2000)
146: return false;
147:
148: if (!classFile.exists())
149: return true;
150: if (jspFile != null
151: && classFile.lastModified() < jspFile.lastModified())
152: return true;
153: lastCheck = now;
154: return false;
155: }
156:
157: public JspPage getJspPage() {
158: return jspPage;
159: }
160:
161: public void setJspPage(JspPage page) {
162: jspPage = page;
163: }
164: }
|