KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > laures > cewolf > Configuration


1 /* ================================================================
2  * Cewolf : Chart enabling Web Objects Framework
3  * ================================================================
4  *
5  * Project Info: http://cewolf.sourceforge.net
6  * Project Lead: Guido Laures (guido@laures.de);
7  *
8  * (C) Copyright 2002, by Guido Laures
9  *
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.
13  *
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.
17  *
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.
21  */

22
23 package de.laures.cewolf;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31
32 /**
33  * This class represents the configuration of the Cewolf framework.
34  * It is designed as singleton and resists in application context.
35  * @author glaures
36  * @since 0.8
37  */

38 public class Configuration {
39
40     public static final String JavaDoc KEY = Configuration.class.getName();
41     private static final String JavaDoc DEFAULT_OVERLIB_URL = "overlib.js";
42     private static final String JavaDoc DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage";
43
44     private String JavaDoc overlibURL = DEFAULT_OVERLIB_URL;
45     private boolean debugged = false;
46
47     private String JavaDoc storageClassName = DEFAULT_STORAGE;
48     private Storage storage = null;
49     private Map JavaDoc parameters = new HashMap JavaDoc();
50
51     /** package protected constructor triggered by servlet */
52     protected Configuration(ServletContext JavaDoc ctx) {
53         ctx.log("configuring cewolf app..");
54         ctx.setAttribute(KEY, this);
55
56         //retrieve the init config params
57
ServletConfig JavaDoc config = (ServletConfig JavaDoc) ctx.getAttribute(CewolfRenderer.INIT_CONFIG);
58         if (config != null)
59         {
60             Enumeration JavaDoc initParams = config.getInitParameterNames();
61             try {
62                 while (initParams.hasMoreElements()) {
63                     String JavaDoc param = (String JavaDoc) initParams.nextElement();
64                     String JavaDoc value = config.getInitParameter(param);
65                     if ("debug".equalsIgnoreCase(param)) {
66                         debugged = Boolean.valueOf(value).booleanValue();
67                     } else if ("overliburl".equalsIgnoreCase(param)) {
68                         overlibURL = value;
69                     } else if ("storage".equalsIgnoreCase(param)) {
70                         storageClassName = value;
71                     } else {
72                         ctx.log(param + " parameter is ignored.");
73                     }
74                     parameters.put(param,value);
75                 }
76             } catch (Throwable JavaDoc t) {
77                 ctx.log("Error in Cewolf config.", t);
78             }
79         }
80         else {
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.");
84         }
85         
86         try {
87             initStorage(ctx);
88         } catch (CewolfException ex) {
89             ctx.log("exception during storage init from class " + storageClassName);
90             ctx.log("using " + DEFAULT_STORAGE);
91             storageClassName = DEFAULT_STORAGE;
92             try {
93                 initStorage(ctx);
94             } catch(CewolfException cwex){
95                 cwex.printStackTrace();
96                 throw new RuntimeException JavaDoc(storageClassName + ".init() threw exception.");
97             }
98         }
99         ctx.log("using storage class " + storageClassName);
100         ctx.log("using overlibURL " + overlibURL);
101         ctx.log("debugging is turned " + (debugged ? "on" : "off"));
102         ctx.log("...done.");
103     }
104     
105     private void initStorage(ServletContext JavaDoc ctx) throws CewolfException {
106         try {
107             storage = (Storage)Class.forName(storageClassName).newInstance();
108         } catch(Exception JavaDoc ex){
109             ex.printStackTrace();
110             throw new CewolfException(ex.getMessage());
111         }
112         storage.init(ctx);
113     }
114
115     private Configuration() {
116     }
117
118     /**
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
122      * object.
123      * @return the config object
124      */

125     public static Configuration getInstance(ServletContext JavaDoc ctx) {
126         Configuration config = null;
127         config = (Configuration) ctx.getAttribute(KEY);
128         
129         if (config == null)
130         {
131             ctx.log("No Configuration for this context. Initializing.");
132             config = new Configuration(ctx);
133             ctx.setAttribute(KEY, config);
134         }
135
136         return config;
137     }
138         
139     /**
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>
143      */

144     public boolean isDebugged() {
145         return debugged;
146     }
147
148     /**
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>
152      * @return String
153      */

154     public String JavaDoc getOverlibURL() {
155         return overlibURL;
156     }
157
158     public Storage getStorage() {
159         return storage;
160     }
161     
162     /**
163      * Get the initialization parameters from Cewolf servlet.
164      * @return The parameter map (String->String) values
165      */

166     public Map JavaDoc getParameters() {
167         return parameters;
168     }
169 }
170
Popular Tags