1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/Configuration.java Sat Feb 28 21:31:02 2009 +0100
1.3 @@ -0,0 +1,169 @@
1.4 +/* ================================================================
1.5 + * Cewolf : Chart enabling Web Objects Framework
1.6 + * ================================================================
1.7 + *
1.8 + * Project Info: http://cewolf.sourceforge.net
1.9 + * Project Lead: Guido Laures (guido@laures.de);
1.10 + *
1.11 + * (C) Copyright 2002, by Guido Laures
1.12 + *
1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
1.15 + * either version 2.1 of the License, or (at your option) any later version.
1.16 + *
1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1.19 + * See the GNU Lesser General Public License for more details.
1.20 + *
1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.23 + * Boston, MA 02111-1307, USA.
1.24 + */
1.25 +
1.26 +package de.laures.cewolf;
1.27 +
1.28 +import java.util.Enumeration;
1.29 +import java.util.HashMap;
1.30 +import java.util.Map;
1.31 +
1.32 +import javax.servlet.ServletConfig;
1.33 +import javax.servlet.ServletContext;
1.34 +
1.35 +/**
1.36 + * This class represents the configuration of the Cewolf framework.
1.37 + * It is designed as singleton and resists in application context.
1.38 + * @author glaures
1.39 + * @since 0.8
1.40 + */
1.41 +public class Configuration {
1.42 +
1.43 + public static final String KEY = Configuration.class.getName();
1.44 + private static final String DEFAULT_OVERLIB_URL = "overlib.js";
1.45 + private static final String DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage";
1.46 +
1.47 + private String overlibURL = DEFAULT_OVERLIB_URL;
1.48 + private boolean debugged = false;
1.49 +
1.50 + private String storageClassName = DEFAULT_STORAGE;
1.51 + private Storage storage = null;
1.52 + private Map parameters = new HashMap();
1.53 +
1.54 + /** package protected constructor triggered by servlet */
1.55 + protected Configuration(ServletContext ctx) {
1.56 + ctx.log("configuring cewolf app..");
1.57 + ctx.setAttribute(KEY, this);
1.58 +
1.59 + //retrieve the init config params
1.60 + ServletConfig config = (ServletConfig) ctx.getAttribute(CewolfRenderer.INIT_CONFIG);
1.61 + if (config != null)
1.62 + {
1.63 + Enumeration initParams = config.getInitParameterNames();
1.64 + try {
1.65 + while (initParams.hasMoreElements()) {
1.66 + String param = (String) initParams.nextElement();
1.67 + String value = config.getInitParameter(param);
1.68 + if ("debug".equalsIgnoreCase(param)) {
1.69 + debugged = Boolean.valueOf(value).booleanValue();
1.70 + } else if ("overliburl".equalsIgnoreCase(param)) {
1.71 + overlibURL = value;
1.72 + } else if ("storage".equalsIgnoreCase(param)) {
1.73 + storageClassName = value;
1.74 + } else {
1.75 + ctx.log(param + " parameter is ignored.");
1.76 + }
1.77 + parameters.put(param,value);
1.78 + }
1.79 + } catch (Throwable t) {
1.80 + ctx.log("Error in Cewolf config.", t);
1.81 + }
1.82 + }
1.83 + else {
1.84 + ctx.log("Cewolf Misconfiguration. You should add a <load-on-startup> tag "
1.85 + + "to your web.xml for the Cewolf rendering servlet.\n"
1.86 + + "A default Configuration will be used if not.");
1.87 + }
1.88 +
1.89 + try {
1.90 + initStorage(ctx);
1.91 + } catch (CewolfException ex) {
1.92 + ctx.log("exception during storage init from class " + storageClassName);
1.93 + ctx.log("using " + DEFAULT_STORAGE);
1.94 + storageClassName = DEFAULT_STORAGE;
1.95 + try {
1.96 + initStorage(ctx);
1.97 + } catch(CewolfException cwex){
1.98 + cwex.printStackTrace();
1.99 + throw new RuntimeException(storageClassName + ".init() threw exception.");
1.100 + }
1.101 + }
1.102 + ctx.log("using storage class " + storageClassName);
1.103 + ctx.log("using overlibURL " + overlibURL);
1.104 + ctx.log("debugging is turned " + (debugged ? "on" : "off"));
1.105 + ctx.log("...done.");
1.106 + }
1.107 +
1.108 + private void initStorage(ServletContext ctx) throws CewolfException {
1.109 + try {
1.110 + storage = (Storage)Class.forName(storageClassName).newInstance();
1.111 + } catch(Exception ex){
1.112 + ex.printStackTrace();
1.113 + throw new CewolfException(ex.getMessage());
1.114 + }
1.115 + storage.init(ctx);
1.116 + }
1.117 +
1.118 + private Configuration() {
1.119 + }
1.120 +
1.121 + /**
1.122 + * Factory method. If no Configuration had been initialized before, a new
1.123 + * one is created, stored in ctx and returned to the caller.
1.124 + * @param ctx the servlet context from where to retrieve the Configuration
1.125 + * object.
1.126 + * @return the config object
1.127 + */
1.128 + public static Configuration getInstance(ServletContext ctx) {
1.129 + Configuration config = null;
1.130 + config = (Configuration) ctx.getAttribute(KEY);
1.131 +
1.132 + if (config == null)
1.133 + {
1.134 + ctx.log("No Configuration for this context. Initializing.");
1.135 + config = new Configuration(ctx);
1.136 + ctx.setAttribute(KEY, config);
1.137 + }
1.138 +
1.139 + return config;
1.140 + }
1.141 +
1.142 + /**
1.143 + * Checks if debugging is configured to be turned on. Configured by
1.144 + * init param <code>debug</code> in web.xml.
1.145 + * @return <code>true</code> if a debugging is on, else <code>false</false>
1.146 + */
1.147 + public boolean isDebugged() {
1.148 + return debugged;
1.149 + }
1.150 +
1.151 + /**
1.152 + * Returns the location of the overlib.js relative to webapp's root.
1.153 + * Configured by init param <code>overliburl</code> in web.xml. Defaults to
1.154 + * <code>overlib.js</code>
1.155 + * @return String
1.156 + */
1.157 + public String getOverlibURL() {
1.158 + return overlibURL;
1.159 + }
1.160 +
1.161 + public Storage getStorage() {
1.162 + return storage;
1.163 + }
1.164 +
1.165 + /**
1.166 + * Get the initialization parameters from Cewolf servlet.
1.167 + * @return The parameter map (String->String) values
1.168 + */
1.169 + public Map getParameters() {
1.170 + return parameters;
1.171 + }
1.172 +}