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