BUG-650: Split out CommitCoordinationTask
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / util / OsgiRegistrationUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.config.manager.impl.util;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.ListIterator;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.ServiceRegistration;
17 import org.osgi.util.tracker.BundleTracker;
18 import org.osgi.util.tracker.ServiceTracker;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class OsgiRegistrationUtil {
23     private static final Logger LOGGER = LoggerFactory.getLogger(OsgiRegistrationUtil.class);
24
25     private OsgiRegistrationUtil() {
26     }
27
28     @SafeVarargs
29     public static <T> AutoCloseable registerService(final BundleContext bundleContext, final T service, final Class<? super T> ... interfaces) {
30         checkNotNull(service);
31         checkNotNull(interfaces);
32         List<AutoCloseable> autoCloseableList = new ArrayList<>();
33         for (Class<? super T> ifc : interfaces) {
34             ServiceRegistration<? super T> serviceRegistration = bundleContext.registerService(ifc, service, null);
35             autoCloseableList.add(wrap(serviceRegistration));
36         }
37         return aggregate(autoCloseableList);
38     }
39
40     public static AutoCloseable wrap(final ServiceRegistration<?> reg) {
41         checkNotNull(reg);
42         return new AutoCloseable() {
43             @Override
44             public void close() throws Exception {
45                 reg.unregister();
46             }
47         };
48     }
49
50     public static AutoCloseable wrap(final BundleTracker<?> bundleTracker) {
51         checkNotNull(bundleTracker);
52         return new AutoCloseable() {
53             @Override
54             public void close() throws Exception {
55                 bundleTracker.close();
56             }
57         };
58     }
59
60     public static AutoCloseable wrap(final ServiceTracker<?, ?> serviceTracker) {
61         checkNotNull(serviceTracker);
62         return new AutoCloseable() {
63             @Override
64             public void close() throws Exception {
65                 serviceTracker.close();
66             }
67         };
68     }
69
70     /**
71      * Close list of auto closeables in reverse order
72      */
73     public static AutoCloseable aggregate(final List<? extends AutoCloseable> list) {
74         checkNotNull(list);
75
76         return new AutoCloseable() {
77             @Override
78             public void close() throws Exception {
79                 Exception firstException = null;
80                 for (ListIterator<? extends AutoCloseable> it = list.listIterator(list.size()); it.hasPrevious();) {
81                     AutoCloseable ac = it.previous();
82                     try {
83                         ac.close();
84                     } catch (Exception e) {
85                         LOGGER.warn("Exception while closing {}", ac, e);
86                         if (firstException == null) {
87                             firstException = e;
88                         } else {
89                             firstException.addSuppressed(e);
90                         }
91                     }
92                 }
93                 if (firstException != null) {
94                     throw firstException;
95                 }
96             }
97         };
98     }
99 }