View Javadoc

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