15
15
16
16
import org .hibernate .boot .CacheRegionDefinition ;
17
17
import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgCollectionCacheType ;
18
- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgConfigPropertyType ;
19
18
import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgEntityCacheType ;
20
- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgEventListenerGroupType ;
21
- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgEventListenerType ;
22
19
import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgHibernateConfiguration ;
23
- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgMappingReferenceType ;
24
20
import org .hibernate .event .spi .EventType ;
25
21
26
22
import org .jboss .logging .Logger ;
@@ -75,41 +71,41 @@ public Map<EventType<?>, Set<String>> getEventListenerMap() {
75
71
* @return The parsed representation
76
72
*/
77
73
public static LoadedConfig consume (JaxbCfgHibernateConfiguration jaxbCfg ) {
78
- final LoadedConfig cfg = new LoadedConfig ( jaxbCfg .getSessionFactory ().getName () );
74
+ final var cfg = new LoadedConfig ( jaxbCfg .getSessionFactory ().getName () );
79
75
80
- for ( JaxbCfgConfigPropertyType jaxbProperty : jaxbCfg .getSessionFactory ().getProperty () ) {
76
+ for ( var jaxbProperty : jaxbCfg .getSessionFactory ().getProperty () ) {
81
77
cfg .addConfigurationValue ( jaxbProperty .getName (), jaxbProperty .getValue () );
82
78
}
83
79
84
- for ( JaxbCfgMappingReferenceType jaxbMapping : jaxbCfg .getSessionFactory ().getMapping () ) {
80
+ for ( var jaxbMapping : jaxbCfg .getSessionFactory ().getMapping () ) {
85
81
cfg .addMappingReference ( MappingReference .consume ( jaxbMapping ) );
86
82
}
87
83
88
84
for ( Object cacheDeclaration : jaxbCfg .getSessionFactory ().getClassCacheOrCollectionCache () ) {
89
85
cfg .addCacheRegionDefinition ( parseCacheRegionDefinition ( cacheDeclaration ) );
90
86
}
91
87
92
- if ( !jaxbCfg .getSessionFactory ().getListener ().isEmpty () ) {
93
- for ( JaxbCfgEventListenerType listener : jaxbCfg .getSessionFactory ().getListener () ) {
94
- final EventType <?> eventType = EventType .resolveEventTypeByName ( listener .getType ().value () );
88
+ final var eventListeners = jaxbCfg .getSessionFactory ().getListener ();
89
+ if ( !eventListeners .isEmpty () ) {
90
+ for ( var listener : eventListeners ) {
91
+ final var eventType = EventType .resolveEventTypeByName ( listener .getType ().value () );
95
92
cfg .addEventListener ( eventType , listener .getClazz () );
96
93
}
97
94
}
98
95
99
- if ( ! jaxbCfg .getSessionFactory ().getEvent (). isEmpty () ) {
100
- for ( JaxbCfgEventListenerGroupType listenerGroup : jaxbCfg . getSessionFactory (). getEvent () ) {
101
- if ( listenerGroup . getListener (). isEmpty () ) {
102
- continue ;
103
- }
104
-
105
- final String eventTypeName = listenerGroup .getType (). value ();
106
- final EventType <?> eventType = EventType . resolveEventTypeByName ( eventTypeName );
107
-
108
- for ( JaxbCfgEventListenerType listener : listenerGroup . getListener () ) {
109
- if ( listener . getType () != null ) {
110
- LOG . debugf ( "Listener [%s] defined as part of a group also defined event type" , listener .getClazz () );
96
+ final var listenerGroups = jaxbCfg .getSessionFactory ().getEvent ();
97
+ if ( ! listenerGroups . isEmpty () ) {
98
+ for ( var listenerGroup : listenerGroups ) {
99
+ if ( ! listenerGroup . getListener (). isEmpty () ) {
100
+ final String eventTypeName = listenerGroup . getType (). value ();
101
+ final var eventType = EventType . resolveEventTypeByName ( eventTypeName );
102
+ for ( var listener : listenerGroup .getListener () ) {
103
+ if ( listener . getType () != null ) {
104
+ LOG . debugf ( "Listener [%s] defined as part of a group also defined event type" ,
105
+ listener . getClazz () );
106
+ }
107
+ cfg . addEventListener ( eventType , listener .getClazz () );
111
108
}
112
- cfg .addEventListener ( eventType , listener .getClazz () );
113
109
}
114
110
}
115
111
}
@@ -118,17 +114,13 @@ public static LoadedConfig consume(JaxbCfgHibernateConfiguration jaxbCfg) {
118
114
}
119
115
120
116
private static String trim (String value ) {
121
- if ( value == null ) {
122
- return null ;
123
- }
117
+ return value == null ? null : value .trim ();
124
118
125
- return value .trim ();
126
119
}
127
120
128
121
private void addConfigurationValue (String propertyName , String value ) {
129
122
value = trim ( value );
130
123
configurationValues .put ( propertyName , value );
131
-
132
124
if ( !propertyName .startsWith ( "hibernate." ) ) {
133
125
configurationValues .put ( "hibernate." + propertyName , value );
134
126
}
@@ -138,7 +130,6 @@ private void addMappingReference(MappingReference mappingReference) {
138
130
if ( mappingReferences == null ) {
139
131
mappingReferences = new ArrayList <>();
140
132
}
141
-
142
133
mappingReferences .add ( mappingReference );
143
134
}
144
135
@@ -152,8 +143,7 @@ private static CacheRegionDefinition parseCacheRegionDefinition(Object cacheDecl
152
143
"all" .equals ( jaxbClassCache .getInclude () )
153
144
);
154
145
}
155
- else {
156
- final JaxbCfgCollectionCacheType jaxbCollectionCache = (JaxbCfgCollectionCacheType ) cacheDeclaration ;
146
+ else if ( cacheDeclaration instanceof JaxbCfgCollectionCacheType jaxbCollectionCache ) {
157
147
return new CacheRegionDefinition (
158
148
CacheRegionDefinition .CacheRegionType .COLLECTION ,
159
149
jaxbCollectionCache .getCollection (),
@@ -162,6 +152,9 @@ private static CacheRegionDefinition parseCacheRegionDefinition(Object cacheDecl
162
152
false
163
153
);
164
154
}
155
+ else {
156
+ throw new IllegalArgumentException ( "Unrecognized cache declaration" );
157
+ }
165
158
}
166
159
167
160
public void addCacheRegionDefinition (CacheRegionDefinition cacheRegionDefinition ) {
@@ -213,22 +206,18 @@ public void merge(LoadedConfig incoming) {
213
206
}
214
207
215
208
protected void addConfigurationValues (Map <String ,Object > configurationValues ) {
216
- if ( configurationValues = = null ) {
217
- return ;
209
+ if ( configurationValues ! = null ) {
210
+ this . configurationValues . putAll ( configurationValues ) ;
218
211
}
219
-
220
- this .configurationValues .putAll ( configurationValues );
221
212
}
222
213
223
214
private void addMappingReferences (List <MappingReference > mappingReferences ) {
224
- if ( mappingReferences == null ) {
225
- return ;
226
- }
227
-
228
- if ( this .mappingReferences == null ) {
229
- this .mappingReferences = new ArrayList <>();
215
+ if ( mappingReferences != null ) {
216
+ if ( this .mappingReferences == null ) {
217
+ this .mappingReferences = new ArrayList <>();
218
+ }
219
+ this .mappingReferences .addAll ( mappingReferences );
230
220
}
231
- this .mappingReferences .addAll ( mappingReferences );
232
221
}
233
222
234
223
private void addCacheRegionDefinitions (List <CacheRegionDefinition > cacheRegionDefinitions ) {
@@ -243,21 +232,19 @@ private void addCacheRegionDefinitions(List<CacheRegionDefinition> cacheRegionDe
243
232
}
244
233
245
234
private void addEventListeners (Map <EventType <?>, Set <String >> eventListenerMap ) {
246
- if ( eventListenerMap == null ) {
247
- return ;
248
- }
249
-
250
- if ( this .eventListenerMap == null ) {
251
- this .eventListenerMap = new HashMap <>();
252
- }
235
+ if ( eventListenerMap != null ) {
236
+ if ( this .eventListenerMap == null ) {
237
+ this .eventListenerMap = new HashMap <>();
238
+ }
253
239
254
- for ( Map .Entry <EventType <?>, Set <String >> incomingEntry : eventListenerMap .entrySet () ) {
255
- Set <String > listenerClasses = this .eventListenerMap .get ( incomingEntry .getKey () );
256
- if ( listenerClasses == null ) {
257
- listenerClasses = new HashSet <>();
258
- this .eventListenerMap .put ( incomingEntry .getKey (), listenerClasses );
240
+ for ( var incomingEntry : eventListenerMap .entrySet () ) {
241
+ var listenerClasses = this .eventListenerMap .get ( incomingEntry .getKey () );
242
+ if ( listenerClasses == null ) {
243
+ listenerClasses = new HashSet <>();
244
+ this .eventListenerMap .put ( incomingEntry .getKey (), listenerClasses );
245
+ }
246
+ listenerClasses .addAll ( incomingEntry .getValue () );
259
247
}
260
- listenerClasses .addAll ( incomingEntry .getValue () );
261
248
}
262
249
}
263
250
0 commit comments