1 8 9 package com.sleepycat.je.dbi; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileNotFoundException ; 14 import java.io.IOException ; 15 import java.util.Enumeration ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.DbInternal; 22 import com.sleepycat.je.EnvironmentConfig; 23 import com.sleepycat.je.config.BooleanConfigParam; 24 import com.sleepycat.je.config.ConfigParam; 25 import com.sleepycat.je.config.EnvironmentParams; 26 import com.sleepycat.je.config.IntConfigParam; 27 import com.sleepycat.je.config.LongConfigParam; 28 29 45 public class DbConfigManager { 46 47 51 private static final String PROPFILE_NAME = "je.properties"; 52 53 57 private Properties props; 58 59 64 private EnvironmentConfig environmentConfig; 65 66 public DbConfigManager(EnvironmentConfig config) 67 throws DbConfigException { 68 69 environmentConfig = config; 70 if (config == null) { 71 props = new Properties (); 72 } else { 73 props = DbInternal.getProps(config); 74 } 75 } 76 77 83 public void addConfigurations(Properties additionalProps) { 84 props.putAll(additionalProps); 85 } 86 87 public EnvironmentConfig getEnvironmentConfig() { 88 return environmentConfig; 89 } 90 91 94 95 101 public synchronized String get(ConfigParam configParam) 102 throws IllegalArgumentException { 103 104 return getConfigParam(props, configParam.getName()); 105 } 106 107 113 public synchronized String get(String configParamName) 114 throws IllegalArgumentException { 115 116 return getConfigParam(props, configParamName); 117 } 118 119 126 public boolean getBoolean(BooleanConfigParam configParam) 127 throws DatabaseException { 128 129 130 String val = get(configParam); 131 return Boolean.valueOf(val).booleanValue(); 132 } 133 134 140 public int getInt(IntConfigParam configParam) 141 throws DatabaseException { 142 143 String val = get(configParam); 145 int intValue = 0; 146 if (val != null) { 147 try { 148 intValue = Integer.parseInt(val); 149 } catch (NumberFormatException e) { 150 154 assert false: e.getMessage(); 155 } 156 } 157 return intValue; 158 } 159 160 166 public long getLong(LongConfigParam configParam) 167 throws DatabaseException { 168 169 170 String val = get(configParam); 171 long longValue = 0; 172 if (val != null) { 173 try { 174 longValue = Long.parseLong(val); 175 } catch (NumberFormatException e) { 176 180 assert false : e.getMessage(); 181 } 182 } 183 return longValue; 184 } 185 186 189 190 194 public static void validateProperties(Properties props, 195 boolean forReplication, 196 String configClassName) 197 throws IllegalArgumentException { 198 199 200 Enumeration propNames = props.propertyNames(); 201 while (propNames.hasMoreElements()) { 202 String name = (String ) propNames.nextElement(); 203 204 ConfigParam param = 205 (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS.get(name); 206 if (param == null) { 207 throw new IllegalArgumentException 208 (name + " is not a valid BDBJE environment configuration"); 209 } 210 211 if (forReplication) { 212 if (!param.isForReplication()) { 213 throw new IllegalArgumentException 214 (name + 215 " is not a replication environment configuration" + 216 " and cannot be used in " + configClassName); 217 } 218 } else { 219 if (param.isForReplication()) { 220 throw new IllegalArgumentException 221 (name + 222 " is a replication environment configuration" + 223 " and cannot be used in " + configClassName); 224 } 225 } 226 227 228 param.validateValue(props.getProperty(name)); 229 } 230 } 231 232 236 public static void applyFileConfig(File envHome, 237 Properties props, 238 boolean forReplication, 239 String errorClassName) 240 throws IllegalArgumentException { 241 242 File paramFile = null; 243 try { 244 Properties fileProps = new Properties (); 245 if (envHome != null) { 246 if (envHome.isFile()) { 247 paramFile = envHome; 248 } else { 249 paramFile = new File (envHome, PROPFILE_NAME); 250 } 251 FileInputStream fis = new FileInputStream (paramFile); 252 fileProps.load(fis); 253 fis.close(); 254 } 255 256 257 validateProperties(fileProps, 258 forReplication, 259 errorClassName); 260 261 262 Iterator iter = fileProps.entrySet().iterator(); 263 while (iter.hasNext()) { 264 Map.Entry propPair = (Map.Entry ) iter.next(); 265 String name = (String ) propPair.getKey(); 266 String value = (String ) propPair.getValue(); 267 setConfigParam(props, 268 name, 269 value, 270 false, 272 false, 274 forReplication); 275 } 276 } catch (FileNotFoundException e) { 277 278 282 } catch (IOException e) { 283 IllegalArgumentException e2 = new IllegalArgumentException 284 ("An error occurred when reading " + paramFile); 285 e2.initCause(e); 286 throw e2; 287 } 288 } 289 290 298 public static void setConfigParam(Properties props, 299 String paramName, 300 String value, 301 boolean requireMutability, 302 boolean validateValue, 303 boolean forReplication) 304 throws IllegalArgumentException { 305 306 307 ConfigParam param = 308 (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS.get(paramName); 309 if (param == null) { 310 throw new IllegalArgumentException 311 (paramName + 312 " is not a valid BDBJE environment configuration"); 313 } 314 315 if (forReplication) { 316 if (!param.isForReplication()) { 317 throw new IllegalArgumentException 318 (paramName + " is not a BDBJE replication configuration."); 319 } 320 } else { 321 if (param.isForReplication()) { 322 throw new IllegalArgumentException 323 (paramName + " is only available for BDBJE replication."); 324 } 325 } 326 327 328 if (requireMutability && !param.isMutable()) { 329 throw new IllegalArgumentException 330 (paramName + 331 " is not a mutable BDBJE environment configuration"); 332 } 333 334 setVal(props, param, value, validateValue); 335 } 336 337 343 public static String getConfigParam(Properties props, String paramName) 344 throws IllegalArgumentException { 345 346 347 ConfigParam param = 348 (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS.get(paramName); 349 if (param == null) { 350 throw new IllegalArgumentException 351 (paramName + 352 " is not a valid BDBJE environment configuration"); 353 } 354 355 return DbConfigManager.getVal(props, param); 356 } 357 358 363 public static String getVal(Properties props, ConfigParam param) { 364 String val = props.getProperty(param.getName()); 365 if (val == null) { 366 val = param.getDefault(); 367 } 368 return val; 369 } 370 371 375 public static void setVal(Properties props, 376 ConfigParam param, 377 String val, 378 boolean validateValue) 379 throws IllegalArgumentException { 380 381 if (validateValue) { 382 param.validateValue(val); 383 } 384 props.setProperty(param.getName(), val); 385 } 386 387 } 388 | Popular Tags |