Class JspCompilationContext


  • public class JspCompilationContext
    extends java.lang.Object
    A 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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.lang.String canonicalURI​(java.lang.String s)
      Canonicalizes a URI string by resolving relative path components like '.' and '..'
      void checkOutputDir()
      Checks and creates the output directory if it does not exist.
      void clearJspLoader()
      Clears the JSP class loader so it will be recreated on next access.
      void compile()
      Compiles the JSP if it is out of date with respect to the generated class file.
      Compiler createCompiler()
      Create a "Compiler" object based on some init param data.
      protected Compiler createCompiler​(java.lang.String className)
      Creates a compiler instance by class name using reflection.
      protected void createOutputDir()
      Creates the output directory path based on the package name and scratch directory.
      java.lang.String getBasePackageName()
      Returns the base package name into which all servlet and associated code is generated.
      java.lang.String getClassFileName()
      Returns the full path name of the compiled class file.
      java.lang.ClassLoader getClassLoader()
      What class loader to use for loading classes while compiling this JSP?
      java.lang.String getClassPath()
      Returns the classpath used by the Java compiler for this JSP.
      Compiler getCompiler()
      Returns the compiler instance for this compilation context.
      protected java.lang.String getDerivedPackageName()
      Returns the derived package name based on the JSP URI path hierarchy.
      java.lang.String getFQCN()
      Returns the fully qualified class name of the generated servlet or tag handler.
      java.lang.String getJavaPath()
      Returns the path of the Java file relative to the work directory.
      java.lang.String getJspFile()
      Path of the JSP URI.
      java.lang.ClassLoader getJspLoader()
      Returns the JSP-specific class loader, creating it if necessary.
      java.lang.Long getLastModified​(java.lang.String resource)
      Returns the last modified time of the given resource using the default tag JAR.
      java.lang.Long getLastModified​(java.lang.String resource, Jar tagJar)
      Returns the last modified time of the given resource.
      Options getOptions()
      Returns the options object for this compilation context.
      java.lang.String getOutputDir()
      The output directory to generate code into.
      java.lang.String getRealPath​(java.lang.String path)
      Gets the actual path of a URI relative to the context of the compilation.
      java.net.URL getResource​(java.lang.String res)
      Gets a resource as a URL, relative to the context of this compilation.
      java.io.InputStream getResourceAsStream​(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.
      JspRuntimeContext getRuntimeContext()
      Returns the JSP runtime context for this compilation context.
      java.lang.String getServletClassName()
      Just the class name (does not include package name) of the generated class.
      ServletContext getServletContext()
      Returns the servlet context for this compilation context.
      java.lang.String getServletJavaFileName()
      Returns the full path name of the Java file into which the servlet is being generated.
      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.
      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.
      TagInfo getTagInfo()
      Returns the tag information for this compilation context.
      TldResourcePath getTldResourcePath​(java.lang.String uri)
      Gets the 'location' of the TLD associated with the given taglib 'uri'.
      ServletWriter getWriter()
      Returns the writer used to write the generated Servlet source code.
      void incrementRemoved()
      Increments the removed counter and removes the wrapper from the runtime context.
      protected static boolean isPathSeparator​(char c)
      Checks whether the given character is a path separator.
      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.
      boolean isRemoved()
      Returns whether this JSP has been removed.
      boolean isTagFile()
      Returns whether this compilation context corresponds to a tag file.
      boolean keepGenerated()
      Returns whether the generated code is kept after compilation.
      java.lang.Class<?> load()
      Loads the compiled servlet class using the JSP class loader.
      protected boolean makeOutputDir()
      Creates the output directory for generated files.
      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.
      void setBasePackageName​(java.lang.String basePackageName)
      The package name into which the servlet class is generated.
      void setClassLoader​(java.lang.ClassLoader loader)
      Sets the class loader to use for loading classes while compiling this JSP.
      void setClassPath​(java.lang.String classPath)
      The classpath that is passed off to the Java compiler.
      void setPrototypeMode​(boolean pm)
      Sets the prototype mode for tag file compilation.
      void setServletClassName​(java.lang.String className)
      Sets the class name for the generated servlet.
      void setTagFileJar​(Jar tagJar)
      Sets the JAR file in which the tag file is packaged.
      void setTagInfo​(TagInfo tagi)
      Sets the tag information for this compilation context.
      void setWriter​(ServletWriter writer)
      Sets the writer used to write the generated Servlet source code.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 context
        options - The compilation options
        context - The servlet context
        jsw - The JSP servlet wrapper
        rctxt - 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 file
        tagInfo - The tag information
        options - The compilation options
        context - The servlet context
        jsw - The JSP servlet wrapper
        rctxt - The JSP runtime context
        tagJar - 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 null if 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.MalformedURLException
        Gets 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 path
        tagJar - The JAR file containing the resource, or null for 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:
        true if this is a tag file
      • getTagInfo

        public TagInfo getTagInfo()
        Returns the tag information for this compilation context.
        Returns:
        the tag info, or null if 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:
        true if 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:
        true if 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:
        true if this JSP has been removed
      • compile

        public void compile()
                     throws JasperException,
                            java.io.FileNotFoundException
        Compiles the JSP if it is out of date with respect to the generated class file.
        Throws:
        JasperException - If a compilation error occurs
        java.io.FileNotFoundException - If the JSP file has been removed
      • load

        public java.lang.Class<?> load()
                                throws JasperException
        Loads 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:
        true if 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:
        true if 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 null if the input is null