ServletContextHooks.java [src/csip] Revision: 2b1730e094311b21acd0b371e2d57f6f57cffa22 Date: Fri Apr 08 21:46:16 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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("Applied 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");
String s = e.getServletContext().getRealPath("/WEB-INF");
if (s != null) {
File file = new File(s).getParentFile();
if (file != null && file.exists()) {
File c = new File(file.getParentFile(), file.getName() + ".json");
if (c.exists()) {
try {
FileInputStream is = new FileInputStream(c);
ControlService.updateConfig(IOUtils.toString(is));
l.info("Applied config '" + c + "'");
} catch (FileNotFoundException ex) {
// ignore
} catch (JSONException | IOException ex) {
ex.printStackTrace(System.err);
}
}
}
}
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());
}
}