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