1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.lifecycle; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.faces.FacesException; |
22 | |
import javax.faces.component.UIViewRoot; |
23 | |
import javax.faces.context.ExternalContext; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.el.EvaluationException; |
26 | |
import javax.faces.event.PhaseListener; |
27 | |
import javax.faces.lifecycle.Lifecycle; |
28 | |
|
29 | |
import org.seasar.framework.util.ArrayUtil; |
30 | |
import org.seasar.teeda.core.JsfConstants; |
31 | |
import org.seasar.teeda.core.util.ErrorPageManager; |
32 | |
import org.seasar.teeda.core.util.NullErrorPageManagerImpl; |
33 | |
import org.seasar.teeda.core.util.PostbackUtil; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class LifecycleImpl extends Lifecycle { |
40 | |
|
41 | 2 | private static final String EXECUTED_ATTR = LifecycleImpl.class.getName() |
42 | |
+ ".EXECUTED"; |
43 | |
|
44 | 6 | private PhaseListener[] phaseListeners = new PhaseListener[0]; |
45 | |
|
46 | |
private Phase restoreViewPhase; |
47 | |
|
48 | |
private AbstractPhase applyRequestValuesPhase; |
49 | |
|
50 | |
private Phase invokeApplicationPhase; |
51 | |
|
52 | |
private Phase renderResponsePhase; |
53 | |
|
54 | |
private Phase processValidationPhase; |
55 | |
|
56 | |
private Phase updateModelValuesPhase; |
57 | |
|
58 | 6 | private ErrorPageManager errorPageManager = new NullErrorPageManagerImpl(); |
59 | |
|
60 | 6 | public LifecycleImpl() { |
61 | 6 | } |
62 | |
|
63 | |
public void execute(final FacesContext context) throws FacesException { |
64 | 5 | final ExternalContext extContext = context.getExternalContext(); |
65 | 5 | final Map requestMap = extContext.getRequestMap(); |
66 | 5 | requestMap.put(JsfConstants.FACES_CONTEXT, context); |
67 | |
try { |
68 | 5 | restoreViewPhase.execute(context); |
69 | 3 | final boolean postback = PostbackUtil.isPostback(requestMap); |
70 | 3 | if (!postback || isFinished(context)) { |
71 | 3 | return; |
72 | |
} |
73 | 0 | if (requestMap.containsKey(EXECUTED_ATTR)) { |
74 | 0 | context.renderResponse(); |
75 | 0 | return; |
76 | |
} |
77 | 0 | requestMap.put(EXECUTED_ATTR, EXECUTED_ATTR); |
78 | |
|
79 | 0 | applyRequestValuesPhase.execute(context); |
80 | 0 | if (isFinished(context)) { |
81 | 0 | applyRequestValuesPhase.initializeChildren(context, context |
82 | |
.getViewRoot()); |
83 | 0 | return; |
84 | |
} |
85 | 0 | if (postback || hasEvent(context)) { |
86 | 0 | processValidationPhase.execute(context); |
87 | 0 | if (isFinished(context)) { |
88 | 0 | return; |
89 | |
} |
90 | |
} |
91 | |
|
92 | 0 | updateModelValuesPhase.execute(context); |
93 | 0 | if (isFinished(context)) { |
94 | 0 | return; |
95 | |
} |
96 | |
|
97 | 0 | invokeApplicationPhase.execute(context); |
98 | 0 | } catch (final EvaluationException e) { |
99 | 0 | final Throwable cause = e.getCause(); |
100 | 0 | if (cause instanceof RuntimeException) { |
101 | 0 | throw (RuntimeException) cause; |
102 | 0 | } else if (cause instanceof Error) { |
103 | 0 | throw (Error) cause; |
104 | |
} else { |
105 | 0 | throw e; |
106 | |
} |
107 | 2 | } catch (final Exception e) { |
108 | 2 | handleException(context, e); |
109 | 0 | } |
110 | 2 | } |
111 | |
|
112 | |
protected void handleException(final FacesContext context, final Exception e) { |
113 | 2 | final ErrorPageManager manager = getErrorPageManager(); |
114 | 2 | final ExternalContext externalContext = context.getExternalContext(); |
115 | |
try { |
116 | 2 | if (manager.handleException(e, context, externalContext)) { |
117 | 2 | context.responseComplete(); |
118 | |
} else { |
119 | 0 | throw (RuntimeException) e; |
120 | |
} |
121 | 0 | } catch (IOException ioe) { |
122 | 0 | throw new FacesException(ioe.getMessage(), ioe); |
123 | 2 | } |
124 | 2 | } |
125 | |
|
126 | |
public void render(final FacesContext context) throws FacesException { |
127 | 1 | if (context.getResponseComplete()) { |
128 | 1 | return; |
129 | |
} |
130 | |
try { |
131 | 0 | renderResponsePhase.execute(context); |
132 | 0 | } catch (FacesException e) { |
133 | 0 | throw e; |
134 | 0 | } catch (Exception e) { |
135 | 0 | handleException(context, e); |
136 | 0 | } |
137 | 0 | } |
138 | |
|
139 | |
protected boolean isFinished(final FacesContext context) |
140 | |
throws FacesException { |
141 | 2 | return context.getResponseComplete() || context.getRenderResponse(); |
142 | |
} |
143 | |
|
144 | |
public void addPhaseListener(final PhaseListener listener) { |
145 | 0 | phaseListeners = (PhaseListener[]) ArrayUtil.add(phaseListeners, |
146 | |
listener); |
147 | 0 | } |
148 | |
|
149 | |
public void removePhaseListener(final PhaseListener listener) { |
150 | 0 | phaseListeners = (PhaseListener[]) ArrayUtil.remove(phaseListeners, |
151 | |
listener); |
152 | 0 | } |
153 | |
|
154 | |
public PhaseListener[] getPhaseListeners() { |
155 | 0 | return phaseListeners; |
156 | |
} |
157 | |
|
158 | |
protected boolean hasEvent(final FacesContext context) { |
159 | 0 | final UIViewRoot viewRoot = context.getViewRoot(); |
160 | 0 | return viewRoot.getEventSize() > 0; |
161 | |
} |
162 | |
|
163 | |
public void setRestoreViewPhase(final Phase restoreViewPhase) { |
164 | 4 | this.restoreViewPhase = restoreViewPhase; |
165 | 4 | } |
166 | |
|
167 | |
public void setApplyRequestValuesPhase( |
168 | |
final AbstractPhase applyRequestValuesPhase) { |
169 | 0 | this.applyRequestValuesPhase = applyRequestValuesPhase; |
170 | 0 | } |
171 | |
|
172 | |
public void setInvokeApplicationPhase(final Phase invokeApplicationPhase) { |
173 | 0 | this.invokeApplicationPhase = invokeApplicationPhase; |
174 | 0 | } |
175 | |
|
176 | |
public void setRenderResponsePhase(final Phase renderPhase) { |
177 | 2 | renderResponsePhase = renderPhase; |
178 | 2 | } |
179 | |
|
180 | |
public void setProcessValidationsPhase(final Phase processValidationPhase) { |
181 | 0 | this.processValidationPhase = processValidationPhase; |
182 | 0 | } |
183 | |
|
184 | |
public void setUpdateModelValuesPhase(final Phase updateModelValuesPhase) { |
185 | 0 | this.updateModelValuesPhase = updateModelValuesPhase; |
186 | 0 | } |
187 | |
|
188 | |
public AbstractPhase getApplyRequestValuesPhase() { |
189 | 0 | return applyRequestValuesPhase; |
190 | |
} |
191 | |
|
192 | |
public Phase getInvokeApplicationPhase() { |
193 | 0 | return invokeApplicationPhase; |
194 | |
} |
195 | |
|
196 | |
public Phase getProcessValidationPhase() { |
197 | 0 | return processValidationPhase; |
198 | |
} |
199 | |
|
200 | |
public Phase getRenderResponsePhase() { |
201 | 0 | return renderResponsePhase; |
202 | |
} |
203 | |
|
204 | |
public Phase getRestoreViewPhase() { |
205 | 0 | return restoreViewPhase; |
206 | |
} |
207 | |
|
208 | |
public Phase getUpdateModelValuesPhase() { |
209 | 0 | return updateModelValuesPhase; |
210 | |
} |
211 | |
|
212 | |
public ErrorPageManager getErrorPageManager() { |
213 | 2 | return errorPageManager; |
214 | |
} |
215 | |
|
216 | |
public void setErrorPageManager(ErrorPageManager errorPageManager) { |
217 | 2 | this.errorPageManager = errorPageManager; |
218 | 2 | } |
219 | |
} |