1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33
34
35 public class ScopeSupport {
36
37
38
39
40
41
42
43
44
45
46 public ScopeSupport(Scope scope) {
47
48 super();
49 this.scope = scope;
50
51 }
52
53
54
55
56
57
58
59
60 protected ScopeListener listeners[] = new ScopeListener[0];
61
62
63
64
65
66 protected Scope scope = null;
67
68
69
70
71
72
73
74
75
76
77
78 public void addScopeListener(ScopeListener listener) {
79
80 synchronized (listeners) {
81 ScopeListener results[] =
82 new ScopeListener[listeners.length + 1];
83 System.arraycopy(listeners, 0, results, 0, listeners.length);
84 results[listeners.length] = listener;
85 listeners = results;
86 }
87
88 }
89
90
91
92
93
94
95
96
97 public void removeScopeListener(ScopeListener listener) {
98
99 synchronized (listeners) {
100 int n = -1;
101 for (int i = 0; i < listeners.length; i++) {
102 if (listeners[i] == listener) {
103 n = i;
104 break;
105 }
106 }
107 if (n < 0)
108 return;
109 ScopeListener results[] =
110 new ScopeListener[listeners.length - 1];
111 int j = 0;
112 for (int i = 0; i < listeners.length; i++) {
113 if (i != n)
114 results[j++] = listeners[i];
115 }
116 listeners = results;
117 }
118
119 }
120
121
122
123
124
125
126
127
128
129
130
131 public void fireBeanAdded(String key, Object value) {
132
133 if (listeners.length == 0)
134 return;
135 ScopeEvent event = new ScopeEvent(scope, key, value);
136 ScopeListener interested[] = null;
137 synchronized (listeners) {
138 interested = (ScopeListener[]) listeners.clone();
139 }
140 for (int i = 0; i < interested.length; i++)
141 interested[i].beanAdded(event);
142
143 }
144
145
146
147
148
149
150
151
152 public void fireBeanRemoved(String key, Object value) {
153
154 if (listeners.length == 0)
155 return;
156 ScopeEvent event = new ScopeEvent(scope, key, value);
157 ScopeListener interested[] = null;
158 synchronized (listeners) {
159 interested = (ScopeListener[]) listeners.clone();
160 }
161 for (int i = 0; i < interested.length; i++)
162 interested[i].beanRemoved(event);
163
164 }
165
166
167
168
169
170
171
172
173 public void fireBeanReplaced(String key, Object value) {
174
175 if (listeners.length == 0)
176 return;
177 ScopeEvent event = new ScopeEvent(scope, key, value);
178 ScopeListener interested[] = null;
179 synchronized (listeners) {
180 interested = (ScopeListener[]) listeners.clone();
181 }
182 for (int i = 0; i < interested.length; i++)
183 interested[i].beanReplaced(event);
184
185 }
186
187
188
189
190
191 public void fireScopeCleared() {
192
193 if (listeners.length == 0)
194 return;
195 ScopeEvent event = new ScopeEvent(scope, null, null);
196 ScopeListener interested[] = null;
197 synchronized (listeners) {
198 interested = (ScopeListener[]) listeners.clone();
199 }
200 for (int i = 0; i < interested.length; i++)
201 interested[i].scopeCleared(event);
202
203 }
204
205
206 }