2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.dom.broker.osgi;
10 import java.util.Collections;
11 import java.util.HashSet;
14 import org.opendaylight.controller.sal.core.api.BrokerService;
15 import org.opendaylight.yangtools.concepts.Registration;
16 import org.osgi.framework.ServiceReference;
17 import static com.google.common.base.Preconditions.*;
19 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
22 private final ServiceReference<T> reference;
24 public AbstractBrokerServiceProxy(ServiceReference<T> ref, T delegate) {
25 this.delegate = checkNotNull(delegate, "Delegate should not be null.");
26 this.reference = checkNotNull(ref, "Reference should not be null.");
29 protected final T getDelegate() {
30 checkState(delegate != null, "Proxy was closed and unregistered.");
34 protected final ServiceReference<T> getReference() {
38 private Set<Registration<?>> registrations = Collections.synchronizedSet(new HashSet<Registration<?>>());
40 protected <R extends Registration<?>> R addRegistration(R registration) {
41 if (registration != null) {
42 registrations.add(registration);
47 protected void closeBeforeUnregistrations() {
51 protected void closeAfterUnregistrations() {
57 if (delegate != null) {
59 RuntimeException potentialException = new RuntimeException(
60 "Uncaught exceptions occured during unregistration");
61 boolean hasSuppressed = false;
62 for (Registration<?> registration : registrations) {
65 } catch (Exception e) {
66 potentialException.addSuppressed(e);
71 throw potentialException;