1 package org.jacorb.notification; 2 3 22 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 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 38 public class ChannelManager implements Disposable 39 { 40 private static final Object [] INTEGER_ARRAY_TEMPLATE = new Integer [0]; 41 42 private final Map channels_ = new HashMap (); 43 44 private final Object channelsLock_ = channels_; 45 46 private boolean isChannelsModified_ = true; 47 48 private int[] cachedKeys_; 49 50 private final List eventListeners_ = new ArrayList (); 51 52 54 public int[] get_all_channels() 55 { 56 synchronized (channelsLock_) 57 { 58 if (isChannelsModified_) 59 { 60 Integer [] _keys = (Integer []) 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 _key = new Integer (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 _key = new Integer (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 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 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 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 i = channels_.entrySet().iterator(); 176 177 while (i.hasNext()) 178 { 179 AbstractEventChannel _channel = (AbstractEventChannel) ((Map.Entry ) i.next()) 180 .getValue(); 181 182 i.remove(); 183 _channel.dispose(); 184 } 185 } 186 187 eventListeners_.clear(); 188 } 189 } | Popular Tags |