Bug 1062 - Allow multiple service references with same name. 28/7328/1
authorTomas Olvecky <tolvecky@cisco.com>
Thu, 22 May 2014 08:23:02 +0000 (10:23 +0200)
committerTomas Olvecky <tolvecky@cisco.com>
Thu, 22 May 2014 08:23:02 +0000 (10:23 +0200)
One module should allow multiple service references with same name
as long as each reference uses different service interface.

Change-Id: I09e448f498ad9bd4fbe005789fa29037e202dd38
Signed-off-by: Tomas Olvecky <tolvecky@cisco.com>
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ConfigRegistryImpl.java
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/SearchableServiceReferenceWritableRegistry.java
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ServiceReferenceRegistryImpl.java
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/BeanToOsgiServiceManager.java

index 8f85972d050cd9d3dc0e8460c21baf3506913db8..b7cdf9475753e92f34fcbeae620a95025c9941a9 100644 (file)
@@ -359,7 +359,7 @@ public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBe
             }
 
             // register services to OSGi
-            Map<String, ServiceInterfaceAnnotation> annotationMapping = configTransactionController.getWritableRegistry().findServiceInterfaces(moduleIdentifier);
+            Map<ServiceInterfaceAnnotation, String /* service ref name */> annotationMapping = configTransactionController.getWritableRegistry().findServiceInterfaces(moduleIdentifier);
             BundleContext bc = configTransactionController.getModuleFactoryBundleContext(
                     entry.getModuleFactory().getImplementationName());
             if (osgiRegistration == null) {
index 7a41a3bfa24567cbad07e5fc1f214bebbe9ac654..4c5e391f74ebbd0147f1eedea77fc9c110999dae 100644 (file)
@@ -21,6 +21,6 @@ public interface SearchableServiceReferenceWritableRegistry extends ServiceRefer
      * @throws java.lang.IllegalArgumentException if any of service qNames is not found
      * @throws java.lang.NullPointerException     if parameter is null
      */
-    Map<String /* service ref */, ServiceInterfaceAnnotation> findServiceInterfaces(ModuleIdentifier moduleIdentifier);
+    Map<ServiceInterfaceAnnotation, String /* service ref name */> findServiceInterfaces(ModuleIdentifier moduleIdentifier);
 
 }
index ceea99415471d1ecd48a0d2cd17f469174aa792a..52bb3f5ed1df99a678e9ad44357cef6eadc4b3cf 100644 (file)
@@ -50,7 +50,7 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
     private final Map<String /* service qName */, ServiceInterfaceAnnotation> serviceQNamesToAnnotations;
     // all Service Interface qNames for sanity checking
     private final Set<String /* qName */> allQNames;
-    Map<ModuleIdentifier, Map<String /* service ref name */, ServiceInterfaceAnnotation >> modulesToServiceRef = new HashMap<>();
+    Map<ModuleIdentifier, Map<ServiceInterfaceAnnotation, String /* service ref name */>> modulesToServiceRef = new HashMap<>();
 
 
     // actual reference database
@@ -241,8 +241,8 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
     }
 
     @Override
-    public Map<String /* service ref */, ServiceInterfaceAnnotation> findServiceInterfaces(ModuleIdentifier moduleIdentifier) {
-        Map<String, ServiceInterfaceAnnotation> result = modulesToServiceRef.get(moduleIdentifier);
+    public Map<ServiceInterfaceAnnotation, String /* service ref name */> findServiceInterfaces(ModuleIdentifier moduleIdentifier) {
+        Map<ServiceInterfaceAnnotation, String /* service ref name */> result = modulesToServiceRef.get(moduleIdentifier);
         if (result == null) {
             return Collections.emptyMap();
         }
@@ -417,7 +417,7 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
         }
         // save to refNames
         refNames.put(serviceReference, moduleIdentifier);
-        Map<String, ServiceInterfaceAnnotation> refNamesToAnnotations = modulesToServiceRef.get(moduleIdentifier);
+        Map<ServiceInterfaceAnnotation, String /* service ref name */> refNamesToAnnotations = modulesToServiceRef.get(moduleIdentifier);
         if (refNamesToAnnotations == null){
             refNamesToAnnotations = new HashMap<>();
             modulesToServiceRef.put(moduleIdentifier, refNamesToAnnotations);
@@ -425,7 +425,7 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
 
         ServiceInterfaceAnnotation annotation = serviceQNamesToAnnotations.get(serviceReference.getServiceInterfaceQName());
         checkNotNull(annotation, "Possible error in code, cannot find annotation for " + serviceReference);
-        refNamesToAnnotations.put(serviceReference.getRefName(), annotation);
+        refNamesToAnnotations.put(annotation, serviceReference.getRefName());
         return result;
     }
 
index fbc7eb6cbe7925ef6da5a96ad69e8c1a38433007..b592fa3c79736d09b22f0b133bef142702783dbb 100644 (file)
@@ -37,7 +37,7 @@ public class BeanToOsgiServiceManager {
      */
     public OsgiRegistration registerToOsgi(AutoCloseable instance, ModuleIdentifier moduleIdentifier,
                                            BundleContext bundleContext,
-                                           Map<String, ServiceInterfaceAnnotation> serviceNamesToAnnotations) {
+                                           Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
         return new OsgiRegistration(instance, moduleIdentifier, bundleContext, serviceNamesToAnnotations);
     }
 
@@ -57,11 +57,11 @@ public class BeanToOsgiServiceManager {
         @GuardedBy("this")
         private final Set<ServiceRegistration<?>> serviceRegistrations;
         @GuardedBy("this")
-        private final Map<String, ServiceInterfaceAnnotation> serviceNamesToAnnotations;
+        private final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations;
 
         public OsgiRegistration(AutoCloseable instance, ModuleIdentifier moduleIdentifier,
                                 BundleContext bundleContext,
-                                Map<String, ServiceInterfaceAnnotation> serviceNamesToAnnotations) {
+                                Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
             this.instance = instance;
             this.moduleIdentifier = moduleIdentifier;
             this.serviceNamesToAnnotations = serviceNamesToAnnotations;
@@ -69,13 +69,13 @@ public class BeanToOsgiServiceManager {
         }
 
         private static Set<ServiceRegistration<?>> registerToSR(AutoCloseable instance, BundleContext bundleContext,
-                                                                Map<String, ServiceInterfaceAnnotation> serviceNamesToAnnotations) {
+                                                                Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
             Set<ServiceRegistration<?>> serviceRegistrations = new HashSet<>();
-            for (Entry<String, ServiceInterfaceAnnotation> entry : serviceNamesToAnnotations.entrySet()) {
-                Class<?> requiredInterface = entry.getValue().osgiRegistrationType();
+            for (Entry<ServiceInterfaceAnnotation, String /* service ref name */> entry : serviceNamesToAnnotations.entrySet()) {
+                Class<?> requiredInterface = entry.getKey().osgiRegistrationType();
                 checkState(requiredInterface.isInstance(instance), instance.getClass().getName() +
                         " instance should implement " + requiredInterface.getName());
-                Dictionary<String, String> propertiesForOsgi = createProps(entry.getKey());
+                Dictionary<String, String> propertiesForOsgi = createProps(entry.getValue());
                 ServiceRegistration<?> serviceRegistration = bundleContext
                         .registerService(requiredInterface.getName(), instance, propertiesForOsgi);
                 serviceRegistrations.add(serviceRegistration);
@@ -95,7 +95,7 @@ public class BeanToOsgiServiceManager {
             serviceRegistrations.clear();
         }
 
-        public synchronized void updateRegistrations(Map<String, ServiceInterfaceAnnotation> newAnnotationMapping,
+        public synchronized void updateRegistrations(Map<ServiceInterfaceAnnotation, String /* service ref name */> newAnnotationMapping,
                                                      BundleContext bundleContext, AutoCloseable newInstance) {
             boolean notEquals = this.instance != newInstance;
             notEquals |= newAnnotationMapping.equals(serviceNamesToAnnotations) == false;