KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > SubscriptionManager


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

22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.avalon.framework.logger.Logger;
29 import org.jacorb.notification.util.LogUtil;
30 import org.omg.CosNotification.EventType;
31 import org.omg.CosNotifyComm.InvalidEventType;
32 import org.omg.CosNotifyComm.NotifySubscribeOperations;
33
34 /**
35  * @author Alphonse Bendt
36  * @version $Id: SubscriptionManager.java,v 1.7 2005/02/13 23:56:59 alphonse.bendt Exp $
37  */

38
39 public class SubscriptionManager extends EventTypeSet implements NotifySubscribeOperations
40 {
41     public static final SubscriptionManager NULL_MANAGER = new SubscriptionManager(Collections.EMPTY_LIST);
42     
43     private final Logger logger_ = LogUtil.getLogger(getClass().getName());
44
45     private final List JavaDoc listeners_;
46
47     public SubscriptionManager()
48     {
49         this(new ArrayList JavaDoc());
50     }
51
52     private SubscriptionManager(List JavaDoc list)
53     {
54         listeners_ = list;
55     }
56
57     ////////////////////////////////////////
58

59     public void addListener(NotifySubscribeOperations listener)
60     {
61         synchronized (listeners_)
62         {
63             listeners_.add(listener);
64         }
65     }
66
67     public void removeListener(NotifySubscribeOperations listener)
68     {
69         synchronized (listeners_)
70         {
71             listeners_.remove(listener);
72         }
73     }
74
75     public void actionSetChanged(EventType[] added, EventType[] removed)
76     {
77         synchronized (listeners_)
78         {
79             // use a iterator on a copy of the original list here.
80
// otherwise the iterator would fail if the list would be
81
// modified concurrently which may happen during
82
// subscription_change.
83
Iterator JavaDoc _i = new ArrayList JavaDoc(listeners_).iterator();
84
85             while (_i.hasNext())
86             {
87                 NotifySubscribeOperations _listener = (NotifySubscribeOperations) _i.next();
88
89                 try
90                 {
91                     _listener.subscription_change(added, removed);
92                 } catch (Exception JavaDoc e)
93                 {
94                     logger_.error("subscription_change failed for " + _listener, e);
95                 }
96             }
97         }
98     }
99
100     public void subscription_change(EventType[] added, EventType[] removed) throws InvalidEventType
101     {
102         try
103         {
104             changeSet(added, removed);
105         } catch (InterruptedException JavaDoc e)
106         {
107           // ignore
108
}
109     }
110
111     public EventType[] obtain_subscription_types()
112     {
113         try
114         {
115             return getAllTypes();
116         } catch (InterruptedException JavaDoc e)
117         {
118             return EMPTY_EVENT_TYPE;
119         }
120     }
121 }
Popular Tags