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 static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
13 import java.util.Collections;
14 import java.util.HashSet;
17 import javax.annotation.Nullable;
19 import org.opendaylight.controller.sal.core.api.BrokerService;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.osgi.framework.ServiceReference;
23 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
26 private final ServiceReference<T> reference;
28 public AbstractBrokerServiceProxy(final @Nullable ServiceReference<T> ref, final T delegate) {
29 this.delegate = checkNotNull(delegate, "Delegate should not be null.");
33 protected final T getDelegate() {
34 checkState(delegate != null, "Proxy was closed and unregistered.");
38 protected final ServiceReference<T> getReference() {
42 private final Set<Registration<?>> registrations = Collections.synchronizedSet(new HashSet<Registration<?>>());
44 protected <R extends Registration<?>> R addRegistration(final R registration) {
45 if (registration != null) {
46 registrations.add(registration);
51 protected void closeBeforeUnregistrations() {
55 protected void closeAfterUnregistrations() {
61 if (delegate != null) {
63 RuntimeException potentialException = new RuntimeException(
64 "Uncaught exceptions occured during unregistration");
65 boolean hasSuppressed = false;
66 for (Registration<?> registration : registrations) {
69 } catch (Exception e) {
70 potentialException.addSuppressed(e);
75 throw potentialException;