diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ExplodedArchiveDescriptor.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ExplodedArchiveDescriptor.java index ba1ec3beb1..61dfe9d3c4 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ExplodedArchiveDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ExplodedArchiveDescriptor.java @@ -145,8 +145,7 @@ public class ExplodedArchiveDescriptor extends AbstractArchiveDescriptor { } private void processZippedRoot(File rootFile, ArchiveContext context) { - try { - final JarFile jarFile = new JarFile(rootFile); + try (final JarFile jarFile = new JarFile(rootFile)){ final Enumeration entries = jarFile.entries(); while ( entries.hasMoreElements() ) { final ZipEntry zipEntry = entries.nextElement(); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/JarFileBasedArchiveDescriptor.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/JarFileBasedArchiveDescriptor.java index 8ba2f98d54..807d922c99 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/JarFileBasedArchiveDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/JarFileBasedArchiveDescriptor.java @@ -53,97 +53,106 @@ public class JarFileBasedArchiveDescriptor extends AbstractArchiveDescriptor { return; } - final Enumeration zipEntries = jarFile.entries(); - while ( zipEntries.hasMoreElements() ) { - final ZipEntry zipEntry = zipEntries.nextElement(); - final String entryName = extractName( zipEntry ); + try { + final Enumeration zipEntries = jarFile.entries(); + while ( zipEntries.hasMoreElements() ) { + final ZipEntry zipEntry = zipEntries.nextElement(); + final String entryName = extractName( zipEntry ); - if ( getEntryBasePrefix() != null && ! entryName.startsWith( getEntryBasePrefix() ) ) { - continue; - } - if ( zipEntry.isDirectory() ) { - continue; - } + if ( getEntryBasePrefix() != null && ! entryName.startsWith( getEntryBasePrefix() ) ) { + continue; + } + if ( zipEntry.isDirectory() ) { + continue; + } - if ( entryName.equals( getEntryBasePrefix() ) ) { - // exact match, might be a nested jar entry (ie from jar:file:..../foo.ear!/bar.jar) - // - // This algorithm assumes that the zipped file is only the URL root (including entry), not - // just any random entry - try (InputStream is = new BufferedInputStream( jarFile.getInputStream( zipEntry ) )) { - final JarInputStream jarInputStream = new JarInputStream( is ); - ZipEntry subZipEntry = jarInputStream.getNextEntry(); - while ( subZipEntry != null ) { - if ( ! subZipEntry.isDirectory() ) { + if ( entryName.equals( getEntryBasePrefix() ) ) { + // exact match, might be a nested jar entry (ie from jar:file:..../foo.ear!/bar.jar) + // + // This algorithm assumes that the zipped file is only the URL root (including entry), not + // just any random entry + try ( final InputStream is = new BufferedInputStream( jarFile.getInputStream( zipEntry ) ); + final JarInputStream jarInputStream = new JarInputStream( is )) { + ZipEntry subZipEntry = jarInputStream.getNextEntry(); + while ( subZipEntry != null ) { + if ( ! subZipEntry.isDirectory() ) { - final String name = extractName( subZipEntry ); - final String relativeName = extractRelativeName( subZipEntry ); - final InputStreamAccess inputStreamAccess = buildByteBasedInputStreamAccess( name, jarInputStream ); + final String name = extractName( subZipEntry ); + final String relativeName = extractRelativeName( subZipEntry ); + final InputStreamAccess inputStreamAccess = buildByteBasedInputStreamAccess( name, jarInputStream ); - final ArchiveEntry entry = new ArchiveEntry() { - @Override - public String getName() { - return name; - } + final ArchiveEntry entry = new ArchiveEntry() { + @Override + public String getName() { + return name; + } - @Override - public String getNameWithinArchive() { - return relativeName; - } + @Override + public String getNameWithinArchive() { + return relativeName; + } - @Override - public InputStreamAccess getStreamAccess() { - return inputStreamAccess; - } - }; + @Override + public InputStreamAccess getStreamAccess() { + return inputStreamAccess; + } + }; - final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler( entry ); - entryHandler.handleEntry( entry, context ); + final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler( entry ); + entryHandler.handleEntry( entry, context ); + } + + subZipEntry = jarInputStream.getNextEntry(); + } + } + catch (Exception e) { + throw new ArchiveException( "Error accessing JarFile entry [" + zipEntry.getName() + "]", e ); + } + } + else { + final String name = extractName( zipEntry ); + final String relativeName = extractRelativeName( zipEntry ); + final InputStreamAccess inputStreamAccess; + try (InputStream is = jarFile.getInputStream( zipEntry )) { + inputStreamAccess = buildByteBasedInputStreamAccess( name, is ); + } + catch (IOException e) { + throw new ArchiveException( + String.format( + "Unable to access stream from jar file [%s] for entry [%s]", + jarFile.getName(), + zipEntry.getName() + ) + ); + } + + final ArchiveEntry entry = new ArchiveEntry() { + @Override + public String getName() { + return name; } - subZipEntry = jarInputStream.getNextEntry(); - } - } - catch (Exception e) { - throw new ArchiveException( "Error accessing JarFile entry [" + zipEntry.getName() + "]", e ); + @Override + public String getNameWithinArchive() { + return relativeName; + } + + @Override + public InputStreamAccess getStreamAccess() { + return inputStreamAccess; + } + }; + + final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler( entry ); + entryHandler.handleEntry( entry, context ); } } - else { - final String name = extractName( zipEntry ); - final String relativeName = extractRelativeName( zipEntry ); - final InputStreamAccess inputStreamAccess; - try (InputStream is = jarFile.getInputStream( zipEntry )) { - inputStreamAccess = buildByteBasedInputStreamAccess( name, is ); - } - catch (IOException e) { - throw new ArchiveException( - String.format( - "Unable to access stream from jar file [%s] for entry [%s]", - jarFile.getName(), - zipEntry.getName() - ) - ); - } - - final ArchiveEntry entry = new ArchiveEntry() { - @Override - public String getName() { - return name; - } - - @Override - public String getNameWithinArchive() { - return relativeName; - } - - @Override - public InputStreamAccess getStreamAccess() { - return inputStreamAccess; - } - }; - - final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler( entry ); - entryHandler.handleEntry( entry, context ); + } + finally { + try { + jarFile.close(); + } + catch ( Exception ignore ) { } } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/ExceptionConverterImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/ExceptionConverterImpl.java index 5b957bac70..438879e25f 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/ExceptionConverterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/ExceptionConverterImpl.java @@ -50,7 +50,10 @@ public class ExceptionConverterImpl implements ExceptionConverter { public RuntimeException convertCommitException(RuntimeException e) { if ( sharedSessionContract.getFactory().getSessionFactoryOptions().isJpaBootstrap() ) { Throwable wrappedException; - if ( e instanceof PersistenceException ) { + if ( e instanceof HibernateException ) { + wrappedException = convert( (HibernateException) e ); + } + else if ( e instanceof PersistenceException ) { Throwable cause = e.getCause() == null ? e : e.getCause(); if ( cause instanceof HibernateException ) { wrappedException = convert( (HibernateException) cause ); @@ -59,9 +62,6 @@ public class ExceptionConverterImpl implements ExceptionConverter { wrappedException = cause; } } - else if ( e instanceof HibernateException ) { - wrappedException = convert( (HibernateException) e ); - } else { wrappedException = e; } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java index cd7c8d5440..17d73b18ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdateTask.java @@ -215,7 +215,9 @@ public class SchemaUpdateTask extends MatchingTask { properties.putAll( getProject().getProperties() ); } else { - properties.load( new FileInputStream( propertiesFile ) ); + try (FileInputStream fip = new FileInputStream( propertiesFile )){ + properties.load( fip ); + } } registryBuilder.applySettings( properties );