Merge pull request #11624 from igor-suhorukov:master

* pr/11624:
  Polish "Fix potential resource leaks"
  Fix potential resource leaks
pull/11655/merge
Stephane Nicoll 7 years ago
commit 4f194c215d

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.boot.devtools.autoconfigure; package org.springframework.boot.devtools.autoconfigure;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -97,7 +99,11 @@ public class DevToolsDataSourceAutoConfiguration {
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
if (dataSourceRequiresShutdown()) { if (dataSourceRequiresShutdown()) {
this.dataSource.getConnection().createStatement().execute("SHUTDOWN"); try (Connection connection = this.dataSource.getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute("SHUTDOWN");
}
}
} }
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -215,8 +215,8 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
@Override @Override
public void writeLoaderClasses(String loaderJarResourceName) throws IOException { public void writeLoaderClasses(String loaderJarResourceName) throws IOException {
URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName); URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName);
JarInputStream inputStream = new JarInputStream( try (JarInputStream inputStream = new JarInputStream(
new BufferedInputStream(loaderJar.openStream())); new BufferedInputStream(loaderJar.openStream()))) {
JarEntry entry; JarEntry entry;
while ((entry = inputStream.getNextJarEntry()) != null) { while ((entry = inputStream.getNextJarEntry()) != null) {
if (entry.getName().endsWith(".class")) { if (entry.getName().endsWith(".class")) {
@ -224,7 +224,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
new InputStreamEntryWriter(inputStream, false)); new InputStreamEntryWriter(inputStream, false));
} }
} }
inputStream.close(); }
} }
/** /**

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -157,7 +157,9 @@ public class JarFile extends java.util.jar.JarFile {
Manifest manifest = (this.manifest == null ? null : this.manifest.get()); Manifest manifest = (this.manifest == null ? null : this.manifest.get());
if (manifest == null) { if (manifest == null) {
if (this.type == JarFileType.NESTED_DIRECTORY) { if (this.type == JarFileType.NESTED_DIRECTORY) {
manifest = new JarFile(this.getRootJarFile()).getManifest(); try (JarFile rootJarFile = new JarFile(this.getRootJarFile())) {
manifest = rootJarFile.getManifest();
}
} }
else { else {
try (InputStream inputStream = getInputStream(MANIFEST_NAME, try (InputStream inputStream = getInputStream(MANIFEST_NAME,

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -123,13 +123,13 @@ public class RunMojo extends AbstractRunMojo {
return this.hasDevtools; return this.hasDevtools;
} }
@SuppressWarnings("resource")
private boolean checkForDevtools() { private boolean checkForDevtools() {
try { try {
URL[] urls = getClassPathUrls(); URL[] urls = getClassPathUrls();
URLClassLoader classLoader = new URLClassLoader(urls); try (URLClassLoader classLoader = new URLClassLoader(urls)) {
return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null); return (classLoader.findResource(RESTARTER_CLASS_LOCATION) != null);
} }
}
catch (Exception ex) { catch (Exception ex) {
return false; return false;
} }

Loading…
Cancel
Save