package com.atlassian.bitbucket.mesh.boot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;

import java.util.concurrent.CountDownLatch;

/**
 * A {@link CommandLineRunner} which blocks on a {@link CountDownLatch} until the Spring {@code ApplicationContext}
 * is destroyed, for use when running standalone.
 */
public class StandaloneRunner implements CommandLineRunner, DisposableBean {

    private static final Logger log = LoggerFactory.getLogger(StandaloneRunner.class);

    private final CountDownLatch latch = new CountDownLatch(1);

    @Override
    public void destroy() {
        log.info("Shutting down");
        latch.countDown();
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Ready to serve");
        latch.await();
    }
}
