forked from utPLSQL/utPLSQL-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReporterManager.java
More file actions
176 lines (143 loc) · 5.99 KB
/
ReporterManager.java
File metadata and controls
176 lines (143 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.utplsql.cli;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.reporter.CoreReporters;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.config.ReporterConfig;
import org.utplsql.cli.reporters.ReporterOptionsAware;
import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
class ReporterManager {
private final List<ReporterOptions> reporterOptionsList;
private List<Exception> reporterGatherErrors;
private ExecutorService executorService;
ReporterManager(ReporterConfig[] reporterConfigs) {
reporterOptionsList = new ArrayList<>();
if (reporterConfigs != null && reporterConfigs.length > 0) {
loadOptionsFromConfigs(reporterConfigs);
} else {
reporterOptionsList.add(getDefaultReporterOption());
}
}
private void loadOptionsFromConfigs(ReporterConfig[] reporterConfigs) {
boolean printToScreen = false;
for (ReporterConfig reporterConfig : reporterConfigs) {
ReporterOptions option = new ReporterOptions(
reporterConfig.getName(),
reporterConfig.getOutput());
option.forceOutputToScreen(reporterConfig.isForceToScreen());
reporterOptionsList.add(option);
// Check printToScreen validity
if (option.outputToScreen() && printToScreen) {
throw new IllegalArgumentException("You cannot configure more than one reporter to output to screen");
}
printToScreen = option.outputToScreen();
}
}
private ReporterOptions getDefaultReporterOption() {
return new ReporterOptions(CoreReporters.UT_DOCUMENTATION_REPORTER.name());
}
private void abortGathering(Exception e) {
addGatherError(e);
executorService.shutdownNow();
}
private void addGatherError(Exception e) {
if (reporterGatherErrors == null) {
reporterGatherErrors = new ArrayList<>();
}
reporterGatherErrors.add(e);
}
boolean haveGatherErrorsOccured() {
return reporterGatherErrors != null && !reporterGatherErrors.isEmpty();
}
List<Exception> getGatherErrors() {
return reporterGatherErrors;
}
/**
* Initializes the reporters so we can use the id to gather results
*
* @param conn Active Connection
* @return List of Reporters
* @throws SQLException
*/
List<Reporter> initReporters(Connection conn, ReporterFactory reporterFactory, CompatibilityProxy compatibilityProxy) throws SQLException {
final List<Reporter> reporterList = new ArrayList<>();
for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = reporterFactory.createReporter(ro.getReporterName());
if (reporter instanceof ReporterOptionsAware) {
((ReporterOptionsAware) reporter).setReporterOptions(ro);
}
reporter.init(conn, compatibilityProxy, reporterFactory);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}
return reporterList;
}
/**
* Starts a separate thread for each Reporter to gather its results
*
* @param executorService
* @param dataSource
*/
void startReporterGatherers(ExecutorService executorService, final DataSource dataSource) {
if (this.executorService != null && !this.executorService.isShutdown()) {
throw new IllegalStateException("There is already a running executor service!");
}
this.executorService = executorService;
reporterOptionsList.forEach((reporterOption) -> executorService.submit(
new GatherReporterOutputTask(dataSource, reporterOption, this::abortGathering)
));
}
List<ReporterOptions> getReporterOptionsList() {
return reporterOptionsList;
}
int getNumberOfReporters() {
return reporterOptionsList.size();
}
/**
* Gathers Reporter Output based on ReporterOptions and prints it to a file, System.out or both
*/
private static class GatherReporterOutputTask implements Runnable {
private final DataSource dataSource;
private final ReporterOptions option;
private final Consumer<Exception> abortFunction;
GatherReporterOutputTask(DataSource dataSource, ReporterOptions reporterOption, Consumer<Exception> abortFunction) {
if (reporterOption.getReporterObj() == null) {
throw new IllegalArgumentException("Reporter " + reporterOption.getReporterName() + " is not initialized");
}
this.dataSource = dataSource;
this.option = reporterOption;
this.abortFunction = abortFunction;
}
@Override
public void run() {
List<PrintStream> printStreams = new ArrayList<>();
PrintStream fileOutStream = null;
try (Connection conn = dataSource.getConnection()) {
if (option.outputToScreen()) {
printStreams.add(System.out);
option.getReporterObj().getOutputBuffer().setFetchSize(1);
}
if (option.outputToFile()) {
fileOutStream = new PrintStream(new FileOutputStream(option.getOutputFileName()));
printStreams.add(fileOutStream);
}
option.getReporterObj().getOutputBuffer().printAvailable(conn, printStreams);
} catch (SQLException | FileNotFoundException e) {
abortFunction.accept(e);
} finally {
if (fileOutStream != null) {
fileOutStream.close();
}
}
}
}
}