1 /*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution, if
20 * any, must include the following acknowlegement:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowlegement may appear in the software itself,
24 * if and wherever such third-party acknowlegements normally appear.
25 *
26 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
27 * Foundation" must not be used to endorse or promote products derived
28 * from this software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache"
32 * nor may "Apache" appear in their names without prior written
33 * permission of the Apache Group.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 *
54 */
55
56
57
58
59
60 package org.apache.jasper;
61
62 import javax.servlet.ServletContext;
63 import javax.servlet.http.HttpServletRequest;
64 import javax.servlet.http.HttpServletResponse;
65
66 import org.apache.jasper.compiler.JspReader;
67 import org.apache.jasper.compiler.ServletWriter;
68 import org.apache.jasper.runtime.JspLoader;
69 import org.apache.jasper.compiler.TagLibraries;
70
71 import org.apache.jasper.compiler.Compiler;
72 import org.apache.jasper.compiler.JspCompiler;
73 import org.apache.jasper.compiler.SunJavaCompiler;
74
75 /**
76 * A place holder for various things that are used through out the JSP
77 * engine. This is a per-request/per-context data structure. Some of
78 * the instance variables are set at different points.
79 *
80 * JspLoader creates this object and passes this off to the "compiler"
81 * subsystem, which then initializes the rest of the variables.
82 *
83 * @author Anil K. Vijendran
84 * @author Harish Prabandham
85 */
86 public class JspEngineContext {
87 JspReader reader;
88 ServletWriter writer;
89 ServletContext context;
90 JspLoader loader;
91 String classpath; // for compiling JSPs.
92 boolean isErrPage;
93 String jspFile;
94 String servletClassName;
95 String servletPackageName;
96 String servletJavaFileName;
97 String contentType;
98 Options options;
99 HttpServletRequest req;
100 HttpServletResponse res;
101
102
103 public JspEngineContext(JspLoader loader, String classpath,
104 ServletContext context, String jspFile,
105 boolean isErrPage, Options options,
106 HttpServletRequest req, HttpServletResponse res)
107 {
108 this.loader = loader;
109 this.classpath = classpath;
110 this.context = context;
111 this.jspFile = jspFile;
112 this.isErrPage = isErrPage;
113 this.options = options;
114 this.req = req;
115 this.res = res;
116 }
117
118 /**
119 * Get the http request we are servicing now...
120 */
121 public HttpServletRequest getRequest() {
122 return req;
123 }
124
125
126 /**
127 * Get the http response we are using now...
128 */
129 public HttpServletResponse getResponse() {
130 return res;
131 }
132
133 /**
134 * The classpath that is passed off to the Java compiler.
135 */
136 public String getClassPath() {
137 return loader.getClassPath() + classpath;
138 }
139
140 /**
141 * Get the input reader for the JSP text.
142 */
143 public JspReader getReader() {
144 return reader;
145 }
146
147 /**
148 * Where is the servlet being generated?
149 */
150 public ServletWriter getWriter() {
151 return writer;
152 }
153
154 /**
155 * Get the ServletContext for the JSP we're processing now.
156 */
157 public ServletContext getServletContext() {
158 return context;
159 }
160
161 /**
162 * What class loader to use for loading classes while compiling
163 * this JSP? I don't think this is used right now -- akv.
164 */
165 public JspLoader getClassLoader() {
166 return loader;
167 }
168
169 /**
170 * Are we processing something that has been declared as an
171 * errorpage?
172 */
173 public boolean isErrorPage() {
174 return isErrPage;
175 }
176
177 /**
178 * What is the scratch directory we are generating code into?
179 * FIXME: In some places this is called scratchDir and in some
180 * other places it is called outputDir.
181 */
182 public String getOutputDir() {
183 return options.scratchDir().toString();
184 }
185
186 /**
187 * Path of the JSP URI. Note that this is not a file name. This is
188 * the context rooted URI of the JSP file.
189 */
190 public String getJspFile() {
191 return jspFile;
192 }
193
194 /**
195 * Just the class name (does not include package name) of the
196 * generated class.
197 */
198 public String getServletClassName() {
199 return servletClassName;
200 }
201
202 /**
203 * The package name into which the servlet class is generated.
204 */
205 public String getServletPackageName() {
206 return servletPackageName;
207 }
208
209 /**
210 * Utility method to get the full class name from the package and
211 * class name.
212 */
213 public final String getFullClassName() {
214 if (servletPackageName == null)
215 return servletClassName;
216 return servletPackageName + "." + servletClassName;
217 }
218
219 /**
220 * Full path name of the Java file into which the servlet is being
221 * generated.
222 */
223 public String getServletJavaFileName() {
224 return servletJavaFileName;
225 }
226
227 /**
228 * Are we keeping generated code around?
229 */
230 public boolean keepGenerated() {
231 return options.keepGenerated();
232 }
233
234 /**
235 * What's the content type of this JSP? Content type includes
236 * content type and encoding.
237 */
238 public String getContentType() {
239 return contentType;
240 }
241
242 /**
243 * Get hold of the Options object for this context.
244 */
245 public Options getOptions() {
246 return options;
247 }
248
249 public void setContentType(String contentType) {
250 this.contentType = contentType;
251 }
252
253 public void setReader(JspReader reader) {
254 this.reader = reader;
255 }
256
257 public void setWriter(ServletWriter writer) {
258 this.writer = writer;
259 }
260
261 public void setServletClassName(String servletClassName) {
262 this.servletClassName = servletClassName;
263 }
264
265 public void setServletPackageName(String servletPackageName) {
266 this.servletPackageName = servletPackageName;
267 }
268
269 public void setServletJavaFileName(String servletJavaFileName) {
270 this.servletJavaFileName = servletJavaFileName;
271 }
272
273 public void setErrorPage(boolean isErrPage) {
274 this.isErrPage = isErrPage;
275 }
276
277 /**
278 * Create a "Compiler" object based on some init param data. This
279 * is not done yet. Right now we're just hardcoding the actual
280 * compilers that are created.
281 */
282 public Compiler createCompiler() throws JasperException {
283 Compiler jspCompiler = new JspCompiler(this);
284 jspCompiler.setJavaCompiler(new SunJavaCompiler());
285 return jspCompiler;
286 }
287 }