package com.izouma.awesomeadmin.container;

import com.izouma.awesomeadmin.util.Const;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.LinkedList;
import java.util.List;

/**
 * 启动监听方法
 *
 * @author OnceMad
 */
public class StartupListener implements ServletContextListener {

    private static List<Lifecycle> startups = new LinkedList<Lifecycle>();

    static {
        //startups.add(new PhotoCheckWorker());
    }


    @Override
    public void contextInitialized(ServletContextEvent sce) {

        System.out.println("==================context Initialized=====================");

        for (Lifecycle lifecycle : startups) {
            System.out.print(lifecycle.getItemInfo() + " is start ");
            if (lifecycle.init()) {
                System.out.println(" successfully.");
            } else {
                System.out.println(" fail.");
            }
        }

        initConst();


        System.out.println("============context Initialized successfully===============");
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {

        System.out.println("==================context Destroyed=====================");

        for (Lifecycle lifecycle : startups) {
            System.out.print(lifecycle.getItemInfo() + " is close ");
            if (lifecycle.close()) {
                System.out.println(" successfully.");
            } else {
                System.out.println(" fail.");
            }
        }

    }


    private void initConst() {

        Const con = new Const();
        con.initData();
        System.out.println("============init const successfully===============");
    }


}
