Package org.apache.jasper
Class JspCompilationContext
- java.lang.Object
-
- org.apache.jasper.JspCompilationContext
-
public class JspCompilationContext extends java.lang.ObjectA placeholder for various things that are used throughout the JSP engine. This is a per-request/per-context data structure. Some of the instance variables are set at different points. Most of the path-related stuff is here - mangling names, versions, dirs, loading resources and dealing with uris.
-
-
Constructor Summary
Constructors Constructor Description JspCompilationContext(java.lang.String tagfile, TagInfo tagInfo, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt, Jar tagJar)Creates a compilation context for a tag file.JspCompilationContext(java.lang.String jspUri, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt)Creates a compilation context for a JSP page.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static java.lang.StringcanonicalURI(java.lang.String s)Canonicalizes a URI string by resolving relative path components like '.' and '..'voidcheckOutputDir()Checks and creates the output directory if it does not exist.voidclearJspLoader()Clears the JSP class loader so it will be recreated on next access.voidcompile()Compiles the JSP if it is out of date with respect to the generated class file.CompilercreateCompiler()Create a "Compiler" object based on some init param data.protected CompilercreateCompiler(java.lang.String className)Creates a compiler instance by class name using reflection.protected voidcreateOutputDir()Creates the output directory path based on the package name and scratch directory.java.lang.StringgetBasePackageName()Returns the base package name into which all servlet and associated code is generated.java.lang.StringgetClassFileName()Returns the full path name of the compiled class file.java.lang.ClassLoadergetClassLoader()What class loader to use for loading classes while compiling this JSP?java.lang.StringgetClassPath()Returns the classpath used by the Java compiler for this JSP.CompilergetCompiler()Returns the compiler instance for this compilation context.protected java.lang.StringgetDerivedPackageName()Returns the derived package name based on the JSP URI path hierarchy.java.lang.StringgetFQCN()Returns the fully qualified class name of the generated servlet or tag handler.java.lang.StringgetJavaPath()Returns the path of the Java file relative to the work directory.java.lang.StringgetJspFile()Path of the JSP URI.java.lang.ClassLoadergetJspLoader()Returns the JSP-specific class loader, creating it if necessary.java.lang.LonggetLastModified(java.lang.String resource)Returns the last modified time of the given resource using the default tag JAR.java.lang.LonggetLastModified(java.lang.String resource, Jar tagJar)Returns the last modified time of the given resource.OptionsgetOptions()Returns the options object for this compilation context.java.lang.StringgetOutputDir()The output directory to generate code into.java.lang.StringgetRealPath(java.lang.String path)Gets the actual path of a URI relative to the context of the compilation.java.net.URLgetResource(java.lang.String res)Gets a resource as a URL, relative to the context of this compilation.java.io.InputStreamgetResourceAsStream(java.lang.String res)Gets a resource as a stream, relative to the meanings of this context's implementation.java.util.Set<java.lang.String>getResourcePaths(java.lang.String path)Gets the resource paths beneath the given path, relative to the context.JspRuntimeContextgetRuntimeContext()Returns the JSP runtime context for this compilation context.java.lang.StringgetServletClassName()Just the class name (does not include package name) of the generated class.ServletContextgetServletContext()Returns the servlet context for this compilation context.java.lang.StringgetServletJavaFileName()Returns the full path name of the Java file into which the servlet is being generated.java.lang.StringgetServletPackageName()Package name for the generated class is made up of the base package name, which is user settable, and the derived package name.JargetTagFileJar()Returns the JAR file in which the tag file for which this JspCompilationContext was created is packaged, or null if this JspCompilationContext does not correspond to a tag file, or if the corresponding tag file is not packaged in a JAR.TagInfogetTagInfo()Returns the tag information for this compilation context.TldResourcePathgetTldResourcePath(java.lang.String uri)Gets the 'location' of the TLD associated with the given taglib 'uri'.ServletWritergetWriter()Returns the writer used to write the generated Servlet source code.voidincrementRemoved()Increments the removed counter and removes the wrapper from the runtime context.protected static booleanisPathSeparator(char c)Checks whether the given character is a path separator.booleanisPrototypeMode()Returns whether we are compiling a tag file in prototype mode, i.e., generating code with class for the tag handler with empty method bodies.booleanisRemoved()Returns whether this JSP has been removed.booleanisTagFile()Returns whether this compilation context corresponds to a tag file.booleankeepGenerated()Returns whether the generated code is kept after compilation.java.lang.Class<?>load()Loads the compiled servlet class using the JSP class loader.protected booleanmakeOutputDir()Creates the output directory for generated files.java.lang.StringresolveRelativeUri(java.lang.String uri)Get the full value of a URI relative to this compilations context uses current file as the base.voidsetBasePackageName(java.lang.String basePackageName)The package name into which the servlet class is generated.voidsetClassLoader(java.lang.ClassLoader loader)Sets the class loader to use for loading classes while compiling this JSP.voidsetClassPath(java.lang.String classPath)The classpath that is passed off to the Java compiler.voidsetPrototypeMode(boolean pm)Sets the prototype mode for tag file compilation.voidsetServletClassName(java.lang.String className)Sets the class name for the generated servlet.voidsetTagFileJar(Jar tagJar)Sets the JAR file in which the tag file is packaged.voidsetTagInfo(TagInfo tagi)Sets the tag information for this compilation context.voidsetWriter(ServletWriter writer)Sets the writer used to write the generated Servlet source code.
-
-
-
Constructor Detail
-
JspCompilationContext
public JspCompilationContext(java.lang.String jspUri, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt)Creates a compilation context for a JSP page.- Parameters:
jspUri- The URI of the JSP page relative to the contextoptions- The compilation optionscontext- The servlet contextjsw- The JSP servlet wrapperrctxt- The JSP runtime context
-
JspCompilationContext
public JspCompilationContext(java.lang.String tagfile, TagInfo tagInfo, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt, Jar tagJar)Creates a compilation context for a tag file.- Parameters:
tagfile- The URI of the tag filetagInfo- The tag informationoptions- The compilation optionscontext- The servlet contextjsw- The JSP servlet wrapperrctxt- The JSP runtime contexttagJar- The JAR containing the tag file
-
-
Method Detail
-
getClassPath
public java.lang.String getClassPath()
Returns the classpath used by the Java compiler for this JSP.- Returns:
- the classpath that is passed off to the Java compiler
-
setClassPath
public void setClassPath(java.lang.String classPath)
The classpath that is passed off to the Java compiler.- Parameters:
classPath- The class path to use
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
What class loader to use for loading classes while compiling this JSP?- Returns:
- the class loader used to load all compiled classes
-
setClassLoader
public void setClassLoader(java.lang.ClassLoader loader)
Sets the class loader to use for loading classes while compiling this JSP.- Parameters:
loader- The class loader
-
getJspLoader
public java.lang.ClassLoader getJspLoader()
Returns the JSP-specific class loader, creating it if necessary.- Returns:
- the JSP class loader
-
clearJspLoader
public void clearJspLoader()
Clears the JSP class loader so it will be recreated on next access.
-
getOutputDir
public java.lang.String getOutputDir()
The output directory to generate code into. The output directory is make up of the scratch directory, which is provided in Options, plus the directory derived from the package name.- Returns:
- the output directory in which the generated sources are placed
-
createCompiler
public Compiler createCompiler()
Create a "Compiler" object based on some init param data. This is not done yet. Right now we're just hardcoding the actual compilers that are created.- Returns:
- the Java compiler wrapper
-
createCompiler
protected Compiler createCompiler(java.lang.String className)
Creates a compiler instance by class name using reflection.- Parameters:
className- The fully qualified compiler class name- Returns:
- the compiler instance, or
nullif the class cannot be loaded
-
getCompiler
public Compiler getCompiler()
Returns the compiler instance for this compilation context.- Returns:
- the compiler instance
-
resolveRelativeUri
public java.lang.String resolveRelativeUri(java.lang.String uri)
Get the full value of a URI relative to this compilations context uses current file as the base.- Parameters:
uri- The relative URI- Returns:
- absolute URI
-
getResourceAsStream
public java.io.InputStream getResourceAsStream(java.lang.String res)
Gets a resource as a stream, relative to the meanings of this context's implementation.- Parameters:
res- the resource to look for- Returns:
- a null if the resource cannot be found or represented as an InputStream.
-
getResource
public java.net.URL getResource(java.lang.String res) throws java.net.MalformedURLExceptionGets a resource as a URL, relative to the context of this compilation.- Parameters:
res- The resource path- Returns:
- the resource URL
- Throws:
java.net.MalformedURLException- If the resource URL is malformed
-
getResourcePaths
public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
Gets the resource paths beneath the given path, relative to the context.- Parameters:
path- The path to list resources for- Returns:
- the set of resource paths
-
getRealPath
public java.lang.String getRealPath(java.lang.String path)
Gets the actual path of a URI relative to the context of the compilation.- Parameters:
path- The webapp path- Returns:
- the corresponding path in the filesystem
-
getTagFileJar
public Jar getTagFileJar()
Returns the JAR file in which the tag file for which this JspCompilationContext was created is packaged, or null if this JspCompilationContext does not correspond to a tag file, or if the corresponding tag file is not packaged in a JAR.- Returns:
- a JAR file
-
setTagFileJar
public void setTagFileJar(Jar tagJar)
Sets the JAR file in which the tag file is packaged.- Parameters:
tagJar- The JAR file
-
getServletClassName
public java.lang.String getServletClassName()
Just the class name (does not include package name) of the generated class.- Returns:
- the class name
-
setServletClassName
public void setServletClassName(java.lang.String className)
Sets the class name for the generated servlet.- Parameters:
className- The class name
-
getJspFile
public java.lang.String getJspFile()
Path of the JSP URI. Note that this is not a file name. This is the context rooted URI of the JSP file.- Returns:
- the path to the JSP
-
getLastModified
public java.lang.Long getLastModified(java.lang.String resource)
Returns the last modified time of the given resource using the default tag JAR.- Parameters:
resource- The resource path- Returns:
- the last modified time in milliseconds, or -1 if not available
-
getLastModified
public java.lang.Long getLastModified(java.lang.String resource, Jar tagJar)Returns the last modified time of the given resource.- Parameters:
resource- The resource pathtagJar- The JAR file containing the resource, ornullfor webapp resources- Returns:
- the last modified time in milliseconds, or -1 if not available
-
isTagFile
public boolean isTagFile()
Returns whether this compilation context corresponds to a tag file.- Returns:
trueif this is a tag file
-
getTagInfo
public TagInfo getTagInfo()
Returns the tag information for this compilation context.- Returns:
- the tag info, or
nullif not a tag file
-
setTagInfo
public void setTagInfo(TagInfo tagi)
Sets the tag information for this compilation context.- Parameters:
tagi- The tag info
-
isPrototypeMode
public boolean isPrototypeMode()
Returns whether we are compiling a tag file in prototype mode, i.e., generating code with class for the tag handler with empty method bodies.- Returns:
trueif we are compiling a tag file in prototype mode
-
setPrototypeMode
public void setPrototypeMode(boolean pm)
Sets the prototype mode for tag file compilation.- Parameters:
pm- The prototype mode flag
-
getServletPackageName
public java.lang.String getServletPackageName()
Package name for the generated class is made up of the base package name, which is user settable, and the derived package name. The derived package name directly mirrors the file hierarchy of the JSP page.- Returns:
- the package name
-
getDerivedPackageName
protected java.lang.String getDerivedPackageName()
Returns the derived package name based on the JSP URI path hierarchy.- Returns:
- the derived package name
-
getBasePackageName
public java.lang.String getBasePackageName()
Returns the base package name into which all servlet and associated code is generated.- Returns:
- the base package name
-
setBasePackageName
public void setBasePackageName(java.lang.String basePackageName)
The package name into which the servlet class is generated.- Parameters:
basePackageName- The package name to use
-
getServletJavaFileName
public java.lang.String getServletJavaFileName()
Returns the full path name of the Java file into which the servlet is being generated.- Returns:
- the full path of the generated Java file
-
getOptions
public Options getOptions()
Returns the options object for this compilation context.- Returns:
- the options object
-
getServletContext
public ServletContext getServletContext()
Returns the servlet context for this compilation context.- Returns:
- the servlet context
-
getRuntimeContext
public JspRuntimeContext getRuntimeContext()
Returns the JSP runtime context for this compilation context.- Returns:
- the runtime context
-
getJavaPath
public java.lang.String getJavaPath()
Returns the path of the Java file relative to the work directory.- Returns:
- the relative path of the Java file
-
getClassFileName
public java.lang.String getClassFileName()
Returns the full path name of the compiled class file.- Returns:
- the class file path
-
getWriter
public ServletWriter getWriter()
Returns the writer used to write the generated Servlet source code.- Returns:
- the servlet writer
-
setWriter
public void setWriter(ServletWriter writer)
Sets the writer used to write the generated Servlet source code.- Parameters:
writer- The servlet writer
-
getTldResourcePath
public TldResourcePath getTldResourcePath(java.lang.String uri)
Gets the 'location' of the TLD associated with the given taglib 'uri'.- Parameters:
uri- The taglib URI- Returns:
- An array of two Strings: The first element denotes the real path to the TLD. If the path to the TLD points to a jar file, then the second element denotes the name of the TLD entry in the jar file. Returns null if the given uri is not associated with any tag library 'exposed' in the web application.
-
keepGenerated
public boolean keepGenerated()
Returns whether the generated code is kept after compilation.- Returns:
trueif generated code is kept
-
incrementRemoved
public void incrementRemoved()
Increments the removed counter and removes the wrapper from the runtime context.
-
isRemoved
public boolean isRemoved()
Returns whether this JSP has been removed.- Returns:
trueif this JSP has been removed
-
compile
public void compile() throws JasperException, java.io.FileNotFoundExceptionCompiles the JSP if it is out of date with respect to the generated class file.- Throws:
JasperException- If a compilation error occursjava.io.FileNotFoundException- If the JSP file has been removed
-
load
public java.lang.Class<?> load() throws JasperExceptionLoads the compiled servlet class using the JSP class loader.- Returns:
- the loaded servlet class
- Throws:
JasperException- If the class cannot be loaded
-
getFQCN
public java.lang.String getFQCN()
Returns the fully qualified class name of the generated servlet or tag handler.- Returns:
- the fully qualified class name
-
checkOutputDir
public void checkOutputDir()
Checks and creates the output directory if it does not exist.
-
makeOutputDir
protected boolean makeOutputDir()
Creates the output directory for generated files.- Returns:
trueif the directory was created or already exists
-
createOutputDir
protected void createOutputDir()
Creates the output directory path based on the package name and scratch directory.
-
isPathSeparator
protected static boolean isPathSeparator(char c)
Checks whether the given character is a path separator.- Parameters:
c- The character to check- Returns:
trueif the character is '/' or '\\'
-
canonicalURI
protected static java.lang.String canonicalURI(java.lang.String s)
Canonicalizes a URI string by resolving relative path components like '.' and '..' and collapsing multiple separators.- Parameters:
s- The URI string to canonicalize- Returns:
- the canonical URI, or
nullif the input isnull
-
-