View Javadoc

1   /*
2    * $Id: FacesSupport.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.jsf;
22  
23  import javax.faces.context.FacesContext;
24  import javax.faces.event.PhaseEvent;
25  import javax.faces.event.PhaseId;
26  import javax.faces.event.PhaseListener;
27  import javax.faces.lifecycle.Lifecycle;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import com.opensymphony.xwork2.ActionContext;
33  
34  /***
35   * Provides common lifecycle phase methods needed by interceptors and results.
36   */
37  public class FacesSupport {
38  
39      private static final String LIFECYCLE_KEY = "lifecycle";
40  
41      /*** Log instance for each class */
42      protected Log log;
43  
44      /***
45       * Marker key for the ActionContext to dictate whether to treat the request
46       * as a JSF faces request and therefore process the Faces phases
47       */
48      protected static final String FACES_ENABLED = "facesEnabled";
49  
50      /*** Initializes log instance for the instance object */
51      protected FacesSupport() {
52          log = LogFactory.getLog(getClass());
53      }
54  
55      /***
56       * Gets the shared lifecycle for this request
57       *
58       * @return The lifecycle
59       */
60      private Lifecycle getLifecycle() {
61          return (Lifecycle) ActionContext.getContext().get(LIFECYCLE_KEY);
62      }
63  
64      /***
65       * Sets the lifecycle for this request
66       *
67       * @param lifecycle
68       *            The lifecycle
69       */
70      protected void setLifecycle(Lifecycle lifecycle) {
71          ActionContext.getContext().put(LIFECYCLE_KEY, lifecycle);
72      }
73  
74      /***
75       * Informs phase listeners before a phase is executed
76       *
77       * @param facesContext
78       *            The current faces context
79       * @param phaseId
80       *            The phase id about to be executed
81       */
82      protected void informPhaseListenersBefore(FacesContext facesContext,
83              PhaseId phaseId) {
84          Lifecycle lifecycle = getLifecycle();
85          PhaseListener[] phaseListeners = lifecycle.getPhaseListeners();
86          for (int i = 0; i < phaseListeners.length; i++) {
87              PhaseListener phaseListener = phaseListeners[i];
88              int listenerPhaseId = phaseListener.getPhaseId().getOrdinal();
89              if (listenerPhaseId == PhaseId.ANY_PHASE.getOrdinal()
90                      || listenerPhaseId == phaseId.getOrdinal()) {
91                  phaseListener.beforePhase(new PhaseEvent(FacesContext
92                          .getCurrentInstance(), phaseId, lifecycle));
93              }
94          }
95  
96      }
97  
98      /***
99       * Informs phase listeners after a phase is executed
100      *
101      * @param facesContext
102      *            The current faces context
103      * @param phaseId
104      *            The phase id that was executed
105      */
106     protected void informPhaseListenersAfter(FacesContext facesContext,
107             PhaseId phaseId) {
108         Lifecycle lifecycle = getLifecycle();
109         PhaseListener[] phaseListeners = lifecycle.getPhaseListeners();
110         for (int i = 0; i < phaseListeners.length; i++) {
111             PhaseListener phaseListener = phaseListeners[i];
112             int listenerPhaseId = phaseListener.getPhaseId().getOrdinal();
113             if (listenerPhaseId == PhaseId.ANY_PHASE.getOrdinal()
114                     || listenerPhaseId == phaseId.getOrdinal()) {
115                 phaseListener.afterPhase(new PhaseEvent(FacesContext
116                         .getCurrentInstance(), phaseId, lifecycle));
117             }
118         }
119 
120     }
121 
122     /***
123      * Checks to see if the response has been completed. Mainly used for better
124      * debugging messages.
125      *
126      * @param facesContext
127      *            The current faces context
128      * @param phase
129      *            The phase id in execution
130      * @param before
131      *            Whether the phase has been executed or not
132      * @return True if the response is complete
133      */
134     protected boolean isResponseComplete(FacesContext facesContext,
135             String phase, boolean before) {
136         boolean flag = false;
137         if (facesContext.getResponseComplete()) {
138             if (log.isDebugEnabled())
139                 log
140                         .debug("exiting from lifecycle.execute in "
141                                 + phase
142                                 + " because getResponseComplete is true from one of the "
143                                 + (before ? "before" : "after") + " listeners");
144             flag = true;
145         }
146         return flag;
147     }
148 
149     /***
150      * Checks to see the render phase should be executed next. Mainly used for
151      * better debugging messages.
152      *
153      * @param facesContext
154      *            The current faces context
155      * @param phase
156      *            The phase id in execution
157      * @param before
158      *            Whether the phase has been executed or not
159      * @return True if the response is complete
160      */
161     protected boolean shouldRenderResponse(FacesContext facesContext,
162             String phase, boolean before) {
163         boolean flag = false;
164         if (facesContext.getRenderResponse()) {
165             if (log.isDebugEnabled())
166                 log.debug("exiting from lifecycle.execute in " + phase
167                         + " because getRenderResponse is true from one of the "
168                         + (before ? "before" : "after") + " listeners");
169             flag = true;
170         }
171         return flag;
172     }
173 }