Skip to content

Commit 1deccba

Browse files
committed
very minor code cleanups in cfg package
1 parent bbfab98 commit 1deccba

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.hibernate.MappingException;
2424
import org.hibernate.SessionFactory;
2525
import org.hibernate.SessionFactoryObserver;
26-
import org.hibernate.boot.Metadata;
2726
import org.hibernate.boot.MetadataBuilder;
2827
import org.hibernate.boot.MetadataSources;
2928
import org.hibernate.boot.SessionFactoryBuilder;
@@ -470,9 +469,9 @@ public Configuration configure(File configFile) throws HibernateException {
470469
* @param highlightSql should logged SQL be highlighted with pretty colors
471470
*/
472471
public Configuration showSql(boolean showSql, boolean formatSql, boolean highlightSql) {
473-
setProperty( AvailableSettings.SHOW_SQL, Boolean.toString(showSql) );
474-
setProperty( AvailableSettings.FORMAT_SQL, Boolean.toString(formatSql) );
475-
setProperty( AvailableSettings.HIGHLIGHT_SQL, Boolean.toString(highlightSql) );
472+
setProperty( JdbcSettings.SHOW_SQL, Boolean.toString(showSql) );
473+
setProperty( JdbcSettings.FORMAT_SQL, Boolean.toString(formatSql) );
474+
setProperty( JdbcSettings.HIGHLIGHT_SQL, Boolean.toString(highlightSql) );
476475
return this;
477476
}
478477

@@ -482,7 +481,7 @@ public Configuration showSql(boolean showSql, boolean formatSql, boolean highlig
482481
* @param action the {@link Action}
483482
*/
484483
public Configuration setSchemaExportAction(Action action) {
485-
setProperty( AvailableSettings.HBM2DDL_AUTO, action.getExternalHbm2ddlName() );
484+
setProperty( SchemaToolingSettings.HBM2DDL_AUTO, action.getExternalHbm2ddlName() );
486485
return this;
487486
}
488487

@@ -493,8 +492,8 @@ public Configuration setSchemaExportAction(Action action) {
493492
* @param pass the password
494493
*/
495494
public Configuration setCredentials(String user, String pass) {
496-
setProperty( AvailableSettings.USER, user );
497-
setProperty( AvailableSettings.PASS, pass );
495+
setProperty( JdbcSettings.USER, user );
496+
setProperty( JdbcSettings.PASS, pass );
498497
return this;
499498
}
500499

@@ -514,7 +513,7 @@ public Configuration setJdbcUrl(String url) {
514513
* @param jndiName the JNDI name of the datasource
515514
*/
516515
public Configuration setDatasource(String jndiName) {
517-
setProperty( AvailableSettings.DATASOURCE, jndiName );
516+
setProperty( JdbcSettings.DATASOURCE, jndiName );
518517
return this;
519518
}
520519

@@ -524,7 +523,7 @@ public Configuration setDatasource(String jndiName) {
524523
* @param transactionType the {@link PersistenceUnitTransactionType}
525524
*/
526525
public Configuration setTransactionType(PersistenceUnitTransactionType transactionType) {
527-
setProperty( AvailableSettings.JAKARTA_TRANSACTION_TYPE, transactionType.toString() );
526+
setProperty( PersistenceSettings.JAKARTA_TRANSACTION_TYPE, transactionType.toString() );
528527
return this;
529528
}
530529

@@ -783,8 +782,8 @@ public Configuration addAnnotatedClass(Class<?> annotatedClass) {
783782
*
784783
* @return this (for method chaining)
785784
*/
786-
public Configuration addAnnotatedClasses(Class... annotatedClasses) {
787-
for (Class annotatedClass : annotatedClasses) {
785+
public Configuration addAnnotatedClasses(Class<?>... annotatedClasses) {
786+
for ( var annotatedClass : annotatedClasses ) {
788787
addAnnotatedClass( annotatedClass );
789788
}
790789
return this;
@@ -814,7 +813,7 @@ public Configuration addPackage(String packageName) throws MappingException {
814813
* @throws MappingException in case there is an error in the mapping data
815814
*/
816815
public Configuration addPackages(String... packageNames) throws MappingException {
817-
for (String packageName : packageNames) {
816+
for ( String packageName : packageNames ) {
818817
addPackage( packageName );
819818
}
820819
return this;
@@ -1078,8 +1077,8 @@ public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throw
10781077
.forEach( metadataBuilder::applyAttributeConverter );
10791078
}
10801079

1081-
final Metadata metadata = metadataBuilder.build();
1082-
final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
1080+
final var metadata = metadataBuilder.build();
1081+
final var sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
10831082

10841083
if ( interceptor != null && interceptor != EmptyInterceptor.INSTANCE ) {
10851084
sessionFactoryBuilder.applyInterceptor( interceptor );
@@ -1130,7 +1129,7 @@ public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throw
11301129
public SessionFactory buildSessionFactory() throws HibernateException {
11311130
log.trace( "Building session factory using internal StandardServiceRegistryBuilder" );
11321131
standardServiceRegistryBuilder.applySettings( properties );
1133-
StandardServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();
1132+
var serviceRegistry = standardServiceRegistryBuilder.build();
11341133
try {
11351134
return buildSessionFactory( serviceRegistry );
11361135
}

hibernate-core/src/main/java/org/hibernate/cfg/Environment.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
package org.hibernate.cfg;
66

77
import java.io.IOException;
8-
import java.io.InputStream;
98
import java.lang.invoke.MethodHandles;
109
import java.util.Properties;
1110

1211
import org.hibernate.HibernateException;
1312
import org.hibernate.Internal;
1413
import org.hibernate.Version;
1514
import org.hibernate.internal.CoreMessageLogger;
16-
import org.hibernate.internal.util.ConfigHelper;
17-
import org.hibernate.internal.util.config.ConfigurationHelper;
1815

1916
import org.jboss.logging.Logger;
2017

18+
import static org.hibernate.internal.util.ConfigHelper.getResourceAsStream;
19+
import static org.hibernate.internal.util.config.ConfigurationHelper.maskOut;
20+
2121
/**
2222
* Provides access to configuration properties passed in {@link Properties} objects.
2323
* <p>
@@ -141,30 +141,27 @@ public final class Environment implements AvailableSettings {
141141
GLOBAL_PROPERTIES = new Properties();
142142

143143
try {
144-
InputStream stream = ConfigHelper.getResourceAsStream( "/hibernate.properties" );
145-
try {
146-
GLOBAL_PROPERTIES.load(stream);
147-
LOG.propertiesLoaded( ConfigurationHelper.maskOut( GLOBAL_PROPERTIES, PASS ) );
148-
}
149-
catch (Exception e) {
150-
LOG.unableToLoadProperties();
151-
}
152-
finally {
153-
try{
154-
stream.close();
144+
try (var stream = getResourceAsStream("/hibernate.properties")) {
145+
try {
146+
GLOBAL_PROPERTIES.load(stream);
147+
LOG.propertiesLoaded( maskOut( GLOBAL_PROPERTIES, PASS ) );
155148
}
156-
catch (IOException ioe){
157-
LOG.unableToCloseStreamError( ioe );
149+
catch (Exception e) {
150+
LOG.unableToLoadProperties();
158151
}
159152
}
153+
catch (IOException ioe) {
154+
LOG.unableToCloseStreamError( ioe );
155+
}
160156
}
161157
catch (HibernateException he) {
162158
LOG.propertiesNotFound();
163159
}
164160

165161
try {
166-
final Properties systemProperties = System.getProperties();
167-
// Must be thread-safe in case an application changes System properties during Hibernate initialization.
162+
final var systemProperties = System.getProperties();
163+
// Must be thread-safe in case an application changes
164+
// System properties during Hibernate initialization.
168165
// See HHH-8383.
169166
synchronized (systemProperties) {
170167
GLOBAL_PROPERTIES.putAll(systemProperties);
@@ -187,7 +184,7 @@ private Environment() {
187184
* with all additional properties specified in {@code hibernate.properties}.
188185
*/
189186
public static Properties getProperties() {
190-
final Properties copy = new Properties();
187+
final var copy = new Properties();
191188
copy.putAll(GLOBAL_PROPERTIES);
192189
return copy;
193190
}

0 commit comments

Comments
 (0)