/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package com.rimfaxe.webserver.compiler.jsp;
import java.util.Vector;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import com.rimfaxe.webserver.runtime.HttpJspBase;
import java.io.*;
import com.rimfaxe.webserver.WebContext;
import com.rimfaxe.webserver.compiler.jsp.error.*;
import com.rimfaxe.webserver.servletapi.jsp.JSPclass;
import com.rimfaxe.webserver.compiler.Library;
import com.rimfaxe.webserver.compiler.*;
/**
* Main JSP compiler class. Changed to accomodate GCJ compiling to native library.
*
* @author Anil K. Vijendran
* @author Mandar Raje
* @author Pierre Delisle
* @author Kin-man Chung
* @author Remy Maucherat
* @author Lars Andersen
*/
public class JspC
{
private String jspUri;
private String javaServletName;
private String javaPackageName;
private String javaFileName;
private WebContext ctxt;
private ServletConfig config;
private PageInfo pageinfo;
private JspErrorHandler err;
private JSPclass jspclass;
/** Creates a new instance of JspC */
public JspC(JSPclass jspclass, WebContext wc)
{
this.jspclass = jspclass;
this.jspUri = jspclass.getJspPage();
this.javaServletName = jspclass.getServletName();
this.javaPackageName = jspclass.getPackageName();
this.javaFileName = wc.getRoot() + "/WEB-INF/rws/jsp/" + this.javaServletName+".java";
this.ctxt = wc;
this.err = new JspErrorHandler();
}
public JSPclass getJspClass()
{
return jspclass;
}
public PageInfo getPageInfo()
{
return pageinfo;
}
public JspErrorHandler getErrorHandler()
{
return err;
}
public WebContext getWebContext() { return ctxt; }
/**
* Compile the jsp file.
*/
public void compileJSP()
throws FileNotFoundException, JspToJavaException, Exception, NativeCompilationException
{
//System.out.println("*** Compile JAVA");
generateJava();
//System.out.println("*** Compile NATIVE");
generateNative();
//System.out.println("*** Load NATIVE");
loadNative();
}
/**
* Compile the java file.
*/
public void generateNative()
throws NativeCompilationException, NativeLinkException
{
Library libjsp = new Library();
libjsp.setOutputFile( jspclass.getNativeFileName() );
libjsp.setOutputDir(ctxt.getRoot()+"/WEB-INF/rws/jsplib");
libjsp.setSource(libjsp.JAVA_SOURCE);
Vector vec = new Vector();
vec.addElement(javaFileName);
libjsp.setInputFiles(vec);
libjsp.addCompileOption("-I"+ctxt.getRoot()+"/WEB-INF/rws/src");
// check for libservlets.so
//java.io.File f1 = new java.io.File(ctxt.getRoot()+"/WEB-INF/rws/lib/libservlets.so");
//if (f1.exists())
//{
// System.out.println("JspC, libservlets.so exists");
// libjsp.addLinkOption("-L"+ctxt.getRoot()+"/WEB-INF/rws/lib");
// libjsp.addLinkOption("-lservlets");
//}
libjsp.compile();
}
public boolean loadNative()
{
String absFile = ctxt.getRoot()+"/WEB-INF/rws/jsplib/"+jspclass.getNativeFileName();
java.io.File f = new java.io.File(absFile);
if (f.exists())
{
if (System.getProperty("java.vm.name").equalsIgnoreCase("GNU libgcj"))
{
try
{
System.load(absFile);
return true;
}
catch (Exception e)
{
}
}
}
return false;
}
/**
* Compile the jsp file from the current engine context
*/
public void generateJava()
throws FileNotFoundException, JspToJavaException, Exception
{
// Setup the ServletWriter
// We try UTF8 by default. If it fails, we use the java encoding
// specified for JspServlet init parameter "javaEncoding".
pageinfo = new PageInfo(new BeanRepository());
//String javaEncoding = "UTF8";
String javaEncoding = com.rimfaxe.webserver.ObjectStore.getConfiguration().getJavaEncoding();
OutputStreamWriter osw = null;
try
{
osw = new OutputStreamWriter(new FileOutputStream(javaFileName),javaEncoding);
}
catch (UnsupportedEncodingException ex)
{
// Try to get the java encoding from the "javaEncoding"
// init parameter for JspServlet.
//javaEncoding = ctxt.getOptions().getJavaEncoding();
if (javaEncoding != null)
{
try
{
osw = new OutputStreamWriter( new FileOutputStream(javaFileName),javaEncoding);
}
catch (UnsupportedEncodingException ex2)
{
// TODO
}
}
else
{
}
}
ServletWriter writer = new ServletWriter(new PrintWriter(osw));
try
{
// Parse the file
ParserController parserCtl = new ParserController(this, ctxt, this.jspUri);
Node.Nodes pageNodes = parserCtl.parse(this.jspUri);
// Validate and process attributes
Validator.validate(this, pageNodes);
// Collect page info
Collector.collect(this, pageNodes);
// generate servlet .java file
Generator.generate(writer, this, pageNodes);
writer.close();
}
catch (JasperException je)
{
throw new JspToJavaException("JSP to java compilation failed");
}
}
public String getIeClassId()
{
return "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
}
public String getServletPackageName()
{
return this.javaPackageName;
}
public String getServletClassName()
{
return this.javaServletName;
}
}