FileDocCategorySizeDatePackage
JspRuntimeContext.javaAPI DocApache Tomcat 6.0.1415991Fri Jul 20 04:20:36 BST 2007org.apache.jasper.compiler

JspRuntimeContext

public final class JspRuntimeContext extends Object
Class for tracking JSP compile time file dependencies when the &060;%@include file="..."%&062; directive is used. A background thread periodically checks the files a JSP page is dependent upon. If a dpendent file changes the JSP page which included it is recompiled. Only used if a web application context is a directory.
author
Glenn L. Nielsen
version
$Revision: 505593 $

Fields Summary
private org.apache.juli.logging.Log
log
private int
jspReloadCount
private ServletContext
context
This web applications ServletContext
private org.apache.jasper.Options
options
private URLClassLoader
parentClassLoader
private PermissionCollection
permissionCollection
private CodeSource
codeSource
private String
classpath
private long
lastCheck
private Map
jsps
Maps JSP pages to their JspServletWrapper's
Constructors Summary
public JspRuntimeContext(ServletContext context, org.apache.jasper.Options options)
Create a JspRuntimeContext for a web application context. Loads in any previously generated dependencies from file.

param
context ServletContext for web application


                           
     
        JspFactoryImpl factory = new JspFactoryImpl();
        SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader());
        if( System.getSecurityManager() != null ) {
            String basePackage = "org.apache.jasper.";
            try {
                factory.getClass().getClassLoader().loadClass( basePackage +
                                                               "runtime.JspFactoryImpl$PrivilegedGetPageContext");
                factory.getClass().getClassLoader().loadClass( basePackage +
                                                               "runtime.JspFactoryImpl$PrivilegedReleasePageContext");
                factory.getClass().getClassLoader().loadClass( basePackage +
                                                               "runtime.JspRuntimeLibrary");
                factory.getClass().getClassLoader().loadClass( basePackage +
                                                               "runtime.JspRuntimeLibrary$PrivilegedIntrospectHelper");
                factory.getClass().getClassLoader().loadClass( basePackage +
                                                               "runtime.ServletResponseWrapperInclude");
                factory.getClass().getClassLoader().loadClass( basePackage +
                                                               "servlet.JspServletWrapper");
            } catch (ClassNotFoundException ex) {
                throw new IllegalStateException(ex);
            }
        }

        JspFactory.setDefaultFactory(factory);
    

        this.context = context;
        this.options = options;

        // Get the parent class loader
        parentClassLoader =
            (URLClassLoader) Thread.currentThread().getContextClassLoader();
        if (parentClassLoader == null) {
            parentClassLoader =
                (URLClassLoader)this.getClass().getClassLoader();
        }

	if (log.isDebugEnabled()) {
	    if (parentClassLoader != null) {
		log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is",
					       parentClassLoader.toString()));
	    } else {
		log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is",
					       "<none>"));
	    }
        }

        initClassPath();

	if (context instanceof org.apache.jasper.servlet.JspCServletContext) {
	    return;
	}

        if (Constants.IS_SECURITY_ENABLED) {
            initSecurity();
        }

        // If this web application context is running from a
        // directory, start the background compilation thread
        String appBase = context.getRealPath("/");         
        if (!options.getDevelopment()
                && appBase != null
                && options.getCheckInterval() > 0) {
            lastCheck = System.currentTimeMillis();
        }                                            
    
Methods Summary
public voidaddWrapper(java.lang.String jspUri, org.apache.jasper.servlet.JspServletWrapper jsw)
Add a new JspServletWrapper.

param
jspUri JSP URI
param
jsw Servlet wrapper for JSP

 

    // ------------------------------------------------------ Public Methods

                       
          
        jsps.put(jspUri, jsw);
    
public voidcheckCompile()
Method used by background thread to check the JSP dependencies registered with this class for JSP's.


        if (lastCheck < 0) {
            // Checking was disabled
            return;
        }
        long now = System.currentTimeMillis();
        if (now > (lastCheck + (options.getCheckInterval() * 1000L))) {
            lastCheck = now;
        } else {
            return;
        }
        
        Object [] wrappers = jsps.values().toArray();
        for (int i = 0; i < wrappers.length; i++ ) {
            JspServletWrapper jsw = (JspServletWrapper)wrappers[i];
            JspCompilationContext ctxt = jsw.getJspEngineContext();
            // JspServletWrapper also synchronizes on this when
            // it detects it has to do a reload
            synchronized(jsw) {
                try {
                    ctxt.compile();
                } catch (FileNotFoundException ex) {
                    ctxt.incrementRemoved();
                } catch (Throwable t) {
                    jsw.getServletContext().log("Background compile failed",
						t);
                }
            }
        }

    
public voiddestroy()
Process a "destory" event for this web application context.

        Iterator servlets = jsps.values().iterator();
        while (servlets.hasNext()) {
            ((JspServletWrapper) servlets.next()).destroy();
        }
    
public java.lang.StringgetClassPath()
The classpath that is passed off to the Java compiler.

        return classpath;
    
public java.security.CodeSourcegetCodeSource()
Get the SecurityManager Policy CodeSource for this web applicaiton context.

return
CodeSource for JSP

        return codeSource;
    
public intgetJspCount()
Returns the number of JSPs for which JspServletWrappers exist, i.e., the number of JSPs that have been loaded into the webapp.

return
The number of JSPs that have been loaded into the webapp

        return jsps.size();
    
public intgetJspReloadCount()
Gets the current value of the JSP reload counter.

return
The current value of the JSP reload counter

        return jspReloadCount;
    
public java.net.URLClassLoadergetParentClassLoader()
Get the parent URLClassLoader.

return
URLClassLoader parent

        return parentClassLoader;
    
public java.security.PermissionCollectiongetPermissionCollection()
Get the SecurityManager PermissionCollection for this web application context.

return
PermissionCollection permissions

        return permissionCollection;
    
public org.apache.jasper.servlet.JspServletWrappergetWrapper(java.lang.String jspUri)
Get an already existing JspServletWrapper.

param
jspUri JSP URI
return
JspServletWrapper for JSP

        return jsps.get(jspUri);
    
public synchronized voidincrementJspReloadCount()
Increments the JSP reload counter.

        jspReloadCount++;
    
private voidinitClassPath()
Method used to initialize classpath for compiles.


        URL [] urls = parentClassLoader.getURLs();
        StringBuffer cpath = new StringBuffer();
        String sep = System.getProperty("path.separator");

        for(int i = 0; i < urls.length; i++) {
            // Tomcat 4 can use URL's other than file URL's,
            // a protocol other than file: will generate a
            // bad file system path, so only add file:
            // protocol URL's to the classpath.
            
            if( urls[i].getProtocol().equals("file") ) {
                cpath.append((String)urls[i].getFile()+sep);
            }
        }    

	cpath.append(options.getScratchDir() + sep);

        String cp = (String) context.getAttribute(Constants.SERVLET_CLASSPATH);
        if (cp == null || cp.equals("")) {
            cp = options.getClassPath();
        }

        classpath = cpath.toString() + cp;

        if(log.isDebugEnabled()) {
            log.debug("Compilation classpath initialized: " + getClassPath());
        }
    
private voidinitSecurity()
Method used to initialize SecurityManager data.


        // Setup the PermissionCollection for this web app context
        // based on the permissions configured for the root of the
        // web app context directory, then add a file read permission
        // for that directory.
        Policy policy = Policy.getPolicy();
        if( policy != null ) {
            try {          
                // Get the permissions for the web app context
                String docBase = context.getRealPath("/");
                if( docBase == null ) {
                    docBase = options.getScratchDir().toString();
                }
                String codeBase = docBase;
                if (!codeBase.endsWith(File.separator)){
                    codeBase = codeBase + File.separator;
                }
                File contextDir = new File(codeBase);
                URL url = contextDir.getCanonicalFile().toURL();
                codeSource = new CodeSource(url,(Certificate[])null);
                permissionCollection = policy.getPermissions(codeSource);

                // Create a file read permission for web app context directory
                if (!docBase.endsWith(File.separator)){
                    permissionCollection.add
                        (new FilePermission(docBase,"read"));
                    docBase = docBase + File.separator;
                } else {
                    permissionCollection.add
                        (new FilePermission
                            (docBase.substring(0,docBase.length() - 1),"read"));
                }
                docBase = docBase + "-";
                permissionCollection.add(new FilePermission(docBase,"read"));

                // Create a file read permission for web app tempdir (work)
                // directory
                String workDir = options.getScratchDir().toString();
                if (!workDir.endsWith(File.separator)){
                    permissionCollection.add
                        (new FilePermission(workDir,"read"));
                    workDir = workDir + File.separator;
                }
                workDir = workDir + "-";
                permissionCollection.add(new FilePermission(workDir,"read"));

                // Allow the JSP to access org.apache.jasper.runtime.HttpJspBase
                permissionCollection.add( new RuntimePermission(
                    "accessClassInPackage.org.apache.jasper.runtime") );

                if (parentClassLoader instanceof URLClassLoader) {
                    URL [] urls = parentClassLoader.getURLs();
                    String jarUrl = null;
                    String jndiUrl = null;
                    for (int i=0; i<urls.length; i++) {
                        if (jndiUrl == null
                                && urls[i].toString().startsWith("jndi:") ) {
                            jndiUrl = urls[i].toString() + "-";
                        }
                        if (jarUrl == null
                                && urls[i].toString().startsWith("jar:jndi:")
                                ) {
                            jarUrl = urls[i].toString();
                            jarUrl = jarUrl.substring(0,jarUrl.length() - 2);
                            jarUrl = jarUrl.substring(0,
                                     jarUrl.lastIndexOf('/")) + "/-";
                        }
                    }
                    if (jarUrl != null) {
                        permissionCollection.add(
                                new FilePermission(jarUrl,"read"));
                        permissionCollection.add(
                                new FilePermission(jarUrl.substring(4),"read"));
                    }
                    if (jndiUrl != null)
                        permissionCollection.add(
                                new FilePermission(jndiUrl,"read") );
                }
            } catch(Exception e) {
                context.log("Security Init for context failed",e);
            }
        }
    
public voidremoveWrapper(java.lang.String jspUri)
Remove a JspServletWrapper.

param
jspUri JSP URI of JspServletWrapper to remove

        jsps.remove(jspUri);
    
public synchronized voidsetJspReloadCount(int count)
Resets the JSP reload counter.

param
count Value to which to reset the JSP reload counter

        this.jspReloadCount = count;