KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > jsp > JspObject


1 package com.quadcap.http.servlets.jsp;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.InputStreamReader JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.Reader JavaDoc;
47
48 import javax.servlet.ServletContext JavaDoc;
49
50 import com.quadcap.util.Debug;
51
52 /**
53  * This is the information we keep track of for a JSP page, once we've
54  * compiled it.
55  *
56  * @author Stan Bailes
57  */

58 public class JspObject {
59     static final String JavaDoc JSP_PACKAGE = "__jsp";
60
61     ServletContext JavaDoc context;
62     String JavaDoc name;
63     String JavaDoc className;
64     String JavaDoc packageName;
65     File JavaDoc packageDir;
66     File JavaDoc javaFile;
67     File JavaDoc classFile;
68     File JavaDoc jspFile = null;
69     long lastCheck = -1;
70     JspPage jspPage;
71
72     public JspObject(ServletContext JavaDoc context,
73              File JavaDoc repository, String JavaDoc name) throws IOException JavaDoc {
74         this.context = context;
75     this.name = name;
76
77         String JavaDoc realPath = context.getRealPath(name);
78         InputStream JavaDoc is = null;
79         if (realPath != null) {
80             this.jspFile = new File JavaDoc(realPath);
81         }
82
83     String JavaDoc 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 JavaDoc 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 JavaDoc jspRoot = new File JavaDoc(repository, JSP_PACKAGE);
99     this.packageDir = new File JavaDoc(jspRoot, pDir);
100     if (!packageDir.isDirectory() && !packageDir.mkdirs()) {
101         throw new IOException JavaDoc("Can't create directory: " +
102                   packageDir);
103     }
104
105     this.javaFile = new File JavaDoc(jspRoot, java + ".java");
106     this.classFile = new File JavaDoc(jspRoot, java + ".class");
107     }
108
109     public String JavaDoc getClassName() { return className; }
110     public String JavaDoc getPackageName() { return packageName; }
111     public String JavaDoc getName() { return name; }
112     public File JavaDoc getJavaFile() { return javaFile; }
113     public File JavaDoc getClassFile() { return classFile; }
114
115     public Reader JavaDoc getJspReader() {
116         InputStream JavaDoc is = context.getResourceAsStream(name);
117         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
118         InputStreamReader JavaDoc r = new InputStreamReader JavaDoc(bis);
119         return r;
120     }
121
122     public String JavaDoc 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