From: Robert Varga Date: Tue, 20 Aug 2019 20:19:57 +0000 (+0200) Subject: Refactor AbstractBrokerAwareActivator X-Git-Tag: release/magnesium~128 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=1c992d01fb7be6dfb310ac82839a8f1876d890de Refactor AbstractBrokerAwareActivator This simplifies the implementation by encapsulating state. Change-Id: I673e71f431026302974057af5933dba14e4c19c3 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBrokerAwareActivator.java b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBrokerAwareActivator.java index 4fc1a517c5..1bfd67671a 100644 --- a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBrokerAwareActivator.java +++ b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/AbstractBrokerAwareActivator.java @@ -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 { + private final BundleContext context; - private static final ExecutorService MD_ACTIVATION_POOL = Executors.newCachedThreadPool(); - private BundleContext context; - private ServiceTracker tracker; - private BindingAwareBroker broker; - private final ServiceTrackerCustomizer customizer = - new ServiceTrackerCustomizer() { + Customizer(final BundleContext context) { + this.context = requireNonNull(context); + } @Override - public BindingAwareBroker addingService(ServiceReference reference) { - broker = context.getService(reference); + public BindingAwareBroker addingService(final ServiceReference reference) { + final BindingAwareBroker broker = context.getService(reference); MD_ACTIVATION_POOL.execute(() -> onBrokerAvailable(broker, context)); return broker; } @Override - public void modifiedService(ServiceReference reference, BindingAwareBroker service) { + public void modifiedService(final ServiceReference reference, + final BindingAwareBroker service) { removedService(reference, service); addingService(reference); } @Override - public void removedService(ServiceReference reference, BindingAwareBroker service) { - broker = context.getService(reference); + public void removedService(final ServiceReference 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 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); } }