1 21 package oracle.toplink.essentials.internal.ejb.cmp3.base; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import java.util.logging.Level ; 29 import java.security.AccessController ; 30 import java.security.PrivilegedAction ; 31 import oracle.toplink.essentials.config.*; 32 import oracle.toplink.essentials.internal.localization.ExceptionLocalization; 33 import oracle.toplink.essentials.internal.sessions.AbstractSession; 34 import oracle.toplink.essentials.logging.SessionLog; 35 36 68 public class PropertiesHandler { 69 70 78 public static String getPropertyValue(String name, Map m) { 79 return Prop.getPropertyValueToApply(name, m, null); 80 } 81 82 public static String getPropertyValueLogDebug(String name, Map m, AbstractSession session) { 83 return Prop.getPropertyValueToApply(name, m, session); 84 } 85 86 94 public static String getPrefixedPropertyValue(String prefix, String suffix, Map m) { 95 return (String )getPrefixValues(prefix, m).get(suffix); 96 } 97 98 108 public static Map getPrefixValues(String prefix, Map m) { 109 return Prop.getPrefixValuesToApply(prefix, m, null); 110 } 111 112 public static Map getPrefixValuesLogDebug(String prefix, Map m, AbstractSession session) { 113 return Prop.getPrefixValuesToApply(prefix, m, session); 114 } 115 116 122 public static String getDefaultPropertyValue(String name) { 123 return Prop.getDefaultPropertyValueToApply(name, null); 124 } 125 126 public static String getDefaultPropertyValueLogDebug(String name, AbstractSession session) { 127 return Prop.getDefaultPropertyValueToApply(name, session); 128 } 129 130 135 protected static boolean shouldUseDefault(String value) { 136 return value != null && value.length() == 0; 137 } 138 139 protected static abstract class Prop { 140 static HashMap mainMap = new HashMap (); 141 Object [] valueArray; 142 HashMap valueMap; 143 String name; 144 String defaultValue; 145 String defaultValueToApply; 146 boolean valueToApplyMayBeNull; 147 boolean shouldReturnOriginalValueIfValueToApplyNotFound; 148 149 static { 150 addProp(new LoggingLevelProp()); 151 addProp(new TargetDatabaseProp()); 152 addProp(new TargetServerProp()); 153 addProp(new CacheSizeProp()); 154 addProp(new CacheTypeProp()); 155 addProp(new CacheSharedProp()); 156 addProp(new DescriptorCustomizerProp()); 157 } 158 159 Prop(String name) { 160 this.name = name; 161 } 162 163 Prop(String name, String defaultValue) { 164 this(name); 165 this.defaultValue = defaultValue; 166 } 167 168 static String getPropertyValueFromMap(String name, Map m) { 169 String value = (String )m.get(name); 170 return value == null ? System.getProperty(name) : value; 171 } 172 173 static Map getPrefixValuesFromMap(String name, Map m) { 176 Map mapOut = new HashMap (); 177 178 Iterator it = (Iterator )AccessController.doPrivileged( 179 new PrivilegedAction () { 180 public Object run() { 181 return System.getProperties().entrySet().iterator(); 182 } 183 } 184 ); 185 186 while(it.hasNext()) { 187 Map.Entry entry = (Map.Entry )it.next(); 188 String str = (String )entry.getKey(); 189 if(str.startsWith(name)) { 190 String entityName = str.substring(name.length(), str.length()); 191 mapOut.put(entityName, entry.getValue()); 192 } 193 } 194 195 it = m.entrySet().iterator(); 196 while(it.hasNext()) { 197 Map.Entry entry = (Map.Entry )it.next(); 198 String str = (String )entry.getKey(); 199 if(str.startsWith(name)) { 200 String entityName = str.substring(name.length(), str.length()); 201 mapOut.put(entityName, entry.getValue()); 202 } 203 } 204 205 return mapOut; 206 } 207 208 static String getPropertyValue(String name, boolean shouldUseDefault, Map m, AbstractSession session) { 209 Prop prop = (Prop)mainMap.get(name); 210 if(prop == null) { 211 return null; 213 } 214 String value = (String )getPropertyValueFromMap(name, m); 215 if(value == null) { 216 return null; 217 } 218 return prop.getValueToApply(value, shouldUseDefault, session); 219 } 220 221 static String getPropertyValueToApply(String name, Map m, AbstractSession session) { 222 Prop prop = (Prop)mainMap.get(name); 223 if(prop == null) { 224 return null; 225 } 226 String value = (String )getPropertyValueFromMap(name, m); 227 if(value == null) { 228 return null; 229 } 230 return prop.getValueToApply(value, shouldUseDefault(value), session); 231 } 232 233 static Map getPrefixValuesToApply(String prefix, Map m, AbstractSession session) { 234 Prop prop = (Prop)mainMap.get(prefix); 235 if(prop == null) { 236 return new HashMap (0); 238 } 239 240 Map mapIn = (Map )getPrefixValuesFromMap(prefix, m); 242 if(mapIn.isEmpty()) { 243 return mapIn; 244 } 245 246 HashMap mapOut = new HashMap (mapIn.size()); 247 Iterator it = mapIn.entrySet().iterator(); 248 while(it.hasNext()) { 249 Map.Entry entry = (Map.Entry )it.next(); 250 String suffix = (String )entry.getKey(); 251 String value = (String )entry.getValue(); 252 mapOut.put(suffix, prop.getValueToApply(value, shouldUseDefault(value), suffix, session)); 253 } 254 return mapOut; 256 } 257 258 static String getDefaultPropertyValueToApply(String name, AbstractSession session) { 259 Prop prop = (Prop)mainMap.get(name); 260 if(prop == null) { 261 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("ejb30-default-for-unknown-property", new Object []{name})); 262 } 263 prop.logDefault(session); 264 return prop.defaultValueToApply; 265 } 266 267 String getValueToApply(String value, boolean shouldUseDefault, AbstractSession session) { 268 return getValueToApply(value, shouldUseDefault, null, session); 269 } 270 271 String getValueToApply(String value, boolean shouldUseDefault, String suffix, AbstractSession session) { 273 if(shouldUseDefault) { 274 logDefault(session, suffix); 275 return defaultValueToApply; 276 } 277 String valueToApply = value; 278 if(valueMap != null) { 279 String key = getUpperCaseString(value); 280 valueToApply = (String )valueMap.get(key); 281 if(valueToApply == null) { 282 boolean notFound = true; 283 if(valueToApplyMayBeNull) { 284 notFound = !valueMap.containsKey(key); 285 } 286 if(notFound) { 287 if(shouldReturnOriginalValueIfValueToApplyNotFound) { 288 valueToApply = value; 289 } else { 290 String propertyName = name; 291 if(suffix != null) { 292 propertyName = propertyName + suffix; 293 } 294 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("ejb30-illegal-property-value", new Object []{propertyName, getPrintValue(value)})); 295 } 296 } 297 } 298 } 299 log(session, value, valueToApply, suffix); 300 return valueToApply; 301 } 302 303 static String getUpperCaseString(String value) { 304 if(value != null) { 305 return value.toUpperCase(); 306 } else { 307 return null; 308 } 309 } 310 static String getPrintValue(String value) { 311 if(value != null) { 312 return value; 313 } else { 314 return "null"; 315 } 316 } 317 void initialize() { 318 if(valueArray != null) { 319 valueMap = new HashMap (valueArray.length); 320 if(valueArray instanceof Object [][]) { 321 Object [][] valueArray2 = (Object [][])valueArray; 322 for(int i=0; i<valueArray2.length; i++) { 323 valueMap.put(getUpperCaseString((String )valueArray2[i][0]), valueArray2[i][1]); 324 if(valueArray2[i][1] == null) { 325 valueToApplyMayBeNull = true; 326 } 327 } 328 } else { 329 for(int i=0; i<valueArray.length; i++) { 330 valueMap.put(getUpperCaseString((String )valueArray[i]), valueArray[i]); 331 if(valueArray[i] == null) { 332 valueToApplyMayBeNull = true; 333 } 334 } 335 } 336 defaultValueToApply = (String )valueMap.get(getUpperCaseString(defaultValue)); 337 } else { 338 defaultValueToApply = defaultValue; 339 } 340 } 341 342 void logDefault(AbstractSession session) { 343 logDefault(session, null); 344 } 345 346 void logDefault(AbstractSession session, String suffix) { 347 if(session != null) { 348 String propertyName = name; 349 if(suffix != null) { 350 propertyName = propertyName + suffix; 351 } 352 if(defaultValue != defaultValueToApply) { 353 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "handler_property_value_default", new Object []{propertyName, defaultValue, defaultValueToApply}); 354 } else { 355 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_default", new Object []{propertyName, defaultValue}); 356 } 357 } 358 } 359 360 void log(AbstractSession session, String value, String valueToApply, String suffix) { 361 if(session != null) { 362 String propertyName = name; 363 if(suffix != null) { 364 propertyName = propertyName + suffix; 365 } 366 if(value != valueToApply) { 367 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "handler_property_value_specified", new Object []{propertyName, value, valueToApply}); 368 } else { 369 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_specified", new Object []{propertyName, value}); 370 } 371 } 372 } 373 374 static void addProp(Prop prop) { 375 prop.initialize(); 376 mainMap.put(prop.name, prop); 377 } 378 } 379 380 protected static class LoggingLevelProp extends Prop { 381 LoggingLevelProp() { 382 super(TopLinkProperties.LOGGING_LEVEL, Level.CONFIG.getName()); 383 valueArray = new Object [] { 384 Level.OFF.getName(), 385 Level.SEVERE.getName(), 386 Level.OFF.getName(), 387 Level.WARNING.getName(), 388 Level.INFO.getName(), 389 Level.CONFIG.getName(), 390 Level.FINE.getName(), 391 Level.FINER.getName(), 392 Level.FINEST.getName(), 393 Level.ALL.getName() 394 }; 395 } 396 } 397 398 protected static class TargetDatabaseProp extends Prop { 399 TargetDatabaseProp() { 400 super(TopLinkProperties.TARGET_DATABASE, TargetDatabase.DEFAULT); 401 this.shouldReturnOriginalValueIfValueToApplyNotFound = true; 402 String pcg = "oracle.toplink.essentials.platform.database."; 403 valueArray = new Object [][] { 404 {TargetDatabase.Auto, pcg + "DatabasePlatform"}, 405 {TargetDatabase.Oracle, pcg + "oracle.OraclePlatform"}, 406 {TargetDatabase.Attunity, pcg + "AttunityPlatform"}, 407 {TargetDatabase.Cloudscape, pcg + "CloudscapePlatform"}, 408 {TargetDatabase.Database, pcg + "DatabasePlatform"}, 409 {TargetDatabase.DB2Mainframe, pcg + "DB2MainframePlatform"}, 410 {TargetDatabase.DB2, pcg + "DB2Platform"}, 411 {TargetDatabase.DBase, pcg + "DBasePlatform"}, 412 {TargetDatabase.Derby, pcg + "DerbyPlatform"}, 413 {TargetDatabase.HSQL, pcg + "HSQLPlatform"}, 414 {TargetDatabase.Informix, pcg + "InformixPlatform"}, 415 {TargetDatabase.JavaDB, pcg + "JavaDBPlatform"}, 416 {TargetDatabase.MySQL4, pcg + "MySQL4Platform"}, 417 {TargetDatabase.PointBase, pcg + "PointBasePlatform"}, 418 {TargetDatabase.PostgreSQL, pcg + "PostgreSQLPlatform"}, 419 {TargetDatabase.SQLAnyWhere, pcg + "SQLAnyWherePlatform"}, 420 {TargetDatabase.SQLServer, pcg + "SQLServerPlatform"}, 421 {TargetDatabase.Sybase, pcg + "SybasePlatform"}, 422 {TargetDatabase.TimesTen, pcg + "TimesTenPlatform"} 423 }; 424 } 425 } 426 427 protected static class TargetServerProp extends Prop { 428 TargetServerProp() { 429 super(TopLinkProperties.TARGET_SERVER, TargetServer.DEFAULT); 430 this.shouldReturnOriginalValueIfValueToApplyNotFound = true; 431 String pcg = "oracle.toplink.essentials.platform.server."; 432 valueArray = new Object [][] { 433 {TargetServer.None, pcg + "NoServerPlatform"}, 434 {TargetServer.OC4J_10_1_3, pcg + "oc4j.Oc4jPlatform"}, 435 {TargetServer.SunAS9, pcg + "sunas.SunAS9ServerPlatform"} 436 }; 437 } 438 } 439 440 protected static class CacheSizeProp extends Prop { 441 CacheSizeProp() { 442 super(TopLinkProperties.CACHE_SIZE_, Integer.toString(1000)); 443 } 444 } 445 446 protected static class CacheTypeProp extends Prop { 447 CacheTypeProp() { 448 super(TopLinkProperties.CACHE_TYPE_, CacheType.DEFAULT); 449 String pcg = "oracle.toplink.essentials.internal.identitymaps."; 450 valueArray = new Object [][] { 451 {CacheType.Weak, pcg + "WeakIdentityMap"}, 452 {CacheType.SoftWeak, pcg + "SoftCacheWeakIdentityMap"}, 453 {CacheType.HardWeak, pcg + "HardCacheWeakIdentityMap"}, 454 {CacheType.Full, pcg + "FullIdentityMap"}, 455 {CacheType.NONE, pcg + "NoIdentityMap"} 456 }; 457 } 458 } 459 460 protected static class CacheSharedProp extends Prop { 461 CacheSharedProp() { 462 super(TopLinkProperties.CACHE_SHARED_, "false"); 463 valueArray = new Object [] { 464 "true", 465 "false" 466 }; 467 } 468 } 469 470 protected static class DescriptorCustomizerProp extends Prop { 471 DescriptorCustomizerProp() { 472 super(TopLinkProperties.DESCRIPTOR_CUSTOMIZER_); 473 } 474 } 475 476 } 477 | Popular Tags |