1 package org.opendaylight.controller.sal.dom.broker.osgi;
3 import java.util.Collections;
4 import java.util.HashSet;
7 import org.opendaylight.controller.sal.core.api.BrokerService;
8 import org.opendaylight.yangtools.concepts.Registration;
9 import org.osgi.framework.ServiceReference;
10 import static com.google.common.base.Preconditions.*;
12 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
15 private final ServiceReference<T> reference;
17 public AbstractBrokerServiceProxy(ServiceReference<T> ref, T delegate) {
18 this.delegate = checkNotNull(delegate, "Delegate should not be null.");
19 this.reference = checkNotNull(ref, "Reference should not be null.");
22 protected final T getDelegate() {
23 checkState(delegate != null, "Proxy was closed and unregistered.");
27 protected final ServiceReference<T> getReference() {
31 private Set<Registration<?>> registrations = Collections.synchronizedSet(new HashSet<Registration<?>>());
33 protected <R extends Registration<?>> R addRegistration(R registration) {
34 if (registration != null) {
35 registrations.add(registration);
40 protected void closeBeforeUnregistrations() {
44 protected void closeAfterUnregistrations() {
50 if (delegate != null) {
52 RuntimeException potentialException = new RuntimeException(
53 "Uncaught exceptions occured during unregistration");
54 boolean hasSuppressed = false;
55 for (Registration<?> registration : registrations) {
58 } catch (Exception e) {
59 potentialException.addSuppressed(e);
64 throw potentialException;