2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.dialog.base;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.faces.context.FacesContext;
24 import javax.servlet.http.HttpSessionBindingEvent;
25 import javax.servlet.http.HttpSessionBindingListener;
26
27 import org.apache.shale.dialog.Constants;
28 import org.apache.shale.dialog.DialogContext;
29 import org.apache.shale.dialog.DialogContextManager;
30 import org.apache.shale.dialog.DialogContextManagerListener;
31 import org.apache.shale.dialog.DialogLifecycleListener;
32
33 /***
34 * <p>Abstract base class for {@link DialogContextManager} implementations.
35 * Provides listener registration and event firing convenience methods.
36 * Subclasses are expected to be serializable.</p>
37 *
38 * @since 1.0.4
39 */
40 public abstract class AbstractDialogContextManager
41 implements DialogContextManager, HttpSessionBindingListener {
42
43
44
45
46
47 /***
48 * <p><code>List</code> of registered {@link DialogContextManagerListener}
49 * instances.</p>
50 */
51 private List listeners = new ArrayList();
52
53
54
55
56
57 /***
58 * <p>Handle an instance of this class being bound into an HttpSession.</p>
59 *
60 * @param event HttpSessionBindingEvent to be handled
61 */
62 public void valueBound(HttpSessionBindingEvent event) {
63
64 DialogLifecycleListener listener = lifecycleListener();
65 if (listener != null) {
66 listener.onInit(this);
67 }
68
69 }
70
71
72 /***
73 * <p>Handle an instance of this class being unbound from an HttpSession.</p>
74 *
75 * @param event HttpSessionBindingEvent to be handled
76 */
77 public void valueUnbound(HttpSessionBindingEvent event) {
78
79 DialogLifecycleListener listener = lifecycleListener();
80 if (listener != null) {
81 listener.onDestroy(this);
82 }
83
84 }
85
86
87
88
89
90 /***
91 * <p>Register a new {@link DialogContextManagerListener} instance.</p>
92 *
93 * @param listener The new listener instance to be registered
94 */
95 public void addDialogContextManagerListener(DialogContextManagerListener listener) {
96
97 if (listener == null) {
98 throw new IllegalArgumentException("Cannot register null DialogContextManagerListener");
99 }
100
101 synchronized (listeners) {
102 if (listeners.contains(listener)) {
103 throw new IllegalArgumentException("DialogContextManagerListener already registered");
104 }
105 listener.setDialogContextManager(this);
106 listeners.add(listener);
107 }
108
109 }
110
111
112 /***
113 * <p>Return the set of currently registered {@link DialogContextManagerListener}s.
114 * If there are no registered listeners, a zero-length array is returned.</p>
115 */
116 public DialogContextManagerListener[] getDialogContextManagerListeners() {
117
118 synchronized (listeners) {
119 return (DialogContextManagerListener[])
120 listeners.toArray(new DialogContextManagerListener[listeners.size()]);
121 }
122
123 }
124
125
126 /***
127 * <p>Deregister an existing {@link DialogContextManagerListener} instance.</p>
128 *
129 * @param listener The existing listener to be deregistered
130 */
131 public void removeDialogContextManagerListener(DialogContextManagerListener listener) {
132
133 if (listener == null) {
134 throw new IllegalArgumentException("Cannot remove null DialogContextManagerListener");
135 }
136
137 boolean removed;
138 synchronized (listeners) {
139 removed = listeners.remove(listener);
140 }
141 if (removed) {
142 listener.setDialogContextManager(null);
143 }
144
145
146 }
147
148
149
150
151
152 /***
153 * <p>Fire an <code>onCreate()</code> event to all registered listeners.</p>
154 *
155 * @param context The {@link DialogContext} instance that has been created
156 */
157 protected void fireOnCreate(DialogContext context) {
158
159 DialogContextManagerListener[] listeners =
160 getDialogContextManagerListeners();
161 for (int i = 0; i < listeners.length; i++) {
162 listeners[i].onCreate(context);
163 }
164
165 }
166
167
168 /***
169 * <p>Fire an <code>onRemove()</code> event to all registered listeners.</p>
170 *
171 * @param context The {@link DialogContext} instance that has been removed
172 */
173 protected void fireOnRemove(DialogContext context) {
174
175 DialogContextManagerListener[] listeners =
176 getDialogContextManagerListeners();
177 for (int i = 0; i < listeners.length; i++) {
178 listeners[i].onRemove(context);
179 }
180
181 }
182
183
184
185
186
187 /***
188 * <p>Return the {@link DialogLifecycleListener} for this application
189 * (if any); otherwise, return <code>null</code>.</p>
190 */
191 private DialogLifecycleListener lifecycleListener() {
192
193 FacesContext context = FacesContext.getCurrentInstance();
194 if (context == null) {
195 return null;
196 }
197 Object result =
198 context.getApplication().getVariableResolver().
199 resolveVariable(context, Constants.LIFECYCLE_ATTR);
200 if ((result != null) && (result instanceof DialogLifecycleListener)) {
201 return (DialogLifecycleListener) result;
202 } else {
203 return null;
204 }
205
206 }
207
208
209 }