package com.atlassian.stash.internal.plugin;

import com.atlassian.plugin.PluginSystemLifecycle;
import com.atlassian.stash.internal.ApplicationConstants;
import com.atlassian.stash.internal.spring.AbstractSmartLifecycle;

/**
 * Integrates the {@code PluginSystemLifecycle} with the Spring lifecycle, starting and stopping the plugin framework
 * with the {@code ApplicationContext}.
 */
public class SpringPluginSystemLifecycle extends AbstractSmartLifecycle {

    private final PluginSystemLifecycle lifecycle;

    public SpringPluginSystemLifecycle(PluginSystemLifecycle lifecycle) {
        this.lifecycle = lifecycle;
    }

    @Override
    public int getPhase() {
        return ApplicationConstants.LIFECYCLE_PHASE_PLUGINS;
    }

    /**
     * Fully initializes the plugin framework.
     * <p>
     * While the plugin framework supports multi-pass startup, that support was primarily built to facilitate instant-on
     * for OnDemand. That means the plugin framework intentionally front-loads as much work into early startup as it can
     * and leaves as little as possible for later. That approach is diametrically opposed to what Bitbucket needs, which
     * is the ability to deploy a small number of mission-critical plugins (SAL and Soy primarily) as quickly as
     * possible.
     */
    @Override
    public void start() {
        lifecycle.init();

        super.start();
    }

    /**
     * Shutdown the plugin framework.
     */
    @Override
    public void stop() {
        lifecycle.shutdown();

        super.stop();
    }
}
