X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Futil%2FOsgiRegistrationUtil.java;h=7c02019544977b8e33d4ecd08ad91d2ab6af3642;hb=refs%2Fchanges%2F73%2F46573%2F5;hp=8873596642e43a8c460a8ee18fb7ef5fef8e9733;hpb=e27d63bdcfbfb0c1078a9c3e5aabf59ae7e2315f;p=controller.git diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/OsgiRegistrationUtil.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/OsgiRegistrationUtil.java index 8873596642..7c02019544 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/OsgiRegistrationUtil.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/OsgiRegistrationUtil.java @@ -8,6 +8,10 @@ package org.opendaylight.controller.config.manager.impl.util; +import static com.google.common.base.Preconditions.checkNotNull; +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.util.tracker.BundleTracker; @@ -15,17 +19,14 @@ import org.osgi.util.tracker.ServiceTracker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; -import java.util.ListIterator; - -import static com.google.common.base.Preconditions.checkNotNull; - public class OsgiRegistrationUtil { - private static final Logger logger = LoggerFactory.getLogger(OsgiRegistrationUtil.class); + private static final Logger LOG = LoggerFactory.getLogger(OsgiRegistrationUtil.class); + + private OsgiRegistrationUtil() { + } @SafeVarargs - public static AutoCloseable registerService(BundleContext bundleContext, T service, Class ... interfaces) { + public static AutoCloseable registerService(final BundleContext bundleContext, final T service, final Class ... interfaces) { checkNotNull(service); checkNotNull(interfaces); List autoCloseableList = new ArrayList<>(); @@ -38,32 +39,17 @@ public class OsgiRegistrationUtil { public static AutoCloseable wrap(final ServiceRegistration reg) { checkNotNull(reg); - return new AutoCloseable() { - @Override - public void close() throws Exception { - reg.unregister(); - } - }; + return reg::unregister; } - public static AutoCloseable wrap(final BundleTracker bundleTracker) { + public static AutoCloseable wrap(final BundleTracker bundleTracker) { checkNotNull(bundleTracker); - return new AutoCloseable() { - @Override - public void close() throws Exception { - bundleTracker.close(); - } - }; + return bundleTracker::close; } public static AutoCloseable wrap(final ServiceTracker serviceTracker) { checkNotNull(serviceTracker); - return new AutoCloseable() { - @Override - public void close() throws Exception { - serviceTracker.close(); - } - }; + return serviceTracker::close; } /** @@ -72,26 +58,23 @@ public class OsgiRegistrationUtil { public static AutoCloseable aggregate(final List list) { checkNotNull(list); - return new AutoCloseable() { - @Override - public void close() throws Exception { - Exception firstException = null; - for (ListIterator it = list.listIterator(list.size()); it.hasPrevious();) { - AutoCloseable ac = it.previous(); - try { - ac.close(); - } catch (Exception e) { - logger.warn("Exception while closing {}", ac, e); - if (firstException == null) { - firstException = e; - } else { - firstException.addSuppressed(e); - } + return () -> { + Exception firstException = null; + for (ListIterator it = list.listIterator(list.size()); it.hasPrevious();) { + AutoCloseable ac = it.previous(); + try { + ac.close(); + } catch (Exception e) { + LOG.warn("Exception while closing {}", ac, e); + if (firstException == null) { + firstException = e; + } else { + firstException.addSuppressed(e); } } - if (firstException != null) { - throw firstException; - } + } + if (firstException != null) { + throw firstException; } }; }