Merge "Added move of branding jar to assembly"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / osgi / AbstractBrokerServiceProxy.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.dom.broker.osgi;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import org.opendaylight.controller.sal.core.api.BrokerService;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.osgi.framework.ServiceReference;
20
21 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
22
23     private T delegate;
24     private final ServiceReference<T> reference;
25
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.");
29     }
30
31     protected final T getDelegate() {
32         checkState(delegate != null, "Proxy was closed and unregistered.");
33         return delegate;
34     }
35
36     protected final ServiceReference<T> getReference() {
37         return reference;
38     }
39
40     private final Set<Registration<?>> registrations = Collections.synchronizedSet(new HashSet<Registration<?>>());
41
42     protected <R extends Registration<?>> R addRegistration(final R registration) {
43         if (registration != null) {
44             registrations.add(registration);
45         }
46         return registration;
47     }
48
49     protected void closeBeforeUnregistrations() {
50         // NOOP
51     }
52
53     protected void closeAfterUnregistrations() {
54         // NOOP
55     }
56
57     @Override
58     public void close() {
59         if (delegate != null) {
60             delegate = null;
61             RuntimeException potentialException = new RuntimeException(
62                     "Uncaught exceptions occured during unregistration");
63             boolean hasSuppressed = false;
64             for (Registration<?> registration : registrations) {
65                 try {
66                     registration.close();
67                 } catch (Exception e) {
68                     potentialException.addSuppressed(e);
69                     hasSuppressed = true;
70                 }
71             }
72             if (hasSuppressed) {
73                 throw potentialException;
74             }
75         }
76     }
77 }