Revert ModuleInfoBundleTracker patches
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BundleContextBackedModuleFactoriesResolver.java
index e9af46819f9147323acaff3bd9ddd14cda8a756e..e8639d588197c146483b434fd83b4fb653a32c49 100644 (file)
@@ -8,12 +8,14 @@
 package org.opendaylight.controller.config.manager.impl.osgi;
 
 import java.util.AbstractMap;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Map.Entry;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
 import org.opendaylight.controller.config.spi.ModuleFactory;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -24,27 +26,42 @@ public class BundleContextBackedModuleFactoriesResolver implements
         ModuleFactoriesResolver {
     private static final Logger LOG = LoggerFactory
             .getLogger(BundleContextBackedModuleFactoriesResolver.class);
-    private ModuleFactoryBundleTracker moduleFactoryBundleTracker;
+    private final BundleContext bundleContext;
 
-    public BundleContextBackedModuleFactoriesResolver() {
-    }
-
-    public void setModuleFactoryBundleTracker(ModuleFactoryBundleTracker moduleFactoryBundleTracker) {
-        this.moduleFactoryBundleTracker = moduleFactoryBundleTracker;
+    public BundleContextBackedModuleFactoriesResolver(
+            BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
     }
 
     @Override
     public Map<String, Map.Entry<ModuleFactory, BundleContext>> getAllFactories() {
-        Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>();
-        for(Entry<ModuleFactory, BundleContext> entry: moduleFactoryBundleTracker.getModuleFactoryEntries()) {
-            ModuleFactory factory = entry.getKey();
-            BundleContext bundleContext = entry.getValue();
-            String moduleName = factory .getImplementationName();
-            if (moduleName == null || moduleName.isEmpty()) {
-                throw new IllegalStateException("Invalid implementation name for " + factory);
+        Collection<ServiceReference<ModuleFactory>> serviceReferences;
+        try {
+            serviceReferences = bundleContext.getServiceReferences(
+                    ModuleFactory.class, null);
+        } catch (InvalidSyntaxException e) {
+            throw new IllegalStateException(e);
+        }
+        Map<String, Map.Entry<ModuleFactory, BundleContext>> result = new HashMap<>(serviceReferences.size());
+        for (ServiceReference<ModuleFactory> serviceReference : serviceReferences) {
+            ModuleFactory factory = bundleContext.getService(serviceReference);
+            // null if the service is not registered, the service object
+            // returned by a ServiceFactory does not
+            // implement the classes under which it was registered or the
+            // ServiceFactory threw an exception.
+            if(factory == null) {
+                throw new NullPointerException("ServiceReference of class" + serviceReference.getClass() + "not found.");
             }
 
-            LOG.debug("Processing factory {} {}", moduleName, factory);
+            String moduleName = factory.getImplementationName();
+            if (moduleName == null || moduleName.isEmpty()) {
+                throw new IllegalStateException(
+                        "Invalid implementation name for " + factory);
+            }
+            if (serviceReference.getBundle() == null || serviceReference.getBundle().getBundleContext() == null) {
+                throw new NullPointerException("Bundle context of " + factory + " ModuleFactory not found.");
+            }
+            LOG.debug("Reading factory {} {}", moduleName, factory);
 
             Map.Entry<ModuleFactory, BundleContext> conflicting = result.get(moduleName);
             if (conflicting != null) {
@@ -54,10 +71,10 @@ public class BundleContextBackedModuleFactoriesResolver implements
                 LOG.error(error);
                 throw new IllegalArgumentException(error);
             } else {
-                result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory, bundleContext));
+                result.put(moduleName, new AbstractMap.SimpleImmutableEntry<>(factory,
+                        serviceReference.getBundle().getBundleContext()));
             }
         }
-
         return result;
     }
 }