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