Fixed few sonar warnings.
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / ModuleInfoBundleTracker.java
index 1006513fe9bcf7efeff49074f57ac8f4256b8407..57740477a1f6c6ec643d94776e7e62ef6e961753 100644 (file)
@@ -7,12 +7,12 @@
  */
 package org.opendaylight.controller.config.manager.impl.osgi.mapping;
 
-import static java.lang.String.format;
-import com.google.common.base.Charsets;
 import com.google.common.io.Resources;
 import java.io.IOException;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
@@ -38,43 +38,48 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
 
 
     private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
-    private final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
+    private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
     private boolean starting;
 
     public ModuleInfoBundleTracker(BundleContext context, RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
         this.moduleInfoRegistry = moduleInfoRegistry;
-        bundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING |
-                Bundle.STOPPING | Bundle.ACTIVE, this);
     }
 
-    public void open() {
-        LOG.debug("ModuleInfoBundleTracker open starting");
+    public void open(BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
+        LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
 
-        starting = true;
-        bundleTracker.open();
+        if(bundleTracker != null) {
+            this.bundleTracker = bundleTracker;
+            starting = true;
+            bundleTracker.open();
 
-        starting = false;
-        moduleInfoRegistry.updateService();
+            starting = false;
+            moduleInfoRegistry.updateService();
+        } else {
+            starting = false;
+        }
 
         LOG.debug("ModuleInfoBundleTracker open complete");
     }
 
     @Override
     public void close() {
-        bundleTracker.close();
+        if(bundleTracker != null) {
+            bundleTracker.close();
+        }
     }
 
     @Override
     public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
         URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
         LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
-        if(resource==null) {
-            return null;
+        if(resource == null) {
+            return Collections.emptyList();
         }
         List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
 
         try {
-            for (String moduleInfoName : Resources.readLines(resource, Charsets.UTF_8)) {
+            for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
                 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
                 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
                 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
@@ -116,7 +121,7 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
         String errorMessage;
         Class<?> clazz = loadClass(moduleInfoClass, bundle);
 
-        if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
+        if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
             errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
             throw new IllegalStateException(errorMessage);
         }
@@ -152,6 +157,6 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
     public static String logMessage(String slfMessage, Object... params) {
         LOG.info(slfMessage, params);
         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
-        return format(formatMessage, params);
+        return String.format(formatMessage, params);
     }
 }