Skip to content

Commit e443e3c

Browse files
committed
Refactor: Cleanup ReporterManager a bit
1 parent 77fd752 commit e443e3c

File tree

1 file changed

+48
-27
lines changed

1 file changed

+48
-27
lines changed

src/main/java/org/utplsql/cli/ReporterManager.java

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818
import java.util.concurrent.ExecutorService;
19+
import java.util.function.Consumer;
1920

2021
class ReporterManager {
2122

@@ -111,38 +112,58 @@ void startReporterGatherers(ExecutorService executorService, final DataSource da
111112

112113
this.executorService = executorService;
113114

114-
// TODO: Implement Init-check
115-
// Gather each reporter results on a separate thread.
116-
for (ReporterOptions ro : reporterOptionsList) {
117-
executorService.submit(() -> {
118-
List<PrintStream> printStreams = new ArrayList<>();
119-
PrintStream fileOutStream = null;
120-
121-
try (Connection conn = dataSource.getConnection()) {
122-
if (ro.outputToScreen()) {
123-
printStreams.add(System.out);
124-
ro.getReporterObj().getOutputBuffer().setFetchSize(1);
125-
}
126-
127-
if (ro.outputToFile()) {
128-
fileOutStream = new PrintStream(new FileOutputStream(ro.getOutputFileName()));
129-
printStreams.add(fileOutStream);
130-
}
131-
132-
ro.getReporterObj().getOutputBuffer().printAvailable(conn, printStreams);
133-
} catch (SQLException | FileNotFoundException e) {
134-
abortGathering(e);
135-
} finally {
136-
if (fileOutStream != null)
137-
fileOutStream.close();
138-
}
139-
});
140-
}
115+
reporterOptionsList.forEach((reporterOption) -> executorService.submit(
116+
new GatherReporterOutputTask(dataSource, reporterOption, this::abortGathering)
117+
));
141118
}
142119

143120
List<ReporterOptions> getReporterOptionsList() {
144121
return reporterOptionsList;
145122
}
146123

147124
int getNumberOfReporters() { return reporterOptionsList.size(); }
125+
126+
/** Gathers Reporter Output based on ReporterOptions and prints it to a file, System.out or both
127+
*/
128+
private static class GatherReporterOutputTask implements Runnable {
129+
130+
private DataSource dataSource;
131+
private ReporterOptions option;
132+
private Consumer<Throwable> abortFunction;
133+
134+
GatherReporterOutputTask( DataSource dataSource, ReporterOptions reporterOption, Consumer<Throwable> abortFunction ) {
135+
136+
if ( reporterOption.getReporterObj() == null )
137+
throw new IllegalArgumentException("Reporter " + reporterOption.getReporterName() + " is not initialized");
138+
139+
this.dataSource = dataSource;
140+
this.option = reporterOption;
141+
this.abortFunction = abortFunction;
142+
}
143+
144+
@Override
145+
public void run() {
146+
List<PrintStream> printStreams = new ArrayList<>();
147+
PrintStream fileOutStream = null;
148+
149+
try (Connection conn = dataSource.getConnection()) {
150+
if (option.outputToScreen()) {
151+
printStreams.add(System.out);
152+
option.getReporterObj().getOutputBuffer().setFetchSize(1);
153+
}
154+
155+
if (option.outputToFile()) {
156+
fileOutStream = new PrintStream(new FileOutputStream(option.getOutputFileName()));
157+
printStreams.add(fileOutStream);
158+
}
159+
160+
option.getReporterObj().getOutputBuffer().printAvailable(conn, printStreams);
161+
} catch (SQLException | FileNotFoundException e) {
162+
abortFunction.accept(e);
163+
} finally {
164+
if (fileOutStream != null)
165+
fileOutStream.close();
166+
}
167+
}
168+
}
148169
}

0 commit comments

Comments
 (0)