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