ServletContextHooks.java [src/csip] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, 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.Arrays;
import java.util.List;
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 = Config.LOG;


  @Override
  public void contextInitialized(ServletContextEvent e) {

    ServletContext ctx = e.getServletContext();
    l.info("Starting " + ctx.getContextPath() + ", CSIP core "
        + Config.getString(Config.CSIP_PLATFORM_VERSION));

    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);

    List<String> confTypes = Arrays.asList(".yaml", ".properties", ".json");

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

    // webapp folder configuration files
    confTypes.forEach(ext -> {
      cc.load(ctx, ext);
    });

    if (!cc.getConfig().isEmpty())
      ControlService.updateConfig(cc.getConfig());

    Config.startup();

    l.info("CSIP Context Created: " + ctx.getContextPath());
  }


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