teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5861: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5861: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.server; bjoern@3478: bjoern@3478: import java.io.IOException; bjoern@3478: bjoern@3478: import javax.servlet.ServletContext; bjoern@3478: import javax.servlet.ServletContextEvent; bjoern@3478: import javax.servlet.ServletContextListener; bjoern@3478: bjoern@3478: import org.apache.log4j.Logger; bjoern@3478: teichmann@5835: import org.dive4elements.river.client.server.LoggingConfigurator; teichmann@5835: import org.dive4elements.river.client.server.features.Features; teichmann@5835: import org.dive4elements.river.client.server.features.XMLFileFeatures; bjoern@3478: bjoern@3478: /** bjoern@3478: * ServletContextListenter to initalize the Features globally for bjoern@3478: * all Servlets bjoern@3478: */ bjoern@3478: public class BaseServletContextListener implements ServletContextListener { bjoern@3478: bjoern@3478: public static final String LOG4J_PROPERTIES = "FLYS_CLIENT_LOG4J_PROPERIES"; bjoern@3478: bjoern@3478: public static final Logger logger = Logger.getLogger(BaseServletContextListener.class); bjoern@3478: bjoern@3478: @Override bjoern@3478: public void contextInitialized(ServletContextEvent sce) { bjoern@3478: ServletContext sc = sce.getServletContext(); bjoern@3478: bjoern@3478: this.initLogging(sc); bjoern@3478: bjoern@3478: String filename = sc.getInitParameter("features-file"); bjoern@3478: bjoern@3478: logger.debug("Initializing ServletContext"); bjoern@3478: try { bjoern@3478: XMLFileFeatures features = new XMLFileFeatures(sc.getRealPath(filename)); bjoern@3478: sc.setAttribute(Features.CONTEXT_ATTRIBUTE, features); bjoern@3478: } catch(IOException e) { bjoern@3478: logger.error(e); bjoern@3478: } bjoern@3478: } bjoern@3478: bjoern@3478: @Override bjoern@3478: public void contextDestroyed(ServletContextEvent sce) { bjoern@3478: //DO NOTHING bjoern@3478: } bjoern@3478: bjoern@3478: bjoern@3478: private void initLogging(ServletContext sc) { bjoern@3478: String log4jProperties = System.getenv(LOG4J_PROPERTIES); bjoern@3478: bjoern@3478: if (log4jProperties == null || log4jProperties.length() == 0) { bjoern@3478: String file = sc.getInitParameter("log4j-properties"); bjoern@3478: bjoern@3478: if (file != null && file.length() > 0) { bjoern@3478: log4jProperties = sc.getRealPath(file); bjoern@3478: } bjoern@3478: } bjoern@3478: System.out.println(log4jProperties); bjoern@3478: bjoern@3478: LoggingConfigurator.init(log4jProperties); bjoern@3478: } bjoern@3478: }