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 org.opendaylight.controller.sal.core.api.BrokerService;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.osgi.framework.ServiceReference;
21 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
24 private final ServiceReference<T> reference;
26 public AbstractBrokerServiceProxy(final ServiceReference<T> ref, final T delegate) {
27 this.delegate = checkNotNull(delegate, "Delegate should not be null.");
28 this.reference = checkNotNull(ref, "Reference should not be null.");
31 protected final T getDelegate() {
32 checkState(delegate != null, "Proxy was closed and unregistered.");
36 protected final ServiceReference<T> getReference() {
40 private final Set<Registration<?>> registrations = Collections.synchronizedSet(new HashSet<Registration<?>>());
42 protected <R extends Registration<?>> R addRegistration(final R registration) {
43 if (registration != null) {
44 registrations.add(registration);
49 protected void closeBeforeUnregistrations() {
53 protected void closeAfterUnregistrations() {
59 if (delegate != null) {
61 RuntimeException potentialException = new RuntimeException(
62 "Uncaught exceptions occured during unregistration");
63 boolean hasSuppressed = false;
64 for (Registration<?> registration : registrations) {
67 } catch (Exception e) {
68 potentialException.addSuppressed(e);
73 throw potentialException;