ServletContextHooks.java [src/csip] Revision: 0904889a146754fe9a6b190735191a13b36f3796 Date: Fri Apr 01 09:14:04 MDT 2016
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* 2010-2013, Olaf David and others, Colorado State University.
*
* CSIP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 2.1.
*
* CSIP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OMS. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
*/
package csip;
import java.io.IOException;
import java.io.InputStream;
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.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 config if present.
*
* @param ctx
*/
private void applyConfig(ServletContext ctx, String file) {
InputStream is = ctx.getResourceAsStream(file);
if (is != null) {
try {
l.info("Apply bundled context config '" + file + "'");
ControlService.updateConfig(IOUtils.toString(is));
} catch (IOException | JSONException ex) {
ex.printStackTrace(System.err);
}
}
}
@Override
public void contextInitialized(ServletContextEvent e) {
Config.startup(e.getServletContext());
applyConfig(e.getServletContext(), "/WEB-INF/csip-defaults.json");
applyConfig(e.getServletContext(), "/META-INF/csip-conf.json");
l.info("CSIP Context Created: " + e.getServletContext().getContextPath());
}
@Override
public void contextDestroyed(ServletContextEvent e) {
Config.shutdown(e.getServletContext());
l.info("CSIP Context Destroyed: " + e.getServletContext().getContextPath());
}
}