/* * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry; import static java.util.Objects.requireNonNull; import com.google.common.base.Preconditions; import java.util.List; import javax.annotation.concurrent.GuardedBy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class AbstractBGPStatementProviderActivator implements StatementProviderActivator, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(AbstractBGPStatementProviderActivator.class); @GuardedBy("this") private List registrations; @GuardedBy("this") protected abstract List startImpl(StatementRegistryProvider context); @Override public final synchronized void start(final StatementRegistryProvider context) { Preconditions.checkState(this.registrations == null); this.registrations = requireNonNull(startImpl(context)); } @Override @SuppressWarnings("checkstyle:IllegalCatch") public final synchronized void stop() { if (this.registrations == null) { return; } for (final AutoCloseable r : this.registrations) { try { r.close(); } catch (final Exception e) { LOG.warn("Failed to close registration", e); } } this.registrations = null; } @Override public final synchronized void close() { stop(); } }