KickJava   Java API By Example, From Geeks To Geeks.

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


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.NotifyPublishOperations;
33
34 /**
35  * @author Alphonse Bendt
36  * @version $Id: OfferManager.java,v 1.6 2005/02/13 23:56:59 alphonse.bendt Exp $
37  */

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

59     public void addListener(NotifyPublishOperations listener)
60     {
61         synchronized (listeners_)
62         {
63             listeners_.add(listener);
64         }
65     }
66
67     public void removeListener(NotifyPublishOperations 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 happens during offer_change.
82
Iterator JavaDoc _i = new ArrayList JavaDoc(listeners_).iterator();
83
84             while (_i.hasNext())
85             {
86                 NotifyPublishOperations _listener = (NotifyPublishOperations) _i.next();
87
88                 try
89                 {
90                     _listener.offer_change(added, removed);
91                 } catch (Exception JavaDoc e)
92                 {
93                     logger_.error("offer_change failed for " + _listener, e);
94                 }
95             }
96         }
97     }
98
99     public void offer_change(EventType[] added, EventType[] removed) throws InvalidEventType
100     {
101         try
102         {
103             changeSet(added, removed);
104         } catch (InterruptedException JavaDoc e)
105         {
106             // ignore
107
}
108     }
109
110     public EventType[] obtain_offered_types()
111     {
112         try
113         {
114             return getAllTypes();
115         } catch (InterruptedException JavaDoc e)
116         {
117             return EMPTY_EVENT_TYPE;
118         }
119     }
120 }
Popular Tags