Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datagrid; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.FileNotFoundException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 11 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 14 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.datagrid.web.DatagridWebRoutable; |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 16 | |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| 20 | import com.hazelcast.config.Config; |
| 21 | import com.hazelcast.config.FileSystemXmlConfig; |
| 22 | import com.hazelcast.core.Hazelcast; |
| 23 | import com.hazelcast.core.HazelcastInstance; |
| 24 | import com.hazelcast.instance.GroupProperties; |
| 25 | |
| 26 | /** |
| 27 | * A datagrid service that uses Hazelcast as a datagrid. |
| 28 | * The relevant data is stored in the Hazelcast datagrid and shared as |
| 29 | * appropriate in a multi-node cluster. |
| 30 | */ |
| 31 | public class HazelcastDatagrid implements IFloodlightModule, IDatagridService { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 32 | static final Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class); |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * The name of Hazelcast instance in this JVM. |
| 36 | */ |
| 37 | public static final String ONOS_HAZELCAST_INSTANCE = "ONOS_HazelcastInstance"; |
| 38 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 39 | private IRestApiService restApi; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 40 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 41 | static final String HAZELCAST_CONFIG_FILE = "datagridConfig"; |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 42 | private static final String HAZELCAST_DEFAULT_XML = "conf/hazelcast.default.xml"; |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 43 | private HazelcastInstance hazelcastInstance; |
| 44 | private Config hazelcastConfig; |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 45 | |
Pavlin Radoslavov | 07af5f2 | 2014-03-21 15:17:58 -0700 | [diff] [blame] | 46 | // |
| 47 | // NOTE: eventChannels is kept thread safe by using explicit "synchronized" |
| 48 | // blocks below. Those are needed to protect the integrity of each entry |
| 49 | // instance, and avoid preemption during channel creation/startup. |
| 50 | // |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 51 | private final Map<String, IEventChannel<?, ?>> eventChannels = new HashMap<>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 52 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 53 | /** |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 54 | * Load the Hazelcast Datagrid configuration file. |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 55 | * |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 56 | * @param configFilename the configuration filename. |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 57 | */ |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 58 | public void loadHazelcastConfig(String configFilename) { |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 59 | /* |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 60 | System.setProperty("hazelcast.socket.receive.buffer.size", "32"); |
| 61 | System.setProperty("hazelcast.socket.send.buffer.size", "32"); |
| 62 | */ |
| 63 | // System.setProperty("hazelcast.heartbeat.interval.seconds", "100"); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 64 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 65 | // Init from configuration file |
| 66 | try { |
| 67 | hazelcastConfig = new FileSystemXmlConfig(configFilename); |
| 68 | } catch (FileNotFoundException e) { |
| 69 | log.error("Error opening Hazelcast XML configuration. File not found: " + configFilename, e); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 70 | |
| 71 | // Fallback mechanism to support running unit test without setup. |
| 72 | log.error("Falling back to default Hazelcast XML {}", HAZELCAST_DEFAULT_XML); |
| 73 | try { |
| 74 | hazelcastConfig = new FileSystemXmlConfig(HAZELCAST_DEFAULT_XML); |
| 75 | } catch (FileNotFoundException e2) { |
| 76 | log.error("Error opening fall back Hazelcast XML configuration. " |
| 77 | + "File not found: " + HAZELCAST_DEFAULT_XML, e2); |
| 78 | // XXX probably should throw some exception to kill ONOS instead. |
| 79 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 80 | } |
Yuta HIGUCHI | 6dfba39 | 2014-05-28 15:45:44 -0700 | [diff] [blame] | 81 | |
| 82 | // set the name of Hazelcast instance in this JVM. |
| 83 | hazelcastConfig.setInstanceName(ONOS_HAZELCAST_INSTANCE); |
| 84 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 85 | /* |
| 86 | hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1"); |
| 87 | hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1"); |
| 88 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_THREAD_COUNT, "1"); |
| 89 | */ |
| 90 | // |
| 91 | hazelcastConfig.setProperty(GroupProperties.PROP_EVENT_QUEUE_CAPACITY, "4000000"); |
| 92 | hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_RECEIVE_BUFFER_SIZE, "4096"); |
| 93 | hazelcastConfig.setProperty(GroupProperties.PROP_SOCKET_SEND_BUFFER_SIZE, "4096"); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Shutdown the Hazelcast Datagrid operation. |
| 98 | */ |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 99 | @Override |
| 100 | protected void finalize() { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 101 | close(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Shutdown the Hazelcast Datagrid operation. |
| 106 | */ |
| 107 | public void close() { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 108 | Hazelcast.shutdownAll(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the collection of offered module services. |
| 113 | * |
| 114 | * @return the collection of offered module services. |
| 115 | */ |
| 116 | @Override |
| 117 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 118 | Collection<Class<? extends IFloodlightService>> l = |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 119 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 120 | l.add(IDatagridService.class); |
| 121 | return l; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the collection of implemented services. |
| 126 | * |
| 127 | * @return the collection of implemented services. |
| 128 | */ |
| 129 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 130 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 131 | getServiceImpls() { |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 132 | Map<Class<? extends IFloodlightService>, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 133 | IFloodlightService> m = |
| 134 | new HashMap<Class<? extends IFloodlightService>, |
| 135 | IFloodlightService>(); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 136 | m.put(IDatagridService.class, this); |
| 137 | return m; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the collection of modules this module depends on. |
| 142 | * |
| 143 | * @return the collection of modules this module depends on. |
| 144 | */ |
| 145 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 146 | public Collection<Class<? extends IFloodlightService>> |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 147 | getModuleDependencies() { |
| 148 | Collection<Class<? extends IFloodlightService>> l = |
| 149 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 150 | l.add(IFloodlightProviderService.class); |
| 151 | l.add(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 152 | return l; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Initialize the module. |
| 157 | * |
| 158 | * @param context the module context to use for the initialization. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 159 | * @throws FloodlightModuleException on error |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 160 | */ |
| 161 | @Override |
| 162 | public void init(FloodlightModuleContext context) |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 163 | throws FloodlightModuleException { |
| 164 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 165 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 166 | // Get the configuration file name and configure the Datagrid |
| 167 | Map<String, String> configMap = context.getConfigParams(this); |
| 168 | String configFilename = configMap.get(HAZELCAST_CONFIG_FILE); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 169 | this.loadHazelcastConfig(configFilename); |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Startup module operation. |
| 174 | * |
| 175 | * @param context the module context to use for the startup. |
| 176 | */ |
| 177 | @Override |
| 178 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 179 | hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig); |
Pavlin Radoslavov | da7ef61 | 2013-10-30 16:12:14 -0700 | [diff] [blame] | 180 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 181 | restApi.addRestletRoutable(new DatagridWebRoutable()); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 185 | * Create an event channel. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 186 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 187 | * If the channel already exists, just return it. |
| 188 | * NOTE: The channel is started automatically. |
| 189 | * |
| 190 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 191 | * @param <K> the type of the Key in the Key-Value store. |
| 192 | * @param <V> the type of the Value in the Key-Value store. |
| 193 | * @param typeK the type of the Key in the Key-Value store. |
| 194 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 195 | * @return the event channel for the channel name. |
| 196 | */ |
| 197 | @Override |
| 198 | public <K, V> IEventChannel<K, V> createChannel(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 199 | Class<K> typeK, Class<V> typeV) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 200 | synchronized (eventChannels) { |
| 201 | IEventChannel<K, V> eventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 202 | createChannelImpl(channelName, typeK, typeV); |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 203 | eventChannel.startup(); |
| 204 | return eventChannel; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 205 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Create an event channel implementation. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 210 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 211 | * If the channel already exists, just return it. |
| 212 | * NOTE: The caller must call IEventChannel.startup() to startup the |
| 213 | * channel operation. |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 214 | * NOTE: The caller must own the lock on "eventChannels". |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 215 | * |
| 216 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 217 | * @param <K> the type of the Key in the Key-Value store. |
| 218 | * @param <V> the type of the Value in the Key-Value store. |
| 219 | * @param typeK the type of the Key in the Key-Value store. |
| 220 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 221 | * @return the event channel for the channel name. |
| 222 | */ |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 223 | private <K, V> IEventChannel<K, V> createChannelImpl( |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 224 | String channelName, |
| 225 | Class<K> typeK, Class<V> typeV) { |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 226 | IEventChannel<?, ?> genericEventChannel = |
| 227 | eventChannels.get(channelName); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 228 | |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 229 | // Add the channel if the first listener |
| 230 | if (genericEventChannel == null) { |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 231 | IEventChannel<K, V> castedEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 232 | new HazelcastEventChannel<K, V>(hazelcastInstance, |
| 233 | channelName, typeK, typeV); |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 234 | eventChannels.put(channelName, castedEventChannel); |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 235 | return castedEventChannel; |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 236 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 237 | |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 238 | // |
| 239 | // TODO: Find if we can use Java internal support to check for |
| 240 | // type mismatch. |
| 241 | // |
| 242 | if (!genericEventChannel.verifyKeyValueTypes(typeK, typeV)) { |
| 243 | throw new ClassCastException("Key-value type mismatch for event channel " + channelName); |
| 244 | } |
| 245 | @SuppressWarnings("unchecked") |
| 246 | IEventChannel<K, V> castedEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 247 | (IEventChannel<K, V>) genericEventChannel; |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 248 | return castedEventChannel; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Add event channel listener. |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 253 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 254 | * NOTE: The channel is started automatically right after the listener |
| 255 | * is added. |
| 256 | * |
| 257 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 258 | * @param listener the listener to add. |
| 259 | * @param <K> the type of the Key in the Key-Value store. |
| 260 | * @param <V> the type of the Value in the Key-Value store. |
| 261 | * @param typeK the type of the Key in the Key-Value store. |
| 262 | * @param typeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 263 | * @return the event channel for the channel name. |
| 264 | */ |
| 265 | @Override |
| 266 | public <K, V> IEventChannel<K, V> addListener(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 267 | IEventChannelListener<K, V> listener, |
| 268 | Class<K> typeK, Class<V> typeV) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 269 | synchronized (eventChannels) { |
| 270 | IEventChannel<K, V> eventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 271 | createChannelImpl(channelName, typeK, typeV); |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 272 | eventChannel.addListener(listener); |
| 273 | eventChannel.startup(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 274 | |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 275 | return eventChannel; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 276 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Remove event channel listener. |
| 281 | * |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 282 | * @param <K> the type of the Key in the Key-Value store. |
| 283 | * @param <V> the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 284 | * @param channelName the event channel name. |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 285 | * @param listener the listener to remove. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 286 | */ |
| 287 | @Override |
| 288 | public <K, V> void removeListener(String channelName, |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 289 | IEventChannelListener<K, V> listener) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 290 | synchronized (eventChannels) { |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 291 | IEventChannel<?, ?> genericEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 292 | eventChannels.get(channelName); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 293 | |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 294 | if (genericEventChannel != null) { |
| 295 | // |
| 296 | // TODO: Find if we can use Java internal support to check for |
| 297 | // type mismatch. |
| 298 | // NOTE: Using "ClassCastException" exception below doesn't |
| 299 | // work. |
| 300 | // |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 301 | @SuppressWarnings("unchecked") |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 302 | IEventChannel<K, V> castedEventChannel = |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 303 | (IEventChannel<K, V>) genericEventChannel; |
Pavlin Radoslavov | 00fad59 | 2014-03-21 11:32:34 -0700 | [diff] [blame] | 304 | castedEventChannel.removeListener(listener); |
| 305 | } |
Ray Milkey | 0ab2d8a | 2014-03-20 14:30:10 -0700 | [diff] [blame] | 306 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 307 | } |
Pavlin Radoslavov | 1eee2c8 | 2013-10-15 02:30:32 -0700 | [diff] [blame] | 308 | } |