Polish "Use try-with-resources to close resources automatically"
- Apply code formatting - Use try-with-resources in many other places that were missed in the pull request Closes gh-8045
This commit is contained in:
@@ -201,13 +201,9 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
|
||||
if (!this.properties.isEmpty()) {
|
||||
FileObject file = this.processingEnv.getFiler()
|
||||
.createResource(StandardLocation.CLASS_OUTPUT, "", PROPERTIES_PATH);
|
||||
OutputStream outputStream = file.openOutputStream();
|
||||
try {
|
||||
try (OutputStream outputStream = file.openOutputStream()) {
|
||||
this.properties.store(outputStream, null);
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,15 +62,11 @@ public class TestConditionMetadataAnnotationProcessor
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
try {
|
||||
try (FileInputStream inputStream = new FileInputStream(file)) {
|
||||
Properties properties = new Properties();
|
||||
properties.load(inputStream);
|
||||
return properties;
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
|
||||
@Test
|
||||
public void simpleRepository() throws IOException {
|
||||
InputStream foo = getInputStreamFor("foo");
|
||||
try {
|
||||
try (InputStream foo = getInputStreamFor("foo")) {
|
||||
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create(foo).build();
|
||||
validateFoo(repo);
|
||||
@@ -50,15 +49,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
"spring.foo.counter");
|
||||
assertThat(repo.getAllProperties()).hasSize(3);
|
||||
}
|
||||
finally {
|
||||
foo.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hintsOnMaps() throws IOException {
|
||||
InputStream map = getInputStreamFor("map");
|
||||
try {
|
||||
try (InputStream map = getInputStreamFor("map")) {
|
||||
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create(map).build();
|
||||
validateMap(repo);
|
||||
@@ -67,16 +62,12 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
"spring.map.keys", "spring.map.values");
|
||||
assertThat(repo.getAllProperties()).hasSize(4);
|
||||
}
|
||||
finally {
|
||||
map.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void severalRepositoriesNoConflict() throws IOException {
|
||||
InputStream foo = getInputStreamFor("foo");
|
||||
InputStream bar = getInputStreamFor("bar");
|
||||
try {
|
||||
try (InputStream foo = getInputStreamFor("foo");
|
||||
InputStream bar = getInputStreamFor("bar")) {
|
||||
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create(foo, bar).build();
|
||||
validateFoo(repo);
|
||||
@@ -87,17 +78,12 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
"spring.bar.counter");
|
||||
assertThat(repo.getAllProperties()).hasSize(6);
|
||||
}
|
||||
finally {
|
||||
foo.close();
|
||||
bar.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void repositoryWithRoot() throws IOException {
|
||||
InputStream foo = getInputStreamFor("foo");
|
||||
InputStream root = getInputStreamFor("root");
|
||||
try {
|
||||
try (InputStream foo = getInputStreamFor("foo");
|
||||
InputStream root = getInputStreamFor("root")) {
|
||||
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create(foo, root).build();
|
||||
validateFoo(repo);
|
||||
@@ -107,17 +93,12 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
"spring.foo.counter", "spring.root.name", "spring.root2.name");
|
||||
assertThat(repo.getAllProperties()).hasSize(5);
|
||||
}
|
||||
finally {
|
||||
foo.close();
|
||||
root.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void severalRepositoriesIdenticalGroups() throws IOException {
|
||||
InputStream foo = getInputStreamFor("foo");
|
||||
InputStream foo2 = getInputStreamFor("foo2");
|
||||
try {
|
||||
try (InputStream foo = getInputStreamFor("foo");
|
||||
InputStream foo2 = getInputStreamFor("foo2")) {
|
||||
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create(foo, foo2).build();
|
||||
assertThat(repo.getAllGroups()).hasSize(1);
|
||||
@@ -132,16 +113,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
"spring.foo.counter", "spring.foo.enabled", "spring.foo.type");
|
||||
assertThat(repo.getAllProperties()).hasSize(5);
|
||||
}
|
||||
finally {
|
||||
foo.close();
|
||||
foo2.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyGroups() throws IOException {
|
||||
InputStream in = getInputStreamFor("empty-groups");
|
||||
try {
|
||||
try (InputStream in = getInputStreamFor("empty-groups")) {
|
||||
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create(in).build();
|
||||
validateEmptyGroup(repo);
|
||||
@@ -149,16 +125,12 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
contains(repo.getAllProperties(), "name", "title");
|
||||
assertThat(repo.getAllProperties()).hasSize(2);
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builderInstancesAreIsolated() throws IOException {
|
||||
InputStream foo = getInputStreamFor("foo");
|
||||
InputStream bar = getInputStreamFor("bar");
|
||||
try {
|
||||
try (InputStream foo = getInputStreamFor("foo");
|
||||
InputStream bar = getInputStreamFor("bar")) {
|
||||
ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder
|
||||
.create();
|
||||
ConfigurationMetadataRepository firstRepo = builder.withJsonResource(foo)
|
||||
@@ -175,10 +147,6 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
|
||||
assertThat(secondRepo.getAllGroups()).hasSize(2);
|
||||
assertThat(secondRepo.getAllProperties()).hasSize(6);
|
||||
}
|
||||
finally {
|
||||
foo.close();
|
||||
bar.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void validateFoo(ConfigurationMetadataRepository repo) {
|
||||
|
||||
@@ -64,13 +64,10 @@ public class MetadataStore {
|
||||
|
||||
public void writeMetadata(ConfigurationMetadata metadata) throws IOException {
|
||||
if (!metadata.getItems().isEmpty()) {
|
||||
OutputStream outputStream = createMetadataResource().openOutputStream();
|
||||
try {
|
||||
try (OutputStream outputStream = createMetadataResource()
|
||||
.openOutputStream()) {
|
||||
new JsonMarshaller().write(metadata, outputStream);
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -815,13 +815,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
}
|
||||
|
||||
private void writeMetadata(File metadataFile, JSONObject metadata) throws Exception {
|
||||
FileWriter writer = new FileWriter(metadataFile);
|
||||
try {
|
||||
try (FileWriter writer = new FileWriter(metadataFile)) {
|
||||
writer.append(metadata.toString(2));
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static class AdditionalMetadata {
|
||||
|
||||
@@ -83,12 +83,11 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
||||
}
|
||||
|
||||
private String loadResource(String name) {
|
||||
InputStreamReader reader = new InputStreamReader(
|
||||
getClass().getResourceAsStream(name));
|
||||
char[] buffer = new char[4096];
|
||||
int read = 0;
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
try (InputStreamReader reader = new InputStreamReader(
|
||||
getClass().getResourceAsStream(name));) {
|
||||
char[] buffer = new char[4096];
|
||||
int read = 0;
|
||||
StringWriter writer = new StringWriter();
|
||||
while ((read = reader.read(buffer)) > 0) {
|
||||
writer.write(buffer, 0, read);
|
||||
}
|
||||
@@ -97,14 +96,6 @@ final class ApplicationPluginAction implements PluginApplicationAction {
|
||||
catch (IOException ex) {
|
||||
throw new GradleException("Failed to read '" + name + "'", ex);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
reader.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -48,18 +48,9 @@ public final class BuildPropertiesWriter {
|
||||
public void writeBuildProperties(ProjectDetails projectDetails) throws IOException {
|
||||
Properties properties = createBuildInfo(projectDetails);
|
||||
createFileIfNecessary(this.outputFile);
|
||||
FileOutputStream outputStream = new FileOutputStream(this.outputFile);
|
||||
try {
|
||||
try (FileOutputStream outputStream = new FileOutputStream(this.outputFile)) {
|
||||
properties.store(outputStream, "Properties");
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
outputStream.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// Continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createFileIfNecessary(File file) throws IOException {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -64,18 +64,14 @@ public abstract class FileUtils {
|
||||
*/
|
||||
public static String sha1Hash(File file) throws IOException {
|
||||
try {
|
||||
DigestInputStream inputStream = new DigestInputStream(
|
||||
new FileInputStream(file), MessageDigest.getInstance("SHA-1"));
|
||||
try {
|
||||
try (DigestInputStream inputStream = new DigestInputStream(
|
||||
new FileInputStream(file), MessageDigest.getInstance("SHA-1"))) {
|
||||
byte[] buffer = new byte[4098];
|
||||
while (inputStream.read(buffer) != -1) {
|
||||
// Read the entire stream
|
||||
}
|
||||
return bytesToHex(inputStream.getMessageDigest().digest());
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
catch (NoSuchAlgorithmException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
|
||||
@@ -49,7 +49,7 @@ import java.util.zip.ZipEntry;
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class JarWriter implements LoaderClassesWriter {
|
||||
public class JarWriter implements LoaderClassesWriter, AutoCloseable {
|
||||
|
||||
private static final String NESTED_LOADER_JAR = "META-INF/loader/spring-boot-loader.jar";
|
||||
|
||||
@@ -128,23 +128,24 @@ public class JarWriter implements LoaderClassesWriter {
|
||||
Enumeration<JarEntry> entries = jarFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = entries.nextElement();
|
||||
ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
|
||||
jarFile.getInputStream(entry));
|
||||
try {
|
||||
if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
|
||||
new CrcAndSize(inputStream).setupStoredEntry(entry);
|
||||
inputStream.close();
|
||||
inputStream = new ZipHeaderPeekInputStream(
|
||||
jarFile.getInputStream(entry));
|
||||
}
|
||||
setUpStoredEntryIfNecessary(jarFile, entry);
|
||||
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
|
||||
jarFile.getInputStream(entry))) {
|
||||
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true);
|
||||
JarEntry transformedEntry = entryTransformer.transform(entry);
|
||||
if (transformedEntry != null) {
|
||||
writeEntry(transformedEntry, entryWriter);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpStoredEntryIfNecessary(JarFile jarFile, JarEntry entry)
|
||||
throws IOException {
|
||||
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
|
||||
jarFile.getInputStream(entry))) {
|
||||
if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
|
||||
new CrcAndSize(inputStream).setupStoredEntry(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -234,6 +235,7 @@ public class JarWriter implements LoaderClassesWriter {
|
||||
* Close the writer.
|
||||
* @throws IOException if the file cannot be closed
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.jarOutput.close();
|
||||
}
|
||||
|
||||
@@ -141,8 +141,7 @@ public abstract class MainClassFinder {
|
||||
while (!stack.isEmpty()) {
|
||||
File file = stack.pop();
|
||||
if (file.isFile()) {
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
try {
|
||||
try (InputStream inputStream = new FileInputStream(file)) {
|
||||
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
|
||||
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
|
||||
String className = convertToClassName(file.getAbsolutePath(),
|
||||
@@ -154,9 +153,6 @@ public abstract class MainClassFinder {
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
if (file.isDirectory()) {
|
||||
pushAllSorted(stack, file.listFiles(PACKAGE_FOLDER_FILTER));
|
||||
@@ -240,9 +236,8 @@ public abstract class MainClassFinder {
|
||||
List<JarEntry> classEntries = getClassEntries(jarFile, classesLocation);
|
||||
Collections.sort(classEntries, new ClassEntryComparator());
|
||||
for (JarEntry entry : classEntries) {
|
||||
InputStream inputStream = new BufferedInputStream(
|
||||
jarFile.getInputStream(entry));
|
||||
try {
|
||||
try (InputStream inputStream = new BufferedInputStream(
|
||||
jarFile.getInputStream(entry))) {
|
||||
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
|
||||
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
|
||||
String className = convertToClassName(entry.getName(),
|
||||
@@ -254,9 +249,6 @@ public abstract class MainClassFinder {
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -184,13 +184,9 @@ public class Repackager {
|
||||
}
|
||||
destination.delete();
|
||||
try {
|
||||
JarFile jarFileSource = new JarFile(workingSource);
|
||||
try {
|
||||
try (JarFile jarFileSource = new JarFile(workingSource)) {
|
||||
repackage(jarFileSource, destination, libraries, launchScript);
|
||||
}
|
||||
finally {
|
||||
jarFileSource.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (!this.backupSource && !this.source.equals(workingSource)) {
|
||||
@@ -221,21 +217,16 @@ public class Repackager {
|
||||
}
|
||||
|
||||
private boolean alreadyRepackaged() throws IOException {
|
||||
JarFile jarFile = new JarFile(this.source);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(this.source)) {
|
||||
Manifest manifest = jarFile.getManifest();
|
||||
return (manifest != null && manifest.getMainAttributes()
|
||||
.getValue(BOOT_VERSION_ATTRIBUTE) != null);
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void repackage(JarFile sourceJar, File destination, Libraries libraries,
|
||||
LaunchScript launchScript) throws IOException {
|
||||
JarWriter writer = new JarWriter(destination, launchScript);
|
||||
try {
|
||||
try (JarWriter writer = new JarWriter(destination, launchScript)) {
|
||||
final List<Library> unpackLibraries = new ArrayList<>();
|
||||
final List<Library> standardLibraries = new ArrayList<>();
|
||||
libraries.doWithLibraries(new LibraryCallback() {
|
||||
@@ -256,14 +247,6 @@ public class Repackager {
|
||||
});
|
||||
repackage(sourceJar, writer, unpackLibraries, standardLibraries);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
writer.close();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void repackage(JarFile sourceJar, JarWriter writer,
|
||||
@@ -309,13 +292,9 @@ public class Repackager {
|
||||
|
||||
private boolean isZip(File file) {
|
||||
try {
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
try {
|
||||
try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
||||
return isZip(fileInputStream);
|
||||
}
|
||||
finally {
|
||||
fileInputStream.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return false;
|
||||
|
||||
@@ -102,13 +102,9 @@ public class FileUtilsTests {
|
||||
@Test
|
||||
public void hash() throws Exception {
|
||||
File file = this.temporaryFolder.newFile();
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
try {
|
||||
try (OutputStream outputStream = new FileOutputStream(file)) {
|
||||
outputStream.write(new byte[] { 1, 2, 3 });
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
assertThat(FileUtils.sha1Hash(file))
|
||||
.isEqualTo("7037807198c22a7d2b0807371d763779a84fdfcf");
|
||||
}
|
||||
|
||||
@@ -455,17 +455,14 @@ public class RepackagerTests {
|
||||
callback.library(new Library(nestedFile, LibraryScope.COMPILE));
|
||||
}
|
||||
});
|
||||
JarFile jarFile = new JarFile(file);
|
||||
try {
|
||||
|
||||
try (JarFile jarFile = new JarFile(file)) {
|
||||
assertThat(
|
||||
jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getMethod())
|
||||
.isEqualTo(ZipEntry.STORED);
|
||||
assertThat(jarFile.getEntry("BOOT-INF/classes/test/nested.jar").getMethod())
|
||||
.isEqualTo(ZipEntry.STORED);
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -508,15 +505,12 @@ public class RepackagerTests {
|
||||
}
|
||||
|
||||
});
|
||||
JarFile jarFile = new JarFile(file);
|
||||
try {
|
||||
|
||||
try (JarFile jarFile = new JarFile(file)) {
|
||||
assertThat(
|
||||
jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getComment())
|
||||
.startsWith("UNPACK:");
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -542,14 +536,10 @@ public class RepackagerTests {
|
||||
}
|
||||
|
||||
});
|
||||
JarFile jarFile = new JarFile(file);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(file)) {
|
||||
assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getSize())
|
||||
.isEqualTo(sourceLength);
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -561,13 +551,9 @@ public class RepackagerTests {
|
||||
File dest = this.temporaryFolder.newFile("dest.jar");
|
||||
Repackager repackager = new Repackager(source);
|
||||
repackager.repackage(dest, NO_LIBRARIES);
|
||||
JarFile jarFile = new JarFile(dest);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(dest)) {
|
||||
assertThat(jarFile.getEntry("META-INF/INDEX.LIST")).isNull();
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -603,14 +589,10 @@ public class RepackagerTests {
|
||||
File dest = this.temporaryFolder.newFile("dest.jar");
|
||||
Repackager repackager = new Repackager(source);
|
||||
repackager.repackage(dest, NO_LIBRARIES);
|
||||
JarFile jarFile = new JarFile(dest);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(dest)) {
|
||||
assertThat(jarFile.getEntry("META-INF/aop.xml")).isNull();
|
||||
assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/aop.xml")).isNotNull();
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasLauncherClasses(File file) throws IOException {
|
||||
@@ -623,23 +605,15 @@ public class RepackagerTests {
|
||||
}
|
||||
|
||||
private JarEntry getEntry(File file, String name) throws IOException {
|
||||
JarFile jarFile = new JarFile(file);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(file)) {
|
||||
return jarFile.getJarEntry(name);
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
private Manifest getManifest(File file) throws IOException {
|
||||
JarFile jarFile = new JarFile(file);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(file)) {
|
||||
return jarFile.getManifest();
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static class MockLauncherScript implements LaunchScript {
|
||||
|
||||
@@ -65,25 +65,17 @@ public class TestJarFile {
|
||||
public void addFile(String filename, File fileToCopy) throws IOException {
|
||||
File file = getFilePath(filename);
|
||||
file.getParentFile().mkdirs();
|
||||
InputStream inputStream = new FileInputStream(fileToCopy);
|
||||
try {
|
||||
try (InputStream inputStream = new FileInputStream(fileToCopy)) {
|
||||
copyToFile(inputStream, file);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void addManifest(Manifest manifest) throws IOException {
|
||||
File manifestFile = new File(this.jarSource, "META-INF/MANIFEST.MF");
|
||||
manifestFile.getParentFile().mkdirs();
|
||||
OutputStream outputStream = new FileOutputStream(manifestFile);
|
||||
try {
|
||||
try (OutputStream outputStream = new FileOutputStream(manifestFile)) {
|
||||
manifest.write(outputStream);
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private File getFilePath(String filename) {
|
||||
@@ -97,13 +89,9 @@ public class TestJarFile {
|
||||
|
||||
private void copyToFile(InputStream inputStream, File file)
|
||||
throws FileNotFoundException, IOException {
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
try {
|
||||
try (OutputStream outputStream = new FileOutputStream(file)) {
|
||||
copy(inputStream, outputStream);
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void copy(InputStream in, OutputStream out) throws IOException {
|
||||
|
||||
@@ -164,35 +164,34 @@ public class PropertiesLauncher extends Launcher {
|
||||
}
|
||||
}
|
||||
for (String config : configs) {
|
||||
InputStream resource = getResource(config);
|
||||
if (resource != null) {
|
||||
debug("Found: " + config);
|
||||
try {
|
||||
this.properties.load(resource);
|
||||
try (InputStream resource = getResource(config)) {
|
||||
if (resource != null) {
|
||||
debug("Found: " + config);
|
||||
loadResource(resource);
|
||||
// Load the first one we find
|
||||
return;
|
||||
}
|
||||
finally {
|
||||
resource.close();
|
||||
else {
|
||||
debug("Not found: " + config);
|
||||
}
|
||||
for (Object key : Collections.list(this.properties.propertyNames())) {
|
||||
String text = this.properties.getProperty((String) key);
|
||||
String value = SystemPropertyUtils
|
||||
.resolvePlaceholders(this.properties, text);
|
||||
if (value != null) {
|
||||
this.properties.put(key, value);
|
||||
}
|
||||
}
|
||||
if ("true".equals(getProperty(SET_SYSTEM_PROPERTIES))) {
|
||||
debug("Adding resolved properties to System properties");
|
||||
for (Object key : Collections.list(this.properties.propertyNames())) {
|
||||
String value = this.properties.getProperty((String) key);
|
||||
System.setProperty((String) key, value);
|
||||
}
|
||||
}
|
||||
// Load the first one we find
|
||||
return;
|
||||
}
|
||||
else {
|
||||
debug("Not found: " + config);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadResource(InputStream resource) throws IOException, Exception {
|
||||
this.properties.load(resource);
|
||||
for (Object key : Collections.list(this.properties.propertyNames())) {
|
||||
String text = this.properties.getProperty((String) key);
|
||||
String value = SystemPropertyUtils.resolvePlaceholders(this.properties, text);
|
||||
if (value != null) {
|
||||
this.properties.put(key, value);
|
||||
}
|
||||
}
|
||||
if ("true".equals(getProperty(SET_SYSTEM_PROPERTIES))) {
|
||||
debug("Adding resolved properties to System properties");
|
||||
for (Object key : Collections.list(this.properties.propertyNames())) {
|
||||
String value = this.properties.getProperty((String) key);
|
||||
System.setProperty((String) key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,13 +91,9 @@ public class ExplodedArchive implements Archive {
|
||||
@Override
|
||||
public Manifest getManifest() throws IOException {
|
||||
if (this.manifest == null && this.manifestFile.exists()) {
|
||||
FileInputStream inputStream = new FileInputStream(this.manifestFile);
|
||||
try {
|
||||
try (FileInputStream inputStream = new FileInputStream(this.manifestFile)) {
|
||||
this.manifest = new Manifest(inputStream);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
return this.manifest;
|
||||
}
|
||||
|
||||
@@ -145,23 +145,15 @@ public class JarFileArchive implements Archive {
|
||||
}
|
||||
|
||||
private void unpack(JarEntry entry, File file) throws IOException {
|
||||
InputStream inputStream = this.jarFile.getInputStream(entry, ResourceAccess.ONCE);
|
||||
try {
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
try {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
outputStream.flush();
|
||||
try (InputStream inputStream = this.jarFile.getInputStream(entry,
|
||||
ResourceAccess.ONCE);
|
||||
OutputStream outputStream = new FileOutputStream(file)) {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
outputStream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -35,13 +35,9 @@ final class Bytes {
|
||||
}
|
||||
|
||||
public static byte[] get(RandomAccessData data) throws IOException {
|
||||
InputStream inputStream = data.getInputStream(ResourceAccess.ONCE);
|
||||
try {
|
||||
try (InputStream inputStream = data.getInputStream(ResourceAccess.ONCE)) {
|
||||
return get(inputStream, data.getSize());
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] get(InputStream inputStream, long length) throws IOException {
|
||||
|
||||
@@ -160,17 +160,13 @@ public class JarFile extends java.util.jar.JarFile {
|
||||
manifest = new JarFile(this.getRootJarFile()).getManifest();
|
||||
}
|
||||
else {
|
||||
InputStream inputStream = getInputStream(MANIFEST_NAME,
|
||||
ResourceAccess.ONCE);
|
||||
if (inputStream == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
try (InputStream inputStream = getInputStream(MANIFEST_NAME,
|
||||
ResourceAccess.ONCE)) {
|
||||
if (inputStream == null) {
|
||||
return null;
|
||||
}
|
||||
manifest = new Manifest(inputStream);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
this.manifest = new SoftReference<>(manifest);
|
||||
}
|
||||
@@ -335,9 +331,8 @@ public class JarFile extends java.util.jar.JarFile {
|
||||
// Fallback to JarInputStream to obtain certificates, not fast but hopefully not
|
||||
// happening that often.
|
||||
try {
|
||||
JarInputStream inputStream = new JarInputStream(
|
||||
getData().getInputStream(ResourceAccess.ONCE));
|
||||
try {
|
||||
try (JarInputStream inputStream = new JarInputStream(
|
||||
getData().getInputStream(ResourceAccess.ONCE))) {
|
||||
java.util.jar.JarEntry certEntry = inputStream.getNextJarEntry();
|
||||
while (certEntry != null) {
|
||||
inputStream.closeEntry();
|
||||
@@ -348,9 +343,6 @@ public class JarFile extends java.util.jar.JarFile {
|
||||
certEntry = inputStream.getNextJarEntry();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -40,8 +40,7 @@ public abstract class TestJarCreator {
|
||||
|
||||
public static void createTestJar(File file, boolean unpackNested) throws Exception {
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream);
|
||||
try {
|
||||
try (JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream)) {
|
||||
writeManifest(jarOutputStream, "j1");
|
||||
writeEntry(jarOutputStream, "1.dat", 1);
|
||||
writeEntry(jarOutputStream, "2.dat", 2);
|
||||
@@ -53,9 +52,6 @@ public abstract class TestJarCreator {
|
||||
writeNestedEntry("nested.jar", unpackNested, jarOutputStream);
|
||||
writeNestedEntry("another-nested.jar", unpackNested, jarOutputStream);
|
||||
}
|
||||
finally {
|
||||
jarOutputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeNestedEntry(String name, boolean unpackNested,
|
||||
|
||||
@@ -205,21 +205,18 @@ public class StartMojo extends AbstractRunMojo {
|
||||
throws IOException, MojoFailureException, MojoExecutionException {
|
||||
try {
|
||||
getLog().debug("Connecting to local MBeanServer at port " + this.jmxPort);
|
||||
JMXConnector connector = execute(this.wait, this.maxAttempts,
|
||||
new CreateJmxConnector(this.jmxPort));
|
||||
if (connector == null) {
|
||||
throw new MojoExecutionException(
|
||||
"JMX MBean server was not reachable before the configured "
|
||||
+ "timeout (" + (this.wait * this.maxAttempts) + "ms");
|
||||
}
|
||||
getLog().debug("Connected to local MBeanServer at port " + this.jmxPort);
|
||||
try {
|
||||
try (JMXConnector connector = execute(this.wait, this.maxAttempts,
|
||||
new CreateJmxConnector(this.jmxPort))) {
|
||||
if (connector == null) {
|
||||
throw new MojoExecutionException(
|
||||
"JMX MBean server was not reachable before the configured "
|
||||
+ "timeout (" + (this.wait * this.maxAttempts)
|
||||
+ "ms");
|
||||
}
|
||||
getLog().debug("Connected to local MBeanServer at port " + this.jmxPort);
|
||||
MBeanServerConnection connection = connector.getMBeanServerConnection();
|
||||
doWaitForSpringApplication(connection);
|
||||
}
|
||||
finally {
|
||||
connector.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw ex;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
@@ -110,14 +110,11 @@ public class StopMojo extends AbstractMojo {
|
||||
|
||||
private void stopForkedProcess()
|
||||
throws IOException, MojoFailureException, MojoExecutionException {
|
||||
JMXConnector connector = SpringApplicationAdminClient.connect(this.jmxPort);
|
||||
try {
|
||||
try (JMXConnector connector = SpringApplicationAdminClient
|
||||
.connect(this.jmxPort)) {
|
||||
MBeanServerConnection connection = connector.getMBeanServerConnection();
|
||||
doStop(connection);
|
||||
}
|
||||
finally {
|
||||
connector.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void stop() throws IOException, MojoFailureException, MojoExecutionException {
|
||||
|
||||
@@ -198,14 +198,10 @@ public final class Verify {
|
||||
.startsWith(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 }));
|
||||
}
|
||||
|
||||
ZipFile zipFile = new ZipFile(this.file);
|
||||
try {
|
||||
try (ZipFile zipFile = new ZipFile(this.file)) {
|
||||
ArchiveVerifier verifier = new ArchiveVerifier(zipFile);
|
||||
verifyZipEntries(verifier);
|
||||
}
|
||||
finally {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected void verifyZipEntries(ArchiveVerifier verifier) throws Exception {
|
||||
|
||||
@@ -125,14 +125,10 @@ public class ModifiedClassPathRunner extends BlockJUnit4ClassRunner {
|
||||
}
|
||||
|
||||
private String[] getClassPath(URL booterJar) throws Exception {
|
||||
JarFile jarFile = new JarFile(new File(booterJar.toURI()));
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(new File(booterJar.toURI()))) {
|
||||
return StringUtils.delimitedListToStringArray(jarFile.getManifest()
|
||||
.getMainAttributes().getValue(Attributes.Name.CLASS_PATH), " ");
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
private URL[] processUrls(URL[] urls, Class<?> testClass) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user