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