KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > CallbackManager


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21
22 package org.jacorb.notification.filter;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
39  * @author Alphonse Bendt
40  * @version $Id: CallbackManager.java,v 1.2 2005/03/31 20:16:47 alphonse.bendt Exp $
41  */

42 public class CallbackManager extends EventTypeSet implements Disposable
43 {
44     private final SynchronizedInt callbackIdPool_ = new SynchronizedInt(0);
45
46     private final Map JavaDoc callbacks_ = new HashMap JavaDoc();
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 JavaDoc key = new Integer JavaDoc(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 JavaDoc removed = callbacks_.remove(new Integer JavaDoc(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 JavaDoc[] keys = (Integer JavaDoc[]) callbacks_.keySet().toArray(new Integer JavaDoc[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 JavaDoc 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
110     {
111         changeSet(replacement, getAllTypes());
112     }
113     
114     public void dispose()
115     {
116         callbacks_.clear();
117     }
118 }
Popular Tags