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%2Futil%2FOsgiRegistrationUtil.java;h=2642e560224c4697c93461c5a91bbc9cd422572e;hp=2df28f0a154b8b7a6e37f1930b5e06d46efda675;hb=2b78ca93f44c372fd72927db6cbd65f5d8387b49;hpb=3dd9d7c6da1baaf72f05e1118c0ca47dc16e3c7b 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 2df28f0a15..2642e56022 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -21,10 +21,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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<>(); @@ -37,60 +41,40 @@ 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; } - /** - * Close list of auto closeables in reverse order - */ + @SuppressWarnings("IllegalCatch") 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 (final 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; } }; }