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 java.util.Iterator;
22  
23  import javax.el.ExpressionFactory;
24  import javax.faces.FactoryFinder;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.event.PhaseListener;
27  import javax.faces.lifecycle.LifecycleFactory;
28  import javax.servlet.ServletContext;
29  import javax.servlet.jsp.JspApplicationContext;
30  import javax.servlet.jsp.JspFactory;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.myfaces.config.RuntimeConfig;
35  import org.apache.myfaces.el.ResolverForJSPInitializer;
36  import org.apache.myfaces.el.unified.ELResolverBuilder;
37  import org.apache.myfaces.el.unified.ResolverBuilderForJSP;
38  import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver;
39  import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.Scope;
40  
41  /**
42   * Initializes MyFaces in a JSP 2.1 environment.
43   *
44   */
45  public class Jsp21FacesInitializer extends AbstractFacesInitializer
46  {
47      /**
48       * The logger instance for this class.
49       */
50      private static final Log log = LogFactory.getLog(Jsp21FacesInitializer.class);
51      
52      /**
53       * Cached instance of the JspFactory to use.
54       */
55      private JspFactory jspFactory;
56      
57      @Override
58      protected void initContainerIntegration(
59              ServletContext servletContext, ExternalContext externalContext)
60      {
61          JspApplicationContext appCtx = 
62              getJspFactory().getJspApplicationContext(servletContext);
63          appCtx.addELContextListener(new FacesELContextListener());
64          
65          // check for user-specified ExpressionFactory
66          ExpressionFactory expressionFactory = getUserDefinedExpressionFactory(externalContext);
67          if (expressionFactory == null)
68          {
69              expressionFactory = appCtx.getExpressionFactory();
70          }
71  
72          RuntimeConfig runtimeConfig =
73              buildConfiguration(servletContext, externalContext, expressionFactory);
74          
75          // configure the el resolver for jsp
76          configureResolverForJSP(appCtx, runtimeConfig);
77      }
78      
79      protected JspFactory getJspFactory()
80      {
81          if (jspFactory == null) {
82              // TODO: this Class.forName will be removed when Tomcat fixes a bug
83              // also, we should then be able to remove jasper.jar from the deployment
84              try {
85                  Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
86              } catch (ClassNotFoundException e) {
87                  ; // ignore
88              } catch (Exception ex) {
89                  log.debug("An unexpected exception occured "
90                          + "while loading the JspRuntimeContext.", ex);
91              }
92  
93              jspFactory = JspFactory.getDefaultFactory();
94          }
95  
96          return jspFactory;
97      }
98  
99      /**
100      * Sets the JspFactory to use. Currently, this method just simplifies
101      * testing.
102      * 
103      * @param jspFactory
104      *            the JspFactory to use
105      */
106     protected void setJspFactory(JspFactory jspFactory)
107     {
108         this.jspFactory = jspFactory;
109     }
110 
111     /**
112      * Register a phase listener to every lifecycle. This listener will lazy fill the el resolver for jsp as soon as the
113      * first lifecycle is executed. This is necessarry to allow a faces application further setup after MyFaces has been
114      * initialized. When the first request is processed no further configuation of the el resolvers is allowed.
115      * 
116      * @param appCtx
117      * @param runtimeConfig
118      */
119     private void configureResolverForJSP(JspApplicationContext appCtx, RuntimeConfig runtimeConfig)
120     {
121         FacesCompositeELResolver facesCompositeELResolver = new FacesCompositeELResolver(Scope.JSP);
122         appCtx.addELResolver(facesCompositeELResolver);
123         PhaseListener resolverForJSPInitializer = new ResolverForJSPInitializer(
124                 createResolverBuilderForJSP(runtimeConfig), facesCompositeELResolver);
125 
126         LifecycleFactory factory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
127         for (Iterator<String> iter = factory.getLifecycleIds(); iter.hasNext();)
128         {
129             factory.getLifecycle(iter.next()).addPhaseListener(resolverForJSPInitializer);
130         }
131     }
132     
133     protected ELResolverBuilder createResolverBuilderForJSP(RuntimeConfig runtimeConfig)
134     {
135         return new ResolverBuilderForJSP(runtimeConfig);
136     }
137 }