1 43 package net.jforum.util.preferences; 44 45 import java.io.File ; 46 import java.io.FileInputStream ; 47 import java.io.FileOutputStream ; 48 import java.io.IOException ; 49 import java.security.InvalidParameterException ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Properties ; 54 55 import net.jforum.ConfigLoader; 56 57 import org.apache.log4j.Logger; 58 59 74 public class SystemGlobals implements VariableStore 75 { 76 private static SystemGlobals globals = new SystemGlobals(); 77 78 private String defaultConfig; 79 private String installationConfig; 80 81 private Properties defaults = new Properties (); 82 private Properties installation = new Properties (); 83 private static List additionalDefaultsList = new ArrayList (); 84 private static Properties queries = new Properties (); 85 private static Properties transientValues = new Properties (); 86 87 private VariableExpander expander = new VariableExpander(this, "${", "}");; 88 89 private static final Logger logger = Logger.getLogger(SystemGlobals.class); 90 91 private SystemGlobals() {} 92 93 99 public static void initGlobals(String appPath, String defaults) throws IOException 100 { 101 globals = new SystemGlobals(); 102 globals.buildSystem(appPath, defaults); 103 } 104 105 private void buildSystem(String appPath, String defaultConfig) throws IOException 106 { 107 if (defaultConfig == null) { 108 throw new InvalidParameterException ("defaultConfig could not be null"); 109 } 110 111 this.defaultConfig = defaultConfig; 112 this.defaults = new Properties (); 113 114 this.defaults.put(ConfigKeys.APPLICATION_PATH, appPath); 115 this.defaults.put(ConfigKeys.DEFAULT_CONFIG, defaultConfig); 116 117 SystemGlobals.loadDefaults(); 118 119 this.installation = new Properties (); 120 this.installationConfig = getVariableValue(ConfigKeys.INSTALLATION_CONFIG); 121 122 for (Iterator iter = additionalDefaultsList.iterator(); iter.hasNext(); ) { 123 loadAdditionalDefaults((String )iter.next()); 124 } 125 126 if (new File (this.installationConfig).exists()) { 127 loadAdditionalDefaults(this.installationConfig); 128 } 129 } 130 131 138 public static void setValue(String field, String value) 139 { 140 globals.installation.put(field, value); 141 globals.expander.clearCache(); 142 } 143 144 149 public static void setTransientValue(String field, String value) 150 { 151 transientValues.put(field, value); 152 } 153 154 159 public static void loadDefaults() throws IOException 160 { 161 FileInputStream input = new FileInputStream (globals.defaultConfig); 162 globals.defaults.load(input); 163 input.close(); 164 globals.expander.clearCache(); 165 } 166 167 173 public static void loadAdditionalDefaults(String file) throws IOException 174 { 175 if (!new File (file).exists()) { 176 logger.info("Cannot find file " + file + ". Will ignore it"); 177 return; 178 } 179 180 FileInputStream input = new FileInputStream (file); 181 globals.installation.load(input); 182 input.close(); 183 184 if (!additionalDefaultsList.contains(file)) { 185 additionalDefaultsList.add(file); 186 } 187 } 188 189 194 public static void saveInstallation() throws IOException 195 { 196 Properties p = new Properties (); 203 p.putAll(globals.installation); 204 FileOutputStream out = new FileOutputStream (globals.installationConfig); 205 p.store(out, "Installation specific configuration options"); 206 out.close(); 207 208 ConfigLoader.listenInstallationConfig(); 209 } 210 211 218 public static String getValue(String field) 219 { 220 return globals.getVariableValue(field); 221 } 222 223 public static String getTransientValue(String field) 224 { 225 return transientValues.getProperty(field); 226 } 227 228 235 public static int getIntValue(String field) 236 { 237 return Integer.parseInt(getValue(field)); 238 } 239 240 247 public static boolean getBoolValue(String field) 248 { 249 return "true".equals(getValue(field)); 250 } 251 252 259 260 public String getVariableValue(String field) 261 { 262 String preExpansion = globals.installation.getProperty(field); 263 264 if (preExpansion == null) { 265 preExpansion = this.defaults.getProperty(field); 266 267 if (preExpansion == null) { 268 return null; 269 } 270 } 271 272 return expander.expandVariables(preExpansion); 273 } 274 275 281 public static void setApplicationPath(String ap) 282 { 283 setValue(ConfigKeys.APPLICATION_PATH, ap); 284 } 285 286 292 public static String getApplicationPath() 293 { 294 return getValue(ConfigKeys.APPLICATION_PATH); 295 } 296 297 312 public static String getApplicationResourceDir() 313 { 314 return getValue(ConfigKeys.RESOURCE_DIR); 315 } 316 317 323 public static void loadQueries(String queryFile) throws IOException 324 { 325 queries.load(new FileInputStream (queryFile)); 326 } 327 328 335 public static String getSql(String sql) 336 { 337 return queries.getProperty(sql); 338 } 339 340 345 public static Iterator fetchConfigKeyIterator() 346 { 347 return globals.defaults.keySet().iterator(); 348 } 349 350 public static Properties getConfigData() 351 { 352 return new Properties (globals.defaults); 353 } 354 } | Popular Tags |