23
23
import java .sql .Statement ;
24
24
import java .util .Collection ;
25
25
import java .util .Enumeration ;
26
- import java .util .Iterator ;
27
26
import java .util .Map ;
28
27
import java .util .Properties ;
29
28
import javax .sql .DataSource ;
52
51
import org .springframework .jdbc .datasource .TransactionAwareDataSourceProxy ;
53
52
import org .springframework .jdbc .support .JdbcUtils ;
54
53
import org .springframework .jdbc .support .lob .LobHandler ;
55
- import org .springframework .util .Assert ;
56
54
import org .springframework .util .ClassUtils ;
57
55
import org .springframework .util .StringUtils ;
58
56
108
106
*/
109
107
public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implements BeanClassLoaderAware {
110
108
111
- private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal ();
109
+ private static final ThreadLocal <DataSource > configTimeDataSourceHolder =
110
+ new ThreadLocal <DataSource >();
112
111
113
- private static final ThreadLocal configTimeTransactionManagerHolder = new ThreadLocal ();
112
+ private static final ThreadLocal <TransactionManager > configTimeTransactionManagerHolder =
113
+ new ThreadLocal <TransactionManager >();
114
114
115
- private static final ThreadLocal configTimeCacheProviderHolder = new ThreadLocal ();
115
+ private static final ThreadLocal <CacheProvider > configTimeCacheProviderHolder =
116
+ new ThreadLocal <CacheProvider >();
116
117
117
- private static final ThreadLocal configTimeLobHandlerHolder = new ThreadLocal ();
118
+ private static final ThreadLocal <LobHandler > configTimeLobHandlerHolder =
119
+ new ThreadLocal <LobHandler >();
118
120
119
121
/**
120
122
* Return the DataSource for the currently configured Hibernate SessionFactory,
@@ -126,7 +128,7 @@ public class LocalSessionFactoryBean extends AbstractSessionFactoryBean implemen
126
128
* @see LocalDataSourceConnectionProvider
127
129
*/
128
130
public static DataSource getConfigTimeDataSource () {
129
- return ( DataSource ) configTimeDataSourceHolder .get ();
131
+ return configTimeDataSourceHolder .get ();
130
132
}
131
133
132
134
/**
@@ -139,7 +141,7 @@ public static DataSource getConfigTimeDataSource() {
139
141
* @see LocalTransactionManagerLookup
140
142
*/
141
143
public static TransactionManager getConfigTimeTransactionManager () {
142
- return ( TransactionManager ) configTimeTransactionManagerHolder .get ();
144
+ return configTimeTransactionManagerHolder .get ();
143
145
}
144
146
145
147
/**
@@ -151,7 +153,7 @@ public static TransactionManager getConfigTimeTransactionManager() {
151
153
* @see #setCacheProvider
152
154
*/
153
155
public static CacheProvider getConfigTimeCacheProvider () {
154
- return ( CacheProvider ) configTimeCacheProviderHolder .get ();
156
+ return configTimeCacheProviderHolder .get ();
155
157
}
156
158
157
159
/**
@@ -166,7 +168,7 @@ public static CacheProvider getConfigTimeCacheProvider() {
166
168
* @see org.springframework.orm.hibernate3.support.BlobSerializableType
167
169
*/
168
170
public static LobHandler getConfigTimeLobHandler () {
169
- return ( LobHandler ) configTimeLobHandlerHolder .get ();
171
+ return configTimeLobHandlerHolder .get ();
170
172
}
171
173
172
174
@@ -204,7 +206,7 @@ public static LobHandler getConfigTimeLobHandler() {
204
206
205
207
private Properties collectionCacheStrategies ;
206
208
207
- private Map eventListeners ;
209
+ private Map < String , Object > eventListeners ;
208
210
209
211
private boolean schemaUpdate = false ;
210
212
@@ -484,7 +486,7 @@ public void setCollectionCacheStrategies(Properties collectionCacheStrategies) {
484
486
* listener objects as values
485
487
* @see org.hibernate.cfg.Configuration#setListener(String, Object)
486
488
*/
487
- public void setEventListeners (Map eventListeners ) {
489
+ public void setEventListeners (Map < String , Object > eventListeners ) {
488
490
this .eventListeners = eventListeners ;
489
491
}
490
492
@@ -506,6 +508,7 @@ public void setBeanClassLoader(ClassLoader beanClassLoader) {
506
508
507
509
508
510
@ Override
511
+ @ SuppressWarnings ("unchecked" )
509
512
protected SessionFactory buildSessionFactory () throws Exception {
510
513
// Create Configuration instance.
511
514
Configuration config = newConfiguration ();
@@ -576,23 +579,22 @@ protected SessionFactory buildSessionFactory() throws Exception {
576
579
if (this .typeDefinitions != null ) {
577
580
// Register specified Hibernate type definitions.
578
581
Mappings mappings = config .createMappings ();
579
- for (int i = 0 ; i < this .typeDefinitions .length ; i ++) {
580
- TypeDefinitionBean typeDef = this .typeDefinitions [i ];
582
+ for (TypeDefinitionBean typeDef : this .typeDefinitions ) {
581
583
mappings .addTypeDef (typeDef .getTypeName (), typeDef .getTypeClass (), typeDef .getParameters ());
582
584
}
583
585
}
584
586
585
587
if (this .filterDefinitions != null ) {
586
588
// Register specified Hibernate FilterDefinitions.
587
- for (int i = 0 ; i < this .filterDefinitions . length ; i ++ ) {
588
- config .addFilterDefinition (this . filterDefinitions [ i ] );
589
+ for (FilterDefinition filterDef : this .filterDefinitions ) {
590
+ config .addFilterDefinition (filterDef );
589
591
}
590
592
}
591
593
592
594
if (this .configLocations != null ) {
593
- for (int i = 0 ; i < this .configLocations . length ; i ++ ) {
595
+ for (Resource resource : this .configLocations ) {
594
596
// Load Hibernate configuration from given location.
595
- config .configure (this . configLocations [ i ] .getURL ());
597
+ config .configure (resource .getURL ());
596
598
}
597
599
}
598
600
@@ -620,42 +622,40 @@ else if (config.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY) != null) {
620
622
621
623
if (this .mappingResources != null ) {
622
624
// Register given Hibernate mapping definitions, contained in resource files.
623
- for (int i = 0 ; i < this .mappingResources . length ; i ++ ) {
624
- Resource resource = new ClassPathResource (this . mappingResources [ i ] .trim (), this .beanClassLoader );
625
+ for (String mapping : this .mappingResources ) {
626
+ Resource resource = new ClassPathResource (mapping .trim (), this .beanClassLoader );
625
627
config .addInputStream (resource .getInputStream ());
626
628
}
627
629
}
628
630
629
631
if (this .mappingLocations != null ) {
630
632
// Register given Hibernate mapping definitions, contained in resource files.
631
- for (int i = 0 ; i < this .mappingLocations . length ; i ++ ) {
632
- config .addInputStream (this . mappingLocations [ i ] .getInputStream ());
633
+ for (Resource resource : this .mappingLocations ) {
634
+ config .addInputStream (resource .getInputStream ());
633
635
}
634
636
}
635
637
636
638
if (this .cacheableMappingLocations != null ) {
637
639
// Register given cacheable Hibernate mapping definitions, read from the file system.
638
- for (int i = 0 ; i < this .cacheableMappingLocations . length ; i ++ ) {
639
- config .addCacheableFile (this . cacheableMappingLocations [ i ] .getFile ());
640
+ for (Resource resource : this .cacheableMappingLocations ) {
641
+ config .addCacheableFile (resource .getFile ());
640
642
}
641
643
}
642
644
643
645
if (this .mappingJarLocations != null ) {
644
646
// Register given Hibernate mapping definitions, contained in jar files.
645
- for (int i = 0 ; i < this .mappingJarLocations .length ; i ++) {
646
- Resource resource = this .mappingJarLocations [i ];
647
+ for (Resource resource : this .mappingJarLocations ) {
647
648
config .addJar (resource .getFile ());
648
649
}
649
650
}
650
651
651
652
if (this .mappingDirectoryLocations != null ) {
652
653
// Register all Hibernate mapping definitions in the given directories.
653
- for (int i = 0 ; i < this .mappingDirectoryLocations . length ; i ++ ) {
654
- File file = this . mappingDirectoryLocations [ i ] .getFile ();
654
+ for (Resource resource : this .mappingDirectoryLocations ) {
655
+ File file = resource .getFile ();
655
656
if (!file .isDirectory ()) {
656
657
throw new IllegalArgumentException (
657
- "Mapping directory location [" + this .mappingDirectoryLocations [i ] +
658
- "] does not denote a directory" );
658
+ "Mapping directory location [" + resource + "] does not denote a directory" );
659
659
}
660
660
config .addDirectory (file );
661
661
}
@@ -698,13 +698,11 @@ else if (strategyAndRegion.length > 0) {
698
698
699
699
if (this .eventListeners != null ) {
700
700
// Register specified Hibernate event listeners.
701
- for (Iterator it = this .eventListeners .entrySet ().iterator (); it .hasNext ();) {
702
- Map .Entry entry = (Map .Entry ) it .next ();
703
- Assert .isTrue (entry .getKey () instanceof String , "Event listener key needs to be of type String" );
704
- String listenerType = (String ) entry .getKey ();
701
+ for (Map .Entry <String , Object > entry : this .eventListeners .entrySet ()) {
702
+ String listenerType = entry .getKey ();
705
703
Object listenerObject = entry .getValue ();
706
704
if (listenerObject instanceof Collection ) {
707
- Collection listeners = (Collection ) listenerObject ;
705
+ Collection < Object > listeners = (Collection < Object > ) listenerObject ;
708
706
EventListeners listenerRegistry = config .getEventListeners ();
709
707
Object [] listenerArray =
710
708
(Object []) Array .newInstance (listenerRegistry .getListenerClassFor (listenerType ), listeners .size ());
@@ -979,8 +977,8 @@ protected void executeSchemaScript(Connection con, String[] sql) throws SQLExcep
979
977
try {
980
978
Statement stmt = con .createStatement ();
981
979
try {
982
- for (int i = 0 ; i < sql . length ; i ++ ) {
983
- executeSchemaStatement (stmt , sql [ i ] );
980
+ for (String sqlStmt : sql ) {
981
+ executeSchemaStatement (stmt , sqlStmt );
984
982
}
985
983
}
986
984
finally {
0 commit comments