1 21 22 package org.apache.derby.iapi.services.property; 23 24 import org.apache.derby.iapi.reference.Property; 25 import org.apache.derby.iapi.reference.SQLState; 26 import org.apache.derby.iapi.reference.Attribute; 27 import org.apache.derby.iapi.reference.EngineType; 28 import org.apache.derby.iapi.services.monitor.Monitor; 29 import org.apache.derby.iapi.services.monitor.ModuleFactory; 30 import org.apache.derby.iapi.error.StandardException; 31 import org.apache.derby.iapi.util.StringUtil; 32 33 import java.util.Properties ; 34 import java.io.Serializable ; 35 import java.util.Dictionary ; 36 37 64 public class PropertyUtil { 65 66 public static final String [] servicePropertyList = { 68 EngineType.PROPERTY, 69 Property.NO_AUTO_BOOT, 70 Property.STORAGE_TEMP_DIRECTORY, 71 Attribute.CRYPTO_PROVIDER, 72 Attribute.CRYPTO_ALGORITHM, 73 Attribute.RESTORE_FROM, 74 Attribute.LOG_DEVICE, 75 Property.LOG_ARCHIVE_MODE 76 }; 77 78 81 public static final int SET_IN_JVM = 0; 82 85 public static final int SET_IN_DATABASE = 1; 86 89 public static final int SET_IN_APPLICATION = 2; 90 91 94 public static final int NOT_SET = -1; 95 96 97 static int whereSet(String key, Dictionary set) { 98 99 boolean dbOnly = isDBOnly(set); 100 101 if (!dbOnly) { 102 if (Monitor.getMonitor().getJVMProperty(key) != null) { 103 return SET_IN_JVM; 104 } 105 } 106 107 if ((set != null) && (set.get(key) != null)) 108 return SET_IN_DATABASE; 109 110 if (!dbOnly) { 111 if (PropertyUtil.getSystemProperty(key) != null) 112 return SET_IN_APPLICATION; 113 } 114 115 return NOT_SET; 116 } 117 118 public static boolean isDBOnly(Dictionary set) { 119 120 if (set == null) 121 return false; 122 123 String value = (String ) set.get(Property.DATABASE_PROPERTIES_ONLY); 124 125 boolean dbOnly = Boolean.valueOf( 126 (value != null ? value.trim() : value)).booleanValue(); 127 128 return dbOnly; 129 } 130 131 public static boolean isDBOnly(Properties set) { 132 133 if (set == null) 134 return false; 135 136 String value = set.getProperty(Property.DATABASE_PROPERTIES_ONLY); 137 138 boolean dbOnly = Boolean.valueOf( 139 (value != null ? value.trim() : value)).booleanValue(); 140 141 return dbOnly; 142 } 143 144 150 public static String getSystemProperty(String key) { 151 return PropertyUtil.getSystemProperty(key, (String ) null); 152 } 153 154 169 public static String getSystemProperty(String key, String defaultValue) { 170 171 ModuleFactory monitor = Monitor.getMonitorLite(); 172 173 String value = monitor.getJVMProperty(key); 174 175 if (value == null) { 176 177 Properties applicationProperties = 178 monitor.getApplicationProperties(); 179 180 if (applicationProperties != null) 181 value = applicationProperties.getProperty(key); 182 } 183 return value == null ? defaultValue : value; 184 } 185 186 187 203 public static String getPropertyFromSet(Properties set, String key) { 204 205 boolean dbOnly = set != null ? isDBOnly(set) : false; 206 207 return PropertyUtil.getPropertyFromSet(dbOnly, set, key); 208 } 209 210 public static Serializable getPropertyFromSet(Dictionary set, String key) { 211 212 boolean dbOnly = set != null ? isDBOnly(set) : false; 213 214 return PropertyUtil.getPropertyFromSet(dbOnly, set, key); 215 } 216 217 public static Serializable getPropertyFromSet(boolean dbOnly, Dictionary set, String key) { 218 219 if (set != null) { 220 221 Serializable value; 222 223 if (!dbOnly) { 224 value = Monitor.getMonitor().getJVMProperty(key); 225 if (value != null) 226 return value; 227 } 228 229 value = (Serializable ) set.get(key); 230 if (value != null) 231 return value; 232 233 if (dbOnly) 234 return null; 235 } 236 237 return PropertyUtil.getSystemProperty(key); 238 } 239 240 public static String getPropertyFromSet(boolean dbOnly, Properties set, String key) { 241 242 if (set != null) { 243 244 String value; 245 246 if (!dbOnly) { 247 value = Monitor.getMonitor().getJVMProperty(key); 248 if (value != null) 249 return value; 250 } 251 252 value = set.getProperty(key); 253 if (value != null) 254 return value; 255 256 if (dbOnly) 257 return null; 258 } 259 260 return PropertyUtil.getSystemProperty(key); 261 } 262 263 268 public static String getDatabaseProperty(PersistentSet set, String key) 269 throws StandardException { 270 271 if (set == null) 272 return null; 273 274 Object obj = set.getProperty(key); 275 if (obj == null) { return null; } 276 return obj.toString(); 277 } 278 279 289 public static String getServiceProperty(PersistentSet set, String key, String defaultValue) 290 throws StandardException { 291 292 293 String value = 294 PropertyUtil.getDatabaseProperty( 295 set, Property.DATABASE_PROPERTIES_ONLY); 296 297 boolean dbOnly = 298 Boolean.valueOf( 299 (value != null ? value.trim() : value)).booleanValue(); 300 301 if (!dbOnly) { 302 value = Monitor.getMonitor().getJVMProperty(key); 303 if (value != null) 304 return value; 305 } 306 307 value = PropertyUtil.getDatabaseProperty(set, key); 308 if (value != null) 309 return value; 310 311 if (dbOnly) { 312 return defaultValue; 313 } 314 315 return PropertyUtil.getSystemProperty(key, defaultValue); 316 } 317 318 319 329 public static String getServiceProperty(PersistentSet set, String key) 330 throws StandardException { 331 return PropertyUtil.getServiceProperty(set, key, (String ) null); 332 } 333 334 339 public static boolean getSystemBoolean(String key) { 340 341 String value = PropertyUtil.getSystemProperty(key); 342 343 return( 344 Boolean.valueOf( 345 (value != null ? value.trim() : value)).booleanValue()); 346 } 347 348 355 public static boolean getServiceBoolean(PersistentSet set, String key, boolean defValue) 356 throws StandardException { 357 358 String value = PropertyUtil.getServiceProperty(set, key); 359 360 return booleanProperty(key, value, defValue); 361 } 362 363 369 public static int getSystemInt(String key, int min, int max, int defaultValue) { 370 return PropertyUtil.handleInt(PropertyUtil.getSystemProperty(key), min, max, defaultValue); 371 } 372 373 382 public static int getServiceInt(PersistentSet set, String key, int min, int max, int defaultValue) 383 throws StandardException { 384 return PropertyUtil.handleInt(PropertyUtil.getServiceProperty(set, key), min, max, defaultValue); 386 } 387 388 398 public static int getServiceInt(PersistentSet set, Properties props, String key, int min, int max, int defaultValue) 399 throws StandardException { 400 401 String value = null; 402 403 if (props != null) 404 value = props.getProperty(key); 405 406 if (value == null) 407 value = PropertyUtil.getServiceProperty(set, key); 408 409 return PropertyUtil.handleInt(value, min, max, defaultValue); 410 } 411 412 418 public static int getSystemInt(String key, int defaultValue) { 419 return PropertyUtil.getSystemInt(key, 0, Integer.MAX_VALUE, defaultValue); 420 } 421 422 425 public static int handleInt(String value, int min, int max, int defaultValue) { 426 427 if (value == null) 428 return defaultValue; 429 430 try { 431 int intValue = Integer.parseInt(value); 432 if ((intValue >= min) && (intValue <= max)) 433 return intValue; 434 } 435 catch (NumberFormatException nfe) 436 { 437 } 439 return defaultValue; 440 } 441 442 455 public static boolean booleanProperty(String p, Serializable v, boolean defaultValue) 456 throws StandardException 457 { 458 if (v==null) 459 return defaultValue; 460 461 String vS = ((String ) v).trim(); 462 if (StringUtil.SQLToLowerCase(vS).equals("true")) 463 return true; 464 if (StringUtil.SQLToLowerCase(vS).equals("false")) 465 return false; 466 467 throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vS); 468 } 469 470 476 public static int intPropertyValue(String p, Serializable v, 477 int minValue, int maxValue, int defaultValue) 478 throws StandardException 479 { 480 if (v==null) 481 return defaultValue; 482 483 String vs = ((String )v).trim(); 484 try { 485 int result = Integer.parseInt(vs); 486 if (result < minValue || result > maxValue) 487 throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vs); 488 return result; 489 } 490 catch (NumberFormatException nfe) { 491 throw StandardException.newException(SQLState.PROPERTY_INVALID_VALUE, p,vs); 492 } 493 } 494 495 499 public static boolean isServiceProperty(String key) 500 { 501 for (int i = 0; i < PropertyUtil.servicePropertyList.length; i++) 502 if (key.equals(PropertyUtil.servicePropertyList[i])) return true; 503 return false; 504 } 505 } 506 507 | Popular Tags |