Merge "Cleanup RpcRoutingStrategy definition"
[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
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.ListIterator;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceRegistration;
18 import org.osgi.util.tracker.BundleTracker;
19 import org.osgi.util.tracker.ServiceTracker;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class OsgiRegistrationUtil {
24     private static final Logger LOGGER = LoggerFactory.getLogger(OsgiRegistrationUtil.class);
25
26     private OsgiRegistrationUtil() {
27     }
28
29     @SafeVarargs
30     public static <T> AutoCloseable registerService(BundleContext bundleContext, T service, Class<? super T> ... interfaces) {
31         checkNotNull(service);
32         checkNotNull(interfaces);
33         List<AutoCloseable> autoCloseableList = new ArrayList<>();
34         for (Class<? super T> ifc : interfaces) {
35             ServiceRegistration<? super T> serviceRegistration = bundleContext.registerService(ifc, service, null);
36             autoCloseableList.add(wrap(serviceRegistration));
37         }
38         return aggregate(autoCloseableList);
39     }
40
41     public static AutoCloseable wrap(final ServiceRegistration<?> reg) {
42         checkNotNull(reg);
43         return new AutoCloseable() {
44             @Override
45             public void close() throws Exception {
46                 reg.unregister();
47             }
48         };
49     }
50
51     public static AutoCloseable wrap(final BundleTracker bundleTracker) {
52         checkNotNull(bundleTracker);
53         return new AutoCloseable() {
54             @Override
55             public void close() throws Exception {
56                 bundleTracker.close();
57             }
58         };
59     }
60
61     public static AutoCloseable wrap(final ServiceTracker<?, ?> serviceTracker) {
62         checkNotNull(serviceTracker);
63         return new AutoCloseable() {
64             @Override
65             public void close() throws Exception {
66                 serviceTracker.close();
67             }
68         };
69     }
70
71     /**
72      * Close list of auto closeables in reverse order
73      */
74     public static AutoCloseable aggregate(final List<? extends AutoCloseable> list) {
75         checkNotNull(list);
76
77         return new AutoCloseable() {
78             @Override
79             public void close() throws Exception {
80                 Exception firstException = null;
81                 for (ListIterator<? extends AutoCloseable> it = list.listIterator(list.size()); it.hasPrevious();) {
82                     AutoCloseable ac = it.previous();
83                     try {
84                         ac.close();
85                     } catch (Exception e) {
86                         LOGGER.warn("Exception while closing {}", ac, e);
87                         if (firstException == null) {
88                             firstException = e;
89                         } else {
90                             firstException.addSuppressed(e);
91                         }
92                     }
93                 }
94                 if (firstException != null) {
95                     throw firstException;
96                 }
97             }
98         };
99     }
100 }