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.rimfaxe.webserver.compiler.jsp;
057:
058: import java.util.Vector;
059: import javax.servlet.ServletContext;
060: import javax.servlet.ServletConfig;
061:
062: import com.rimfaxe.webserver.runtime.HttpJspBase;
063: import java.io.*;
064: import com.rimfaxe.webserver.WebContext;
065: import com.rimfaxe.webserver.compiler.jsp.error.*;
066: import com.rimfaxe.webserver.servletapi.jsp.JSPclass;
067: import com.rimfaxe.webserver.compiler.Library;
068: import com.rimfaxe.webserver.compiler.*;
069:
070: /**
071: * Main JSP compiler class. Changed to accomodate GCJ compiling to native library.
072: *
073: * @author Anil K. Vijendran
074: * @author Mandar Raje
075: * @author Pierre Delisle
076: * @author Kin-man Chung
077: * @author Remy Maucherat
078: * @author Lars Andersen
079: */
080: public class JspC {
081:
082: private String jspUri;
083: private String javaServletName;
084: private String javaPackageName;
085: private String javaFileName;
086:
087: private WebContext ctxt;
088:
089: private ServletConfig config;
090:
091: private PageInfo pageinfo;
092: private JspErrorHandler err;
093:
094: private JSPclass jspclass;
095:
096: /** Creates a new instance of JspC */
097: public JspC(JSPclass jspclass, WebContext wc) {
098: this .jspclass = jspclass;
099: this .jspUri = jspclass.getJspPage();
100:
101: this .javaServletName = jspclass.getServletName();
102: this .javaPackageName = jspclass.getPackageName();
103: this .javaFileName = wc.getRoot() + "/WEB-INF/rws/jsp/"
104: + this .javaServletName + ".java";
105: this .ctxt = wc;
106: this .err = new JspErrorHandler();
107: }
108:
109: public JSPclass getJspClass() {
110: return jspclass;
111: }
112:
113: public PageInfo getPageInfo() {
114: return pageinfo;
115: }
116:
117: public JspErrorHandler getErrorHandler() {
118: return err;
119: }
120:
121: public WebContext getWebContext() {
122: return ctxt;
123: }
124:
125: /**
126: * Compile the jsp file.
127: */
128: public void compileJSP() throws FileNotFoundException,
129: JspToJavaException, Exception, NativeCompilationException {
130: //System.out.println("*** Compile JAVA");
131: generateJava();
132: //System.out.println("*** Compile NATIVE");
133: generateNative();
134: //System.out.println("*** Load NATIVE");
135: loadNative();
136: }
137:
138: /**
139: * Compile the java file.
140: */
141: public void generateNative() throws NativeCompilationException,
142: NativeLinkException {
143: Library libjsp = new Library();
144: libjsp.setOutputFile(jspclass.getNativeFileName());
145: libjsp.setOutputDir(ctxt.getRoot() + "/WEB-INF/rws/jsplib");
146:
147: libjsp.setSource(libjsp.JAVA_SOURCE);
148:
149: Vector vec = new Vector();
150: vec.addElement(javaFileName);
151: libjsp.setInputFiles(vec);
152:
153: libjsp.addCompileOption("-I" + ctxt.getRoot()
154: + "/WEB-INF/rws/src");
155:
156: // check for libservlets.so
157: //java.io.File f1 = new java.io.File(ctxt.getRoot()+"/WEB-INF/rws/lib/libservlets.so");
158: //if (f1.exists())
159: //{
160: // System.out.println("JspC, libservlets.so exists");
161: // libjsp.addLinkOption("-L"+ctxt.getRoot()+"/WEB-INF/rws/lib");
162: // libjsp.addLinkOption("-lservlets");
163: //}
164:
165: libjsp.compile();
166: }
167:
168: public boolean loadNative() {
169: String absFile = ctxt.getRoot() + "/WEB-INF/rws/jsplib/"
170: + jspclass.getNativeFileName();
171:
172: java.io.File f = new java.io.File(absFile);
173: if (f.exists()) {
174: if (System.getProperty("java.vm.name").equalsIgnoreCase(
175: "GNU libgcj")) {
176: try {
177: System.load(absFile);
178: return true;
179: } catch (Exception e) {
180: }
181: }
182: }
183:
184: return false;
185: }
186:
187: /**
188: * Compile the jsp file from the current engine context
189: */
190: public void generateJava() throws FileNotFoundException,
191: JspToJavaException, Exception {
192: // Setup the ServletWriter
193: // We try UTF8 by default. If it fails, we use the java encoding
194: // specified for JspServlet init parameter "javaEncoding".
195:
196: pageinfo = new PageInfo(new BeanRepository());
197:
198: //String javaEncoding = "UTF8";
199:
200: String javaEncoding = com.rimfaxe.webserver.ObjectStore
201: .getConfiguration().getJavaEncoding();
202: OutputStreamWriter osw = null;
203: try {
204: osw = new OutputStreamWriter(new FileOutputStream(
205: javaFileName), javaEncoding);
206: } catch (UnsupportedEncodingException ex) {
207: // Try to get the java encoding from the "javaEncoding"
208: // init parameter for JspServlet.
209: //javaEncoding = ctxt.getOptions().getJavaEncoding();
210: if (javaEncoding != null) {
211: try {
212: osw = new OutputStreamWriter(new FileOutputStream(
213: javaFileName), javaEncoding);
214: } catch (UnsupportedEncodingException ex2) {
215: // TODO
216: }
217: } else {
218:
219: }
220: }
221:
222: ServletWriter writer = new ServletWriter(new PrintWriter(osw));
223:
224: try {
225: // Parse the file
226:
227: ParserController parserCtl = new ParserController(this ,
228: ctxt, this .jspUri);
229:
230: Node.Nodes pageNodes = parserCtl.parse(this .jspUri);
231:
232: // Validate and process attributes
233:
234: Validator.validate(this , pageNodes);
235:
236: // Collect page info
237:
238: Collector.collect(this , pageNodes);
239:
240: // generate servlet .java file
241:
242: Generator.generate(writer, this , pageNodes);
243: writer.close();
244: } catch (JasperException je) {
245: throw new JspToJavaException(
246: "JSP to java compilation failed");
247: }
248:
249: }
250:
251: public String getIeClassId() {
252: return "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
253: }
254:
255: public String getServletPackageName() {
256: return this .javaPackageName;
257: }
258:
259: public String getServletClassName() {
260: return this.javaServletName;
261: }
262: }
|