Refactor AbstractBrokerAwareActivator 31/83831/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 20 Aug 2019 20:19:57 +0000 (22:19 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 20 Aug 2019 22:32:37 +0000 (00:32 +0200)
This simplifies the implementation by encapsulating state.

Change-Id: I673e71f431026302974057af5933dba14e4c19c3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBrokerAwareActivator.java

index 4fc1a51..1bfd676 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.controller.sal.binding.api;
 
+import static java.util.Objects.requireNonNull;
+
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
@@ -18,49 +20,48 @@ import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 @Deprecated
 public abstract class AbstractBrokerAwareActivator implements BundleActivator {
+    private final class Customizer implements ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> {
+        private final BundleContext context;
 
-    private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool();
-    private BundleContext context;
-    private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
-    private BindingAwareBroker broker;
-    private final ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker> customizer =
-            new ServiceTrackerCustomizer<BindingAwareBroker, BindingAwareBroker>() {
+        Customizer(final BundleContext context) {
+            this.context = requireNonNull(context);
+        }
 
         @Override
-        public BindingAwareBroker addingService(ServiceReference<BindingAwareBroker> reference) {
-            broker = context.getService(reference);
+        public BindingAwareBroker addingService(final ServiceReference<BindingAwareBroker> reference) {
+            final BindingAwareBroker broker = context.getService(reference);
             MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context));
             return broker;
         }
 
         @Override
-        public void modifiedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
+        public void modifiedService(final ServiceReference<BindingAwareBroker> reference,
+                final BindingAwareBroker service) {
             removedService(reference, service);
             addingService(reference);
         }
 
         @Override
-        public void removedService(ServiceReference<BindingAwareBroker> reference, BindingAwareBroker service) {
-            broker = context.getService(reference);
+        public void removedService(final ServiceReference<BindingAwareBroker> reference,
+                final BindingAwareBroker service) {
+            final BindingAwareBroker broker = context.getService(reference);
             MD_ACTIVATION_POOL.execute(() -> onBrokerRemoved(broker, context));
         }
+    }
 
-    };
+    private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool();
 
+    private ServiceTracker<BindingAwareBroker, BindingAwareBroker> tracker;
 
     @Override
-    public final void start(BundleContext bundleContext) {
-        this.context = bundleContext;
+    public final void start(final BundleContext bundleContext) {
         startImpl(bundleContext);
-        tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, customizer);
+        tracker = new ServiceTracker<>(bundleContext, BindingAwareBroker.class, new Customizer(bundleContext));
         tracker.open();
-
     }
 
-
-
     @Override
-    public final  void stop(BundleContext bundleContext) {
+    public final void stop(final BundleContext bundleContext) {
         if (tracker != null) {
             tracker.close();
         }
@@ -85,7 +86,7 @@ public abstract class AbstractBrokerAwareActivator implements BundleActivator {
      *             listeners, unregister all services registered by this bundle,
      *             and release all services used by this bundle.
      */
-    protected void startImpl(BundleContext bundleContext) {
+    protected void startImpl(final BundleContext bundleContext) {
         // NOOP
     }
 
@@ -106,13 +107,13 @@ public abstract class AbstractBrokerAwareActivator implements BundleActivator {
      *         listeners, unregister all services registered by the bundle, and
      *         release all services used by the bundle.
      */
-    protected void stopImpl(BundleContext bundleContext) {
+    protected void stopImpl(final BundleContext bundleContext) {
         // NOOP
     }
 
     protected abstract void onBrokerAvailable(BindingAwareBroker bindingBroker, BundleContext bundleContext);
 
-    protected void onBrokerRemoved(BindingAwareBroker bindingBroker, BundleContext bundleContext) {
+    protected void onBrokerRemoved(final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
         stopImpl(bundleContext);
     }
 }

©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.