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