1 package com.quadcap.http.servlets.jsp; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.File ; 43 import java.io.InputStream ; 44 import java.io.InputStreamReader ; 45 import java.io.IOException ; 46 import java.io.Reader ; 47 48 import javax.servlet.ServletContext ; 49 50 import com.quadcap.util.Debug; 51 52 58 public class JspObject { 59 static final String JSP_PACKAGE = "__jsp"; 60 61 ServletContext context; 62 String name; 63 String className; 64 String packageName; 65 File packageDir; 66 File javaFile; 67 File classFile; 68 File jspFile = null; 69 long lastCheck = -1; 70 JspPage jspPage; 71 72 public JspObject(ServletContext context, 73 File repository, String name) throws IOException { 74 this.context = context; 75 this.name = name; 76 77 String realPath = context.getRealPath(name); 78 InputStream is = null; 79 if (realPath != null) { 80 this.jspFile = new File (realPath); 81 } 82 83 String java = name; 84 if (java.endsWith(".jsp")) java = java.substring(0, java.length()-4); 85 if (java.indexOf('/') == 0) java = java.substring(1); 86 87 this.className = java; 88 String pDir = JSP_PACKAGE; 89 this.packageName = JSP_PACKAGE; 90 int idx = java.lastIndexOf('/'); 91 if (idx > 0) { 92 this.className = java.substring(idx+1); 93 this.packageName = JSP_PACKAGE + '.' + 94 java.substring(0, idx).replace('/', '.'); 95 pDir = java.substring(0, idx); 96 } 97 98 File jspRoot = new File (repository, JSP_PACKAGE); 99 this.packageDir = new File (jspRoot, pDir); 100 if (!packageDir.isDirectory() && !packageDir.mkdirs()) { 101 throw new IOException ("Can't create directory: " + 102 packageDir); 103 } 104 105 this.javaFile = new File (jspRoot, java + ".java"); 106 this.classFile = new File (jspRoot, java + ".class"); 107 } 108 109 public String getClassName() { return className; } 110 public String getPackageName() { return packageName; } 111 public String getName() { return name; } 112 public File getJavaFile() { return javaFile; } 113 public File getClassFile() { return classFile; } 114 115 public Reader getJspReader() { 116 InputStream is = context.getResourceAsStream(name); 117 BufferedInputStream bis = new BufferedInputStream (is); 118 InputStreamReader r = new InputStreamReader (bis); 119 return r; 120 } 121 122 public String getFullClassName() { return packageName + "." + className; } 123 124 public boolean needRecompile() { 125 long now = System.currentTimeMillis(); 126 long since = now - lastCheck; 127 if (since < 2000) return false; 128 129 if (!classFile.exists()) return true; 130 if (jspFile != null && 131 classFile.lastModified() < jspFile.lastModified()) return true; 132 lastCheck = now; 133 return false; 134 } 135 136 public JspPage getJspPage() { return jspPage; } 137 public void setJspPage(JspPage page) { 138 jspPage = page; 139 } 140 } 141 | Popular Tags |