|
16 | 16 | import java.util.ArrayList;
|
17 | 17 | import java.util.List;
|
18 | 18 | import java.util.concurrent.ExecutorService;
|
| 19 | +import java.util.function.Consumer; |
19 | 20 |
|
20 | 21 | class ReporterManager {
|
21 | 22 |
|
@@ -111,38 +112,58 @@ void startReporterGatherers(ExecutorService executorService, final DataSource da
|
111 | 112 |
|
112 | 113 | this.executorService = executorService;
|
113 | 114 |
|
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 | + )); |
141 | 118 | }
|
142 | 119 |
|
143 | 120 | List<ReporterOptions> getReporterOptionsList() {
|
144 | 121 | return reporterOptionsList;
|
145 | 122 | }
|
146 | 123 |
|
147 | 124 | 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 | + } |
148 | 169 | }
|
0 commit comments