KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > ConfigManager


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5  
6 /******************************** Package */
7 package com.raptus.owxv3;
8
9 /******************************** Imports */
10 import java.util.Hashtable JavaDoc;
11
12 /******************************** Raptus-Header */
13 /**
14  * This singleton is started up by the startup-servlet. The servlet passes a
15  * hashtable with servlet-params specified. This servlet-params are used
16  * for configuring and setting up the correct manager/handler class.
17  *
18  * Configuration-keys begin with PARAM_PREFIX. This class needs at least the
19  * param HANDLER_KEY.
20  *
21  * HANDLER_KEY is the manager to instantiate.
22  *
23  * With the help of this encapsulation, configurationdata can be read and written
24  * without dependencies to the storage used.
25  *
26  * @see com.raptus.owxv3.ConfigManagerIFace ConfigManagerIFace
27  *
28  *
29  * <hr>
30  * <table width="100%" border="0">
31  * <tr>
32  * <td width="24%"><b>Filename</b></td><td width="76%">ConfigManager.java</td>
33  * </tr>
34  * <tr>
35  * <td width="24%"><b>Author</b></td><td width="76%">Pascal Mainini (pmainini@raptus.com)</td>
36  * </tr>
37  * <tr>
38  * <td width="24%"><b>Date</b></td><td width="76%">27th of March 2001</td>
39  * </tr>
40  * </table>
41  * <hr>
42  * <table width="100%" border="0">
43  * <tr>
44  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
45  * </tr>
46  * <tr>
47  * <td width="24%">2001-03-27/pm</td><td width="76%">created</td>
48  * </tr>
49  * </table>
50  * <hr>
51  */

52 public class ConfigManager extends Object JavaDoc
53 {
54
55 /******************************** Constants */
56     /**
57      * This constant specifies the prefix which identifies a key in the
58      * servlet-params-hash belonging to ConfigManager-initial-config.
59      *
60      * <b>Change this only if you know exactly why!</b>
61      */

62     public static final String JavaDoc PARAM_PREFIX = "ConfigManager.";
63
64     /**
65      * This key is used when retrieving the classname of the correct
66      * config-manager.
67      *
68      * <b>hange this only if you know exactly why!</b>
69      */

70     protected static final String JavaDoc HANDLER_KEY = "HandlerClass";
71
72
73 /******************************** Members */
74     /**
75      * A singleton instance (autmatically created)
76      */

77     protected static ConfigManager _instance = null;
78
79     /**
80      * OWX-Config which is returned as access to configuration.
81      */

82     protected Configuration myConfiguration = null;
83
84     /**
85      * This hashtable stores all information needed to configure
86      * this class and it's instantiated managers. The startup-servlet
87      * needs to pass it to the constructor.
88      */

89     protected Hashtable JavaDoc myServletParams = null;
90
91     /**
92      * Instance of the handler used to access config. Is specified
93      * with the hashkey HANDLER_KEY.
94      */

95     protected ConfigManagerIFace myConfigHandler = null;
96
97     /**
98      * This points to the LoggingManager-singleton used for logging.
99      */

100     protected LoggingManager myLogger = null;
101
102
103 /******************************** Constructors */
104     /**
105      * This constructor is only called from getInstance(Hashtable params),
106      * it sets up the servlet-params-hashtable.
107      *
108      * @param params All servletparams needed, in a hashtable
109      */

110     private ConfigManager(Hashtable JavaDoc params)
111     {
112         this.myServletParams = params;
113     }
114
115     public ConfigManager()
116     {
117         this.myServletParams = new Hashtable JavaDoc();
118     }
119
120 /******************************** Methods */
121     /**
122      * the singleton mechanism
123      *
124      * @return the only instance of this class, or null if it hasn't been instantiated yet.
125      */

126     public static ConfigManager getInstance()
127     {
128         return _instance;
129     }
130
131     /**
132      * this is a special adapted version of the singleton-mechanism only used
133      * by the startup-servlet. If there's actually no instance of this class,
134      * then it's constructed with the params-hashtable.
135      *
136      * For the very first time, the class <b>MUSST</b> be instantiated this way,
137      * because each call of getInstance() before calling this will end up in returning
138      * null.
139      *
140      * @param params The hashtable containing the servlet-params.
141      * @return the only instance of this class, or null if initializing failed.
142      */

143     public static ConfigManager getInstance(Hashtable JavaDoc params)
144     {
145         if(_instance == null)
146             _instance = new ConfigManager(params);
147
148         if(!_instance.initialize())
149             return null;
150
151         return _instance;
152     }
153     
154
155
156     /**
157      * @return initalized Configuration-object or null if something went wrong.
158      */

159     public Configuration getConfiguration()
160     {
161         Configuration ret;
162
163         try
164         {
165             ret = (Configuration) myConfiguration.clone();
166         }
167         catch(Exception JavaDoc e) { return null; }
168
169         return ret;
170     }
171
172     /**
173      * This method is called only by getInstance(Hashtable hash) in order to set
174      * up important stuff.
175      *
176      * @return true if all init-stuff was successful, false if not.
177      */

178     protected boolean initialize()
179     {
180         // get the logger
181
myLogger = LoggingManager.getInstance();
182         if(myLogger == null)
183             return false;
184
185         LoggingManager.log("Initializing ConfigManager...",
186                            this, LoggingManager.IMPORTANCE_INFO);
187
188         // Load the appropriate ConfigHandler depending on handlerClass.
189
String JavaDoc handlerClass = (String JavaDoc) myServletParams.get(PARAM_PREFIX + HANDLER_KEY);
190         if(handlerClass == null || handlerClass.length() == 0)
191         {
192             LoggingManager.log("No ConfigHandler specified in servletparams!",
193                          this, LoggingManager.IMPORTANCE_FATAL);
194             return false;
195         }
196         try
197         {
198             Class JavaDoc classObj = Class.forName(handlerClass);
199             if(classObj == null)
200             {
201                 LoggingManager.log("Couldn't load handlerclass!",
202                              this, LoggingManager.IMPORTANCE_FATAL);
203                 return false;
204             }
205
206             myConfigHandler = (ConfigManagerIFace) classObj.newInstance();
207                 if(myConfigHandler == null)
208                 {
209                     LoggingManager.log("Couldn't instantiate handlerclass!",
210                                  this, LoggingManager.IMPORTANCE_FATAL);
211                     return false;
212                 }
213
214         }
215         catch(Exception JavaDoc e)
216         {
217             LoggingManager.log("Exception happened while loading handlerclass: " + e.toString(),
218                          this, LoggingManager.IMPORTANCE_FATAL);
219             return false;
220         }
221
222         if(!myConfigHandler.initialize(myServletParams))
223         {
224             LoggingManager.log("Couldn't initialize handlerclass!",
225                                  this, LoggingManager.IMPORTANCE_FATAL);
226                     return false;
227         }
228         myConfiguration = myConfigHandler.getConfiguration();
229         if(myConfiguration == null)
230         {
231             LoggingManager.log("Handlerclass didn't return any configuration!",
232                                  this, LoggingManager.IMPORTANCE_FATAL);
233                     return false;
234         }
235
236         LoggingManager.log("Successfully initialised ConfigManager!",
237                      this, LoggingManager.IMPORTANCE_INFO);
238         return true;
239     }
240
241     /**
242      * This method can be used to store back an Configuration-object in the way it
243      * was loaded.
244      *
245      * @param cfg Configuration-object to save.
246      * @return true if saving was successful, false if not.
247      */

248     public boolean saveConfiguration(Configuration cfg)
249     {
250         return myConfigHandler.saveConfiguration(cfg);
251     }
252 }
253
Popular Tags