Pre-construct YangModuleInfo service name 87/56487/3
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 3 May 2017 15:49:10 +0000 (17:49 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 5 May 2017 21:23:47 +0000 (21:23 +0000)
This cleans up ModuleInfoBundleTracker instantiation as well
as making a tiny bit faster when looking up services.

Change-Id: I2bdce2fdca9cefd56192b04f74ed7c594187d425
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ConfigManagerActivator.java
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/mapping/ModuleInfoBundleTracker.java

index a5d77ea..9d86942 100644 (file)
@@ -58,15 +58,16 @@ public class ConfigManagerActivator implements BundleActivator, SynchronousBundl
             final BindingContextProvider bindingContextProvider = new BindingContextProvider();
 
             final RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
-                    moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, bindingContextProvider, context);
+                    moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext,
+                    bindingContextProvider, context);
 
-            final ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(context, moduleInfoRegistryWrapper);
+            final ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
 
             // start config registry
-            final BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
-                    context);
-            this.configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, this.configMBeanServer,
-                    bindingContextProvider);
+            final BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver =
+                    new BundleContextBackedModuleFactoriesResolver(context);
+            this.configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver,
+                this.configMBeanServer, bindingContextProvider);
 
             // track bundles containing factories
             final BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
@@ -89,11 +90,14 @@ public class ConfigManagerActivator implements BundleActivator, SynchronousBundl
                     new JMXNotifierConfigRegistry(this.configRegistry, this.configMBeanServer);
 
             // register config registry to OSGi
-            final AutoCloseable clsReg = OsgiRegistrationUtil.registerService(context, moduleInfoBackedContext, ClassLoadingStrategy.class);
-            final AutoCloseable configRegReg = OsgiRegistrationUtil.registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
+            final AutoCloseable clsReg = OsgiRegistrationUtil.registerService(context, moduleInfoBackedContext,
+                ClassLoadingStrategy.class);
+            final AutoCloseable configRegReg = OsgiRegistrationUtil.registerService(context, notifyingConfigRegistry,
+                ConfigRegistry.class);
 
             // register config registry to jmx
-            final ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(this.configMBeanServer);
+            final ConfigRegistryJMXRegistrator configRegistryJMXRegistrator =
+                    new ConfigRegistryJMXRegistrator(this.configMBeanServer);
             try {
                 configRegistryJMXRegistrator.registerToJMXNoNotifications(this.configRegistry);
             } catch (final InstanceAlreadyExistsException e) {
@@ -102,7 +106,8 @@ public class ConfigManagerActivator implements BundleActivator, SynchronousBundl
             }
 
             // register config registry to jmx
-            final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = new ConfigRegistryJMXRegistrator(this.configMBeanServer);
+            final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications =
+                    new ConfigRegistryJMXRegistrator(this.configMBeanServer);
             try {
                 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
             } catch (final InstanceAlreadyExistsException e) {
@@ -112,16 +117,18 @@ public class ConfigManagerActivator implements BundleActivator, SynchronousBundl
             }
 
             // TODO wire directly via moduleInfoBundleTracker
-            final ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
-                    blankTransactionServiceTracker);
+            final ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context,
+                    ModuleFactory.class, blankTransactionServiceTracker);
             serviceTracker.open();
 
-            final AutoCloseable configMgrReg = OsgiRegistrationUtil.registerService(context, this, ConfigSystemService.class);
+            final AutoCloseable configMgrReg = OsgiRegistrationUtil.registerService(context, this,
+                ConfigSystemService.class);
 
             final List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg,
                     OsgiRegistrationUtil.wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
                     configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications,
-                    OsgiRegistrationUtil.wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry, configMgrReg);
+                    OsgiRegistrationUtil.wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry,
+                    configMgrReg);
             this.autoCloseable = OsgiRegistrationUtil.aggregate(list);
 
             context.addBundleListener(this);
index 5774047..c659707 100644 (file)
@@ -19,7 +19,6 @@ import org.opendaylight.yangtools.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.util.tracker.BundleTracker;
 import org.osgi.util.tracker.BundleTrackerCustomizer;
@@ -35,20 +34,22 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
     private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
 
     public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
-
+    private static final String YANG_MODULE_INFO_SERVICE_PATH = MODULE_INFO_PROVIDER_PATH_PREFIX
+            + YangModelBindingProvider.class.getName();
 
     private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
+
     private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
     private boolean starting;
 
-    public ModuleInfoBundleTracker(BundleContext context, RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
+    public ModuleInfoBundleTracker(final RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
         this.moduleInfoRegistry = moduleInfoRegistry;
     }
 
-    public void open(BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
+    public void open(final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
         LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
 
-        if(bundleTracker != null) {
+        if (bundleTracker != null) {
             this.bundleTracker = bundleTracker;
             starting = true;
             bundleTracker.open();
@@ -64,16 +65,17 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
 
     @Override
     public void close() {
-        if(bundleTracker != null) {
+        if (bundleTracker != null) {
             bundleTracker.close();
+            bundleTracker = null;
         }
     }
 
     @Override
-    public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
-        URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
+    public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(final Bundle bundle, final BundleEvent event) {
+        URL resource = bundle.getEntry(YANG_MODULE_INFO_SERVICE_PATH);
         LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
-        if(resource == null) {
+        if (resource == null) {
             return Collections.emptyList();
         }
         List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
@@ -99,12 +101,14 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
     }
 
     @Override
-    public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
+    public void modifiedBundle(final Bundle bundle, final BundleEvent event,
+            final Collection<ObjectRegistration<YangModuleInfo>> object) {
     }
 
     @Override
-    public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
-        if(regs == null) {
+    public void removedBundle(final Bundle bundle, final BundleEvent event,
+            final Collection<ObjectRegistration<YangModuleInfo>> regs) {
+        if (regs == null) {
             return;
         }
 
@@ -117,15 +121,16 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
         }
     }
 
-    private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
+    private static YangModuleInfo retrieveModuleInfo(final String moduleInfoClass, final Bundle bundle) {
         String errorMessage;
         Class<?> clazz = loadClass(moduleInfoClass, bundle);
 
         if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
-            errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
+            errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz,
+                YangModelBindingProvider.class, bundle);
             throw new IllegalStateException(errorMessage);
         }
-        YangModelBindingProvider instance;
+        final YangModelBindingProvider instance;
         try {
             Object instanceObj = clazz.newInstance();
             instance = YangModelBindingProvider.class.cast(instanceObj);
@@ -145,16 +150,17 @@ public final class ModuleInfoBundleTracker implements AutoCloseable,
         }
     }
 
-    private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
+    private static Class<?> loadClass(final String moduleInfoClass, final Bundle bundle) {
         try {
             return bundle.loadClass(moduleInfoClass);
         } catch (ClassNotFoundException e) {
-            String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
+            String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass,
+                bundle, e);
             throw new IllegalStateException(errorMessage);
         }
     }
 
-    public static String logMessage(String slfMessage, Object... params) {
+    public static String logMessage(final String slfMessage, final Object... params) {
         LOG.info(slfMessage, params);
         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
         return String.format(formatMessage, params);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.