<commit_before>/* Copyright 2005 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.tools.checks;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This context listener executes SafeDelegatingCheckRunner, logs its results,
 * and exposes its results in the ServletContext.  
 * 
 * The intent is that the ICheckRunner delegate of SafeDelegatingCheckRunner,
 * configured in Spring, be configured to run useful runtime sanity checks that will
 * help uPortal deployers to understand what went wrong in their deployments
 * when dependencies are not present.
 * 
 * 
 * The current implementation logs successes only via Commons Logging whereas
 * it logs failures both via Commons Logging and via the Servlet API's logging 
 * mechanism and to System.err.  This overkill of logging is intended to give the 
 * primary intended audience --
 * new uPortal deployers who may not yet be comfortable with configuring uPortal
 * logging -- the maximum chance of encountering the output.
 * 
 * Subclassing this class: this class is designed to be subclassed to replace the
 * implementation of the method that logs the results from the SafeDelegatingCheckRunner.
 * 
 * @author <EMAIL>
 * @version $Revision$ $Date$
 */
public class CheckingContextListener 
    implements ServletContextListener {
    
    /**
     * The name of the servlet context attribute which this listener will set at
     * context initialization to contain the List of CheckAndResult instances representing
     * the results of running the configured checks.
     */
    public static final String RESULTS_SC_KEY = "org.jasig.portal.tools.checks.CheckingContextListener.RESULTS";
    
    protected final Log log = LogFactory.getLog(getClass());
    
    public final void contextInitialized(ServletContextEvent sce) {
        
        ServletContext servletContext = sce.getServletContext();
        
        SafeDelegatingCheckRunner checkRunner = new SafeDelegatingCheckRunner();
            
        List results = checkRunner.doChecks();
        
        logResults(results, servletContext);
        
        // we've already logged the results of each individual check.  Now we
        // expose the results of these checks in the ServletContext so that 
        // views such as JSPs, channels can render them.
        
        servletContext.setAttribute(RESULTS_SC_KEY, results);
 
    }

    public void contextDestroyed(ServletContextEvent sce) {
        // do nothing
    }

   
    
    /**
     * Log the results reported by the check runner.
     * 
     * You can subclass this class and override this method to change the
     * logging behavior.
     * 
     * @param results List of CheckAndResult instances
     */
    protected void logResults(List results, ServletContext servletContext) {
        
        if (results == null) {
            log.error("Cannot log null results.");<commit_msg>UP-853 better JavaDoc comments about intended extension of context listener.<commit_after>/* Copyright 2005 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.tools.checks;

import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This context listener executes SafeDelegatingCheckRunner, logs its results,
 * and exposes its results in the ServletContext.  
 * 
 * The intent is that the ICheckRunner delegate of SafeDelegatingCheckRunner,
 * configured in Spring, be configured to run useful runtime sanity checks that will
 * help uPortal deployers to understand what went wrong in their deployments
 * when dependencies are not present.
 * 
 * 
 * The current implementation logs successes only via Commons Logging whereas
 * it logs failures via Commons Logging and via the Servlet API's logging 
 * mechanism and to System.err.  This overkill of logging is intended to give the 
 * primary intended audience --
 * new uPortal deployers who may not yet be comfortable with configuring uPortal
 * logging -- the maximum chance of encountering the output.
 * 
 * The intent is that in a healthy happy production uPortal deployment, no
 * checks will fail and so this overkill of logging won't be a problem.
 * 
 * Subclassing this class: this class is designed to be subclassed to replace the
 * implementation of the method that logs the results from the SafeDelegatingCheckRunner.
 * In this way you can implement some other logging strategy.
 * 
 * @author <EMAIL>
 * @version $Revision$ $Date$
 */
public class CheckingContextListener 
    implements ServletContextListener {
    
    /**
     * The name of the servlet context attribute which this listener will set at
     * context initialization to contain the List of CheckAndResult instances representing
     * the results of running the configured checks.
     */
    public static final String RESULTS_SC_KEY = "org.jasig.portal.tools.checks.CheckingContextListener.RESULTS";
    
    protected final Log log = LogFactory.getLog(getClass());
    
    public final void contextInitialized(ServletContextEvent sce) {
        /*
         * This method is final because the intended extension strategy for this 
         * class is to accept this default implementation of check execution but to
         * override the logResults method to implement alternative logging or other
         * handling of the results.
         */
        
        ServletContext servletContext = sce.getServletContext();
        
        SafeDelegatingCheckRunner checkRunner = new SafeDelegatingCheckRunner();
            
        List results = checkRunner.doChecks();
        
        logResults(results, servletContext);
        
        // we've already logged the results of each individual check.  Now we
        // expose the results of these checks in the ServletContext so that 
        // views such as JSPs, channels can render them.
        
        servletContext.setAttribute(RESULTS_SC_KEY, results);
 
    }

    public void contextDestroyed(ServletContextEvent sce) {
        // do nothing
    }

   
    
    /**
     * Log the results reported by the check runner.
     * 
     * You can subclass this class and override this method to change the
     * logging behavior.  You can also, by overriding this method and throwing
     * a RuntimeException, veto the loading of the context.
     * 
     * The default implementation of this method is safe such that it will not throw.
     * Since the rest of contextInitialized is also safe, the default implementation
     * of this ContextListener is safe and will not, no matter how abjectly the
     * checks fail, itself abort the context initialization.
     * 
     * @param results List of CheckAndResult instances
     * @param servletContext the context in which we're running
     */
    protected void logResults(List results, ServletContext servletContext) {
        
        if (results == null) {
            log.error("Cannot log null results.");
{'fraction_non_alphanumeric': 0.05748344370860927, 'fraction_numerical': 0.001456953642384106, 'mean_word_length': 3.1307439824945296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7487449', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1802, 'n_words': 787}