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          {
84              // TODO: this Class.forName will be removed when Tomcat fixes a bug
85              // also, we should then be able to remove jasper.jar from the deployment
86              try
87              {
88                  Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
89              }
90              catch (ClassNotFoundException e)
91              {
92                  // ignore
93              }
94              catch (Exception ex)
95              {
96                  log.log(Level.FINE, "An unexpected exception occured "
97                          + "while loading the JspRuntimeContext.", ex);
98              }
99  
100             jspFactory = JspFactory.getDefaultFactory();
101         }
102 
103         return jspFactory;
104     }
105 
106     /**
107      * Sets the JspFactory to use. Currently, this method just simplifies
108      * testing.
109      * 
110      * @param jspFactory
111      *            the JspFactory to use
112      */
113     protected void setJspFactory(JspFactory jspFactory)
114     {
115         this.jspFactory = jspFactory;
116     }
117 
118     /**
119      * Register a phase listener to every lifecycle. This listener will lazy fill the el resolver for jsp as soon as the
120      * first lifecycle is executed. This is necessarry to allow a faces application further setup after MyFaces has been
121      * initialized. When the first request is processed no further configuation of the el resolvers is allowed.
122      * 
123      * @param appCtx
124      * @param runtimeConfig
125      */
126     private void configureResolverForJSP(JspApplicationContext appCtx, RuntimeConfig runtimeConfig)
127     {
128         FacesCompositeELResolver facesCompositeELResolver = new FacesCompositeELResolver(Scope.JSP);
129         appCtx.addELResolver(facesCompositeELResolver);
130         PhaseListener resolverForJSPInitializer = new ResolverForJSPInitializer(
131                 createResolverBuilderForJSP(runtimeConfig), facesCompositeELResolver);
132 
133         LifecycleFactory factory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
134         for (Iterator<String> iter = factory.getLifecycleIds(); iter.hasNext();)
135         {
136             factory.getLifecycle(iter.next()).addPhaseListener(resolverForJSPInitializer);
137         }
138     }
139     
140     protected ELResolverBuilder createResolverBuilderForJSP(RuntimeConfig runtimeConfig)
141     {
142         return new ResolverBuilderForJSP(runtimeConfig);
143     }
144 }