ServletContextHooks.java [src/csip] Revision: d5970d7c0e30454d24a80dc6a55dd59e3bcbc398  Date: Sat Apr 15 07:53:13 MDT 2017
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2017, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import java.io.File;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Context Registration.
 *
 * @author Olaf David
 */
@WebListener
public class ServletContextHooks implements ServletContextListener {

    static Logger l = Logger.getLogger(ServletContextHooks.class.getName());


    @Override
    public void contextInitialized(ServletContextEvent e) {
        ServletContext ctx = e.getServletContext();
        Config.startup(ctx);

        ContextConfig cc = new ContextConfig();

        // Context internal settings.
        cc.load(ctx, "/WEB-INF/csip-defaults.json");
        cc.load(ctx, "/META-INF/csip-conf.json");  // deprecated

        // Servlet init params.
        cc.load(ctx);

        // Tomcat/conf folder as <context>.yaml
        cc.load(new File(System.getProperty("catalina.home") + File.separatorChar + "conf" + ctx.getContextPath() + ".yaml"));

        // webapp folder
        cc.load(ctx, ".json");
        cc.load(ctx, ".properties");
        cc.load(ctx, ".yaml");

        if (!cc.getConfig().isEmpty()) {
            ControlService.updateConfig(cc.getConfig());
        }
        
        l.info("CSIP Context Created: " + ctx.getContextPath());
    }


    @Override
    public void contextDestroyed(ServletContextEvent e) {
        Config.shutdown(e.getServletContext());
        l.info("CSIP Context Destroyed: " + e.getServletContext().getContextPath());
    }
}