1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.taglib.core; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.logging.Logger; |
23 | |
|
24 | |
import javax.el.ValueExpression; |
25 | |
import javax.faces.FacesException; |
26 | |
import javax.faces.component.UIViewRoot; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
import javax.faces.event.AbortProcessingException; |
29 | |
import javax.faces.event.PhaseEvent; |
30 | |
import javax.faces.event.PhaseId; |
31 | |
import javax.faces.event.PhaseListener; |
32 | |
import javax.faces.webapp.UIComponentELTag; |
33 | |
import javax.servlet.jsp.JspException; |
34 | |
import javax.servlet.jsp.tagext.Tag; |
35 | |
import javax.servlet.jsp.tagext.TagSupport; |
36 | |
|
37 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspAttribute; |
38 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag; |
39 | |
import org.apache.myfaces.shared.util.ClassUtils; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
@JSFJspTag(name = "f:phaseListener", bodyContent = "empty") |
48 | 0 | public class PhaseListenerTag extends TagSupport |
49 | |
{ |
50 | |
|
51 | 0 | private static final Logger log = Logger.getLogger(PhaseListenerTag.class.getName()); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private ValueExpression type = null; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | private ValueExpression binding = null; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
@JSFJspAttribute(className="javax.el.ValueExpression", |
67 | |
deferredValueType="java.lang.String") |
68 | |
public void setType(ValueExpression type) |
69 | |
{ |
70 | 0 | this.type = type; |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@JSFJspAttribute(className="javax.el.ValueExpression", |
77 | |
deferredValueType="javax.faces.event.PhaseListener") |
78 | |
public void setBinding(ValueExpression binding) |
79 | |
{ |
80 | 0 | this.binding = binding; |
81 | 0 | } |
82 | |
|
83 | |
@Override |
84 | |
public int doStartTag() throws JspException |
85 | |
{ |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 0 | Tag parent = this; |
93 | 0 | UIComponentELTag parentTag = null; |
94 | 0 | while ((parent = parent.getParent()) != null) |
95 | |
{ |
96 | 0 | if (parent instanceof UIComponentELTag) |
97 | |
{ |
98 | 0 | parentTag = (UIComponentELTag)parent; |
99 | |
} |
100 | |
} |
101 | |
|
102 | 0 | if (parentTag == null) |
103 | |
{ |
104 | 0 | throw new JspException("Not nested in a UIViewRoot Error for tag with handler class: " |
105 | |
+ this.getClass().getName()); |
106 | |
} |
107 | |
|
108 | 0 | if (!parentTag.getCreated()) |
109 | |
{ |
110 | 0 | return SKIP_BODY; |
111 | |
} |
112 | |
|
113 | 0 | UIViewRoot root = (UIViewRoot)parentTag.getComponentInstance(); |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | 0 | PhaseListener listener = null; |
129 | |
try |
130 | |
{ |
131 | 0 | listener = new BindingPhaseListener(binding, type); |
132 | |
} |
133 | 0 | catch (Exception ex) |
134 | |
{ |
135 | 0 | throw new JspException(ex.getMessage(), ex); |
136 | 0 | } |
137 | |
|
138 | 0 | root.addPhaseListener(listener); |
139 | |
|
140 | 0 | return SKIP_BODY; |
141 | |
|
142 | |
} |
143 | |
|
144 | 0 | private static class BindingPhaseListener implements PhaseListener, Serializable |
145 | |
{ |
146 | |
|
147 | 0 | private transient PhaseListener phaseListenerCache = null; |
148 | |
private ValueExpression type; |
149 | |
private ValueExpression binding; |
150 | |
|
151 | |
|
152 | |
BindingPhaseListener(ValueExpression binding, ValueExpression type) |
153 | 0 | { |
154 | 0 | this.binding = binding; |
155 | 0 | this.type = type; |
156 | 0 | } |
157 | |
|
158 | |
public void afterPhase(PhaseEvent event) |
159 | |
{ |
160 | 0 | PhaseListener listener = getPhaseListener(); |
161 | 0 | if (listener != null) |
162 | |
{ |
163 | 0 | listener.afterPhase(event); |
164 | |
} |
165 | 0 | } |
166 | |
|
167 | |
public void beforePhase(PhaseEvent event) |
168 | |
{ |
169 | 0 | PhaseListener listener = getPhaseListener(); |
170 | 0 | if (listener != null) |
171 | |
{ |
172 | 0 | listener.beforePhase(event); |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
public PhaseId getPhaseId() |
177 | |
{ |
178 | 0 | PhaseListener listener = getPhaseListener(); |
179 | 0 | if (listener != null) |
180 | |
{ |
181 | 0 | return listener.getPhaseId(); |
182 | |
} |
183 | |
|
184 | 0 | return null; |
185 | |
|
186 | |
} |
187 | |
|
188 | |
private PhaseListener getPhaseListener() |
189 | |
{ |
190 | |
|
191 | 0 | if (phaseListenerCache != null) |
192 | |
{ |
193 | 0 | return phaseListenerCache; |
194 | |
} |
195 | |
else |
196 | |
{ |
197 | |
|
198 | 0 | phaseListenerCache = getPhaseListnerInstance(binding, type); |
199 | |
} |
200 | 0 | if (phaseListenerCache == null) |
201 | |
{ |
202 | 0 | log.warning("PhaseListener will not be installed. Both binding and type are null."); |
203 | |
|
204 | |
} |
205 | |
|
206 | 0 | return phaseListenerCache; |
207 | |
|
208 | |
} |
209 | |
|
210 | |
private PhaseListener getPhaseListnerInstance(ValueExpression binding, ValueExpression type) |
211 | |
{ |
212 | 0 | FacesContext currentFacesContext = FacesContext.getCurrentInstance(); |
213 | 0 | Object phasesInstance = null; |
214 | 0 | if (binding != null) |
215 | |
{ |
216 | 0 | phasesInstance = binding.getValue(currentFacesContext.getELContext()); |
217 | |
} |
218 | 0 | else if (type != null) |
219 | |
{ |
220 | |
try |
221 | |
{ |
222 | 0 | phasesInstance = ClassUtils.newInstance((String)type.getValue(currentFacesContext.getELContext())); |
223 | |
} |
224 | 0 | catch (FacesException ex) |
225 | |
{ |
226 | 0 | throw new AbortProcessingException(ex.getMessage(), ex); |
227 | 0 | } |
228 | |
} |
229 | 0 | return (PhaseListener)phasesInstance; |
230 | |
|
231 | |
} |
232 | |
|
233 | |
} |
234 | |
} |