Skip to content

Commit

Permalink
Merge pull request #307 from hazendaz/master
Browse files Browse the repository at this point in the history
Another set of propery closing resources
  • Loading branch information
hazendaz committed May 7, 2023
2 parents 114baa0 + 3c965b4 commit 28abe61
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
os: [ubuntu-latest, macOS-latest, windows-latest]
java: [11, 17, 20, 21-ea]
distribution: ['zulu']
fail-fast: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public void executeSql(Reader reader) {
* SQL to execute.
*/
public void executeSql(String sql) {
executeSql(new StringReader(sql));
try (StringReader reader = new StringReader(sql)) {
executeSql(reader);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public void execute(Map<String, Object> bindingMap) {
bindings.putAll(bindingMap);
try {
printStream.println(Util.horizontalLine("Applying JSR-223 hook : " + scriptFile.getName(), 80));
engine.eval(new InputStreamReader(new FileInputStream(scriptFile), Charset.forName(charset)));
try (
InputStreamReader stream = new InputStreamReader(new FileInputStream(scriptFile), Charset.forName(charset))) {
engine.eval(stream);
}
if (functionName != null || objectName != null && methodName != null) {
Invocable invocable = (Invocable) engine;
if (functionName != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public void execute(Map<String, Object> bindingMap) {
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
context.executeSql(new StringReader(replacer.replace(outputStream.toString(charset))));
try (StringReader reader = new StringReader(replacer.replace(outputStream.toString(charset)))) {
context.executeSql(reader);
}
} catch (IOException e) {
throw new MigrationException("Error occurred while running SQL hook script.", e);
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/apache/ibatis/migration/io/Resources.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -39,9 +39,6 @@ public class Resources {
*/
private static Charset charset;

Resources() {
}

/**
* Returns the default classloader (may be null).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.apache.ibatis.migration.operations;

import java.io.PrintStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
Expand Down Expand Up @@ -82,7 +83,9 @@ public DownOperation operate(ConnectionProvider connectionProvider, MigrationLoa
hook.beforeEach(hookBindings);
}
println(printStream, Util.horizontalLine("Undoing: " + change.getFilename(), 80));
runner.runScript(migrationsLoader.getScriptReader(change, true));
try (Reader reader = migrationsLoader.getScriptReader(change, true)) {
runner.runScript(reader);
}
if (changelogExists(con, option)) {
deleteChange(con, change, option);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -53,7 +53,6 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration
int stepCount = 0;
Map<String, Object> hookBindings = new HashMap<>();
println(printStream, "WARNING: Running pending migrations out of order can create unexpected results.");
Reader scriptReader = null;
try {
ScriptRunner runner = getScriptRunner(con, option, printStream);
for (Change change : pending) {
Expand All @@ -67,8 +66,9 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration
hook.beforeEach(hookBindings);
}
println(printStream, Util.horizontalLine("Applying: " + change.getFilename(), 80));
scriptReader = migrationsLoader.getScriptReader(change, false);
runner.runScript(scriptReader);
try (Reader scriptReader = migrationsLoader.getScriptReader(change, false)) {
runner.runScript(scriptReader);
}
insertChangelog(change, con, option);
println(printStream);
if (hook != null) {
Expand All @@ -85,10 +85,6 @@ public PendingOperation operate(ConnectionProvider connectionProvider, Migration
return this;
} catch (Exception e) {
throw new MigrationException("Error executing command. Cause: " + e, e);
} finally {
if (scriptReader != null) {
scriptReader.close();
}
}
} catch (Throwable e) {
while (e instanceof MigrationException && e.getCause() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public void runScript(Reader reader) {

private void executeFullScript(Reader reader) {
StringBuilder script = new StringBuilder();
try {
BufferedReader lineReader = new BufferedReader(reader);
try (BufferedReader lineReader = new BufferedReader(reader)) {
String line;
while ((line = lineReader.readLine()) != null) {
script.append(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,19 +475,17 @@ void shouldRetainLineBreakAfterDelimiter() throws Exception {
// @formatter:on
}

// Closed by callers
private String readAsString(Reader reader) throws IOException {
try {
StringBuilder buffer = new StringBuilder();
int res;
while ((res = reader.read()) != -1) {
buffer.append((char) res);
}
return buffer.toString();
} finally {
reader.close();
StringBuilder buffer = new StringBuilder();
int res;
while ((res = reader.read()) != -1) {
buffer.append((char) res);
}
return buffer.toString();
}

// Closed by callers
private InputStream strToInputStream(String str) {
return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
}
Expand Down

0 comments on commit 28abe61

Please sign in to comment.