001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.sun.portal.providers.jsp.jasper3.jasper;
057:
058: import java.io.*;
059:
060: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.JspReader;
061: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.ServletWriter;
062: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.TagLibraries;
063: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.CommandLineCompiler;
064: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.Compiler;
065:
066: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.SunJavaCompiler;
067: import com.sun.portal.providers.jsp.jasper3.jasper.compiler.JavaCompiler;
068:
069: //import com.sun.portal.providers.jsp.jasper3.jasper.runtime.JspLoader;
070: // Use the jasper loader - the only function used is to add a jar
071: import com.sun.portal.providers.jsp.jasper3.jasper.servlet.JasperLoader;
072:
073: /**
074: * Holds data used on a per-page compilation context that would otherwise spill
075: * over to other pages being compiled. Things like the taglib classloaders
076: * and directives.
077: *
078: *@author Danno Ferrin
079: */
080: public class CommandLineContext implements JspCompilationContext {
081:
082: String classPath;
083: JspReader reader;
084: ServletWriter writer;
085: JasperLoader loader;
086: boolean errPage;
087: String jspFile;
088: String servletClassName;
089: String servletPackageName;
090: String servletJavaFileName;
091: String contentType;
092: Options options;
093:
094: String uriBase;
095: File uriRoot;
096:
097: boolean packageFlag;
098:
099: public CommandLineContext(JasperLoader newLoader,
100: String newClassPath, String newJspFile, String newUriBase,
101: String newUriRoot, boolean newErrPage, Options newOptions)
102: throws JasperException {
103: loader = newLoader;
104: classPath = newClassPath;
105: uriBase = newUriBase;
106: String tUriRoot = newUriRoot;
107: jspFile = newJspFile;
108: errPage = newErrPage;
109: options = newOptions;
110:
111: if (uriBase == null) {
112: uriBase = "/";
113: } else if (uriBase.charAt(0) != '/') {
114: // strip the basde slash since it will be combined with the
115: // uriBase to generate a file
116: uriBase = "/" + uriBase;
117: }
118:
119: if (uriBase.charAt(uriBase.length() - 1) != '/') {
120: uriBase += '/';
121: }
122:
123: if (tUriRoot == null) {
124: uriRoot = new File("");
125: } else {
126: uriRoot = new File(tUriRoot);
127: if (!uriRoot.exists() || !uriRoot.isDirectory()) {
128: throw new JasperException(Constants
129: .getString("jsp.error.jspc.uriroot_not_dir"));
130: }
131: }
132: }
133:
134: /**
135: * The classpath that is passed off to the Java compiler.
136: */
137: public String getClassPath() {
138: return loader.getClassPath() + classPath;
139: }
140:
141: /**
142: * Get the input reader for the JSP text.
143: */
144: public JspReader getReader() {
145: return reader;
146: }
147:
148: /**
149: * Where is the servlet being generated?
150: */
151: public ServletWriter getWriter() {
152: return writer;
153: }
154:
155: /**
156: * What class loader to use for loading classes while compiling
157: * this JSP? I don't think this is used right now -- akv.
158: */
159: public ClassLoader getClassLoader() {
160: return loader;
161: }
162:
163: public void addJar(String jar) throws IOException {
164: loader.addJar(jar);
165: }
166:
167: /**
168: * Are we processing something that has been declared as an
169: * errorpage?
170: */
171: public boolean isErrorPage() {
172: return errPage;
173: }
174:
175: /**
176: * What is the scratch directory we are generating code into?
177: * FIXME: In some places this is called scratchDir and in some
178: * other places it is called outputDir.
179: */
180: public String getOutputDir() {
181: return options.getScratchDir().toString();
182: }
183:
184: /**
185: * Path of the JSP URI. Note that this is not a file name. This is
186: * the context rooted URI of the JSP file.
187: */
188: public String getJspFile() {
189: return jspFile;
190: }
191:
192: /**
193: * Just the class name (does not include package name) of the
194: * generated class.
195: */
196: public String getServletClassName() {
197: return servletClassName;
198: }
199:
200: /**
201: * The package name into which the servlet class is generated.
202: */
203: public String getServletPackageName() {
204: return servletPackageName;
205: }
206:
207: /**
208: * Utility method to get the full class name from the package and
209: * class name.
210: */
211: public String getFullClassName() {
212: String pkg = getServletPackageName();
213: if (pkg != null) {
214: pkg += ".";
215: } else {
216: pkg = "";
217: }
218: return pkg + getServletClassName();
219: }
220:
221: /**
222: * Full path name of the Java file into which the servlet is being
223: * generated.
224: */
225: public String getServletJavaFileName() {
226: return servletJavaFileName;
227: }
228:
229: /**
230: * Are we keeping generated code around?
231: */
232: public boolean keepGenerated() {
233: return options.getKeepGenerated();
234: }
235:
236: /**
237: * What's the content type of this JSP? Content type includes
238: * content type and encoding.
239: */
240: public String getContentType() {
241: return contentType;
242: }
243:
244: /**
245: * Get hold of the Options object for this context.
246: */
247: public Options getOptions() {
248: return options;
249: }
250:
251: public void setContentType(String contentType) {
252: this .contentType = contentType;
253: }
254:
255: public void setReader(JspReader reader) {
256: this .reader = reader;
257: }
258:
259: public void setWriter(ServletWriter writer) {
260: this .writer = writer;
261: }
262:
263: public void setServletClassName(String servletClassName) {
264: this .servletClassName = servletClassName;
265: }
266:
267: public void setServletPackageName(String servletPackageName) {
268: this .servletPackageName = servletPackageName;
269: }
270:
271: public void setServletJavaFileName(String servletJavaFileName) {
272: this .servletJavaFileName = servletJavaFileName;
273: }
274:
275: public void setErrorPage(boolean isErrPage) {
276: errPage = isErrPage;
277: }
278:
279: public void setPackageFlag(boolean flag) {
280: packageFlag = flag;
281: }
282:
283: public boolean getPackageFlag() {
284: return packageFlag;
285: }
286:
287: /**
288: * Create a "Compiler" object based on some cmd line data.
289: * Use jspCompilerPlugin if specified.
290: */
291: public Compiler createCompiler() throws JasperException {
292: Compiler jspCompiler = new CommandLineCompiler(this );
293:
294: Class javaCompilerPlugin = options.getJspCompilerPlugin();
295: if (javaCompilerPlugin != null) {
296: JavaCompiler javac = null;
297: try {
298: javac = (JavaCompiler) javaCompilerPlugin.newInstance();
299: } catch (Exception ex) {
300: throw new JasperException(
301: Constants
302: .getString("jsp.warning.compiler.class.cantcreate"));
303: }
304:
305: String compilerPath = options.getJspCompilerPath();
306: if (compilerPath != null)
307: javac.setCompilerPath(compilerPath);
308:
309: jspCompiler.setJavaCompiler(javac);
310: }
311:
312: return jspCompiler;
313: }
314:
315: /**
316: * Get the full value of a URI relative to this compilations context
317: * uses current file as the base.
318: */
319: public String resolveRelativeUri(String uri) {
320: if (uri.startsWith("/")) {
321: return uri;
322: } else {
323: return uriBase + uri;
324: }
325: }
326:
327: /**
328: * Gets a resource as a stream, relative to the meanings of this
329: * context's implementation.
330: *@returns a null if the resource cannot be found or represented
331: * as an InputStream.
332: */
333: public java.io.InputStream getResourceAsStream(String res) {
334: InputStream in;
335: // fisrt try and get it from the URI
336: try {
337: in = new FileInputStream(getRealPath(res));
338: } catch (IOException ioe) {
339: in = null;
340: }
341: // next, try it as an absolute name
342: if (in == null)
343: try {
344: in = new FileInputStream(res);
345: } catch (IOException ioe) {
346: in = null;
347: }
348: // that dind't work, last chance is to try the classloaders
349: if (in == null) {
350: in = loader.getResourceAsStream(res);
351: }
352: return in;
353: }
354:
355: /**
356: * Gets the actual path of a URI relative to the context of
357: * the compilation.
358: */
359: public String getRealPath(String path) {
360: path = resolveRelativeUri(path);
361: if (path.startsWith("/")) {
362: path = path.substring(1);
363: }
364: File f = new File(uriRoot, path
365: .replace('/', File.separatorChar));
366: return f.getAbsolutePath();
367: }
368:
369: }
|