Coverage Report - org.apache.myfaces.webapp.Jsp20FacesInitializer
 
Classes in this File Line Coverage Branch Coverage Complexity
Jsp20FacesInitializer
0%
0/24
0%
0/16
3.167
Jsp20FacesInitializer$1
N/A
N/A
3.167
Jsp20FacesInitializer$ErrorExpressionFactory
0%
0/5
N/A
3.167
 
 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.logging.Level;
 22  
 import java.util.logging.Logger;
 23  
 
 24  
 import javax.el.ELContext;
 25  
 import javax.el.ExpressionFactory;
 26  
 import javax.el.MethodExpression;
 27  
 import javax.el.ValueExpression;
 28  
 import javax.faces.FacesException;
 29  
 import javax.faces.context.ExternalContext;
 30  
 import javax.servlet.ServletContext;
 31  
 
 32  
 /**
 33  
  * Initializes MyFaces in a JSP 2.0 (or less) environment.
 34  
  * 
 35  
  */
 36  0
 public class Jsp20FacesInitializer extends AbstractFacesInitializer
 37  
 {
 38  
     /**
 39  
      * The logger instance for this class.
 40  
      */
 41  
     //private static final Log log = LogFactory.getLog(Jsp20FacesInitializer.class);
 42  0
     private static final Logger log = Logger.getLogger(Jsp20FacesInitializer.class.getName());
 43  
 
 44  
     /**
 45  
      * The ExpressionFactory implementation of the EL-RI.
 46  
      */
 47  
     private static final String EL_RI_EXPRESSION_FACTORY_IMPL = "com.sun.el.ExpressionFactoryImpl";
 48  
 
 49  
     /**
 50  
      * Jasper's ExpressionFactory implementation.
 51  
      */
 52  
     private static final String JASPER_EL_EXPRESSION_FACTORY_IMPL = "org.apache.el.ExpressionFactoryImpl";
 53  
 
 54  
     /**
 55  
      * All known ExpressionFactory implementations.
 56  
      */
 57  0
     private static final String[] KNOWN_EXPRESSION_FACTORIES =
 58  
             new String[] { EL_RI_EXPRESSION_FACTORY_IMPL, JASPER_EL_EXPRESSION_FACTORY_IMPL };
 59  
 
 60  
     @Override
 61  
     protected void initContainerIntegration(ServletContext servletContext, ExternalContext externalContext)
 62  
     {
 63  0
         if (log.isLoggable(Level.INFO))
 64  
         {
 65  0
             log.info("This application isn't running in a JSP 2.1 container.");
 66  
         }
 67  
 
 68  
         // It's possible to run MyFaces in a JSP 2.0 Container, but the user has to provide
 69  
         // the ExpressionFactory implementation to use as there is no JspApplicationContext
 70  
         // we could ask for. Having said that, though, the user only has to provide it, if
 71  
         // there is no known ExpressionFactory available (i.e. if neither
 72  
         // "com.sun.el.ExpressionFactoryImpl" nor "org.apache.el.ExpressionFactoryImpl"
 73  
         // are available).
 74  0
         ExpressionFactory expressionFactory = getUserDefinedExpressionFactory(externalContext);
 75  
 
 76  0
         if (expressionFactory == null)
 77  
         {
 78  0
             if (log.isLoggable(Level.INFO))
 79  
             {
 80  0
                 log.info("Either you haven't specified the ExpressionFactory implementation, or an " 
 81  
                         + "error occured while instantiating the implementation you've specified. "
 82  
                         + "However, attempting to load a known implementation.");
 83  
             }
 84  
 
 85  0
             expressionFactory = findExpressionFactory(KNOWN_EXPRESSION_FACTORIES);
 86  0
             if (expressionFactory == null)
 87  
             { // if we still haven't got a valid implementation
 88  0
                 if (log.isLoggable(Level.SEVERE))
 89  
                 {
 90  0
                     log.severe("No valid ExpressionFactory implementation is available "
 91  
                             + "but that's required as this application isn't running in a JSP 2.1 container.");
 92  
                 }
 93  
 
 94  
                 // use a dummy implementation that reports the error again
 95  0
                 expressionFactory = new ErrorExpressionFactory();
 96  
             }
 97  
         }
 98  
 
 99  0
         if (log.isLoggable(Level.FINE))
 100  
         {
 101  0
             log.fine("The following ExpressionFactory implementation will " + "be used: '" + expressionFactory + "'.");
 102  
         }
 103  
 
 104  0
         buildConfiguration(servletContext, externalContext, expressionFactory);
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Attempts to find a valid ExpressionFactory implementation. Each of the given
 109  
      * "ExpressionFactory implementation candidates" will be tried to instantiate. If an attempt succeeded, the
 110  
      * ExpressionFactory implementation will be returned (i.e. the first valid ExpressionFactory implementation will be
 111  
      * returned) and if no attempt succeeds, <code>null</code> will be returned.
 112  
      * 
 113  
      * @param expressionFactoryClassNames
 114  
      *            "ExpresionFactory implementation candidates"
 115  
      * 
 116  
      * @return the newly created ExpressionFactory implementation, or <code>null</code>, if there is no valid
 117  
      *         implementation
 118  
      */
 119  
     private static ExpressionFactory findExpressionFactory(String[] expressionFactoryClassNames)
 120  
     {
 121  0
         for (String expressionFactoryClassName : expressionFactoryClassNames)
 122  
         {
 123  0
             ExpressionFactory expressionFactory = loadExpressionFactory(expressionFactoryClassName);
 124  0
             if (expressionFactory != null)
 125  
             {
 126  0
                 return expressionFactory;
 127  
             }
 128  
         }
 129  
 
 130  0
         return null;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Dummy implementation informing the user that there is no valid ExpressionFactory implementation available. This
 135  
      * class makes it easier for the user to understand why the application crashes. Otherwise he would have to deal
 136  
      * with NullPointerExceptions.
 137  
      * 
 138  
      */
 139  0
     private class ErrorExpressionFactory extends ExpressionFactory
 140  
     {
 141  
 
 142  
         @Override
 143  
         public Object coerceToType(Object obj, Class<?> targetType)
 144  
         {
 145  0
             throw new FacesException("No valid ExpressionFactory implementation is available "
 146  
                     + "but that's required as this application isn't running in a JSP 2.1 container.");
 147  
         }
 148  
 
 149  
         @Override
 150  
         public MethodExpression createMethodExpression(ELContext context, String expression,
 151  
                                                        Class<?> expectedReturnType, Class<?>[] expectedParamTypes)
 152  
         {
 153  0
             throw new FacesException("No valid ExpressionFactory implementation is available "
 154  
                     + "but that's required as this application isn't running in a JSP 2.1 container.");
 155  
         }
 156  
 
 157  
         @Override
 158  
         public ValueExpression createValueExpression(Object instance, Class<?> expectedType)
 159  
         {
 160  0
             throw new FacesException("No valid ExpressionFactory implementation is available "
 161  
                     + "but that's required as this application isn't running in a JSP 2.1 container.");
 162  
         }
 163  
 
 164  
         @Override
 165  
         public ValueExpression createValueExpression(ELContext context, String expression, Class<?> expectedType)
 166  
         {
 167  0
             throw new FacesException("No valid ExpressionFactory implementation is available "
 168  
                     + "but that's required as this application isn't running in a JSP 2.1 container.");
 169  
         }
 170  
 
 171  
     }
 172  
 
 173  
 }