Merge "Split UriBuilder into a separate type"
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / AbstractRIBExtensionProviderActivator.java
1 /*
2  * Copyright (c) 2013 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.protocol.bgp.rib.spi;
9
10 import java.util.List;
11
12 import javax.annotation.concurrent.GuardedBy;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import com.google.common.base.Preconditions;
18
19 public abstract class AbstractRIBExtensionProviderActivator implements RIBExtensionProviderActivator {
20         private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBExtensionProviderActivator.class);
21
22         @GuardedBy("this")
23         private List<AutoCloseable> registrations;
24
25         @GuardedBy("this")
26         protected abstract List<AutoCloseable> startRIBExtensionProviderImpl(RIBExtensionProviderContext context);
27
28         @Override
29         public final synchronized void startRIBExtensionProvider(final RIBExtensionProviderContext context) {
30                 Preconditions.checkState(this.registrations == null);
31
32                 this.registrations = Preconditions.checkNotNull(startRIBExtensionProviderImpl(context));
33         }
34
35         @Override
36         public final synchronized void stopRIBExtensionProvider() {
37                 Preconditions.checkState(this.registrations != null);
38
39                 for (final AutoCloseable r : this.registrations) {
40                         try {
41                                 r.close();
42                         } catch (final Exception e) {
43                                 LOG.warn("Failed to close registration", e);
44                         }
45                 }
46
47                 this.registrations = null;
48         }
49 }