1 5 6 7 package com.raptus.owxv3; 8 9 10 import java.util.Hashtable ; 11 12 13 52 public class ConfigManager extends Object 53 { 54 55 56 62 public static final String PARAM_PREFIX = "ConfigManager."; 63 64 70 protected static final String HANDLER_KEY = "HandlerClass"; 71 72 73 74 77 protected static ConfigManager _instance = null; 78 79 82 protected Configuration myConfiguration = null; 83 84 89 protected Hashtable myServletParams = null; 90 91 95 protected ConfigManagerIFace myConfigHandler = null; 96 97 100 protected LoggingManager myLogger = null; 101 102 103 104 110 private ConfigManager(Hashtable params) 111 { 112 this.myServletParams = params; 113 } 114 115 public ConfigManager() 116 { 117 this.myServletParams = new Hashtable (); 118 } 119 120 121 126 public static ConfigManager getInstance() 127 { 128 return _instance; 129 } 130 131 143 public static ConfigManager getInstance(Hashtable 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 159 public Configuration getConfiguration() 160 { 161 Configuration ret; 162 163 try 164 { 165 ret = (Configuration) myConfiguration.clone(); 166 } 167 catch(Exception e) { return null; } 168 169 return ret; 170 } 171 172 178 protected boolean initialize() 179 { 180 myLogger = LoggingManager.getInstance(); 182 if(myLogger == null) 183 return false; 184 185 LoggingManager.log("Initializing ConfigManager...", 186 this, LoggingManager.IMPORTANCE_INFO); 187 188 String handlerClass = (String ) 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 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 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 248 public boolean saveConfiguration(Configuration cfg) 249 { 250 return myConfigHandler.saveConfiguration(cfg); 251 } 252 } 253 | Popular Tags |