Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ScopeSupport |
|
| 3.5714285714285716;3.571 |
1 | /* | |
2 | * Copyright 1999-2001,2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.apache.commons.workflow.util; | |
18 | ||
19 | ||
20 | import java.util.Map; | |
21 | import org.apache.commons.workflow.Scope; | |
22 | import org.apache.commons.workflow.ScopeEvent; | |
23 | import org.apache.commons.workflow.ScopeListener; | |
24 | ||
25 | ||
26 | /** | |
27 | * <strong>ScopeSupport</strong> is a convenience class for managing the | |
28 | * firing of <code>ScopeEvents</code> to registered | |
29 | * <code>ScopeListeners</code>. | |
30 | * | |
31 | * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ | |
32 | * @author Craig R. McClanahan | |
33 | */ | |
34 | ||
35 | public class ScopeSupport { | |
36 | ||
37 | ||
38 | // ----------------------------------------------------------- Constructors | |
39 | ||
40 | ||
41 | /** | |
42 | * Construct a new ScopeSupport object associated with the specified Scope. | |
43 | * | |
44 | * @param scope Scope for whom we will fire events | |
45 | */ | |
46 | public ScopeSupport(Scope scope) { | |
47 | ||
48 | 0 | super(); |
49 | 0 | this.scope = scope; |
50 | ||
51 | 0 | } |
52 | ||
53 | ||
54 | // ----------------------------------------------------- Instance Variables | |
55 | ||
56 | ||
57 | /** | |
58 | * The set of registered <code>ScopeListener</code> event listeners. | |
59 | */ | |
60 | 0 | protected ScopeListener listeners[] = new ScopeListener[0]; |
61 | ||
62 | ||
63 | /** | |
64 | * The <code>Scope</code> for whom we will fire events. | |
65 | */ | |
66 | 0 | protected Scope scope = null; |
67 | ||
68 | ||
69 | // ------------------------------------------------- Event Listener Methods | |
70 | ||
71 | ||
72 | /** | |
73 | * Add a listener that is notified each time beans are added, | |
74 | * replaced, or removed in this scope. | |
75 | * | |
76 | * @param listener The ScopeListener to be added | |
77 | */ | |
78 | public void addScopeListener(ScopeListener listener) { | |
79 | ||
80 | 0 | synchronized (listeners) { |
81 | 0 | ScopeListener results[] = |
82 | new ScopeListener[listeners.length + 1]; | |
83 | 0 | System.arraycopy(listeners, 0, results, 0, listeners.length); |
84 | 0 | results[listeners.length] = listener; |
85 | 0 | listeners = results; |
86 | 0 | } |
87 | ||
88 | 0 | } |
89 | ||
90 | ||
91 | /** | |
92 | * Remove a listener that is notified each time beans are added, | |
93 | * replaced, or removed in this scope. | |
94 | * | |
95 | * @param listener The ScopeListener to be removed | |
96 | */ | |
97 | public void removeScopeListener(ScopeListener listener) { | |
98 | ||
99 | 0 | synchronized (listeners) { |
100 | 0 | int n = -1; |
101 | 0 | for (int i = 0; i < listeners.length; i++) { |
102 | 0 | if (listeners[i] == listener) { |
103 | 0 | n = i; |
104 | 0 | break; |
105 | } | |
106 | } | |
107 | 0 | if (n < 0) |
108 | 0 | return; |
109 | 0 | ScopeListener results[] = |
110 | new ScopeListener[listeners.length - 1]; | |
111 | 0 | int j = 0; |
112 | 0 | for (int i = 0; i < listeners.length; i++) { |
113 | 0 | if (i != n) |
114 | 0 | results[j++] = listeners[i]; |
115 | } | |
116 | 0 | listeners = results; |
117 | 0 | } |
118 | ||
119 | 0 | } |
120 | ||
121 | ||
122 | // --------------------------------------------------- Event Firing Methods | |
123 | ||
124 | ||
125 | /** | |
126 | * Fire a <code>beanAdded()</code> event to all registered listeners. | |
127 | * | |
128 | * @param key Key of the bean that was added | |
129 | * @param value Value of the bean that was added | |
130 | */ | |
131 | public void fireBeanAdded(String key, Object value) { | |
132 | ||
133 | 0 | if (listeners.length == 0) |
134 | 0 | return; |
135 | 0 | ScopeEvent event = new ScopeEvent(scope, key, value); |
136 | 0 | ScopeListener interested[] = null; |
137 | 0 | synchronized (listeners) { |
138 | 0 | interested = (ScopeListener[]) listeners.clone(); |
139 | 0 | } |
140 | 0 | for (int i = 0; i < interested.length; i++) |
141 | 0 | interested[i].beanAdded(event); |
142 | ||
143 | 0 | } |
144 | ||
145 | ||
146 | /** | |
147 | * Fire a <code>beanRemoved()</code> event to all registered listeners. | |
148 | * | |
149 | * @param key Key of the bean that was removed | |
150 | * @param value Value of the bean that was removed | |
151 | */ | |
152 | public void fireBeanRemoved(String key, Object value) { | |
153 | ||
154 | 0 | if (listeners.length == 0) |
155 | 0 | return; |
156 | 0 | ScopeEvent event = new ScopeEvent(scope, key, value); |
157 | 0 | ScopeListener interested[] = null; |
158 | 0 | synchronized (listeners) { |
159 | 0 | interested = (ScopeListener[]) listeners.clone(); |
160 | 0 | } |
161 | 0 | for (int i = 0; i < interested.length; i++) |
162 | 0 | interested[i].beanRemoved(event); |
163 | ||
164 | 0 | } |
165 | ||
166 | ||
167 | /** | |
168 | * Fire a <code>beanReplaced()</code> event to all registered listeners. | |
169 | * | |
170 | * @param key Key of the bean that was replaced | |
171 | * @param value Old value of the bean that was replaced | |
172 | */ | |
173 | public void fireBeanReplaced(String key, Object value) { | |
174 | ||
175 | 0 | if (listeners.length == 0) |
176 | 0 | return; |
177 | 0 | ScopeEvent event = new ScopeEvent(scope, key, value); |
178 | 0 | ScopeListener interested[] = null; |
179 | 0 | synchronized (listeners) { |
180 | 0 | interested = (ScopeListener[]) listeners.clone(); |
181 | 0 | } |
182 | 0 | for (int i = 0; i < interested.length; i++) |
183 | 0 | interested[i].beanReplaced(event); |
184 | ||
185 | 0 | } |
186 | ||
187 | ||
188 | /** | |
189 | * Fire a <code>scopeCleared()</code> event to all registered listeners. | |
190 | */ | |
191 | public void fireScopeCleared() { | |
192 | ||
193 | 0 | if (listeners.length == 0) |
194 | 0 | return; |
195 | 0 | ScopeEvent event = new ScopeEvent(scope, null, null); |
196 | 0 | ScopeListener interested[] = null; |
197 | 0 | synchronized (listeners) { |
198 | 0 | interested = (ScopeListener[]) listeners.clone(); |
199 | 0 | } |
200 | 0 | for (int i = 0; i < interested.length; i++) |
201 | 0 | interested[i].scopeCleared(event); |
202 | ||
203 | 0 | } |
204 | ||
205 | ||
206 | } |