Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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.jasper.compiler;
 
 import static org.jboss.web.JasperMessages.MESSAGES;
 
 import java.io.File;
 import java.net.URL;
 import java.util.Map;
 
 
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(s):
Glenn L. Nielsen
Version:
$Revision: 1237 $
 
 public final class JspRuntimeContext {
 
     /*
      * Counts how many times the webapp's JSPs have been reloaded.
      */
     private int jspReloadCount;

    
Preload classes required at runtime by a JSP servlet so that we don't get a defineClassInPackage security exception.
 
     static {
         JspFactoryImpl factory = new JspFactoryImpl();
         SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader());
         if( System.getSecurityManager() != null ) {
             String basePackage = "org.apache.jasper.";
             try {
                 factory.getClass().getClassLoader().loadClassbasePackage +
                                                                "runtime.JspFactoryImpl$PrivilegedGetPageContext");
                 factory.getClass().getClassLoader().loadClassbasePackage +
                                                                "runtime.JspFactoryImpl$PrivilegedReleasePageContext");
                 factory.getClass().getClassLoader().loadClassbasePackage +
                                                                "runtime.JspRuntimeLibrary");
                 factory.getClass().getClassLoader().loadClassbasePackage +
                                                                "runtime.JspRuntimeLibrary$PrivilegedIntrospectHelper");
                 factory.getClass().getClassLoader().loadClassbasePackage +
                                                                "runtime.ServletResponseWrapperInclude");
                 factory.getClass().getClassLoader().loadClassbasePackage +
                                                                "servlet.JspServletWrapper");
             } catch (ClassNotFoundException ex) {
                 throw new IllegalStateException(ex);
             }
         }
 
         JspFactory.setDefaultFactory(factory);
     }
 
     // ----------------------------------------------------------- Constructors
 
    
Create a JspRuntimeContext for a web application context. Loads in any previously generated dependencies from file.

Parameters:
context ServletContext for web application
    public JspRuntimeContext(ServletContext contextOptions options) {
        this. = context;
        this. = options;
        // Get the parent class loader
        if ( == null) {
             = this.getClass().getClassLoader();
        }
                ? .toString() : "[none]");
        initClassPath();
        if (.) {
            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) {
             = System.currentTimeMillis();
        }                                            
    }
    // ----------------------------------------------------- Instance Variables

    
This web applications ServletContext
    private ServletContext context;
    private Options options;
    private CodeSource codeSource;                    
    private String classpath;
    private long lastCheck = -1L;

    
Maps JSP pages to their JspServletWrapper's
 
    // ------------------------------------------------------ Public Methods

    
Add a new JspServletWrapper.

Parameters:
jspUri JSP URI
jsw Servlet wrapper for JSP
    public void addWrapper(String jspUriJspServletWrapper jsw) {
        .put(jspUrijsw);
    }

    
Get an already existing JspServletWrapper.

Parameters:
jspUri JSP URI
Returns:
JspServletWrapper for JSP
    public JspServletWrapper getWrapper(String jspUri) {
        return .get(jspUri);
    }

    
Remove a JspServletWrapper.

Parameters:
jspUri JSP URI of JspServletWrapper to remove
    public void removeWrapper(String jspUri) {
        .remove(jspUri);
    }

    
Returns the number of JSPs for which JspServletWrappers exist, i.e., the number of JSPs that have been loaded into the webapp.

Returns:
The number of JSPs that have been loaded into the webapp
    public int getJspCount() {
        return .size();
    }

    
Get the SecurityManager Policy CodeSource for this web applicaiton context.

Returns:
CodeSource for JSP
    public CodeSource getCodeSource() {
        return ;
    }

    
Get the parent URLClassLoader.

Returns:
URLClassLoader parent
    public ClassLoader getParentClassLoader() {
        return ;
    }

    
Get the SecurityManager PermissionCollection for this web application context.

Returns:
PermissionCollection permissions
        return ;
    }

    
Process a "destory" event for this web application context.
                                                        
    public void destroy() {
        Iterator servlets = .values().iterator();
        while (servlets.hasNext()) {
            ((JspServletWrapperservlets.next()).destroy();
        }
    }

    
Increments the JSP reload counter.
    public synchronized void incrementJspReloadCount() {
        ++;
    }

    
Resets the JSP reload counter.

Parameters:
count Value to which to reset the JSP reload counter
    public synchronized void setJspReloadCount(int count) {
        this. = count;
    }

    
Gets the current value of the JSP reload counter.

Returns:
The current value of the JSP reload counter
    public int getJspReloadCount() {
        return ;
    }


    
Method used by background thread to check the JSP dependencies registered with this class for JSP's.
    public void checkCompile() {
        if ( < 0) {
            // Checking was disabled
            return;
        }
        long now = System.currentTimeMillis();
        if (now > ( + (.getCheckInterval() * 1000L))) {
             = now;
        } else {
            return;
        }
        
        Object [] wrappers = .values().toArray();
        for (int i = 0; i < wrappers.lengthi++ ) {
            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(.backgroundCompilationFailed(), t);
                }
            }
        }
    }

    
The classpath that is passed off to the Java compiler.
    public String getClassPath() {
        return ;
    }
    // -------------------------------------------------------- Private Methods


    
Method used to initialize classpath for compiles.
    private void initClassPath() {
        if (!( instanceof URLClassLoader)) {
            return;
        }
        
        URL [] urls = ((URLClassLoader).getURLs();
        StringBuilder cpath = new StringBuilder();
        String sep = System.getProperty("path.separator");
        for(int i = 0; i < urls.lengthi++) {
            // 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.
            
            ifurls[i].getProtocol().equals("file") ) {
                cpath.append((String)urls[i].getFile()+sep);
            }
        }    
	cpath.append(.getScratchDir() + sep);
        if (cp == null || cp.equals("")) {
            cp = .getClassPath();
        }
         = cpath.toString() + cp;
    }

    
Method used to initialize SecurityManager data.
    private void initSecurity() {
        // 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();
        ifpolicy != null ) {
            try {          
                // Get the permissions for the web app context
                String docBase = .getRealPath("/");
                ifdocBase == null ) {
                    docBase = .getScratchDir().toString();
                }
                String codeBase = docBase;
                if (!codeBase.endsWith(.)){
                    codeBase = codeBase + .;
                }
                File contextDir = new File(codeBase);
                URL url = contextDir.getCanonicalFile().toURL();
                 = new CodeSource(url,(Certificate[])null);
                 = policy.getPermissions();
                // Create a file read permission for web app context directory
                if (!docBase.endsWith(.)){
                    .add
                        (new FilePermission(docBase,"read"));
                    docBase = docBase + .;
                } else {
                    .add
                        (new FilePermission
                            (docBase.substring(0,docBase.length() - 1),"read"));
                }
                docBase = docBase + "-";
                .add(new FilePermission(docBase,"read"));
                // Create a file read permission for web app tempdir (work)
                // directory
                String workDir = .getScratchDir().toString();
                if (!workDir.endsWith(.)){
                    .add
                        (new FilePermission(workDir,"read"));
                    workDir = workDir + .;
                }
                workDir = workDir + "-";
                .add(new FilePermission(workDir,"read"));
                // Allow the JSP to access org.apache.jasper.runtime.HttpJspBase
                .addnew RuntimePermission(
                    "accessClassInPackage.org.apache.jasper.runtime") );
                if ( instanceof URLClassLoader) {
                    URL [] urls = ((URLClassLoader).getURLs();
                    String jarUrl = null;
                    String jndiUrl = null;
                    for (int i=0; i<urls.lengthi++) {
                        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) {
                        .add(
                                new FilePermission(jarUrl,"read"));
                        .add(
                                new FilePermission(jarUrl.substring(4),"read"));
                    }
                    if (jndiUrl != null)
                        .add(
                                new FilePermission(jndiUrl,"read") );
                }
            } catch(Exception e) {
                .log(.errorInitializingSecurity(), e);
            }
        }
    }
New to GrepCode? Check out our FAQ X