001: /* ================================================================
002: * Cewolf : Chart enabling Web Objects Framework
003: * ================================================================
004: *
005: * Project Info: http://cewolf.sourceforge.net
006: * Project Lead: Guido Laures (guido@laures.de);
007: *
008: * (C) Copyright 2002, by Guido Laures
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package de.laures.cewolf;
024:
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import javax.servlet.ServletConfig;
030: import javax.servlet.ServletContext;
031:
032: /**
033: * This class represents the configuration of the Cewolf framework.
034: * It is designed as singleton and resists in application context.
035: * @author glaures
036: * @since 0.8
037: */
038: public class Configuration {
039:
040: public static final String KEY = Configuration.class.getName();
041: private static final String DEFAULT_OVERLIB_URL = "overlib.js";
042: private static final String DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage";
043:
044: private String overlibURL = DEFAULT_OVERLIB_URL;
045: private boolean debugged = false;
046:
047: private String storageClassName = DEFAULT_STORAGE;
048: private Storage storage = null;
049: private Map parameters = new HashMap();
050:
051: /** package protected constructor triggered by servlet */
052: protected Configuration(ServletContext ctx) {
053: ctx.log("configuring cewolf app..");
054: ctx.setAttribute(KEY, this );
055:
056: //retrieve the init config params
057: ServletConfig config = (ServletConfig) ctx
058: .getAttribute(CewolfRenderer.INIT_CONFIG);
059: if (config != null) {
060: Enumeration initParams = config.getInitParameterNames();
061: try {
062: while (initParams.hasMoreElements()) {
063: String param = (String) initParams.nextElement();
064: String value = config.getInitParameter(param);
065: if ("debug".equalsIgnoreCase(param)) {
066: debugged = Boolean.valueOf(value)
067: .booleanValue();
068: } else if ("overliburl".equalsIgnoreCase(param)) {
069: overlibURL = value;
070: } else if ("storage".equalsIgnoreCase(param)) {
071: storageClassName = value;
072: } else {
073: ctx.log(param + " parameter is ignored.");
074: }
075: parameters.put(param, value);
076: }
077: } catch (Throwable t) {
078: ctx.log("Error in Cewolf config.", t);
079: }
080: } else {
081: ctx
082: .log("Cewolf Misconfiguration. You should add a <load-on-startup> tag "
083: + "to your web.xml for the Cewolf rendering servlet.\n"
084: + "A default Configuration will be used if not.");
085: }
086:
087: try {
088: initStorage(ctx);
089: } catch (CewolfException ex) {
090: ctx.log("exception during storage init from class "
091: + storageClassName);
092: ctx.log("using " + DEFAULT_STORAGE);
093: storageClassName = DEFAULT_STORAGE;
094: try {
095: initStorage(ctx);
096: } catch (CewolfException cwex) {
097: cwex.printStackTrace();
098: throw new RuntimeException(storageClassName
099: + ".init() threw exception.");
100: }
101: }
102: ctx.log("using storage class " + storageClassName);
103: ctx.log("using overlibURL " + overlibURL);
104: ctx.log("debugging is turned " + (debugged ? "on" : "off"));
105: ctx.log("...done.");
106: }
107:
108: private void initStorage(ServletContext ctx) throws CewolfException {
109: try {
110: storage = (Storage) Class.forName(storageClassName)
111: .newInstance();
112: } catch (Exception ex) {
113: ex.printStackTrace();
114: throw new CewolfException(ex.getMessage());
115: }
116: storage.init(ctx);
117: }
118:
119: private Configuration() {
120: }
121:
122: /**
123: * Factory method. If no Configuration had been initialized before, a new
124: * one is created, stored in ctx and returned to the caller.
125: * @param ctx the servlet context from where to retrieve the Configuration
126: * object.
127: * @return the config object
128: */
129: public static Configuration getInstance(ServletContext ctx) {
130: Configuration config = null;
131: config = (Configuration) ctx.getAttribute(KEY);
132:
133: if (config == null) {
134: ctx
135: .log("No Configuration for this context. Initializing.");
136: config = new Configuration(ctx);
137: ctx.setAttribute(KEY, config);
138: }
139:
140: return config;
141: }
142:
143: /**
144: * Checks if debugging is configured to be turned on. Configured by
145: * init param <code>debug</code> in web.xml.
146: * @return <code>true</code> if a debugging is on, else <code>false</false>
147: */
148: public boolean isDebugged() {
149: return debugged;
150: }
151:
152: /**
153: * Returns the location of the overlib.js relative to webapp's root.
154: * Configured by init param <code>overliburl</code> in web.xml. Defaults to
155: * <code>overlib.js</code>
156: * @return String
157: */
158: public String getOverlibURL() {
159: return overlibURL;
160: }
161:
162: public Storage getStorage() {
163: return storage;
164: }
165:
166: /**
167: * Get the initialization parameters from Cewolf servlet.
168: * @return The parameter map (String->String) values
169: */
170: public Map getParameters() {
171: return parameters;
172: }
173: }
|