1515
1616import org .hibernate .boot .CacheRegionDefinition ;
1717import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgCollectionCacheType ;
18- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgConfigPropertyType ;
1918import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgEntityCacheType ;
20- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgEventListenerGroupType ;
2119import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgEventListenerType ;
2220import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgHibernateConfiguration ;
23- import org .hibernate .boot .jaxb .cfg .spi .JaxbCfgMappingReferenceType ;
2421import org .hibernate .event .spi .EventType ;
2522
2623import org .jboss .logging .Logger ;
@@ -75,41 +72,41 @@ public Map<EventType<?>, Set<String>> getEventListenerMap() {
7572 * @return The parsed representation
7673 */
7774 public static LoadedConfig consume (JaxbCfgHibernateConfiguration jaxbCfg ) {
78- final LoadedConfig cfg = new LoadedConfig ( jaxbCfg .getSessionFactory ().getName () );
75+ final var cfg = new LoadedConfig ( jaxbCfg .getSessionFactory ().getName () );
7976
80- for ( JaxbCfgConfigPropertyType jaxbProperty : jaxbCfg .getSessionFactory ().getProperty () ) {
77+ for ( var jaxbProperty : jaxbCfg .getSessionFactory ().getProperty () ) {
8178 cfg .addConfigurationValue ( jaxbProperty .getName (), jaxbProperty .getValue () );
8279 }
8380
84- for ( JaxbCfgMappingReferenceType jaxbMapping : jaxbCfg .getSessionFactory ().getMapping () ) {
81+ for ( var jaxbMapping : jaxbCfg .getSessionFactory ().getMapping () ) {
8582 cfg .addMappingReference ( MappingReference .consume ( jaxbMapping ) );
8683 }
8784
8885 for ( Object cacheDeclaration : jaxbCfg .getSessionFactory ().getClassCacheOrCollectionCache () ) {
8986 cfg .addCacheRegionDefinition ( parseCacheRegionDefinition ( cacheDeclaration ) );
9087 }
9188
92- if ( !jaxbCfg .getSessionFactory ().getListener ().isEmpty () ) {
93- for ( JaxbCfgEventListenerType listener : jaxbCfg .getSessionFactory ().getListener () ) {
94- final EventType <?> eventType = EventType .resolveEventTypeByName ( listener .getType ().value () );
89+ final var eventListeners = jaxbCfg .getSessionFactory ().getListener ();
90+ if ( !eventListeners .isEmpty () ) {
91+ for ( var listener : eventListeners ) {
92+ final var eventType = EventType .resolveEventTypeByName ( listener .getType ().value () );
9593 cfg .addEventListener ( eventType , listener .getClazz () );
9694 }
9795 }
9896
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 () );
97+ final var listenerGroups = jaxbCfg .getSessionFactory ().getEvent ();
98+ if ( ! listenerGroups . isEmpty () ) {
99+ for ( var listenerGroup : listenerGroups ) {
100+ if ( ! listenerGroup . getListener (). isEmpty () ) {
101+ final String eventTypeName = listenerGroup . getType (). value ();
102+ final var eventType = EventType . resolveEventTypeByName ( eventTypeName );
103+ for ( var listener : listenerGroup .getListener () ) {
104+ if ( listener . getType () != null ) {
105+ LOG . debugf ( "Listener [%s] defined as part of a group also defined event type" ,
106+ listener . getClazz () );
107+ }
108+ cfg . addEventListener ( eventType , listener .getClazz () );
111109 }
112- cfg .addEventListener ( eventType , listener .getClazz () );
113110 }
114111 }
115112 }
@@ -118,17 +115,13 @@ public static LoadedConfig consume(JaxbCfgHibernateConfiguration jaxbCfg) {
118115 }
119116
120117 private static String trim (String value ) {
121- if ( value == null ) {
122- return null ;
123- }
118+ return value == null ? null : value .trim ();
124119
125- return value .trim ();
126120 }
127121
128122 private void addConfigurationValue (String propertyName , String value ) {
129123 value = trim ( value );
130124 configurationValues .put ( propertyName , value );
131-
132125 if ( !propertyName .startsWith ( "hibernate." ) ) {
133126 configurationValues .put ( "hibernate." + propertyName , value );
134127 }
@@ -138,7 +131,6 @@ private void addMappingReference(MappingReference mappingReference) {
138131 if ( mappingReferences == null ) {
139132 mappingReferences = new ArrayList <>();
140133 }
141-
142134 mappingReferences .add ( mappingReference );
143135 }
144136
@@ -152,8 +144,7 @@ private static CacheRegionDefinition parseCacheRegionDefinition(Object cacheDecl
152144 "all" .equals ( jaxbClassCache .getInclude () )
153145 );
154146 }
155- else {
156- final JaxbCfgCollectionCacheType jaxbCollectionCache = (JaxbCfgCollectionCacheType ) cacheDeclaration ;
147+ else if ( cacheDeclaration instanceof JaxbCfgCollectionCacheType jaxbCollectionCache ) {
157148 return new CacheRegionDefinition (
158149 CacheRegionDefinition .CacheRegionType .COLLECTION ,
159150 jaxbCollectionCache .getCollection (),
@@ -162,6 +153,9 @@ private static CacheRegionDefinition parseCacheRegionDefinition(Object cacheDecl
162153 false
163154 );
164155 }
156+ else {
157+ throw new IllegalArgumentException ( "Unrecognized cache declaration" );
158+ }
165159 }
166160
167161 public void addCacheRegionDefinition (CacheRegionDefinition cacheRegionDefinition ) {
@@ -213,22 +207,18 @@ public void merge(LoadedConfig incoming) {
213207 }
214208
215209 protected void addConfigurationValues (Map <String ,Object > configurationValues ) {
216- if ( configurationValues = = null ) {
217- return ;
210+ if ( configurationValues ! = null ) {
211+ this . configurationValues . putAll ( configurationValues ) ;
218212 }
219-
220- this .configurationValues .putAll ( configurationValues );
221213 }
222214
223215 private void addMappingReferences (List <MappingReference > mappingReferences ) {
224- if ( mappingReferences == null ) {
225- return ;
226- }
227-
228- if ( this .mappingReferences == null ) {
229- this .mappingReferences = new ArrayList <>();
216+ if ( mappingReferences != null ) {
217+ if ( this .mappingReferences == null ) {
218+ this .mappingReferences = new ArrayList <>();
219+ }
220+ this .mappingReferences .addAll ( mappingReferences );
230221 }
231- this .mappingReferences .addAll ( mappingReferences );
232222 }
233223
234224 private void addCacheRegionDefinitions (List <CacheRegionDefinition > cacheRegionDefinitions ) {
@@ -243,21 +233,19 @@ private void addCacheRegionDefinitions(List<CacheRegionDefinition> cacheRegionDe
243233 }
244234
245235 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- }
236+ if ( eventListenerMap != null ) {
237+ if ( this .eventListenerMap == null ) {
238+ this .eventListenerMap = new HashMap <>();
239+ }
253240
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 );
241+ for ( var incomingEntry : eventListenerMap .entrySet () ) {
242+ var listenerClasses = this .eventListenerMap .get ( incomingEntry .getKey () );
243+ if ( listenerClasses == null ) {
244+ listenerClasses = new HashSet <>();
245+ this .eventListenerMap .put ( incomingEntry .getKey (), listenerClasses );
246+ }
247+ listenerClasses .addAll ( incomingEntry .getValue () );
259248 }
260- listenerClasses .addAll ( incomingEntry .getValue () );
261249 }
262250 }
263251
0 commit comments