1 package org.hibernate.cfg; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.sql.Connection ; 7 import java.sql.Statement ; 8 import java.sql.Timestamp ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import org.hibernate.util.PropertiesHelper; 18 19 20 151 public final class Environment { 152 153 public static final String VERSION = "3.1 beta 1"; 154 155 158 public static final String CONNECTION_PROVIDER ="hibernate.connection.provider_class"; 159 162 public static final String DRIVER ="hibernate.connection.driver_class"; 163 166 public static final String ISOLATION ="hibernate.connection.isolation"; 167 170 public static final String URL ="hibernate.connection.url"; 171 174 public static final String USER ="hibernate.connection.username"; 175 178 public static final String PASS ="hibernate.connection.password"; 179 182 public static final String AUTOCOMMIT ="hibernate.connection.autocommit"; 183 186 public static final String POOL_SIZE ="hibernate.connection.pool_size"; 187 190 public static final String DATASOURCE ="hibernate.connection.datasource"; 191 194 public static final String CONNECTION_PREFIX = "hibernate.connection"; 195 196 199 public static final String JNDI_CLASS ="hibernate.jndi.class"; 200 203 public static final String JNDI_URL ="hibernate.jndi.url"; 204 207 public static final String JNDI_PREFIX = "hibernate.jndi"; 208 211 public static final String SESSION_FACTORY_NAME = "hibernate.session_factory_name"; 212 213 216 public static final String DIALECT ="hibernate.dialect"; 217 220 public static final String DEFAULT_SCHEMA = "hibernate.default_schema"; 221 224 public static final String DEFAULT_CATALOG = "hibernate.default_catalog"; 225 226 229 public static final String SHOW_SQL ="hibernate.show_sql"; 230 233 public static final String USE_SQL_COMMENTS ="hibernate.use_sql_comments"; 234 237 public static final String MAX_FETCH_DEPTH = "hibernate.max_fetch_depth"; 238 241 public static final String DEFAULT_BATCH_FETCH_SIZE = "hibernate.default_batch_fetch_size"; 242 245 public static final String USE_STREAMS_FOR_BINARY = "hibernate.jdbc.use_streams_for_binary"; 246 250 public static final String USE_SCROLLABLE_RESULTSET = "hibernate.jdbc.use_scrollable_resultset"; 251 256 public static final String USE_GET_GENERATED_KEYS = "hibernate.jdbc.use_get_generated_keys"; 257 261 public static final String STATEMENT_FETCH_SIZE = "hibernate.jdbc.fetch_size"; 262 265 public static final String STATEMENT_BATCH_SIZE = "hibernate.jdbc.batch_size"; 266 269 public static final String BATCH_STRATEGY = "hibernate.jdbc.factory_class"; 270 273 public static final String BATCH_VERSIONED_DATA = "hibernate.jdbc.batch_versioned_data"; 274 277 public static final String OUTPUT_STYLESHEET ="hibernate.xml.output_stylesheet"; 278 279 282 public static final String C3P0_MAX_SIZE = "hibernate.c3p0.max_size"; 283 286 public static final String C3P0_MIN_SIZE = "hibernate.c3p0.min_size"; 287 288 291 public static final String C3P0_TIMEOUT = "hibernate.c3p0.timeout"; 292 295 public static final String C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements"; 296 299 public static final String C3P0_ACQUIRE_INCREMENT = "hibernate.c3p0.acquire_increment"; 300 303 public static final String C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period"; 304 305 308 public static final String PROXOOL_PREFIX = "hibernate.proxool"; 309 312 public static final String PROXOOL_XML = "hibernate.proxool.xml"; 313 316 public static final String PROXOOL_PROPERTIES = "hibernate.proxool.properties"; 317 320 public static final String PROXOOL_EXISTING_POOL = "hibernate.proxool.existing_pool"; 321 326 public static final String PROXOOL_POOL_ALIAS = "hibernate.proxool.pool_alias"; 327 328 331 public static final String AUTO_CLOSE_SESSION = "hibernate.transaction.auto_close_session"; 332 335 public static final String FLUSH_BEFORE_COMPLETION = "hibernate.transaction.flush_before_completion"; 336 339 public static final String RELEASE_CONNECTIONS = "hibernate.connection.release_mode"; 340 343 public static final String TRANSACTION_STRATEGY = "hibernate.transaction.factory_class"; 344 347 public static final String TRANSACTION_MANAGER_STRATEGY = "hibernate.transaction.manager_lookup_class"; 348 351 public static final String USER_TRANSACTION = "jta.UserTransaction"; 352 353 356 public static final String CACHE_PROVIDER = "hibernate.cache.provider_class"; 357 360 public static final String CACHE_NAMESPACE = "hibernate.cache.jndi"; 361 364 public static final String USE_QUERY_CACHE = "hibernate.cache.use_query_cache"; 365 368 public static final String QUERY_CACHE_FACTORY = "hibernate.cache.query_cache_factory"; 369 372 public static final String USE_SECOND_LEVEL_CACHE = "hibernate.cache.use_second_level_cache"; 373 376 public static final String USE_MINIMAL_PUTS = "hibernate.cache.use_minimal_puts"; 377 380 public static final String CACHE_REGION_PREFIX = "hibernate.cache.region_prefix"; 381 384 public static final String USE_STRUCTURED_CACHE = "hibernate.cache.use_structured_entries"; 385 386 389 public static final String GENERATE_STATISTICS = "hibernate.generate_statistics"; 390 391 public static final String USE_IDENTIFIER_ROLLBACK = "hibernate.use_identifier_rollback"; 392 393 396 public static final String USE_REFLECTION_OPTIMIZER = "hibernate.cglib.use_reflection_optimizer"; 397 398 401 public static final String QUERY_TRANSLATOR = "hibernate.query.factory_class"; 402 406 public static final String QUERY_SUBSTITUTIONS = "hibernate.query.substitutions"; 407 411 public static final String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto"; 412 413 418 public static final String SQL_EXCEPTION_CONVERTER = "hibernate.jdbc.sql_exception_converter"; 419 420 424 public static final String WRAP_RESULT_SETS = "hibernate.jdbc.wrap_result_sets"; 425 426 429 public static final String ORDER_UPDATES = "hibernate.order_updates"; 430 431 434 public static final String DEFAULT_ENTITY_MODE = "hibernate.default_entity_mode"; 435 436 437 private static final boolean ENABLE_BINARY_STREAMS; 438 private static final boolean ENABLE_REFLECTION_OPTIMIZER; 439 private static final boolean JVM_SUPPORTS_LINKED_HASH_COLLECTIONS; 440 private static final boolean JVM_HAS_TIMESTAMP_BUG; 441 private static final boolean JVM_HAS_JDK14_TIMESTAMP; 442 private static final boolean JVM_SUPPORTS_GET_GENERATED_KEYS; 443 444 private static final Properties GLOBAL_PROPERTIES; 445 private static final HashMap ISOLATION_LEVELS = new HashMap (); 446 private static final Map OBSOLETE_PROPERTIES = new HashMap (); 447 448 private static final Log log = LogFactory.getLog(Environment.class); 449 450 453 public static void verifyProperties(Properties props) { 454 Iterator iter = props.keySet().iterator(); 455 while ( iter.hasNext() ) { 456 Object oldProp = iter.next(); 457 Object newProp = OBSOLETE_PROPERTIES.get(oldProp); 458 if ( newProp!=null ) log.warn("Usage of obsolete property: " + oldProp + " no longer supported, use: " + newProp); 459 } 460 } 461 462 static { 463 464 log.info("Hibernate " + VERSION); 465 466 ISOLATION_LEVELS.put( new Integer (Connection.TRANSACTION_NONE), "NONE" ); 467 ISOLATION_LEVELS.put( new Integer (Connection.TRANSACTION_READ_UNCOMMITTED), "READ_UNCOMMITTED" ); 468 ISOLATION_LEVELS.put( new Integer (Connection.TRANSACTION_READ_COMMITTED), "READ_COMMITTED" ); 469 ISOLATION_LEVELS.put( new Integer (Connection.TRANSACTION_REPEATABLE_READ), "REPEATABLE_READ" ); 470 ISOLATION_LEVELS.put( new Integer (Connection.TRANSACTION_SERIALIZABLE), "SERIALIZABLE" ); 471 472 GLOBAL_PROPERTIES = new Properties (); 473 GLOBAL_PROPERTIES.setProperty( USE_REFLECTION_OPTIMIZER, Boolean.TRUE.toString() ); 474 475 InputStream stream = Environment.class.getResourceAsStream("/hibernate.properties"); 476 if ( stream==null ) { 477 log.info("hibernate.properties not found"); 478 } 479 else { 480 try { 481 GLOBAL_PROPERTIES.load(stream); 482 log.info( "loaded properties from resource hibernate.properties: " + PropertiesHelper.maskOut(GLOBAL_PROPERTIES, PASS) ); 483 } 484 catch (Exception e) { 485 log.error("problem loading properties from hibernate.properties"); 486 } 487 finally { 488 try{ 489 stream.close(); 490 } 491 catch (IOException ioe){ 492 log.error("could not close stream on hibernate.properties", ioe); 493 } 494 } 495 } 496 497 try { 498 GLOBAL_PROPERTIES.putAll( System.getProperties() ); 499 } 500 catch (SecurityException se) { 501 log.warn("could not copy system properties, system properties will be ignored"); 502 } 503 504 verifyProperties(GLOBAL_PROPERTIES); 505 506 ENABLE_BINARY_STREAMS = PropertiesHelper.getBoolean(USE_STREAMS_FOR_BINARY, GLOBAL_PROPERTIES); 507 ENABLE_REFLECTION_OPTIMIZER = PropertiesHelper.getBoolean(USE_REFLECTION_OPTIMIZER, GLOBAL_PROPERTIES); 508 509 if (ENABLE_BINARY_STREAMS) log.info("using java.io streams to persist binary types"); 510 if (ENABLE_REFLECTION_OPTIMIZER) log.info("using CGLIB reflection optimizer"); 511 512 boolean getGeneratedKeysSupport; 513 try { 514 Statement .class.getMethod("getGeneratedKeys", null); 515 getGeneratedKeysSupport = true; 516 } 517 catch (NoSuchMethodException nsme) { 518 getGeneratedKeysSupport = false; 519 } 520 JVM_SUPPORTS_GET_GENERATED_KEYS = getGeneratedKeysSupport; 521 if (!JVM_SUPPORTS_GET_GENERATED_KEYS) log.info("JVM does not support Statement.getGeneratedKeys()"); 522 523 boolean linkedHashSupport; 524 try { 525 Class.forName("java.util.LinkedHashSet"); 526 linkedHashSupport = true; 527 } 528 catch (ClassNotFoundException cnfe) { 529 linkedHashSupport = false; 530 } 531 JVM_SUPPORTS_LINKED_HASH_COLLECTIONS = linkedHashSupport; 532 if (!JVM_SUPPORTS_LINKED_HASH_COLLECTIONS) log.info("JVM does not support LinkedHasMap, LinkedHashSet - ordered maps and sets disabled"); 533 534 JVM_HAS_TIMESTAMP_BUG = new Timestamp (123456789).getTime() != 123456789; 535 if (JVM_HAS_TIMESTAMP_BUG) log.info("using workaround for JVM bug in java.sql.Timestamp"); 536 Timestamp t = new Timestamp (0); 537 t.setNanos(5 * 1000000); 538 JVM_HAS_JDK14_TIMESTAMP = t.getTime() == 5; 539 if (JVM_HAS_JDK14_TIMESTAMP) { 540 log.info("using JDK 1.4 java.sql.Timestamp handling"); 541 } 542 else { 543 log.info("using pre JDK 1.4 java.sql.Timestamp handling"); 544 } 545 } 546 547 550 public static boolean jvmHasTimestampBug() { 551 return JVM_HAS_TIMESTAMP_BUG; 552 } 553 554 557 public static boolean jvmHasJDK14Timestamp() { 558 return JVM_HAS_JDK14_TIMESTAMP; 559 } 560 561 566 public static boolean jvmSupportsLinkedHashCollections() { 567 return JVM_SUPPORTS_LINKED_HASH_COLLECTIONS; 568 } 569 570 public static boolean jvmSupportsGetGeneratedKeys() { 571 return JVM_SUPPORTS_GET_GENERATED_KEYS; 572 } 573 574 579 public static boolean useStreamsForBinary() { 580 return ENABLE_BINARY_STREAMS; 581 } 582 583 588 public static boolean useReflectionOptimizer() { 589 return ENABLE_REFLECTION_OPTIMIZER; 590 } 591 592 private Environment() { throw new UnsupportedOperationException (); } 593 594 599 public static Properties getProperties() { 600 Properties copy = new Properties (); 601 copy.putAll(GLOBAL_PROPERTIES); 602 return copy; 603 } 604 605 612 public static String isolationLevelToString(int isolation) { 613 return (String ) ISOLATION_LEVELS.get( new Integer (isolation) ); 614 } 615 616 } 617 618 619 620 621 622 623 624 | Popular Tags |