View Javadoc

1   package org.votech.plastic.managers;
2   
3   import java.util.Collection;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   
7   /**
8    * Handles the notification of listeners.
9    * @author jdt
10   *
11   */
12  public abstract class AbstractObservableManager implements ObservableManager  {
13  //TODO move this
14  	public interface ManagerObserver {
15  
16  		void down();
17  
18  		void up();
19  
20  	}
21  
22      //TODO switch to copyonwrite implementation when we switch to java5 and remove
23      //the synchronization
24  	private Collection observers = new HashSet();
25      
26      public AbstractObservableManager() {
27          //Ensure we try to unregister on closing
28          Runtime.getRuntime().addShutdownHook(new Thread() {
29             public void run() {
30                  disconnect();
31             }
32          });
33      }
34  
35  	/* (non-Javadoc)
36  	 * @see org.votech.plastic.managers.ObservableManager#addObserver(org.votech.plastic.managers.AbstractManager.ManagerObserver)
37  	 */
38  	public synchronized void addObserver(ManagerObserver observer) {
39  		observers.add(observer);
40  	}
41  
42  	/* (non-Javadoc)
43  	 * @see org.votech.plastic.managers.ObservableManager#removeObserver(org.votech.plastic.managers.AbstractManager.ManagerObserver)
44  	 */
45  	public synchronized void removeObserver(ManagerObserver observer) {
46  		observers.remove(observer);
47  	}
48  
49  	protected synchronized void notifyObserversUp() {
50  		Iterator it = observers.iterator(); //	TODO ought to do this asynch
51  		while(it.hasNext()) {
52  			ManagerObserver observer = (ManagerObserver) it.next();
53  			observer.up();
54  		}
55  		
56  	}
57  
58  	protected synchronized void notifyObserversDown() {
59  		Iterator it = observers.iterator(); //	TODO ought to do this asynch
60  		while(it.hasNext()) {
61  			ManagerObserver observer = (ManagerObserver) it.next();
62  			observer.down();
63  		}
64  	}
65  
66  }