franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf; franta-hg@1: franta-hg@1: import java.util.Enumeration; franta-hg@1: import java.util.HashMap; franta-hg@1: import java.util.Map; franta-hg@1: franta-hg@1: import javax.servlet.ServletConfig; franta-hg@1: import javax.servlet.ServletContext; franta-hg@1: franta-hg@1: /** franta-hg@1: * This class represents the configuration of the Cewolf framework. franta-hg@1: * It is designed as singleton and resists in application context. franta-hg@1: * @author glaures franta-hg@1: * @since 0.8 franta-hg@1: */ franta-hg@1: public class Configuration { franta-hg@1: franta-hg@1: public static final String KEY = Configuration.class.getName(); franta-hg@1: private static final String DEFAULT_OVERLIB_URL = "overlib.js"; franta-hg@1: private static final String DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage"; franta-hg@1: franta-hg@1: private String overlibURL = DEFAULT_OVERLIB_URL; franta-hg@1: private boolean debugged = false; franta-hg@1: franta-hg@1: private String storageClassName = DEFAULT_STORAGE; franta-hg@1: private Storage storage = null; franta-hg@1: private Map parameters = new HashMap(); franta-hg@1: franta-hg@1: /** package protected constructor triggered by servlet */ franta-hg@1: protected Configuration(ServletContext ctx) { franta-hg@1: ctx.log("configuring cewolf app.."); franta-hg@1: ctx.setAttribute(KEY, this); franta-hg@1: franta-hg@1: //retrieve the init config params franta-hg@1: ServletConfig config = (ServletConfig) ctx.getAttribute(CewolfRenderer.INIT_CONFIG); franta-hg@1: if (config != null) franta-hg@1: { franta-hg@1: Enumeration initParams = config.getInitParameterNames(); franta-hg@1: try { franta-hg@1: while (initParams.hasMoreElements()) { franta-hg@1: String param = (String) initParams.nextElement(); franta-hg@1: String value = config.getInitParameter(param); franta-hg@1: if ("debug".equalsIgnoreCase(param)) { franta-hg@1: debugged = Boolean.valueOf(value).booleanValue(); franta-hg@1: } else if ("overliburl".equalsIgnoreCase(param)) { franta-hg@1: overlibURL = value; franta-hg@1: } else if ("storage".equalsIgnoreCase(param)) { franta-hg@1: storageClassName = value; franta-hg@1: } else { franta-hg@1: ctx.log(param + " parameter is ignored."); franta-hg@1: } franta-hg@1: parameters.put(param,value); franta-hg@1: } franta-hg@1: } catch (Throwable t) { franta-hg@1: ctx.log("Error in Cewolf config.", t); franta-hg@1: } franta-hg@1: } franta-hg@1: else { franta-hg@1: ctx.log("Cewolf Misconfiguration. You should add a tag " franta-hg@1: + "to your web.xml for the Cewolf rendering servlet.\n" franta-hg@1: + "A default Configuration will be used if not."); franta-hg@1: } franta-hg@1: franta-hg@1: try { franta-hg@1: initStorage(ctx); franta-hg@1: } catch (CewolfException ex) { franta-hg@1: ctx.log("exception during storage init from class " + storageClassName); franta-hg@1: ctx.log("using " + DEFAULT_STORAGE); franta-hg@1: storageClassName = DEFAULT_STORAGE; franta-hg@1: try { franta-hg@1: initStorage(ctx); franta-hg@1: } catch(CewolfException cwex){ franta-hg@1: cwex.printStackTrace(); franta-hg@1: throw new RuntimeException(storageClassName + ".init() threw exception."); franta-hg@1: } franta-hg@1: } franta-hg@1: ctx.log("using storage class " + storageClassName); franta-hg@1: ctx.log("using overlibURL " + overlibURL); franta-hg@1: ctx.log("debugging is turned " + (debugged ? "on" : "off")); franta-hg@1: ctx.log("...done."); franta-hg@1: } franta-hg@1: franta-hg@1: private void initStorage(ServletContext ctx) throws CewolfException { franta-hg@1: try { franta-hg@1: storage = (Storage)Class.forName(storageClassName).newInstance(); franta-hg@1: } catch(Exception ex){ franta-hg@1: ex.printStackTrace(); franta-hg@1: throw new CewolfException(ex.getMessage()); franta-hg@1: } franta-hg@1: storage.init(ctx); franta-hg@1: } franta-hg@1: franta-hg@1: private Configuration() { franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Factory method. If no Configuration had been initialized before, a new franta-hg@1: * one is created, stored in ctx and returned to the caller. franta-hg@1: * @param ctx the servlet context from where to retrieve the Configuration franta-hg@1: * object. franta-hg@1: * @return the config object franta-hg@1: */ franta-hg@1: public static Configuration getInstance(ServletContext ctx) { franta-hg@1: Configuration config = null; franta-hg@1: config = (Configuration) ctx.getAttribute(KEY); franta-hg@1: franta-hg@1: if (config == null) franta-hg@1: { franta-hg@1: ctx.log("No Configuration for this context. Initializing."); franta-hg@1: config = new Configuration(ctx); franta-hg@1: ctx.setAttribute(KEY, config); franta-hg@1: } franta-hg@1: franta-hg@1: return config; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Checks if debugging is configured to be turned on. Configured by franta-hg@1: * init param debug in web.xml. franta-hg@1: * @return true if a debugging is on, else false franta-hg@1: */ franta-hg@1: public boolean isDebugged() { franta-hg@1: return debugged; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Returns the location of the overlib.js relative to webapp's root. franta-hg@1: * Configured by init param overliburl in web.xml. Defaults to franta-hg@1: * overlib.js franta-hg@1: * @return String franta-hg@1: */ franta-hg@1: public String getOverlibURL() { franta-hg@1: return overlibURL; franta-hg@1: } franta-hg@1: franta-hg@1: public Storage getStorage() { franta-hg@1: return storage; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Get the initialization parameters from Cewolf servlet. franta-hg@1: * @return The parameter map (String->String) values franta-hg@1: */ franta-hg@1: public Map getParameters() { franta-hg@1: return parameters; franta-hg@1: } franta-hg@1: }