KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jmx > WaitForChildren


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.jmx;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.oddjob.Structural;
11 import org.oddjob.structural.StructuralEvent;
12 import org.oddjob.structural.StructuralListener;
13
14 public class WaitForChildren implements StructuralListener {
15     private static final Logger logger = Logger.getLogger(WaitForChildren.class);
16     
17     private final Structural structural;
18     private List JavaDoc children;
19     
20     public WaitForChildren(Object JavaDoc o) {
21         structural = (Structural) o;
22     }
23     
24     public void waitFor(int count) {
25         children = new ArrayList JavaDoc();
26         structural.addStructuralListener(this);
27         try {
28             synchronized (this) {
29                 while (children.size() != count) {
30                     logger.debug("Waiting for [" + structural +
31                             "] to have [" + count + "] children (has "
32                             + children.size() + ")");
33                     wait(5000);
34                 }
35             }
36         } catch (InterruptedException JavaDoc e) {
37         } finally {
38             structural.removeStructuralListener(this);
39         }
40         
41     }
42     
43     public Object JavaDoc[] children() {
44         return children.toArray();
45     }
46     
47     synchronized public void childAdded(StructuralEvent event) {
48         children.add(event.getIndex(), event.getChild());
49         notifyAll();
50     }
51     
52     synchronized public void childRemoved(StructuralEvent event) {
53         children.remove(event.getIndex());
54         notifyAll();
55     }
56 }
Popular Tags