Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,26 @@ public LoadedConfig loadConfigXmlResource(String cfgXmlResourceName) {

public LoadedConfig loadConfigXmlFile(File cfgXmlFile) {
try {
FileInputStream cfgFileStream = null;
try {
cfgFileStream = new FileInputStream( cfgXmlFile );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using the auto-closable?

try (FileInputStream stream = new FileInputStream( cfgXmlFile )) {
			return LoadedConfig.consume( jaxbProcessorHolder.getValue()
					.unmarshal( stream,
							new Origin( FILE, cfgXmlFile.getAbsolutePath() ) ) );
		}
catch (FileNotFoundException e) {
			throw new ConfigurationException(
					"Specified cfg.xml file [" + cfgXmlFile.getAbsolutePath() + "] does not exist"
			);
		}
		catch (IOException e) {
			log.debug( "Unable to close cfg.xml URL stream", e );
		}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems reasonable.


return LoadedConfig.consume( jaxbProcessorHolder.getValue()
.unmarshal( new FileInputStream( cfgXmlFile ),
new Origin( FILE, cfgXmlFile.getAbsolutePath() ) ) );
final JaxbCfgHibernateConfiguration jaxbCfg = jaxbProcessorHolder.getValue().unmarshal(
cfgFileStream,
new Origin( SourceType.FILE, cfgXmlFile.getAbsolutePath() )
);

return LoadedConfig.consume( jaxbCfg );
}
finally {
try {
assert cfgFileStream != null;
cfgFileStream.close();
}
catch (IOException e) {
log.debug( "Unable to close cfg.xml URL stream", e );
}
}
}
catch (FileNotFoundException e) {
throw new ConfigurationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.boot.cfgXml;

import org.hibernate.boot.cfgxml.internal.ConfigLoader;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.util.ServiceRegistryUtil;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertTrue;

/**
* Test that makes sure the input stream inside {@link ConfigLoader#loadConfigXmlFile(File)}
* gets closed.
*
* @author Mehmet Ali Emektar(inspired from CfgXmlResourceNameClosingTest)
*/
@JiraKey( value = "HHH-12986" )
public class CfgXmlResourceFileClosingTest extends BaseUnitTestCase {
private static class InputStreamWrapper extends InputStream {
private final InputStream wrapped;
private boolean wasClosed = false;

public InputStreamWrapper(InputStream wrapped) {
this.wrapped = wrapped;
}

@Override
public int read() throws IOException {
return wrapped.read();
}

@Override
public void close() throws IOException {
wrapped.close();
wasClosed = true;
super.close();
}

public boolean wasClosed() {
return wasClosed;
}
}

private static class LocalClassLoaderServiceImpl extends ClassLoaderServiceImpl {
final List<InputStreamWrapper> openedStreams = new ArrayList<InputStreamWrapper>();
boolean stopped = false;

@Override
public InputStream locateResourceStream(String name) {
InputStreamWrapper stream = new InputStreamWrapper( super.locateResourceStream( name ) );
openedStreams.add( stream );
return stream;
}

@Override
public void stop() {
for ( InputStreamWrapper openedStream : openedStreams ) {
if ( !openedStream.wasClosed ) {
try {
openedStream.close();
}
catch (IOException ignore) {
}
}
}
openedStreams.clear();
stopped = true;
super.stop();
}
}

LocalClassLoaderServiceImpl classLoaderService = new LocalClassLoaderServiceImpl();

@Test
public void testStreamClosing() {
String filePath = "src/test/java/org/hibernate/orm/test/boot/cfgXml/hibernate.cfg.xml";
File cfgXmlFile = new File(filePath);
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder()
.applyClassLoaderService( classLoaderService )
.build();
StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder( bsr )
.configure( cfgXmlFile)
.build();
try {
for ( InputStreamWrapper openedStream : classLoaderService.openedStreams ) {
assertTrue( openedStream.wasClosed );
}
}
finally {
StandardServiceRegistryBuilder.destroy( ssr );
}

assertTrue( classLoaderService.stopped );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
~ SPDX-License-Identifier: Apache-2.0
~ Copyright Red Hat Inc. and Hibernate Authors
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:hsqldb:.</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region_prefix">hibernate.test</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.testing.cache.CachingRegionFactory</property>
<mapping class="org.hibernate.orm.test.jpa.Item"/>
<mapping class="org.hibernate.orm.test.jpa.Cat"/>
<mapping class="org.hibernate.orm.test.jpa.Kitten"/>
<mapping class="org.hibernate.orm.test.jpa.Distributor"/>
<class-cache class="org.hibernate.orm.test.jpa.Item" usage="read-write"/>
<collection-cache collection="org.hibernate.orm.test.jpa.Item.distributors" usage="read-write" region="RegionName"/>
</session-factory>
</hibernate-configuration>