2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.infrautils.inject;
10 import java.util.concurrent.atomic.AtomicReference;
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * Support class for {@link Lifecycle}. Provides a convenient base
18 * implementation including correct thread safety, exception handling and check
19 * for accidental unnecessary re-start & stop. Subclasses must implement
20 * {@link #start()} & {@link #stop()}.
22 * @author Michael Vorburger (with guidance re. AtomicReference from Tom Pantelis)
24 public abstract class AbstractLifecycle implements Lifecycle {
25 private static final Logger LOG = LoggerFactory.getLogger(AbstractLifecycle.class);
31 private final AtomicReference<State> state = new AtomicReference<>(State.STOPPED);
33 protected abstract void start() throws Exception;
35 protected abstract void stop() throws Exception;
38 * Please implement {@link #start()} instead of overriding this (here intentionally final) method.
41 @PostConstruct // NOTE: @PostConstruct is *NOT* inherited from interface, so must be here
42 @SuppressWarnings("checkstyle:IllegalCatch")
43 public final void init() throws ModuleSetupRuntimeException {
44 if (state.compareAndSet(State.STOPPED, State.STARTED)) {
47 } catch (Exception e) {
48 throw new ModuleSetupRuntimeException(e);
51 LOG.warn("Lifecycled object already started; ignoring start()");
56 * Please implement {@link #stop()} instead of overriding this (here intentionally final) method.
59 @PreDestroy // NOTE: @PostConstruct is *NOT* inherited from interface, so must be here
60 @SuppressWarnings("checkstyle:IllegalCatch")
61 public final void destroy() throws ModuleSetupRuntimeException {
62 if (state.compareAndSet(State.STARTED, State.STOPPED)) {
65 } catch (Exception e) {
66 throw new ModuleSetupRuntimeException(e);
69 LOG.warn("Lifecycled object already stopped; ignoring stop()");
74 public boolean isRunning() {
75 return state.get() == State.STARTED;
78 protected void checkIsRunning() {
80 throw new IllegalStateException("Lifecycled object is already stopped: " + this.toString());