KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2003 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.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.jacorb.notification.interfaces.Disposable;
30 import org.jacorb.notification.interfaces.EventChannelEvent;
31 import org.jacorb.notification.interfaces.EventChannelEventListener;
32 import org.omg.CosNotifyChannelAdmin.ChannelNotFound;
33
34 /**
35  * @author Alphonse Bendt
36  * @version $Id: ChannelManager.java,v 1.4 2005/02/13 23:56:59 alphonse.bendt Exp $
37  */

38 public class ChannelManager implements Disposable
39 {
40     private static final Object JavaDoc[] INTEGER_ARRAY_TEMPLATE = new Integer JavaDoc[0];
41
42     private final Map JavaDoc channels_ = new HashMap JavaDoc();
43
44     private final Object JavaDoc channelsLock_ = channels_;
45
46     private boolean isChannelsModified_ = true;
47
48     private int[] cachedKeys_;
49
50     private final List JavaDoc eventListeners_ = new ArrayList JavaDoc();
51
52     //////////////////////////////
53

54     public int[] get_all_channels()
55     {
56         synchronized (channelsLock_)
57         {
58             if (isChannelsModified_)
59             {
60                 Integer JavaDoc[] _keys = (Integer JavaDoc[]) channels_.keySet().toArray(INTEGER_ARRAY_TEMPLATE);
61
62                 cachedKeys_ = new int[_keys.length];
63
64                 for (int x = 0; x < _keys.length; ++x)
65                 {
66                     cachedKeys_[x] = _keys[x].intValue();
67                 }
68
69                 isChannelsModified_ = false;
70             }
71         }
72         return cachedKeys_;
73     }
74
75     public AbstractEventChannel get_channel_servant(int id) throws ChannelNotFound
76     {
77         Integer JavaDoc _key = new Integer JavaDoc(id);
78
79         synchronized (channelsLock_)
80         {
81             if (channels_.containsKey(_key))
82             {
83                 return (AbstractEventChannel) channels_.get(_key);
84             }
85
86             throw new ChannelNotFound("The Channel " + id + " does not exist");
87         }
88     }
89
90     public void add_channel(int key, final AbstractEventChannel channel)
91     {
92         final Integer JavaDoc _key = new Integer JavaDoc(key);
93
94         synchronized (channelsLock_)
95         {
96             channels_.put(_key, channel);
97             isChannelsModified_ = true;
98         }
99
100         channel.addDisposeHook(new Disposable()
101         {
102             public void dispose()
103             {
104                 synchronized (channelsLock_)
105                 {
106                     channels_.remove(_key);
107                     isChannelsModified_ = true;
108                 }
109
110                 fireChannelRemoved(channel);
111             }
112         });
113
114         fireChannelAdded(channel);
115     }
116
117     private void fireChannelRemoved(AbstractEventChannel channel)
118     {
119         EventChannelEvent _event = new EventChannelEvent(channel);
120
121         synchronized (eventListeners_)
122         {
123             Iterator JavaDoc i = eventListeners_.iterator();
124
125             while (i.hasNext())
126             {
127                 ((EventChannelEventListener) i.next()).actionEventChannelDestroyed(_event);
128             }
129         }
130     }
131
132     private void fireChannelAdded(AbstractEventChannel servant)
133     {
134         EventChannelEvent _event = new EventChannelEvent(servant);
135
136         synchronized (eventListeners_)
137         {
138             Iterator JavaDoc i = eventListeners_.iterator();
139
140             while (i.hasNext())
141             {
142                 ((EventChannelEventListener) i.next()).actionEventChannelCreated(_event);
143             }
144         }
145     }
146
147     public void addEventChannelEventListener(EventChannelEventListener listener)
148     {
149         synchronized (eventListeners_)
150         {
151             eventListeners_.add(listener);
152         }
153     }
154
155     public void removeEventChannelEventListener(EventChannelEventListener listener)
156     {
157         synchronized (eventListeners_)
158         {
159             eventListeners_.remove(listener);
160         }
161     }
162
163     public Iterator JavaDoc getChannelIterator()
164     {
165         synchronized (channelsLock_)
166         {
167             return channels_.entrySet().iterator();
168         }
169     }
170
171     public void dispose()
172     {
173         synchronized (channelsLock_)
174         {
175             Iterator JavaDoc i = channels_.entrySet().iterator();
176
177             while (i.hasNext())
178             {
179                 AbstractEventChannel _channel = (AbstractEventChannel) ((Map.Entry JavaDoc) i.next())
180                         .getValue();
181
182                 i.remove();
183                 _channel.dispose();
184             }
185         }
186
187         eventListeners_.clear();
188     }
189 }
Popular Tags