From 9155e6f9f61d81f6d59aaa5f7677a44f066dce5d Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Wed, 3 Apr 2013 10:51:04 +0200 Subject: [PATCH] Added Google Guava as dependency. Replaced Map for SAL listeners. Signed-off-by: Tony Tkacik --- opendaylight/sal/yang-prototype/sal/pom.xml | 29 +- .../sal/sal-binding-broker-impl/pom.xml | 72 +-- .../sal/binding/impl/BrokerUtils.java | 40 -- .../sal/binding/impl/NotificationModule.java | 550 +++++++++--------- .../sal/sal-broker-impl/pom.xml | 6 +- .../controller/sal/core/impl/BrokerImpl.java | 1 - .../controller/sal/core/impl/Utils.java | 40 -- .../core/impl/notify/NotificationModule.java | 44 +- 8 files changed, 363 insertions(+), 419 deletions(-) delete mode 100644 opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java delete mode 100644 opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/Utils.java diff --git a/opendaylight/sal/yang-prototype/sal/pom.xml b/opendaylight/sal/yang-prototype/sal/pom.xml index 2c494e6bb9..1b633b4a25 100644 --- a/opendaylight/sal/yang-prototype/sal/pom.xml +++ b/opendaylight/sal/yang-prototype/sal/pom.xml @@ -20,8 +20,23 @@ sal-core-demo - + + + + com.google.guava + guava + 14.0.1 + jar + + + org.slf4j + slf4j-api + 1.7.2 + + + + junit junit @@ -30,14 +45,10 @@ true - org.slf4j - slf4j-api - 1.7.2 - - - org.slf4j - slf4j-simple - 1.7.2 + org.mockito + mockito-all + 1.9.5 + test diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/pom.xml b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/pom.xml index 9c5552edbd..f126744652 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/pom.xml +++ b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/pom.xml @@ -1,35 +1,41 @@ - - 4.0.0 - - org.opendaylight.controller - sal - 1.0-SNAPSHOT - - sal-binding-broker-impl - - - - - - org.opendaylight.controller - sal-binding-api - 1.0-SNAPSHOT - - - org.opendaylight.controller - sal-binding-spi - 1.0-SNAPSHOT - - - org.opendaylight.controller - sal-core-api - 1.0-SNAPSHOT - + + 4.0.0 + + org.opendaylight.controller + sal + 1.0-SNAPSHOT + + sal-binding-broker-impl - - org.slf4j - slf4j-api - 1.6.4 - - + + + + + org.opendaylight.controller + sal-binding-api + 1.0-SNAPSHOT + + + org.opendaylight.controller + sal-binding-spi + 1.0-SNAPSHOT + + + org.opendaylight.controller + sal-core-api + 1.0-SNAPSHOT + + + + org.slf4j + slf4j-api + + + + com.google.guava + guava + jar + + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java deleted file mode 100644 index e174559c88..0000000000 --- a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/BrokerUtils.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.impl; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class BrokerUtils { - private BrokerUtils() { - - } - - - public static void addToMap(Map, List> map, Class key, V value) { - List list = map.get(key); - if (list == null) { - list = new ArrayList(); - map.put(key, list); - } - list.add(value); - } - - public static void removeFromMap(Map, List> map, Class key, - V value) { - List list = map.get(key); - if (list == null) { - return; - } - list.remove(value); - if (list.isEmpty()) { - map.remove(key); - } - } -} diff --git a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java index f029a2f9f4..89c44643ed 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java +++ b/opendaylight/sal/yang-prototype/sal/sal-binding-broker-impl/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationModule.java @@ -1,274 +1,276 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.binding.impl; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; -import org.opendaylight.controller.sal.binding.api.BindingAwareService; -import org.opendaylight.controller.sal.binding.api.NotificationProviderService; -import org.opendaylight.controller.sal.binding.api.NotificationService; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerSession; -import org.opendaylight.controller.sal.binding.spi.SALBindingModule; -import org.opendaylight.controller.sal.binding.spi.Mapper; -import org.opendaylight.controller.sal.binding.spi.MappingProvider; -import org.opendaylight.controller.sal.binding.spi.MappingProvider.MappingExtensionFactory; - -import org.opendaylight.controller.sal.core.api.Broker.ProviderSession; -import org.opendaylight.controller.yang.binding.DataObject; -import org.opendaylight.controller.yang.binding.Notification; -import org.opendaylight.controller.yang.binding.NotificationListener; -import org.opendaylight.controller.yang.common.QName; -import org.opendaylight.controller.yang.data.api.CompositeNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class NotificationModule implements SALBindingModule { - - private ProviderSession biSession; - private org.opendaylight.controller.sal.core.api.notify.NotificationProviderService biNotifyService; - private BindingAwareBroker broker; - private MappingProvider mappingProvider; - - private Map, List> listeners = new HashMap, List>(); - private Set biNotifications = new HashSet(); - private static final Logger log = LoggerFactory - .getLogger(NotificationModule.class); - private final BindingIndependentListener biListener = new BindingIndependentListener(); - - @Override - public Set> getProvidedServices() { - - Set> ret = new HashSet>(); - ret.add(NotificationService.class); - ret.add(NotificationProviderService.class); - return ret; - } - - @Override - public T getServiceForSession( - Class service, ConsumerSession session) { - if (service == null) - throw new IllegalArgumentException("Service should not be null"); - if (session == null) - throw new IllegalArgumentException("Session should not be null"); - - if (NotificationProviderSession.class.equals(service)) { - if (session instanceof org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderSession) { - @SuppressWarnings("unchecked") - T ret = (T) new NotificationProviderSession(session); - return ret; - } else { - throw new IllegalArgumentException( - "NotificationProviderService is available only to ProviderSession"); - } - } - - if (NotificationService.class.equals(service)) { - @SuppressWarnings("unchecked") - T ret = (T) new NotificationSession(session); - return ret; - } - return null; - } - - @Override - public Set> getSupportedProviderFunctionality() { - return Collections.emptySet(); - } - - @Override - public void setBroker(BindingAwareBroker broker) { - this.broker = broker; - } - - @Override - public void setMappingProvider(MappingProvider provider) { - this.mappingProvider = provider; - } - - @Override - public void onBISessionAvailable(ProviderSession session) { - biSession = session; - if (biSession != null) { - biNotifyService = session - .getService(org.opendaylight.controller.sal.core.api.notify.NotificationProviderService.class); - } - } - - private void notify(Notification notification) { - notifyBindingIndependent(notification); - notifyBindingAware(notification); - } - - private void notifyBindingAware(Notification notification) { - Class type = notification.getClass(); - List toNotify = listeners.get(type); - - // Invocation of notification on registered listeners - if (toNotify != null) { - - // We get factory for Notification Invoker - MappingExtensionFactory invokerFactory = mappingProvider - .getExtensionFactory(NotificationInvoker.class); - - // We get generated invoker for NoficiationListener interface - // associated to Notification Type - NotificationInvoker invoker = invokerFactory.forClass(type); - for (NotificationListener listener : toNotify) { - try { - // Invoker invokes the right method on subtype of - // NotificationListener - // associated to the type of notification - invoker.notify(notification, listener); - } catch (Exception e) { - - } - } - } - } - - private void notifyBindingIndependent(Notification notification) { - Class type = notification.getClass(); - - if (biSession == null) { - return; - } - if (biSession.isClosed()) { - return; - } - if (biNotifyService == null) { - return; - } - - // FIXME: Somehow we need to resolve this for class hierarchy. - // probably use type.getInterfaces() - Mapper mapper = mappingProvider.getMapper(type); - CompositeNode domNotification = mapper.domFromObject(notification); - - biNotifyService.sendNotification(domNotification); - } - - private void addBAListener(Class notificationType, - NotificationListener listener) { - - BrokerUtils.addToMap(listeners, notificationType, listener); - Mapper mapper = mappingProvider - .getMapper(notificationType); - QName biType = mapper.getQName(); - if (false == biNotifications.contains(biType)) { - // The listener is not registered for binding independent - // notification - biNotifications.add(biType); - - if (biNotifyService != null) { - biNotifyService.addNotificationListener(biType, biListener); - } - } - - } - - private void removeBAListener( - Class notificationType, - NotificationListener listener) { - BrokerUtils.removeFromMap(listeners, notificationType, listener); - } - - private class NotificationSession implements NotificationService { - private final ConsumerSession session; - - public NotificationSession(ConsumerSession session) { - this.session = session; - } - - private Map, List> sessionListeners = new HashMap, List>(); - - @Override - public void addNotificationListener( - Class notificationType, - NotificationListener listener) { - - NotificationModule.this.addBAListener(notificationType, listener); - BrokerUtils.addToMap(sessionListeners, notificationType, listener); - - } - - @Override - public void removeNotificationListener( - Class notificationType, - NotificationListener listener) { - BrokerUtils.removeFromMap(sessionListeners, notificationType, - listener); - NotificationModule.this - .removeBAListener(notificationType, listener); - } - - } - - private class NotificationProviderSession extends NotificationSession - implements NotificationProviderService { - - public NotificationProviderSession(ConsumerSession session) { - super(session); - } - - @Override - public void notify(Notification notification) { - NotificationModule.this.notify(notification); - } - - } - - private class BindingIndependentListener - implements - org.opendaylight.controller.sal.core.api.notify.NotificationListener { - - @Override - public Set getSupportedNotifications() { - return biNotifications; - } - - @Override - public void onNotification(CompositeNode notification) { - NotificationModule.this - .onBindingIndependentNotification(notification); - } - - } - - private void onBindingIndependentNotification(CompositeNode biNotification) { - QName biType = biNotification.getNodeType(); - - Mapper mapper = mappingProvider.getMapper(biType); - if (mapper == null) { - log.info("Received notification does not have a binding defined."); - return; - } - Class type = mapper.getDataObjectClass(); - - // We check if the received QName / type is really Notification - if (Notification.class.isAssignableFrom(type)) { - Notification notification = (Notification) mapper - .objectFromDom(biNotification); - notifyBindingAware(notification); - } else { - // The generated type for this QName does not inherits from - // notification something went wrong - generated APIs and/or - // provider sending notification - // which was incorectly described in the YANG schema. - log.error("Received notification " + biType - + " is not binded as notification"); - } - - } -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.impl; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.sal.binding.api.BindingAwareService; +import org.opendaylight.controller.sal.binding.api.NotificationProviderService; +import org.opendaylight.controller.sal.binding.api.NotificationService; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerSession; +import org.opendaylight.controller.sal.binding.spi.SALBindingModule; +import org.opendaylight.controller.sal.binding.spi.Mapper; +import org.opendaylight.controller.sal.binding.spi.MappingProvider; +import org.opendaylight.controller.sal.binding.spi.MappingProvider.MappingExtensionFactory; + +import org.opendaylight.controller.sal.core.api.Broker.ProviderSession; +import org.opendaylight.controller.yang.binding.DataObject; +import org.opendaylight.controller.yang.binding.Notification; +import org.opendaylight.controller.yang.binding.NotificationListener; +import org.opendaylight.controller.yang.common.QName; +import org.opendaylight.controller.yang.data.api.CompositeNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; + +public class NotificationModule implements SALBindingModule { + + private ProviderSession biSession; + private org.opendaylight.controller.sal.core.api.notify.NotificationProviderService biNotifyService; + private MappingProvider mappingProvider; + + private Multimap, NotificationListener> listeners = HashMultimap + .create(); + private Set biNotifications = new HashSet(); + private static final Logger log = LoggerFactory + .getLogger(NotificationModule.class); + private final BindingIndependentListener biListener = new BindingIndependentListener(); + private BindingAwareBroker broker; + + @Override + public Set> getProvidedServices() { + + Set> ret = new HashSet>(); + ret.add(NotificationService.class); + ret.add(NotificationProviderService.class); + return ret; + } + + @Override + public T getServiceForSession( + Class service, ConsumerSession session) { + if (service == null) + throw new IllegalArgumentException("Service should not be null"); + if (session == null) + throw new IllegalArgumentException("Session should not be null"); + + if (NotificationProviderSession.class.equals(service)) { + if (session instanceof org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderSession) { + @SuppressWarnings("unchecked") + T ret = (T) new NotificationProviderSession(session); + return ret; + } else { + throw new IllegalArgumentException( + "NotificationProviderService is available only to ProviderSession"); + } + } + + if (NotificationService.class.equals(service)) { + @SuppressWarnings("unchecked") + T ret = (T) new NotificationSession(session); + return ret; + } + return null; + } + + @Override + public Set> getSupportedProviderFunctionality() { + return Collections.emptySet(); + } + + @Override + public void setBroker(BindingAwareBroker broker) { + this.broker = broker; + } + + @Override + public void setMappingProvider(MappingProvider provider) { + this.mappingProvider = provider; + } + + @Override + public void onBISessionAvailable(ProviderSession session) { + biSession = session; + if (biSession != null) { + biNotifyService = session + .getService(org.opendaylight.controller.sal.core.api.notify.NotificationProviderService.class); + } + } + + private void notify(Notification notification) { + notifyBindingIndependent(notification); + notifyBindingAware(notification); + } + + private void notifyBindingAware(Notification notification) { + Class type = notification.getClass(); + Collection toNotify = listeners.get(type); + + // Invocation of notification on registered listeners + if (toNotify != null) { + + // We get factory for Notification Invoker + MappingExtensionFactory invokerFactory = mappingProvider + .getExtensionFactory(NotificationInvoker.class); + + // We get generated invoker for NoficiationListener interface + // associated to Notification Type + NotificationInvoker invoker = invokerFactory.forClass(type); + for (NotificationListener listener : toNotify) { + try { + // Invoker invokes the right method on subtype of + // NotificationListener + // associated to the type of notification + invoker.notify(notification, listener); + } catch (Exception e) { + + } + } + } + } + + private void notifyBindingIndependent(Notification notification) { + Class type = notification.getClass(); + + if (biSession == null) { + return; + } + if (biSession.isClosed()) { + return; + } + if (biNotifyService == null) { + return; + } + + // FIXME: Somehow we need to resolve this for class hierarchy. + // probably use type.getInterfaces() + Mapper mapper = mappingProvider + .mapperForClass(type); + CompositeNode domNotification = mapper.domFromObject(notification); + + biNotifyService.sendNotification(domNotification); + } + + private void addBAListener(Class notificationType, + NotificationListener listener) { + + listeners.put(notificationType, listener); + Mapper mapper = mappingProvider + .mapperForClass(notificationType); + QName biType = mapper.getQName(); + if (false == biNotifications.contains(biType)) { + // The listener is not registered for binding independent + // notification + biNotifications.add(biType); + + if (biNotifyService != null) { + biNotifyService.addNotificationListener(biType, biListener); + } + } + + } + + private void removeBAListener( + Class notificationType, + NotificationListener listener) { + listeners.remove(notificationType, listener); + } + + private class NotificationSession implements NotificationService { + private final ConsumerSession session; + private Multimap, NotificationListener> sessionListeners = HashMultimap + .create(); + + public NotificationSession(ConsumerSession session) { + this.session = session; + } + + @Override + public void addNotificationListener( + Class notificationType, + NotificationListener listener) { + + NotificationModule.this.addBAListener(notificationType, listener); + sessionListeners.put(notificationType, listener); + + } + + @Override + public void removeNotificationListener( + Class notificationType, + NotificationListener listener) { + sessionListeners.remove(notificationType, listener); + NotificationModule.this + .removeBAListener(notificationType, listener); + } + + } + + private class NotificationProviderSession extends NotificationSession + implements NotificationProviderService { + + public NotificationProviderSession(ConsumerSession session) { + super(session); + } + + @Override + public void notify(Notification notification) { + NotificationModule.this.notify(notification); + } + + } + + private class BindingIndependentListener + implements + org.opendaylight.controller.sal.core.api.notify.NotificationListener { + + @Override + public Set getSupportedNotifications() { + return biNotifications; + } + + @Override + public void onNotification(CompositeNode notification) { + NotificationModule.this + .onBindingIndependentNotification(notification); + } + + } + + private void onBindingIndependentNotification(CompositeNode biNotification) { + QName biType = biNotification.getNodeType(); + + Mapper mapper = mappingProvider.mapperForQName(biType); + if (mapper == null) { + log.info("Received notification does not have a binding defined."); + return; + } + Class type = mapper.getDataObjectClass(); + + // We check if the received QName / type is really Notification + if (Notification.class.isAssignableFrom(type)) { + Notification notification = (Notification) mapper + .objectFromDom(biNotification); + notifyBindingAware(notification); + } else { + // The generated type for this QName does not inherits from + // notification something went wrong - generated APIs and/or + // provider sending notification + // which was incorectly described in the YANG schema. + log.error("Received notification " + biType + + " is not binded as notification"); + } + + } +} diff --git a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/pom.xml b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/pom.xml index 131c00dc2a..d81c6ac2ff 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/pom.xml +++ b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/pom.xml @@ -21,7 +21,11 @@ org.slf4j slf4j-api - 1.6.4 + + + com.google.guava + guava + jar \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/BrokerImpl.java b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/BrokerImpl.java index 97879fb969..84bc056950 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/BrokerImpl.java +++ b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/BrokerImpl.java @@ -12,7 +12,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import org.opendaylight.controller.sal.core.api.Broker; diff --git a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/Utils.java b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/Utils.java deleted file mode 100644 index 810049f44e..0000000000 --- a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/Utils.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.core.impl; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.opendaylight.controller.yang.common.QName; - - -public class Utils { - - public static void addToMap(Map> map, QName key, T value) { - List list = map.get(key); - if (list == null) { - list = new ArrayList(); - map.put(key, list); - } - list.add(value); - } - - public static void removeFromMap(Map> map, QName key, - T value) { - List list = map.get(key); - if (list == null) { - return; - } - list.remove(value); - if (list.isEmpty()) { - map.remove(key); - } - } -} - diff --git a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/notify/NotificationModule.java b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/notify/NotificationModule.java index fd19e7e6bc..8f9632be9e 100644 --- a/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/notify/NotificationModule.java +++ b/opendaylight/sal/yang-prototype/sal/sal-broker-impl/src/main/java/org/opendaylight/controller/sal/core/impl/notify/NotificationModule.java @@ -7,10 +7,9 @@ */ package org.opendaylight.controller.sal.core.impl.notify; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -24,28 +23,30 @@ import org.opendaylight.controller.sal.core.api.notify.NotificationListener; import org.opendaylight.controller.sal.core.api.notify.NotificationProviderService; import org.opendaylight.controller.sal.core.api.notify.NotificationService; import org.opendaylight.controller.sal.core.impl.BrokerServiceImpl; -import org.opendaylight.controller.sal.core.impl.Utils; import org.opendaylight.controller.sal.core.spi.BrokerModule; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Multimap; public class NotificationModule implements BrokerModule { private static Logger log = LoggerFactory .getLogger(NotificationModule.class); - private Map> listeners = new HashMap>(); + private Multimap listeners = HashMultimap + .create(); + + private static final Set> providedServices = ImmutableSet + .of((Class) NotificationService.class, + NotificationProviderService.class); @Override public Set> getProvidedServices() { - // FIXME Refactor - Set> ret = new HashSet>(); - ret.add(NotificationService.class); - ret.add(NotificationProviderService.class); - return ret; + return providedServices; } @Override @@ -77,7 +78,7 @@ public class NotificationModule implements BrokerModule { private void sendNotification(CompositeNode notification) { QName type = notification.getNodeType(); - List toNotify = listeners.get(type); + Collection toNotify = listeners.get(type); log.info("Publishing notification " + type); if (toNotify == null) { @@ -109,7 +110,8 @@ public class NotificationModule implements BrokerModule { private class NotificationConsumerSessionImpl extends BrokerServiceImpl implements NotificationService { - Map> consumerListeners = new HashMap>(); + private Multimap consumerListeners = HashMultimap + .create(); private boolean closed = false; @Override @@ -124,8 +126,8 @@ public class NotificationModule implements BrokerModule { throw new IllegalArgumentException("Listener must not be null."); } - Utils.addToMap(consumerListeners, notification, listener); - Utils.addToMap(listeners, notification, listener); + consumerListeners.put(notification, listener); + listeners.put(notification, listener); log.info("Registered listener for notification: " + notification); } @@ -140,17 +142,18 @@ public class NotificationModule implements BrokerModule { if (listener == null) { throw new IllegalArgumentException("Listener must not be null."); } - Utils.removeFromMap(consumerListeners, notification, listener); - Utils.removeFromMap(listeners, notification, listener); + consumerListeners.remove(notification, listener); + listeners.remove(notification, listener); } @Override public void closeSession() { closed = true; - Set>> toRemove = consumerListeners - .entrySet(); - for (Entry> entry : toRemove) { - listeners.get(entry.getKey()).removeAll(entry.getValue()); + Map> toRemove = consumerListeners + .asMap(); + for (Entry> entry : toRemove + .entrySet()) { + listeners.remove(entry.getKey(), entry.getValue()); } } @@ -179,4 +182,3 @@ public class NotificationModule implements BrokerModule { return Collections.emptySet(); } } - -- 2.36.6