1 21 22 package org.jacorb.notification.filter; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.apache.avalon.framework.logger.Logger; 29 import org.jacorb.notification.EventTypeSet; 30 import org.jacorb.notification.interfaces.Disposable; 31 import org.jacorb.notification.util.LogUtil; 32 import org.omg.CosNotification.EventType; 33 import org.omg.CosNotifyComm.InvalidEventType; 34 import org.omg.CosNotifyComm.NotifySubscribe; 35 36 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 37 38 42 public class CallbackManager extends EventTypeSet implements Disposable 43 { 44 private final SynchronizedInt callbackIdPool_ = new SynchronizedInt(0); 45 46 private final Map callbacks_ = new HashMap (); 47 48 private final Logger logger_ = LogUtil.getLogger(getClass().getName()); 49 50 public int attach_callback(NotifySubscribe subscriber) 51 { 52 final int id = callbackIdPool_.increment(); 53 54 Integer key = new Integer (id); 55 56 callbacks_.put(key, subscriber); 57 58 if (logger_.isInfoEnabled()) 59 { 60 logger_.info("attach_callback: ID=" + id + " Subscriber=" + subscriber); 61 } 62 63 return id; 64 } 65 66 public void detach_callback(int id) 67 { 68 Object removed = callbacks_.remove(new Integer (id)); 69 70 if (logger_.isInfoEnabled()) 71 { 72 boolean success = (removed != null); 73 logger_.info("detach_callback: ID=" + id + " Success=" + success); 74 } 75 } 76 77 public int[] get_callbacks() 78 { 79 Integer [] keys = (Integer []) callbacks_.keySet().toArray(new Integer [0]); 80 81 int[] ids = new int[keys.length]; 82 83 for (int i = 0; i < keys.length; i++) 84 { 85 ids[i] = keys[i].intValue(); 86 } 87 88 return ids; 89 } 90 91 protected void actionSetChanged(EventType[] added, EventType[] removed) 92 { 93 Iterator i = callbacks_.keySet().iterator(); 94 95 while (i.hasNext()) 96 { 97 NotifySubscribe notifySubscribe = (NotifySubscribe) callbacks_.get(i.next()); 98 99 try 100 { 101 notifySubscribe.subscription_change(added, removed); 102 } catch (InvalidEventType e) 103 { 104 logger_.error("error during subscription_change", e); 105 } 106 } 107 } 108 109 public void replaceWith(EventType[] replacement) throws InterruptedException ![JavaDoc](../../../../../cmn/javadoc.gif) 110 { 111 changeSet(replacement, getAllTypes()); 112 } 113 114 public void dispose() 115 { 116 callbacks_.clear(); 117 } 118 } | Popular Tags |