Skip to content

Commit 4223e43

Browse files
committed
HHH-10120 - InputStream not closed in ConfigLoader.loadConfigXmlResource(String)
(cherry picked from commit 4777d58)
1 parent 4181768 commit 4223e43

File tree

2 files changed

+128
-5
lines changed

2 files changed

+128
-5
lines changed

hibernate-core/src/main/java/org/hibernate/boot/cfgxml/internal/ConfigLoader.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ public LoadedConfig loadConfigXmlResource(String cfgXmlResourceName) {
5252
if ( stream == null ) {
5353
throw new ConfigurationException( "Could not locate cfg.xml resource [" + cfgXmlResourceName + "]" );
5454
}
55-
final JaxbCfgHibernateConfiguration jaxbCfg = jaxbProcessorHolder.getValue().unmarshal(
56-
stream,
57-
new Origin( SourceType.RESOURCE, cfgXmlResourceName )
58-
);
5955

60-
return LoadedConfig.consume( jaxbCfg );
56+
try {
57+
final JaxbCfgHibernateConfiguration jaxbCfg = jaxbProcessorHolder.getValue().unmarshal(
58+
stream,
59+
new Origin( SourceType.RESOURCE, cfgXmlResourceName )
60+
);
61+
62+
return LoadedConfig.consume( jaxbCfg );
63+
}
64+
finally {
65+
try {
66+
stream.close();
67+
}
68+
catch (IOException e) {
69+
log.debug( "Unable to close cfg.xml resource stream", e );
70+
}
71+
}
6172
}
6273

6374
public LoadedConfig loadConfigXmlFile(File cfgXmlFile) {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.boot.cfgXml;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
import org.hibernate.boot.cfgxml.internal.ConfigLoader;
15+
import org.hibernate.boot.registry.BootstrapServiceRegistry;
16+
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
17+
import org.hibernate.boot.registry.StandardServiceRegistry;
18+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
19+
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
20+
import org.hibernate.engine.config.spi.ConfigurationService;
21+
22+
import org.hibernate.testing.TestForIssue;
23+
import org.hibernate.testing.junit4.BaseUnitTestCase;
24+
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
29+
30+
/**
31+
* Test that makes sure the input stream inside {@link ConfigLoader#loadConfigXmlResource(java.lang.String)}
32+
* gets closed.
33+
*
34+
* @author Steve Ebersole
35+
*/
36+
@TestForIssue( jiraKey = "HHH-10120" )
37+
public class CfgXmlResourceNameClosingTest extends BaseUnitTestCase {
38+
private static class InputStreamWrapper extends InputStream {
39+
private final InputStream wrapped;
40+
private boolean wasClosed = false;
41+
42+
public InputStreamWrapper(InputStream wrapped) {
43+
this.wrapped = wrapped;
44+
}
45+
46+
@Override
47+
public int read() throws IOException {
48+
return wrapped.read();
49+
}
50+
51+
@Override
52+
public void close() throws IOException {
53+
wrapped.close();
54+
wasClosed = true;
55+
super.close();
56+
}
57+
58+
public boolean wasClosed() {
59+
return wasClosed;
60+
}
61+
}
62+
63+
private static class LocalClassLoaderServiceImpl extends ClassLoaderServiceImpl {
64+
final List<InputStreamWrapper> openedStreams = new ArrayList<InputStreamWrapper>();
65+
boolean stopped = false;
66+
67+
@Override
68+
public InputStream locateResourceStream(String name) {
69+
InputStreamWrapper stream = new InputStreamWrapper( super.locateResourceStream( name ) );
70+
openedStreams.add( stream );
71+
return stream;
72+
}
73+
74+
@Override
75+
public void stop() {
76+
for ( InputStreamWrapper openedStream : openedStreams ) {
77+
if ( !openedStream.wasClosed ) {
78+
try {
79+
openedStream.close();
80+
}
81+
catch (IOException ignore) {
82+
}
83+
}
84+
}
85+
openedStreams.clear();
86+
stopped = true;
87+
super.stop();
88+
}
89+
}
90+
91+
LocalClassLoaderServiceImpl classLoaderService = new LocalClassLoaderServiceImpl();
92+
93+
@Test
94+
public void testStreamClosing() {
95+
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder()
96+
.applyClassLoaderService( classLoaderService )
97+
.build();
98+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder( bsr )
99+
.configure( "org/hibernate/test/boot/cfgXml/hibernate.cfg.xml" )
100+
.build();
101+
try {
102+
for ( InputStreamWrapper openedStream : classLoaderService.openedStreams ) {
103+
assertTrue( openedStream.wasClosed );
104+
}
105+
}
106+
finally {
107+
StandardServiceRegistryBuilder.destroy( ssr );
108+
}
109+
110+
assertTrue( classLoaderService.stopped );
111+
}
112+
}

0 commit comments

Comments
 (0)