|
What this is
Other links
The source code
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.facade;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.Servlet;
import javax.servlet.jsp.JspFactory;
import org.apache.jasper.Constants;
import org.apache.jasper.JasperEngineContext;
import org.apache.jasper.JasperException;
import org.apache.jasper.JasperOptionsImpl;
import org.apache.jasper.JspCompilationContext;
import org.apache.jasper.Options;
import org.apache.jasper.compiler.Compiler;
import org.apache.jasper.compiler.JasperMangler;
import org.apache.jasper.compiler.JavaCompiler;
import org.apache.jasper.compiler.JikesJavaCompiler;
import org.apache.jasper.compiler.Mangler;
import org.apache.jasper.compiler.SunJavaCompiler;
import org.apache.jasper.runtime.HttpJspBase;
import org.apache.jasper.runtime.JspFactoryImpl;
import org.apache.tomcat.core.BaseInterceptor;
import org.apache.tomcat.core.Context;
import org.apache.tomcat.core.ContextManager;
import org.apache.tomcat.core.Handler;
import org.apache.tomcat.core.Request;
import org.apache.tomcat.core.Response;
import org.apache.tomcat.core.TomcatException;
import org.apache.tomcat.util.compat.Jdk11Compat;
import org.apache.tomcat.util.depend.DependManager;
import org.apache.tomcat.util.depend.Dependency;
import org.apache.tomcat.util.log.Log;
/**
* Plug in the JSP engine (a.k.a Jasper)!
* Tomcat uses a "built-in" mapping for jsps ( *.jsp -> jsp ). "jsp"
* can be either a real servlet (JspServlet) that compiles the jsp
* and include the resource, or we can "intercept" and do the
* compilation and mapping in requestMap stage.
*
* JspInterceptor will be invoked once per jsp, and will add an exact
* mapping - all further invocation are identical with servlet invocations
* with direct maps, with no extra overhead.
*
* Future - better abstraction for jsp->java converter ( jasper ), better
* abstraction for java->class, plugin other jsp implementations,
* better scalability.
*
* @author Anil K. Vijendran
* @author Harish Prabandham
* @author Costin Manolache
*/
public class JspInterceptor extends BaseInterceptor {
static final String JIKES="org.apache.jasper.compiler.JikesJavaCompiler";
static final String JSP_SERVLET="org.apache.jasper.servlet.JspServlet";
Properties args=new Properties(); // args for jasper
boolean useJspServlet=false;
boolean useWebAppCL=false;
String jspServletCN=JSP_SERVLET;
String runtimePackage;
// -------------------- Jasper options --------------------
// Options that affect jasper functionality. Will be set on
// JspServlet ( if useJspServlet="true" ) or TomcatOptions.
// IMPORTANT: periodically test for new jasper options
/**
* Are we keeping generated code around?
*/
public void setKeepGenerated( String s ) {
args.put( "keepgenerated", s );
}
/**
* Are we supporting large files?
*/
public void setLargeFile( String s ) {
args.put( "largefile", s );
}
/**
* Are we supporting HTML mapped servlets?
*/
public void setMappedFile( String s ) {
args.put( "mappedfile", s );
}
/**
* Should errors be sent to client or thrown into stderr?
*/
public void setSendErrToClient( String s ) {
args.put( "sendErrToClient", s );
}
/**
* Class ID for use in the plugin tag when the browser is IE.
*/
public void setIEClassId( String s ) {
args.put( "ieClassId", s );
}
/**
* What classpath should I use while compiling the servlets
* generated from JSP files?
*/
public void setClassPath( String s ) {
args.put( "classpath", s );
}
/**
* What is my scratch dir?
*/
public void setScratchdir( String s ) {
args.put( "scratchdir", s );
}
/**
* Path of the compiler to use for compiling JSP pages.
*/
public void setJspCompilerPath( String s ) {
args.put( "jspCompilerPath", s );
}
/**
* What compiler plugin should I use to compile the servlets
* generated from JSP files?
* @deprecated Use setJavaCompiler instead
*/
public void setJspCompilerPlugin( String s ) {
args.put( "jspCompilerPlugin", s );
}
/** Include debug information in generated classes
*/
public void setClassDebugInfo( String s ) {
args.put("classDebugInfo", s );
}
public void setProperty( String n, String v ) {
args.put( n, v );
}
public void setJikesClasspath( String cp ) {
if( cp != null ) {
System.getProperties().put("jikes.class.path", cp);
if( debug > 0 )
log("Setting jikes.class.path to " + cp);
}
}
// -------------------- JspInterceptor properties --------------------
/** Use the old JspServlet to execute Jsps, instead of the
new code. Note that init() never worked (AFAIK) and it'll
be slower - but given the stability of JspServlet it may
be a safe option. This will significantly slow down jsps.
Default is false.
*/
public void setUseJspServlet( boolean b ) {
useJspServlet=b;
}
/** Specify the implementation class of the jsp servlet.
*/
public void setJspServlet( String s ) {
jspServletCN=s;
}
/**
* What compiler should I use to compile the servlets
* generated from JSP files? Default is "javac" ( you can use
* "jikes" as a shortcut ).
*/
public void setJavaCompiler( String type ) {
if( "jikes".equals( type ) )
type=JIKES;
if( "javac".equals( type ) )
type="org.apache.jasper.compiler.SunJavaCompiler";
args.put( "jspCompilerPlugin", type );
}
int pageContextPoolSize=JspFactoryImpl.DEFAULT_POOL_SIZE;
/** Set the PageContext pool size for jasper factory.
0 will disable pooling of PageContexts.
*/
public void setPageContextPoolSize(int i) {
pageContextPoolSize=i;
}
/** The generator will produce code using a different
runtime ( default is org.apache.jasper.runtime ).
The runtime must use the same names for classes as the
default one, so the code will compile.
*/
public void setRuntimePackage(String rp ) {
runtimePackage=rp;
}
/** Compile using the web application classloader. This
was added as part of dealing a problem with
tools.jar on some HP-UX systems.
*/
public void setUseWebAppCL(boolean b) {
useWebAppCL=b;
}
// -------------------- Hooks --------------------
/**
* Jasper-specific initializations, add work dir to classpath,
*/
public void addContext(ContextManager cm, Context ctx)
throws TomcatException
{
if( runtimePackage!=null ) {
Constants.JSP_RUNTIME_PACKAGE=runtimePackage;
Constants.JSP_SERVLET_BASE=runtimePackage+".HttpJspBase";
}
JspFactoryImpl factory=new JspFactoryImpl(pageContextPoolSize);
JspFactory.setDefaultFactory(factory);
// jspServlet uses it's own loader. We need to add workdir
// to the context classpath to use URLLoader and normal
// operation
// XXX alternative: use WEB-INF/classes for generated files
if( ! useJspServlet ) {
try {
// Note: URLClassLoader in JDK1.2.2 doesn't work with file URLs
// that contain '\' characters. Insure only '/' is used.
// jspServlet uses it's own mechanism
URL url=new URL( "file", null,
ctx.getWorkDir().getAbsolutePath().replace('\\','/') + "/");
ctx.addClassPath( url );
if( debug > 9 ) log( "Added to classpath: " + url );
} catch( MalformedURLException ex ) {
ex.printStackTrace();
}
}
if( (useJspServlet && !ctx.isTrusted())
|| useWebAppCL ) {
try {
File f=new File( cm.getInstallDir(),
"lib/container/jasper.jar" );
URL url=new URL( "file", null,
f.getAbsolutePath().replace('\\','/') );
ctx.addClassPath( url );
if( debug > 9 ) log( "Added to classpath: " + url );
f=new File( System.getProperty( "java.home" ) +
"/../lib/tools.jar");
if( ! f.exists() ) {
// On some systems java.home gets set to the root of jdk.
// That's a bug, but we can work around and be nice.
f=new File( System.getProperty( "java.home" ) +
"/lib/tools.jar");
if( ! f.exists() ) {
log("Tools.jar not found " +
System.getProperty( "java.home" ));
} else {
log("Detected wrong java.home value " +
System.getProperty( "java.home" ));
}
}
url=new URL( "file", "" , f.getAbsolutePath() );
ctx.addClassPath( url );
if( debug > 9 ) log( "Added to classpath: " + url );
} catch( MalformedURLException ex ) {
ex.printStackTrace();
}
}
}
/** Do the needed initialization if jspServlet is used.
* It must be called after Web.xml is read ( WebXmlReader ).
*/
public void contextInit(Context ctx)
throws TomcatException
{
if(ctx.getContainer("*.jsp") == null)
ctx.addServletMapping( "*.jsp", "jsp");
if( useJspServlet ) {
// prepare jsp servlet.
Handler jasper=ctx.getServletByName( "jsp" );
if ( debug>10) log( "Got jasper servlet " + jasper );
ServletHandler jspServlet=(ServletHandler)jasper;
if( jspServlet.getServletClassName() == null ) {
log( "Jsp already defined in web.xml " +
jspServlet.getServletClassName() );
return;
}
if( debug>-1)
log( "jspServlet=" + jspServlet.getServletClassName());
Enumeration enum=args.keys();
while( enum.hasMoreElements() ) {
String s=(String)enum.nextElement();
String v=(String)args.get(s);
if( debug>0 ) log( "Setting " + s + "=" + v );
jspServlet.getServletInfo().addInitParam(s, v );
}
if( debug > 0 ) {
//enable jasperServlet logging
log( "Seetting debug on jsp servlet");
org.apache.jasper.Constants.jasperLog=
loghelper;
// org.apache.jasper.Constants.jasperLog.
// setVerbosityLevel("debug");
}
jspServlet.setServletClassName(jspServletCN);
}
if( useWebAppCL ) {
//Extra test/warnings for tools.jar
try {
ctx.getClassLoader().loadClass( "sun.tools.javac.Main" );
if( debug>0) log( "Found javac in context init");
} catch( ClassNotFoundException ex ) {
if( debug>0) log( "javac not found in context init");
}
}
}
/** Set the HttpJspBase classloader before init,
* as required by Jasper
*/
public void preServletInit( Context ctx, Handler sw )
throws TomcatException
{
if( ! (sw instanceof ServletHandler) )
return;
try {
// requires that everything is compiled
Servlet theServlet = ((ServletHandler)sw).getServlet();
if (theServlet instanceof HttpJspBase) {
if( debug > 9 )
log( "PreServletInit: HttpJspBase.setParentClassLoader" +
sw );
HttpJspBase h = (HttpJspBase) theServlet;
h.setClassLoader(ctx.getClassLoader());
}
} catch(Exception ex ) {
throw new TomcatException( ex );
}
}
//-------------------- Main hook - compile the jsp file if needed
/** Detect if the request is for a JSP page and if it is find
the associated servlet name and compile if needed.
That insures that init() will take place on the equivalent
servlet - and behave exactly like a servlet.
A request is for a JSP if:
- the handler is a ServletHandler ( i.e. defined in web.xml
or dynamically loaded servlet ) and it has a "path" instead of
class name
- the handler has a special name "jsp". That means a *.jsp -> jsp
needs to be defined. This is a tomcat-specific mechanism ( not
part of the standard ) and allow users to associate other extensions
with JSP by using the "fictious" jsp handler.
An (cleaner?) alternative for mapping other extensions would be
to set them on JspInterceptor.
*/
public int requestMap( Request req ) {
if( useJspServlet ) {
// no further processing - jspServlet will take care
// of the processing as before ( all processing
// will happen in the handle() pipeline.
return 0;
}
Handler wrapper=req.getHandler();
if( wrapper==null )
return 0;
// It's not a jsp if it's not "*.jsp" mapped or a servlet
if( (! "jsp".equals( wrapper.getName())) &&
(! (wrapper instanceof ServletHandler)) ) {
return 0;
}
ServletHandler handler=null;
String jspFile=null;
// There are 2 cases: extension mapped and exact map with
// a
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.