001: package org.jacorb.notification;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2003 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.jacorb.notification.interfaces.Disposable;
030: import org.jacorb.notification.interfaces.EventChannelEvent;
031: import org.jacorb.notification.interfaces.EventChannelEventListener;
032: import org.omg.CosNotifyChannelAdmin.ChannelNotFound;
033:
034: /**
035: * @author Alphonse Bendt
036: * @version $Id: ChannelManager.java,v 1.5 2005/08/21 13:29:03 alphonse.bendt Exp $
037: */
038: public class ChannelManager implements Disposable {
039: private static final Object[] INTEGER_ARRAY_TEMPLATE = new Integer[0];
040:
041: private final Map channels_ = new HashMap();
042:
043: private final Object channelsLock_ = channels_;
044:
045: private boolean isChannelsModified_ = true;
046:
047: private int[] cachedKeys_;
048:
049: private final List eventListeners_ = new ArrayList();
050:
051: //////////////////////////////
052:
053: public int[] get_all_channels() {
054: synchronized (channelsLock_) {
055: if (isChannelsModified_) {
056: Integer[] _keys = (Integer[]) channels_.keySet()
057: .toArray(INTEGER_ARRAY_TEMPLATE);
058:
059: cachedKeys_ = new int[_keys.length];
060:
061: for (int x = 0; x < _keys.length; ++x) {
062: cachedKeys_[x] = _keys[x].intValue();
063: }
064:
065: isChannelsModified_ = false;
066: }
067: }
068: return cachedKeys_;
069: }
070:
071: public AbstractEventChannel get_channel_servant(int id)
072: throws ChannelNotFound {
073: Integer _key = new Integer(id);
074:
075: synchronized (channelsLock_) {
076: if (channels_.containsKey(_key)) {
077: return (AbstractEventChannel) channels_.get(_key);
078: }
079:
080: throw new ChannelNotFound("The Channel " + id
081: + " does not exist");
082: }
083: }
084:
085: public void add_channel(int key, final AbstractEventChannel channel) {
086: final Integer _key = new Integer(key);
087:
088: synchronized (channelsLock_) {
089: channels_.put(_key, channel);
090: isChannelsModified_ = true;
091: }
092:
093: channel.registerDisposable(new Disposable() {
094: public void dispose() {
095: synchronized (channelsLock_) {
096: channels_.remove(_key);
097: isChannelsModified_ = true;
098: }
099:
100: fireChannelRemoved(channel);
101: }
102: });
103:
104: fireChannelAdded(channel);
105: }
106:
107: private void fireChannelRemoved(AbstractEventChannel channel) {
108: EventChannelEvent _event = new EventChannelEvent(channel);
109:
110: synchronized (eventListeners_) {
111: Iterator i = eventListeners_.iterator();
112:
113: while (i.hasNext()) {
114: ((EventChannelEventListener) i.next())
115: .actionEventChannelDestroyed(_event);
116: }
117: }
118: }
119:
120: private void fireChannelAdded(AbstractEventChannel servant) {
121: EventChannelEvent _event = new EventChannelEvent(servant);
122:
123: synchronized (eventListeners_) {
124: Iterator i = eventListeners_.iterator();
125:
126: while (i.hasNext()) {
127: ((EventChannelEventListener) i.next())
128: .actionEventChannelCreated(_event);
129: }
130: }
131: }
132:
133: public void addEventChannelEventListener(
134: EventChannelEventListener listener) {
135: synchronized (eventListeners_) {
136: eventListeners_.add(listener);
137: }
138: }
139:
140: public void removeEventChannelEventListener(
141: EventChannelEventListener listener) {
142: synchronized (eventListeners_) {
143: eventListeners_.remove(listener);
144: }
145: }
146:
147: public Iterator getChannelIterator() {
148: synchronized (channelsLock_) {
149: return channels_.entrySet().iterator();
150: }
151: }
152:
153: public void dispose() {
154: synchronized (channelsLock_) {
155: Iterator i = channels_.entrySet().iterator();
156:
157: while (i.hasNext()) {
158: AbstractEventChannel _channel = (AbstractEventChannel) ((Map.Entry) i
159: .next()).getValue();
160:
161: i.remove();
162: _channel.destroy();
163: }
164: }
165:
166: eventListeners_.clear();
167: }
168: }
|