1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.tag.jsf.core; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.Serializable; |
23 | |
|
24 | |
import javax.el.ELException; |
25 | |
import javax.el.ValueExpression; |
26 | |
import javax.faces.FacesException; |
27 | |
import javax.faces.component.UIComponent; |
28 | |
import javax.faces.component.UIViewRoot; |
29 | |
import javax.faces.context.FacesContext; |
30 | |
import javax.faces.event.AbortProcessingException; |
31 | |
import javax.faces.event.PhaseEvent; |
32 | |
import javax.faces.event.PhaseId; |
33 | |
import javax.faces.event.PhaseListener; |
34 | |
import javax.faces.view.facelets.ComponentHandler; |
35 | |
import javax.faces.view.facelets.FaceletContext; |
36 | |
import javax.faces.view.facelets.FaceletException; |
37 | |
import javax.faces.view.facelets.TagAttribute; |
38 | |
import javax.faces.view.facelets.TagAttributeException; |
39 | |
import javax.faces.view.facelets.TagConfig; |
40 | |
import javax.faces.view.facelets.TagException; |
41 | |
import javax.faces.view.facelets.TagHandler; |
42 | |
|
43 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
44 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
45 | |
import org.apache.myfaces.view.facelets.util.ReflectionUtil; |
46 | |
|
47 | |
@JSFFaceletTag( |
48 | |
name = "f:phaseListener", |
49 | |
bodyContent = "empty", |
50 | |
tagClass="org.apache.myfaces.taglib.core.PhaseListenerTag") |
51 | |
public class PhaseListenerHandler extends TagHandler |
52 | |
{ |
53 | |
|
54 | |
private final static class LazyPhaseListener implements PhaseListener, Serializable |
55 | |
{ |
56 | |
|
57 | |
private static final long serialVersionUID = -6496143057319213401L; |
58 | |
|
59 | |
private final String type; |
60 | |
|
61 | |
private final ValueExpression binding; |
62 | |
|
63 | |
public LazyPhaseListener(String type, ValueExpression binding) |
64 | 0 | { |
65 | 0 | this.type = type; |
66 | 0 | this.binding = binding; |
67 | 0 | } |
68 | |
|
69 | |
private PhaseListener getInstance() |
70 | |
{ |
71 | 0 | PhaseListener instance = null; |
72 | 0 | FacesContext faces = FacesContext.getCurrentInstance(); |
73 | 0 | if (faces == null) |
74 | |
{ |
75 | 0 | return null; |
76 | |
} |
77 | 0 | if (this.binding != null) |
78 | |
{ |
79 | 0 | instance = (PhaseListener) binding.getValue(faces.getELContext()); |
80 | |
} |
81 | 0 | if (instance == null && type != null) |
82 | |
{ |
83 | |
try |
84 | |
{ |
85 | 0 | instance = (PhaseListener) ReflectionUtil.forName(this.type).newInstance(); |
86 | |
} |
87 | 0 | catch (Exception e) |
88 | |
{ |
89 | 0 | throw new AbortProcessingException("Couldn't Lazily instantiate PhaseListener", e); |
90 | 0 | } |
91 | 0 | if (this.binding != null) |
92 | |
{ |
93 | 0 | binding.setValue(faces.getELContext(), instance); |
94 | |
} |
95 | |
} |
96 | 0 | return instance; |
97 | |
} |
98 | |
|
99 | |
public void afterPhase(PhaseEvent event) |
100 | |
{ |
101 | 0 | PhaseListener pl = this.getInstance(); |
102 | 0 | if (pl != null) |
103 | |
{ |
104 | 0 | pl.afterPhase(event); |
105 | |
} |
106 | 0 | } |
107 | |
|
108 | |
public void beforePhase(PhaseEvent event) |
109 | |
{ |
110 | 0 | PhaseListener pl = this.getInstance(); |
111 | 0 | if (pl != null) |
112 | |
{ |
113 | 0 | pl.beforePhase(event); |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
public PhaseId getPhaseId() |
118 | |
{ |
119 | 0 | PhaseListener pl = this.getInstance(); |
120 | 0 | return (pl != null) ? pl.getPhaseId() : PhaseId.ANY_PHASE; |
121 | |
} |
122 | |
|
123 | |
} |
124 | |
|
125 | |
private final TagAttribute binding; |
126 | |
|
127 | |
private final String listenerType; |
128 | |
|
129 | |
public PhaseListenerHandler(TagConfig config) |
130 | |
{ |
131 | 0 | super(config); |
132 | 0 | TagAttribute type = this.getAttribute("type"); |
133 | 0 | this.binding = this.getAttribute("binding"); |
134 | 0 | if (type != null) |
135 | |
{ |
136 | 0 | if (!type.isLiteral()) |
137 | |
{ |
138 | 0 | throw new TagAttributeException(type, "Must be a literal class name of type PhaseListener"); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | |
|
143 | |
try |
144 | |
{ |
145 | 0 | ReflectionUtil.forName(type.getValue()); |
146 | |
} |
147 | 0 | catch (ClassNotFoundException e) |
148 | |
{ |
149 | 0 | throw new TagAttributeException(type, "Couldn't qualify PhaseListener", e); |
150 | 0 | } |
151 | |
} |
152 | 0 | this.listenerType = type.getValue(); |
153 | |
} |
154 | |
else |
155 | |
{ |
156 | 0 | this.listenerType = null; |
157 | |
} |
158 | 0 | } |
159 | |
|
160 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
161 | |
ELException |
162 | |
{ |
163 | 0 | if (ComponentHandler.isNew(parent)) |
164 | |
{ |
165 | 0 | UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent); |
166 | 0 | if (root == null) |
167 | |
{ |
168 | 0 | throw new TagException(this.tag, "UIViewRoot not available"); |
169 | |
} |
170 | 0 | ValueExpression b = null; |
171 | 0 | if (this.binding != null) |
172 | |
{ |
173 | 0 | b = this.binding.getValueExpression(ctx, PhaseListener.class); |
174 | |
} |
175 | |
|
176 | 0 | PhaseListener pl = new LazyPhaseListener(this.listenerType, b); |
177 | |
|
178 | 0 | root.addPhaseListener(pl); |
179 | |
} |
180 | 0 | } |
181 | |
} |