costin 01/01/27 21:54:35
Added: src/share/org/apache/jasper JasperEngineContext.java
JasperOptionsImpl.java
src/share/org/apache/jasper/compiler JasperMangler.java
Log:
JspInterceptor: adding support for all options supported by Jasper.
This is a slightly bigger change - the Mangler, Options and EngineContext
are moved out of JspInterceptor ( to respect the organization of
jasper and CommandLineCompiler ).
( they were developed with JspInterceptor as an experimental way to
speed up jsp execution - probably at the beggining of tomcat3.2 development,
now the code is stable, there is no reason to play with the jasper liaison )
Revision Changes Path
1.1
jakarta-tomcat/src/share/org/apache/jasper/JasperEngineContext.java
Index: JasperEngineContext.java
===================================================================
/*
* 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 [EMAIL PROTECTED]
*
* 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 org.apache.jasper;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.jasper.compiler.JspReader;
import org.apache.jasper.compiler.ServletWriter;
import org.apache.jasper.compiler.Compiler;
import java.util.*;
import java.io.*;
import java.net.*;
import org.apache.jasper.JspCompilationContext;
/** Alternative implementation of JspCompilationContext ( in addition
to the servlet and standalone ). Used by JspInterceptor - but
it's in no way specific to tomcat.
*/
public class JasperEngineContext implements JspCompilationContext {
JspReader reader;
ServletWriter writer;
ServletContext context;
ClassLoader loader;
boolean isErrPage;
String jspFile;
String servletClassName;
String servletPackageName;
String servletJavaFileName;
String contentType;
Options options;
String cpath; // for compiling JSPs.
ServletContext sctx;
String outputDir;
public JasperEngineContext()
{
}
public void setClassPath( String s ) {
cpath=s;
}
/**
* The classpath that is passed off to the Java compiler.
*/
public String getClassPath() {
return cpath;
}
/**
* Get the input reader for the JSP text.
*/
public JspReader getReader() {
if( debug>0 ) log("getReader " + reader );
return reader;
}
/**
* Where is the servlet being generated?
*/
public ServletWriter getWriter() {
if( debug>0 ) log("getWriter " + writer );
return writer;
}
public void setServletContext( Object o ) {
sctx=(ServletContext)o;
}
/**
* Get the ServletContext for the JSP we're processing now.
*/
public ServletContext getServletContext() {
if( debug>0 ) log("getCtx " + sctx );
return sctx;
}
/**
* What class loader to use for loading classes while compiling
* this JSP? I don't think this is used right now -- akv.
*/
public ClassLoader getClassLoader() {
if( debug>0 ) log("getLoader " + loader );
return loader;
}
public void setClassLoader( ClassLoader cl ) {
loader=cl;
}
public void addJar( String jar ) throws IOException {
if( debug>0 ) log("Add jar " + jar);
//loader.addJar( jar );
}
/**
* Are we processing something that has been declared as an
* errorpage?
*/
public boolean isErrorPage() {
if( debug>0 ) log("isErrorPage " + isErrPage );
return isErrPage;
}
/**
* What is the scratch directory we are generating code into?
* FIXME: In some places this is called scratchDir and in some
* other places it is called outputDir.
*/
public String getOutputDir() {
if( debug>0 ) log("getOutputDir " + outputDir );
return outputDir;
}
public void setOutputDir(String s ) {
outputDir=s;
}
/**
* Path of the JSP URI. Note that this is not a file name. This is
* the context rooted URI of the JSP file.
*/
public String getJspFile() {
if( debug>0 ) log("getJspFile " + jspFile);
return jspFile;
}
public void setJspFile( String s ) {
jspFile=s;
}
/**
* Just the class name (does not include package name) of the
* generated class.
*/
public String getServletClassName() {
if( debug>0 ) log("getServletClassName " + servletClassName);
return servletClassName;
}
public void setServletClassName( String s ) {
servletClassName=s;
}
/**
* The package name into which the servlet class is generated.
*/
public String getServletPackageName() {
if( debug>0 ) log("getServletPackageName " +
servletPackageName );
return servletPackageName;
}
/**
* Utility method to get the full class name from the package and
* class name.
*/
public final String getFullClassName() {
if( debug>0 ) log("getServletPackageName " +
servletPackageName + "." + servletClassName);
if (servletPackageName == null)
return servletClassName;
return servletPackageName + "." + servletClassName;
}
/**
* Full path name of the Java file into which the servlet is being
* generated.
*/
public String getServletJavaFileName() {
if( debug>0 ) log("getServletPackageName " +
servletPackageName + "." + servletClassName);
return servletJavaFileName;
}
/**
* Are we keeping generated code around?
*/
public boolean keepGenerated() {
return options.getKeepGenerated();
}
/**
* What's the content type of this JSP? Content type includes
* content type and encoding.
*/
public String getContentType() {
return contentType;
}
/**
* Get hold of the Options object for this context.
*/
public Options getOptions() {
return options;
}
public void setOptions(Options options) {
this.options=options;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setReader(JspReader reader) {
this.reader = reader;
}
public void setWriter(ServletWriter writer) {
this.writer = writer;
}
public void setServletPackageName(String servletPackageName) {
this.servletPackageName = servletPackageName;
}
public void setServletJavaFileName(String servletJavaFileName) {
this.servletJavaFileName = servletJavaFileName;
}
public void setErrorPage(boolean isErrPage) {
this.isErrPage = isErrPage;
}
public Compiler createCompiler() throws JasperException {
if( debug>0 ) log("createCompiler ");
return null;
}
public String resolveRelativeUri(String uri)
{
if( debug>0 ) log("resolveRelativeUri " + uri);
return null;
}
public java.io.InputStream getResourceAsStream(String res)
{
if( debug>0 ) log("getResourceAsStream " + res);
return sctx.getResourceAsStream(res);
}
/**
* Gets the actual path of a URI relative to the context of
* the compilation.
*/
public String getRealPath(String path)
{
if( debug>0 ) log("getRealPath " + path + " = " +
sctx.getRealPath( path ));
return sctx.getRealPath( path );
}
// development tracing
private static int debug=0;
private void log( String s ) {
System.out.println("JasperEngineContext: "+ s);
}
}
1.1
jakarta-tomcat/src/share/org/apache/jasper/JasperOptionsImpl.java
Index: JasperOptionsImpl.java
===================================================================
/*
* 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 [EMAIL PROTECTED]
*
* 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 org.apache.jasper;
import java.util.*;
import java.io.*;
import java.net.*;
import org.apache.jasper.Options;
/** Another implementation of Options, backed by a Properties file
* and with no external dependencies.
*/
public class JasperOptionsImpl implements Options {
static final String ieClassId =
"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
// cache
private Class jspCompilerPlugin = null;
// special property.
private Object protectionDomain;
Properties args;
public JasperOptionsImpl( Properties args ) {
this.args=args;
}
// -------------------- Options implementation --------------------
public boolean getKeepGenerated() {
return s2b( args.getProperty("keepgenerated", "true") );
}
public String getJavaEncoding() {
return args.getProperty("javaEncoding", "UTF8");
}
public boolean getLargeFile() {
return s2b( args.getProperty("largefile", "false"));
}
public boolean getMappedFile() {
return s2b( args.getProperty("mappedfile"));
}
public boolean getSendErrorToClient() {
return s2b( args.getProperty( "sendErrToClient" ));
}
public boolean getClassDebugInfo() {
return s2b( args.getProperty( "classDebugInfo" ));
}
public String getIeClassId() {
return args.getProperty( "ieClassId" , ieClassId);
}
public File getScratchDir() {
if( debug>0 ) log("Options: getScratchDir " );
return new File( args.getProperty( "scratchdir" ));
}
public final Object getProtectionDomain() {
if( debug>0 ) log("Options: GetPD" );
return protectionDomain;
}
public String getClassPath() {
if( debug>0 ) log("Options: GetCP " );
return args.getProperty( "classpath" );
}
public Class getJspCompilerPlugin() {
if( debug>0 ) log("Options: getJspCompilerPlugin " );
if( jspCompilerPlugin!= null ) return jspCompilerPlugin;
String type=args.getProperty( "jspCompilerPlugin" );
if( type != null ) {
try {
jspCompilerPlugin=Class.forName(type);
} catch(Exception ex ) {
ex.printStackTrace();
}
}
return jspCompilerPlugin;
}
public String getJspCompilerPath() {
return args.getProperty( "jspCompilerPath" );
}
// -------------------- Setters --------------------
public void setProtectionDomain( Object pd ) {
protectionDomain=pd;
}
// --------------------
private boolean s2b( String s ) {
return new Boolean( s ).booleanValue();
}
// trace for development purpose --------------------
private static int debug=0;
private void log(String s) {
System.err.println(s);
}
}
1.1
jakarta-tomcat/src/share/org/apache/jasper/compiler/JasperMangler.java
Index: JasperMangler.java
===================================================================
/*
* 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 [EMAIL PROTECTED]
*
* 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 org.apache.jasper.compiler;
import java.util.*;
import java.io.*;
import java.net.*;
import org.apache.jasper.compiler.Mangler;
// utils - can be moved here if needed
import org.apache.tomcat.util.JavaGeneratorTool;
import org.apache.tomcat.util.FileUtil;
/** Mangler implementation - use the directory of the jsp file as a package
name, minimize "special" encoding - in general, simpler and predictible
names for the common case.
This file is also using a special mechanism for the "versioned" classes
( based on Anil's idea of generating new class each time the jsp file
changes - without a context restart that looses data ).
We use an additional file per jsp saving the current version - at
startup the file will be read to avoid recompilation. That removes the
need for a "special" class loader and the hacks in reading internal
class info.
*/
public class JasperMangler implements Mangler{
public JasperMangler(String workDir, String docBase, String jspFile)
{
this.jspFile=jspFile;
this.workDir=workDir;
this.docBase=docBase;
init();
}
/** Versioned class name ( without package ).
*/
public String getClassName() {
return JavaGeneratorTool.getVersionedName( baseClassN, version );
}
/**
* Full path to the generated java file ( including version )
*/
public String getJavaFileName() {
return javaFileName;
}
/** The package name ( "." separated ) of the generated
* java file
*/
public String getPackageName() {
if( pkgDir!=null ) {
return pkgDir.replace('/', '.');
} else {
return null;
}
}
/** Full path to the compiled class file ( including version )
*/
public String getClassFileName() {
return classFileName;
}
// -------------------- JspInterceptor fields --------------------
/** Returns the jsp file, as declared by <jsp-file> in server.xml
* or the context-relative path that was extension mapped to jsp
*/
public String getJspFile() {
return jspFile;
}
/** Returns the directory where the class is located, using
* the normal class loader rules.
*/
public String getClassDir() {
return classDir;
}
/** The class name ( package + class + versioning ) of the
* compilation result
*/
public String getServletClassName() {
if( pkgDir!=null ) {
return getPackageName() + "." + getClassName();
} else {
return getClassName();
}
}
public int getVersion() {
return version;
}
// In Jasper = not used - it's specific to the class scheme
// used by JspServlet
// Full path to the class file - without version.
public String getBaseClassName() {
return baseClassN;
}
public String getPackageDir() {
return pkgDir;
}
public String getJspFilePath() {
return FileUtil.safePath( docBase, jspFile);
}
private String fixInvalidChars(String className) {
// Fix for invalid characters. From CommandLineCompiler
StringBuffer modifiedClassName = new StringBuffer();
for (int i = 0; i < className.length(); i++) {
char c=className.charAt(i);
if (Character.isLetterOrDigit(c) == true ||
c=='_' ||
c=='/' )
modifiedClassName.append(className.substring(i,i+1));
else
modifiedClassName.append(mangleChar(className.charAt(i)));
}
return modifiedClassName.toString();
}
private static final String mangleChar(char ch) {
if(ch == File.separatorChar) {
ch = '/';
}
String s = Integer.toHexString(ch);
int nzeros = 5 - s.length();
char[] result = new char[6];
result[0] = '_';
for (int i = 1; i <= nzeros; i++)
result[i] = '0';
for (int i = nzeros+1, j = 0; i < 6; i++, j++)
result[i] = s.charAt(j);
return new String(result);
}
/** compute basic names - pkgDir and baseClassN
*/
private void init() {
int lastComp=jspFile.lastIndexOf( "/" );
if( lastComp > 0 ) {
// has package
// ignore the first "/" of jspFile
pkgDir=jspFile.substring( 1, lastComp );
}
// remove "special" words, replace "."
if( pkgDir!=null ) {
pkgDir=JavaGeneratorTool.manglePackage(pkgDir);
pkgDir=pkgDir.replace('.', '_');
pkgDir=fixInvalidChars( pkgDir );
classDir=workDir + "/" + pkgDir;
} else {
classDir=workDir;
}
int extIdx=jspFile.lastIndexOf( "." );
if( extIdx<0 ) {
// no "."
if( lastComp > 0 )
baseClassN=jspFile.substring( lastComp+1 );
else
baseClassN=jspFile.substring( 1 );
} else {
if( lastComp > 0 )
baseClassN=jspFile.substring( lastComp+1, extIdx );
else
baseClassN=jspFile.substring( 1, extIdx );
}
baseClassN=fixInvalidChars( baseClassN );
// System.out.println("XXXMangler: " + jspFile + " " +
// pkgDir + " " + baseClassN);
// extract version from the .class dir, using the base name
version=JavaGeneratorTool.readVersion(classDir,
baseClassN);
if( version==-1 ) {
version=0;
}
updateVersionPaths();
}
private void updateVersionPaths() {
// version dependent stuff
String baseName=classDir + "/" + JavaGeneratorTool.
getVersionedName( baseClassN, version);
javaFileName= baseName + ".java";
classFileName=baseName + ".class";
}
/** Move to a new class name, if a changes has been detected.
*/
public void nextVersion() {
version++;
JavaGeneratorTool.writeVersion( getClassDir(), baseClassN, version);
updateVersionPaths();
}
// context-relative jsp path
// extracted from the <jsp-file> or the result of a *.jsp mapping
private String jspFile;
// version of the compiled java file
private int version;
private String workDir;
private String docBase;
// the "/" separted version
private String pkgDir;
// class name without package and version
private String baseClassN;
private String classDir;
private String javaFileName;
private String classFileName;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]