Merge changes Ic12888cc,I669cc282,Iad2d6119
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / AbstractPCEPExtensionProviderActivator.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.pcep.spi.pojo;
9
10 import java.util.List;
11
12 import javax.annotation.concurrent.GuardedBy;
13
14 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderActivator;
15 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.base.Preconditions;
20
21 public abstract class AbstractPCEPExtensionProviderActivator implements PCEPExtensionProviderActivator {
22         private static final Logger LOG = LoggerFactory.getLogger(AbstractPCEPExtensionProviderActivator.class);
23
24         @GuardedBy("this")
25         private List<AutoCloseable> registrations;
26
27         @GuardedBy("this")
28         protected abstract List<AutoCloseable> startImpl(PCEPExtensionProviderContext context);
29
30         @Override
31         public final synchronized void start(final PCEPExtensionProviderContext context) {
32                 Preconditions.checkState(this.registrations == null);
33
34                 this.registrations = Preconditions.checkNotNull(startImpl(context));
35         }
36
37         @Override
38         public final synchronized void stop() {
39                 Preconditions.checkState(this.registrations != null);
40
41                 for (final AutoCloseable r : this.registrations) {
42                         try {
43                                 r.close();
44                         } catch (final Exception e) {
45                                 LOG.warn("Failed to close registration", e);
46                         }
47                 }
48
49                 this.registrations = null;
50         }
51 }