275107ed8fb062bcfaf7c766cd0701ab3f0625af
[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 javax.annotation.Nullable;
18
19 import org.opendaylight.controller.sal.core.api.BrokerService;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.osgi.framework.ServiceReference;
22
23 public abstract class AbstractBrokerServiceProxy<T extends BrokerService> implements AutoCloseable, BrokerService {
24
25     private T delegate;
26     private final ServiceReference<T> reference;
27
28     public AbstractBrokerServiceProxy(final @Nullable ServiceReference<T> ref, final T delegate) {
29         this.delegate = checkNotNull(delegate, "Delegate should not be null.");
30         this.reference = ref;
31     }
32
33     protected final T getDelegate() {
34         checkState(delegate != null, "Proxy was closed and unregistered.");
35         return delegate;
36     }
37
38     protected final ServiceReference<T> getReference() {
39         return reference;
40     }
41
42     private final Set<Registration> registrations = Collections.synchronizedSet(new HashSet<Registration>());
43
44     protected <R extends Registration> R addRegistration(final R registration) {
45         if (registration != null) {
46             registrations.add(registration);
47         }
48         return registration;
49     }
50
51     protected void closeBeforeUnregistrations() {
52         // NOOP
53     }
54
55     protected void closeAfterUnregistrations() {
56         // NOOP
57     }
58
59     @Override
60     public void close() {
61         if (delegate != null) {
62             delegate = null;
63             RuntimeException potentialException = new RuntimeException(
64                     "Uncaught exceptions occured during unregistration");
65             boolean hasSuppressed = false;
66             for (Registration registration : registrations) {
67                 try {
68                     registration.close();
69                 } catch (Exception e) {
70                     potentialException.addSuppressed(e);
71                     hasSuppressed = true;
72                 }
73             }
74             if (hasSuppressed) {
75                 throw potentialException;
76             }
77         }
78     }
79 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.