From 5a4238acfcea821f38b660170b8c0ffdb58375bd Mon Sep 17 00:00:00 2001 From: igor-suhorukov Date: Sat, 13 Jan 2018 15:10:52 +0300 Subject: [PATCH 1/2] Fix potential resource leaks See gh-11624 --- .../DevToolsDataSourceAutoConfiguration.java | 8 +++++++- .../boot/loader/tools/JarWriter.java | 16 ++++++++-------- .../springframework/boot/loader/jar/JarFile.java | 4 +++- .../org/springframework/boot/maven/RunMojo.java | 5 +++-- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java index 218592072e..52b912da13 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.boot.devtools.autoconfigure; +import java.sql.Connection; +import java.sql.Statement; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -97,7 +99,11 @@ public class DevToolsDataSourceAutoConfiguration { @Override public void destroy() throws Exception { if (dataSourceRequiresShutdown()) { - this.dataSource.getConnection().createStatement().execute("SHUTDOWN"); + try (Connection connection = this.dataSource.getConnection()) { + try (Statement statement = connection.createStatement()) { + statement.execute("SHUTDOWN"); + } + } } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java index c0b2b8760e..c120e808db 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java @@ -215,16 +215,16 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable { @Override public void writeLoaderClasses(String loaderJarResourceName) throws IOException { URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName); - JarInputStream inputStream = new JarInputStream( - new BufferedInputStream(loaderJar.openStream())); - JarEntry entry; - while ((entry = inputStream.getNextJarEntry()) != null) { - if (entry.getName().endsWith(".class")) { - writeEntry(new JarArchiveEntry(entry), - new InputStreamEntryWriter(inputStream, false)); + try (JarInputStream inputStream = new JarInputStream( + new BufferedInputStream(loaderJar.openStream()))) { + JarEntry entry; + while ((entry = inputStream.getNextJarEntry()) != null) { + if (entry.getName().endsWith(".class")) { + writeEntry(new JarArchiveEntry(entry), + new InputStreamEntryWriter(inputStream, false)); + } } } - inputStream.close(); } /** diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java index e01c965b8c..10d0a80cb0 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java @@ -157,7 +157,9 @@ public class JarFile extends java.util.jar.JarFile { Manifest manifest = (this.manifest == null ? null : this.manifest.get()); if (manifest == null) { if (this.type == JarFileType.NESTED_DIRECTORY) { - manifest = new JarFile(this.getRootJarFile()).getManifest(); + try (JarFile rootJarFile = new JarFile(this.getRootJarFile())) { + manifest = rootJarFile.getManifest(); + } } else { try (InputStream inputStream = getInputStream(MANIFEST_NAME, diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java index 3fd71b5204..3463e43426 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java @@ -127,8 +127,9 @@ public class RunMojo extends AbstractRunMojo { private boolean checkForDevtools() { try { URL[] urls = getClassPathUrls(); - URLClassLoader classLoader = new URLClassLoader(urls); - return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null); + try (URLClassLoader classLoader = new URLClassLoader(urls)) { + return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null); + } } catch (Exception ex) { return false; From d43346d6c2fdf4b69abb90946259d781ff8e4bb4 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 17 Jan 2018 13:56:53 +0100 Subject: [PATCH 2/2] Polish "Fix potential resource leaks" Closes gh-11624 --- .../autoconfigure/DevToolsDataSourceAutoConfiguration.java | 2 +- .../java/org/springframework/boot/loader/tools/JarWriter.java | 2 +- .../main/java/org/springframework/boot/loader/jar/JarFile.java | 2 +- .../src/main/java/org/springframework/boot/maven/RunMojo.java | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java index 52b912da13..8ab67190d9 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java index c120e808db..ab75f8d190 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java index 10d0a80cb0..22696fe22c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java index 3463e43426..6d37364e17 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -123,7 +123,6 @@ public class RunMojo extends AbstractRunMojo { return this.hasDevtools; } - @SuppressWarnings("resource") private boolean checkForDevtools() { try { URL[] urls = getClassPathUrls();