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