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