1 /* ================================================================
2 * Cewolf : Chart enabling Web Objects Framework
3 * ================================================================
5 * Project Info: http://cewolf.sourceforge.net
6 * Project Lead: Guido Laures (guido@laures.de);
8 * (C) Copyright 2002, by Guido Laures
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
23 package de.laures.cewolf;
25 import java.util.Enumeration;
26 import java.util.HashMap;
29 import javax.servlet.ServletConfig;
30 import javax.servlet.ServletContext;
33 * This class represents the configuration of the Cewolf framework.
34 * It is designed as singleton and resists in application context.
38 public class Configuration {
40 public static final String KEY = Configuration.class.getName();
41 private static final String DEFAULT_OVERLIB_URL = "overlib.js";
42 private static final String DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage";
44 private String overlibURL = DEFAULT_OVERLIB_URL;
45 private boolean debugged = false;
47 private String storageClassName = DEFAULT_STORAGE;
48 private Storage storage = null;
49 private Map parameters = new HashMap();
51 /** package protected constructor triggered by servlet */
52 protected Configuration(ServletContext ctx) {
53 ctx.log("configuring cewolf app..");
54 ctx.setAttribute(KEY, this);
56 //retrieve the init config params
57 ServletConfig config = (ServletConfig) ctx.getAttribute(CewolfRenderer.INIT_CONFIG);
60 Enumeration initParams = config.getInitParameterNames();
62 while (initParams.hasMoreElements()) {
63 String param = (String) initParams.nextElement();
64 String value = config.getInitParameter(param);
65 if ("debug".equalsIgnoreCase(param)) {
66 debugged = Boolean.valueOf(value).booleanValue();
67 } else if ("overliburl".equalsIgnoreCase(param)) {
69 } else if ("storage".equalsIgnoreCase(param)) {
70 storageClassName = value;
72 ctx.log(param + " parameter is ignored.");
74 parameters.put(param,value);
76 } catch (Throwable t) {
77 ctx.log("Error in Cewolf config.", t);
81 ctx.log("Cewolf Misconfiguration. You should add a <load-on-startup> tag "
82 + "to your web.xml for the Cewolf rendering servlet.\n"
83 + "A default Configuration will be used if not.");
88 } catch (CewolfException ex) {
89 ctx.log("exception during storage init from class " + storageClassName);
90 ctx.log("using " + DEFAULT_STORAGE);
91 storageClassName = DEFAULT_STORAGE;
94 } catch(CewolfException cwex){
95 cwex.printStackTrace();
96 throw new RuntimeException(storageClassName + ".init() threw exception.");
99 ctx.log("using storage class " + storageClassName);
100 ctx.log("using overlibURL " + overlibURL);
101 ctx.log("debugging is turned " + (debugged ? "on" : "off"));
105 private void initStorage(ServletContext ctx) throws CewolfException {
107 storage = (Storage)Class.forName(storageClassName).newInstance();
108 } catch(Exception ex){
109 ex.printStackTrace();
110 throw new CewolfException(ex.getMessage());
115 private Configuration() {
119 * Factory method. If no Configuration had been initialized before, a new
120 * one is created, stored in ctx and returned to the caller.
121 * @param ctx the servlet context from where to retrieve the Configuration
123 * @return the config object
125 public static Configuration getInstance(ServletContext ctx) {
126 Configuration config = null;
127 config = (Configuration) ctx.getAttribute(KEY);
131 ctx.log("No Configuration for this context. Initializing.");
132 config = new Configuration(ctx);
133 ctx.setAttribute(KEY, config);
140 * Checks if debugging is configured to be turned on. Configured by
141 * init param <code>debug</code> in web.xml.
142 * @return <code>true</code> if a debugging is on, else <code>false</false>
144 public boolean isDebugged() {
149 * Returns the location of the overlib.js relative to webapp's root.
150 * Configured by init param <code>overliburl</code> in web.xml. Defaults to
151 * <code>overlib.js</code>
154 public String getOverlibURL() {
158 public Storage getStorage() {
163 * Get the initialization parameters from Cewolf servlet.
164 * @return The parameter map (String->String) values
166 public Map getParameters() {