1 // ========================================================================
2 // Copyright (c) 2005-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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 import org.eclipse.jetty.util.log.Logger;
22
23 /* ------------------------------------------------------------ */
24 /** Container.
25 * This class allows a containment events to be generated from update methods.
26 *
27 * The style of usage is: <pre>
28 * public void setFoo(Foo foo)
29 * {
30 * getContainer().update(this,this.foo,foo,"foo");
31 * this.foo=foo;
32 * }
33 *
34 * public void setBars(Bar[] bars)
35 * {
36 * getContainer().update(this,this.bars,bars,"bar");
37 * this.bars=bars;
38 * }
39 * </pre>
40 */
41 public class Container
42 {
43 private static final Logger LOG = Log.getLogger(Container.class);
44 private final CopyOnWriteArrayList<Container.Listener> _listeners=new CopyOnWriteArrayList<Container.Listener>();
45
46 public void addEventListener(Container.Listener listener)
47 {
48 _listeners.add(listener);
49 }
50
51 public void removeEventListener(Container.Listener listener)
52 {
53 _listeners.remove(listener);
54 }
55
56 /* ------------------------------------------------------------ */
57 /** Update single parent to child relationship.
58 * @param parent The parent of the child.
59 * @param oldChild The previous value of the child. If this is non null and differs from <code>child</code>, then a remove event is generated.
60 * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated.
61 * @param relationship The name of the relationship
62 */
63 public void update(Object parent, Object oldChild, final Object child, String relationship)
64 {
65 if (oldChild!=null && !oldChild.equals(child))
66 remove(parent,oldChild,relationship);
67 if (child!=null && !child.equals(oldChild))
68 add(parent,child,relationship);
69 }
70
71 /* ------------------------------------------------------------ */
72 /** Update single parent to child relationship.
73 * @param parent The parent of the child.
74 * @param oldChild The previous value of the child. If this is non null and differs from <code>child</code>, then a remove event is generated.
75 * @param child The current child. If this is non null and differs from <code>oldChild</code>, then an add event is generated.
76 * @param relationship The name of the relationship
77 * @param addRemove If true add/remove is called for the new/old children as well as the relationships
78 */
79 public void update(Object parent, Object oldChild, final Object child, String relationship,boolean addRemove)
80 {
81 if (oldChild!=null && !oldChild.equals(child))
82 {
83 remove(parent,oldChild,relationship);
84 if (addRemove)
85 removeBean(oldChild);
86 }
87
88 if (child!=null && !child.equals(oldChild))
89 {
90 if (addRemove)
91 addBean(child);
92 add(parent,child,relationship);
93 }
94 }
95
96 /* ------------------------------------------------------------ */
97 /** Update multiple parent to child relationship.
98 * @param parent The parent of the child.
99 * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the <code>children</code> array.
100 * This array is modified and children that remain in the new children array are nulled out of the old children array.
101 * @param children The current array of children. An add event is generated for any child in this array but not in the <code>oldChildren</code> array.
102 * @param relationship The name of the relationship
103 */
104 public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship)
105 {
106 update(parent,oldChildren,children,relationship,false);
107 }
108
109 /* ------------------------------------------------------------ */
110 /** Update multiple parent to child relationship.
111 * @param parent The parent of the child.
112 * @param oldChildren The previous array of children. A remove event is generated for any child in this array but not in the <code>children</code> array.
113 * This array is modified and children that remain in the new children array are nulled out of the old children array.
114 * @param children The current array of children. An add event is generated for any child in this array but not in the <code>oldChildren</code> array.
115 * @param relationship The name of the relationship
116 * @param addRemove If true add/remove is called for the new/old children as well as the relationships
117 */
118 public void update(Object parent, Object[] oldChildren, final Object[] children, String relationship, boolean addRemove)
119 {
120 Object[] newChildren = null;
121 if (children!=null)
122 {
123 newChildren = new Object[children.length];
124
125 for (int i=children.length;i-->0;)
126 {
127 boolean new_child=true;
128 if (oldChildren!=null)
129 {
130 for (int j=oldChildren.length;j-->0;)
131 {
132 if (children[i]!=null && children[i].equals(oldChildren[j]))
133 {
134 oldChildren[j]=null;
135 new_child=false;
136 }
137 }
138 }
139 if (new_child)
140 newChildren[i]=children[i];
141 }
142 }
143
144 if (oldChildren!=null)
145 {
146 for (int i=oldChildren.length;i-->0;)
147 {
148 if (oldChildren[i]!=null)
149 {
150 remove(parent,oldChildren[i],relationship);
151 if (addRemove)
152 removeBean(oldChildren[i]);
153 }
154 }
155 }
156
157 if (newChildren!=null)
158 {
159 for (int i=0;i<newChildren.length;i++)
160 if (newChildren[i]!=null)
161 {
162 if (addRemove)
163 addBean(newChildren[i]);
164 add(parent,newChildren[i],relationship);
165 }
166 }
167 }
168
169 /* ------------------------------------------------------------ */
170 public void addBean(Object obj)
171 {
172 if (_listeners!=null)
173 {
174 for (int i=0; i<LazyList.size(_listeners); i++)
175 {
176 Listener listener=(Listener)LazyList.get(_listeners, i);
177 listener.addBean(obj);
178 }
179 }
180 }
181
182 /* ------------------------------------------------------------ */
183 public void removeBean(Object obj)
184 {
185 if (_listeners!=null)
186 {
187 for (int i=0; i<LazyList.size(_listeners); i++)
188 ((Listener)LazyList.get(_listeners, i)).removeBean(obj);
189 }
190 }
191
192 /* ------------------------------------------------------------ */
193 /** Add a parent child relationship
194 * @param parent
195 * @param child
196 * @param relationship
197 */
198 private void add(Object parent, Object child, String relationship)
199 {
200 if (LOG.isDebugEnabled())
201 LOG.debug("Container "+parent+" + "+child+" as "+relationship);
202 if (_listeners!=null)
203 {
204 Relationship event=new Relationship(this,parent,child,relationship);
205 for (int i=0; i<LazyList.size(_listeners); i++)
206 ((Listener)LazyList.get(_listeners, i)).add(event);
207 }
208 }
209
210 /* ------------------------------------------------------------ */
211 /** remove a parent child relationship
212 * @param parent
213 * @param child
214 * @param relationship
215 */
216 private void remove(Object parent, Object child, String relationship)
217 {
218 if (LOG.isDebugEnabled())
219 LOG.debug("Container "+parent+" - "+child+" as "+relationship);
220 if (_listeners!=null)
221 {
222 Relationship event=new Relationship(this,parent,child,relationship);
223 for (int i=0; i<LazyList.size(_listeners); i++)
224 ((Listener)LazyList.get(_listeners, i)).remove(event);
225 }
226 }
227
228 /* ------------------------------------------------------------ */
229 /** A Container event.
230 * @see Listener
231 */
232 public static class Relationship
233 {
234 private final WeakReference<Object> _parent;
235 private final WeakReference<Object> _child;
236 private String _relationship;
237 private Container _container;
238
239 private Relationship(Container container, Object parent,Object child, String relationship)
240 {
241 _container=container;
242 _parent=new WeakReference<Object>(parent);
243 _child=new WeakReference<Object>(child);
244 _relationship=relationship;
245 }
246
247 public Container getContainer()
248 {
249 return _container;
250 }
251
252 public Object getChild()
253 {
254 return _child.get();
255 }
256
257 public Object getParent()
258 {
259 return _parent.get();
260 }
261
262 public String getRelationship()
263 {
264 return _relationship;
265 }
266
267 @Override
268 public String toString()
269 {
270 return _parent+"---"+_relationship+"-->"+_child;
271 }
272
273 @Override
274 public int hashCode()
275 {
276 return _parent.hashCode()+_child.hashCode()+_relationship.hashCode();
277 }
278
279 @Override
280 public boolean equals(Object o)
281 {
282 if (o==null || !(o instanceof Relationship))
283 return false;
284 Relationship r = (Relationship)o;
285 return r._parent.get()==_parent.get() && r._child.get()==_child.get() && r._relationship.equals(_relationship);
286 }
287 }
288
289 /* ------------------------------------------------------------ */
290 /** Listener.
291 * A listener for Container events.
292 */
293 public interface Listener extends EventListener
294 {
295 public void addBean(Object bean);
296 public void removeBean(Object bean);
297 public void add(Container.Relationship relationship);
298 public void remove(Container.Relationship relationship);
299 }
300 }