1 4 package org.oddjob.jmx; 5 6 import java.util.ArrayList ; 7 import java.util.List ; 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 children; 19 20 public WaitForChildren(Object o) { 21 structural = (Structural) o; 22 } 23 24 public void waitFor(int count) { 25 children = new ArrayList (); 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 e) { 37 } finally { 38 structural.removeStructuralListener(this); 39 } 40 41 } 42 43 public Object [] 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 |