Coverage Report - org.apache.myfaces.webapp.AbstractFacesInitializer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractFacesInitializer
0%
0/49
0%
0/28
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.webapp;
 20  
 
 21  
 import java.util.List;
 22  
 
 23  
 import javax.el.ExpressionFactory;
 24  
 import javax.faces.context.ExternalContext;
 25  
 import javax.servlet.ServletContext;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 import org.apache.myfaces.application.ApplicationImpl;
 30  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 31  
 import org.apache.myfaces.config.FacesConfigValidator;
 32  
 import org.apache.myfaces.config.FacesConfigurator;
 33  
 import org.apache.myfaces.config.RuntimeConfig;
 34  
 import org.apache.myfaces.context.servlet.ServletExternalContextImpl;
 35  
 import org.apache.myfaces.shared_impl.util.StateUtils;
 36  
 import org.apache.myfaces.shared_impl.webapp.webxml.WebXml;
 37  
 
 38  
 /**
 39  
  * Performs common initialization tasks.
 40  
  *
 41  
  */
 42  0
 public abstract class AbstractFacesInitializer implements FacesInitializer
 43  
 {
 44  
     /**
 45  
      * The logger instance for this class.
 46  
      */
 47  0
     private static final Log log = LogFactory.getLog(AbstractFacesInitializer.class);
 48  
     
 49  
     /**
 50  
      * This parameter specifies the ExpressionFactory implementation to use.
 51  
      */
 52  
     @JSFWebConfigParam(since="1.2.7")
 53  
     protected static final String EXPRESSION_FACTORY = "org.apache.myfaces.EXPRESSION_FACTORY";
 54  
 
 55  
     /**
 56  
      * Performs all necessary initialization tasks like configuring this JSF
 57  
      * application.
 58  
      */
 59  
     public void initFaces(ServletContext servletContext)
 60  
     {
 61  
         try {
 62  0
             if (log.isTraceEnabled()) {
 63  0
                 log.trace("Initializing MyFaces");
 64  
             }
 65  
 
 66  
             // Some parts of the following configuration tasks have been implemented 
 67  
             // by using an ExternalContext. However, that's no problem as long as no 
 68  
             // one tries to call methods depending on either the ServletRequest or 
 69  
             // the ServletResponse.
 70  0
             ExternalContext externalContext = new ServletExternalContextImpl(
 71  
                     servletContext, null, null);
 72  
 
 73  
             // Parse and validate the web.xml configuration file
 74  0
             WebXml webXml = WebXml.getWebXml(externalContext);
 75  0
             if (webXml == null) {
 76  0
                 if (log.isWarnEnabled()) {
 77  0
                     log.warn("Couldn't find the web.xml configuration file. "
 78  
                             + "Abort initializing MyFaces.");
 79  
                 }
 80  
 
 81  0
                 return;
 82  0
             } else if (webXml.getFacesServletMappings().isEmpty()) {
 83  0
                 if (log.isWarnEnabled()) {
 84  0
                     log.warn("No mappings of FacesServlet found. Abort initializing MyFaces.");
 85  
                 }
 86  
 
 87  0
                 return;
 88  
             }
 89  
             
 90  0
             initContainerIntegration(servletContext, externalContext);
 91  
 
 92  0
             String useEncryption = servletContext.getInitParameter(StateUtils.USE_ENCRYPTION);
 93  0
             if (!"false".equals(useEncryption)) { // the default value is true
 94  0
                 StateUtils.initSecret(servletContext);
 95  
             }
 96  
 
 97  0
             if (log.isInfoEnabled()) {
 98  0
                 log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized.");
 99  
             }
 100  0
         } catch (Exception ex) {
 101  0
             log.error("An error occured while initializing MyFaces: "
 102  
                     + ex.getMessage(), ex);
 103  0
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Cleans up all remaining resources (well, theoretically).
 108  
      * 
 109  
      */
 110  
     public void destroyFaces(ServletContext servletContext)
 111  
     {
 112  
         // TODO is it possible to make a real cleanup?
 113  0
     }
 114  
 
 115  
     /**
 116  
      * Configures this JSF application. It's required that every
 117  
      * FacesInitializer (i.e. every subclass) calls this method during
 118  
      * initialization.
 119  
      * 
 120  
      * @param servletContext
 121  
      *            the current ServletContext
 122  
      * @param externalContext
 123  
      *            the current ExternalContext
 124  
      * @param expressionFactory
 125  
      *            the ExpressionFactory to use
 126  
      * 
 127  
      * @return the current runtime configuration
 128  
      */
 129  
     protected RuntimeConfig buildConfiguration(ServletContext servletContext,
 130  
             ExternalContext externalContext, ExpressionFactory expressionFactory)
 131  
     {
 132  0
         RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext);
 133  0
         runtimeConfig.setExpressionFactory(expressionFactory);
 134  
         
 135  0
         ApplicationImpl.setInitializingRuntimeConfig(runtimeConfig);
 136  
         
 137  
         // And configure everything
 138  0
         new FacesConfigurator(externalContext).configure();
 139  
         
 140  0
         validateFacesConfig(servletContext, externalContext);
 141  
         
 142  0
         return runtimeConfig;
 143  
     }
 144  
     
 145  
     protected void validateFacesConfig(ServletContext servletContext, ExternalContext externalContext)
 146  
     {
 147  0
         String validate = servletContext.getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM);
 148  0
         if ("true".equals(validate) && log.isWarnEnabled()) { // the default value is false
 149  0
             List<String> warnings = FacesConfigValidator.validate(
 150  
                     externalContext, servletContext.getRealPath("/"));
 151  
             
 152  0
             for (String warning : warnings) {
 153  0
                 log.warn(warning);
 154  
             }
 155  
         }
 156  0
     }
 157  
     
 158  
     /**
 159  
      * Try to load user-definied ExpressionFactory. Returns <code>null</code>,
 160  
      * if no custom ExpressionFactory was specified. 
 161  
      * 
 162  
      * @param externalContext the current ExternalContext
 163  
      * 
 164  
      * @return User-specified ExpressionFactory, or 
 165  
      *          <code>null</code>, if no no custom implementation was specified
 166  
      * 
 167  
      */
 168  
     protected static ExpressionFactory getUserDefinedExpressionFactory(ExternalContext externalContext)
 169  
     {
 170  0
         String expressionFactoryClassName = externalContext.getInitParameter(EXPRESSION_FACTORY);
 171  0
         if (expressionFactoryClassName != null
 172  
                 && expressionFactoryClassName.trim().length() > 0) {
 173  0
             if (log.isDebugEnabled()) {
 174  0
                 log.debug("Attempting to load the ExpressionFactory implementation " 
 175  
                         + "you've specified: '" + expressionFactoryClassName + "'.");
 176  
             }
 177  
             
 178  0
             return loadExpressionFactory(expressionFactoryClassName);
 179  
         }
 180  
         
 181  0
         return null;
 182  
     }
 183  
     
 184  
     /**
 185  
      * Loads and instantiates the given ExpressionFactory implementation.
 186  
      * 
 187  
      * @param expressionFactoryClassName
 188  
      *            the class name of the ExpressionFactory implementation
 189  
      * 
 190  
      * @return the newly created ExpressionFactory implementation, or
 191  
      *         <code>null</code>, if an error occurred
 192  
      */
 193  
     protected static ExpressionFactory loadExpressionFactory(String expressionFactoryClassName) 
 194  
     {
 195  
        try {
 196  0
            Class<?> expressionFactoryClass = Class.forName(expressionFactoryClassName);
 197  0
            return (ExpressionFactory) expressionFactoryClass.newInstance();
 198  0
        } catch (Exception ex) {
 199  0
            if (log.isDebugEnabled()) {
 200  0
                log.debug("An error occured while instantiating a new ExpressionFactory. " 
 201  
                    + "Attempted to load class '" + expressionFactoryClassName + "'.", ex);
 202  
            }
 203  
        }
 204  
        
 205  0
        return null;
 206  
     }
 207  
 
 208  
     /**
 209  
      * Performs initialization tasks depending on the current environment.
 210  
      * 
 211  
      * @param servletContext
 212  
      *            the current ServletContext
 213  
      * @param externalContext
 214  
      *            the current ExternalContext
 215  
      */
 216  
     protected abstract void initContainerIntegration(
 217  
             ServletContext servletContext, ExternalContext externalContext);
 218  
 
 219  
 }