ServletContextHooks.java [src/csip] Revision: af2572bb0c10661c98f5d6a3f90ba60952b21a2a  Date: Mon Apr 10 15:11:16 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 csip.utils.Services;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONException;

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

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


    /**
     * Apply an initial configuration if present.
     *
     * @param ctx
     */
    private void applyConfig(ServletContext ctx, String file) {
        InputStream is = ctx.getResourceAsStream(file);
        if (is != null) {
            try {
                l.info("Applied bundled context config '" + file + "'");
                if (file.endsWith(".json")) {
                    ControlService.updateConfig(IOUtils.toString(is));
                } else if (file.endsWith(".properties")) {
                    Properties p = new Properties();
                    p.load(is);
                    ControlService.updateConfig(p);
                }
            } catch (IOException | JSONException ex) {
                ex.printStackTrace(System.err);
            }
            try {
                is.close();
            } catch (IOException ex) {
            }
        }
    }


    private void applyContextInitParams(ServletContext ctx) {
        Enumeration<String> params = ctx.getInitParameterNames();
        Properties p = new Properties();
        while (params.hasMoreElements()) {
            String key = (String) params.nextElement();
            String value = ctx.getInitParameter(key);
            p.setProperty(key, value);
        }
        if (!p.isEmpty()) {
            l.info("Applied servlet context parameter'" + p.toString() + "'");
            ControlService.updateConfig(p);
        }
    }


    @Override
    public void contextInitialized(ServletContextEvent e) {
        ServletContext ctx = e.getServletContext();
        Config.startup(ctx);
        applyConfig(ctx, "/WEB-INF/csip-defaults.json");
        applyConfig(ctx, "/WEB-INF/csip-defaults.properties");
        applyConfig(ctx, "/META-INF/csip-conf.json");  // deprecated

        applyContextInitParams(ctx);

        File c = Services.getContextFile(ctx, ".json");
        if (c != null) {
            try {
                ControlService.updateConfig(FileUtils.readFileToString(c));
                l.info("Applied config  '" + c + "'");
            } catch (FileNotFoundException ex) {
                // ignore
            } catch (JSONException | IOException ex) {
                ex.printStackTrace(System.err);
            }
        }
        c = Services.getContextFile(ctx, ".properties");
        if (c != null) {
            try {
                Properties p = new Properties();
                p.load(new FileInputStream(c));
                ControlService.updateConfig(p);
                l.info("Applied config  '" + c + "'");
            } catch (FileNotFoundException ex) {
                // ignore
            } catch (IOException ex) {
                ex.printStackTrace(System.err);
            }
        }
        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());
    }
}