00001
package com.quadcap.http.servlets.jsp;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00054
00055
00056
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 }