1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.util.component;
15 import java.util.EventListener;
16
17 import org.eclipse.jetty.util.LazyList;
18 import org.eclipse.jetty.util.log.Log;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class Container
39 {
40 private Object _listeners;
41
42 public synchronized void addEventListener(Container.Listener listener)
43 {
44 _listeners=LazyList.add(_listeners,listener);
45 }
46
47 public synchronized void removeEventListener(Container.Listener listener)
48 {
49 _listeners=LazyList.remove(_listeners,listener);
50 }
51
52
53
54
55
56
57
58
59 public synchronized void update(Object parent, Object oldChild, final Object child, String relationship)
60 {
61 if (oldChild!=null && !oldChild.equals(child))
62 remove(parent,oldChild,relationship);
63 if (child!=null && !child.equals(oldChild))
64 add(parent,child,relationship);
65 }
66
67
68
69
70
71
72
73
74
75 public synchronized void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove)
76 {
77 if (oldChild!=null && !oldChild.equals(child))
78 {
79 remove(parent,oldChild,relationship);
80 if (addRemove)
81 removeBean(oldChild);
82 }
83
84 if (child!=null && !child.equals(oldChild))
85 {
86 if (addRemove)
87 addBean(child);
88 add(parent,child,relationship);
89 }
90 }
91
92
93
94
95
96
97
98
99
100 public synchronized void update(Object parent, Object[] oldChildren, final Object[] children, String relationship)
101 {
102 update(parent,oldChildren,children,relationship,false);
103 }
104
105
106
107
108
109
110
111
112
113
114 public synchronized void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove)
115 {
116 Object[] newChildren = null;
117 if (children!=null)
118 {
119 newChildren = new Object[children.length];
120
121 for (int i=children.length;i-->0;)
122 {
123 boolean new_child=true;
124 if (oldChildren!=null)
125 {
126 for (int j=oldChildren.length;j-->0;)
127 {
128 if (children[i]!=null && children[i].equals(oldChildren[j]))
129 {
130 oldChildren[j]=null;
131 new_child=false;
132 }
133 }
134 }
135 if (new_child)
136 newChildren[i]=children[i];
137 }
138 }
139
140 if (oldChildren!=null)
141 {
142 for (int i=oldChildren.length;i-->0;)
143 {
144 if (oldChildren[i]!=null)
145 {
146 remove(parent,oldChildren[i],relationship);
147 if (addRemove)
148 removeBean(oldChildren[i]);
149 }
150 }
151 }
152
153 if (newChildren!=null)
154 {
155 for (int i=0;i<newChildren.length;i++)
156 if (newChildren[i]!=null)
157 {
158 if (addRemove)
159 addBean(newChildren[i]);
160 add(parent,newChildren[i],relationship);
161 }
162 }
163 }
164
165
166 public void addBean(Object obj)
167 {
168 if (_listeners!=null)
169 {
170 for (int i=0; i<LazyList.size(_listeners); i++)
171 {
172 Listener listener=(Listener)LazyList.get(_listeners, i);
173 listener.addBean(obj);
174 }
175 }
176 }
177
178
179 public void removeBean(Object obj)
180 {
181 if (_listeners!=null)
182 {
183 for (int i=0; i<LazyList.size(_listeners); i++)
184 ((Listener)LazyList.get(_listeners, i)).removeBean(obj);
185 }
186 }
187
188
189
190
191
192
193
194 private void add(Object parent, Object child, String relationship)
195 {
196 if (Log.isDebugEnabled())
197 Log.debug("Container "+parent+" + "+child+" as "+relationship);
198 if (_listeners!=null)
199 {
200 Relationship event=new Relationship(this,parent,child,relationship);
201 for (int i=0; i<LazyList.size(_listeners); i++)
202 ((Listener)LazyList.get(_listeners, i)).add(event);
203 }
204 }
205
206
207
208
209
210
211
212 private void remove(Object parent, Object child, String relationship)
213 {
214 if (Log.isDebugEnabled())
215 Log.debug("Container "+parent+" - "+child+" as "+relationship);
216 if (_listeners!=null)
217 {
218 Relationship event=new Relationship(this,parent,child,relationship);
219 for (int i=0; i<LazyList.size(_listeners); i++)
220 ((Listener)LazyList.get(_listeners, i)).remove(event);
221 }
222 }
223
224
225
226
227
228
229 public static class Relationship
230 {
231 private Object _parent;
232 private Object _child;
233 private String _relationship;
234 private Container _container;
235
236 private Relationship(Container container, Object parent,Object child, String relationship)
237 {
238 _container=container;
239 _parent=parent;
240 _child=child;
241 _relationship=relationship;
242 }
243
244 public Container getContainer()
245 {
246 return _container;
247 }
248
249 public Object getChild()
250 {
251 return _child;
252 }
253
254 public Object getParent()
255 {
256 return _parent;
257 }
258
259 public String getRelationship()
260 {
261 return _relationship;
262 }
263
264 @Override
265 public String toString()
266 {
267 return _parent+"---"+_relationship+"-->"+_child;
268 }
269
270 @Override
271 public int hashCode()
272 {
273 return _parent.hashCode()+_child.hashCode()+_relationship.hashCode();
274 }
275
276 @Override
277 public boolean equals(Object o)
278 {
279 if (o==null || !(o instanceof Relationship))
280 return false;
281 Relationship r = (Relationship)o;
282 return r._parent==_parent && r._child==_child && r._relationship.equals(_relationship);
283 }
284 }
285
286
287
288
289
290 public interface Listener extends EventListener
291 {
292 public void addBean(Object bean);
293 public void removeBean(Object bean);
294 public void add(Container.Relationship relationship);
295 public void remove(Container.Relationship relationship);
296
297 }
298 }