X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fosgi%2FExtensibleBundleTracker.java;h=132005ac7c4a426f1e96c24399e9c4ac0157fdd2;hp=a0d9122ac6f121e2110455e9cf196103c8ddc245;hb=f43b01b81319959b1907e3e04537f5169e7f33d8;hpb=3314dddc5692bed5eb837b0739ac3bb5ed1962ab diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ExtensibleBundleTracker.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ExtensibleBundleTracker.java index a0d9122ac6..132005ac7c 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ExtensibleBundleTracker.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ExtensibleBundleTracker.java @@ -8,7 +8,7 @@ package org.opendaylight.controller.config.manager.impl.osgi; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -21,42 +21,44 @@ import org.osgi.util.tracker.BundleTrackerCustomizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** - * * Extensible bundle tracker. Takes several BundleTrackerCustomizers and * propagates bundle events to all of them. * - * Primary customizer may return tracking object, - * which will be passed to it during invocation of - * {@link BundleTrackerCustomizer#removedBundle(Bundle, BundleEvent, Future)} - * - * - * This extender modifies behaviour to not leak platform thread - * in {@link BundleTrackerCustomizer#addingBundle(Bundle, BundleEvent)} - * but deliver this event from its own single threaded executor. + *

+ * Primary customizer may return tracking object, which will be passed to it + * during invocation of + * {@link BundleTrackerCustomizer#removedBundle(Bundle, BundleEvent, Object)} * - * If bundle is removed before event for adding bundle was executed, - * that event is cancelled. If addingBundle event is currently in progress - * or was already executed, platform thread is block untill addingBundle - * finishes so bundle could be removed correctly in platform thread. + *

+ * This extender modifies behavior to not leak platform thread in + * {@link BundleTrackerCustomizer#addingBundle(Bundle, BundleEvent)} but deliver + * this event from its own single threaded executor. * + *

+ * If bundle is removed before event for adding bundle was executed, that event + * is cancelled. If addingBundle event is currently in progress or was already + * executed, platform thread is block until addingBundle finishes so bundle + * could be removed correctly in platform thread. * - * Method {@link BundleTrackerCustomizer#removedBundle(Bundle, BundleEvent, Object)} - * is never invoked on registered trackers. + *

+ * Method + * {@link BundleTrackerCustomizer#removedBundle(Bundle, BundleEvent, Object)} is + * never invoked on registered trackers. * - * @param + * @param value */ public final class ExtensibleBundleTracker extends BundleTracker> { private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder() - .setNameFormat("config-bundle-tracker-%d").build(); + .setNameFormat("config-bundle-tracker-%d").build(); private final ExecutorService eventExecutor; private final BundleTrackerCustomizer primaryTracker; private final BundleTrackerCustomizer[] additionalTrackers; private static final Logger LOG = LoggerFactory.getLogger(ExtensibleBundleTracker.class); - public ExtensibleBundleTracker(final BundleContext context, final BundleTrackerCustomizer primaryBundleTrackerCustomizer, + public ExtensibleBundleTracker(final BundleContext context, + final BundleTrackerCustomizer primaryBundleTrackerCustomizer, final BundleTrackerCustomizer... additionalBundleTrackerCustomizers) { this(context, Bundle.ACTIVE, primaryBundleTrackerCustomizer, additionalBundleTrackerCustomizers); } @@ -73,55 +75,35 @@ public final class ExtensibleBundleTracker extends BundleTracker> { @Override public Future addingBundle(final Bundle bundle, final BundleEvent event) { - LOG.trace("Submiting AddingBundle for bundle {} and event {} to be processed asynchronously",bundle,event); - Future future = eventExecutor.submit(new Callable() { - @Override - public T call() throws Exception { - try { - T primaryTrackerRetVal = primaryTracker.addingBundle(bundle, event); + LOG.trace("Submiting AddingBundle for bundle {} and event {} to be processed asynchronously", bundle, event); + return eventExecutor.submit(() -> { + T primaryTrackerRetVal = primaryTracker.addingBundle(bundle, event); - forEachAdditionalBundle(new BundleStrategy() { - @Override - public void execute(final BundleTrackerCustomizer tracker) { - tracker.addingBundle(bundle, event); - } - }); - LOG.trace("AddingBundle for {} and event {} finished successfully",bundle,event); - return primaryTrackerRetVal; - } catch (Exception e) { - LOG.error("Failed to add bundle {}", bundle, e); - throw e; - } - } + forEachAdditionalBundle(tracker -> tracker.addingBundle(bundle, event)); + LOG.trace("AddingBundle for {} and event {} finished successfully", bundle, event); + return primaryTrackerRetVal; }); - return future; } @Override public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Future object) { // Intentionally NOOP - } @Override public void removedBundle(final Bundle bundle, final BundleEvent event, final Future object) { - if(!object.isDone() && object.cancel(false)) { + if (!object.isDone() && object.cancel(false)) { // We canceled adding event before it was processed // so it is safe to return - LOG.trace("Adding Bundle event for {} was cancelled. No additional work required.",bundle); + LOG.trace("Adding Bundle event for {} was cancelled. No additional work required.", bundle); return; } try { - LOG.trace("Invoking removedBundle event for {}",bundle); + LOG.trace("Invoking removedBundle event for {}", bundle); primaryTracker.removedBundle(bundle, event, object.get()); - forEachAdditionalBundle(new BundleStrategy() { - @Override - public void execute(final BundleTrackerCustomizer tracker) { - tracker.removedBundle(bundle, event, null); - } - }); - LOG.trace("Removed bundle event for {} finished successfully.",bundle); - } catch (Exception e) { + forEachAdditionalBundle(tracker -> tracker.removedBundle(bundle, event, null)); + LOG.trace("Removed bundle event for {} finished successfully.", bundle); + } catch (final ExecutionException | InterruptedException e) { LOG.error("Failed to remove bundle {}", bundle, e); } } @@ -135,5 +117,4 @@ public final class ExtensibleBundleTracker extends BundleTracker> { private interface BundleStrategy { void execute(BundleTrackerCustomizer tracker); } - }