franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@16
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@16
|
8 |
* (at your option) any later version.
|
franta-hg@16
|
9 |
*
|
franta-hg@16
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
13 |
* GNU General Public License for more details.
|
franta-hg@16
|
14 |
*
|
franta-hg@16
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
17 |
*/
|
franta-hg@14
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@14
|
19 |
|
franta-hg@67
|
20 |
import info.globalcode.sql.dk.configuration.Configuration;
|
franta-hg@65
|
21 |
import info.globalcode.sql.dk.configuration.ConfigurationException;
|
franta-hg@26
|
22 |
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
|
franta-hg@65
|
23 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@67
|
24 |
import info.globalcode.sql.dk.configuration.FormatterDefinition;
|
franta-hg@160
|
25 |
import info.globalcode.sql.dk.configuration.Properties;
|
franta-hg@159
|
26 |
import info.globalcode.sql.dk.configuration.Property;
|
franta-hg@69
|
27 |
import info.globalcode.sql.dk.formatting.ColumnsHeader;
|
franta-hg@159
|
28 |
import info.globalcode.sql.dk.formatting.FakeSqlArray;
|
franta-hg@69
|
29 |
import info.globalcode.sql.dk.formatting.Formatter;
|
franta-hg@69
|
30 |
import info.globalcode.sql.dk.formatting.FormatterContext;
|
franta-hg@69
|
31 |
import info.globalcode.sql.dk.formatting.FormatterException;
|
franta-hg@17
|
32 |
import java.io.BufferedReader;
|
franta-hg@160
|
33 |
import java.io.ByteArrayOutputStream;
|
franta-hg@17
|
34 |
import java.io.InputStreamReader;
|
franta-hg@14
|
35 |
import java.io.PrintStream;
|
franta-hg@159
|
36 |
import java.sql.Array;
|
franta-hg@158
|
37 |
import java.sql.Driver;
|
franta-hg@159
|
38 |
import java.sql.DriverPropertyInfo;
|
franta-hg@65
|
39 |
import java.sql.SQLException;
|
franta-hg@70
|
40 |
import java.util.ArrayList;
|
franta-hg@70
|
41 |
import java.util.EnumSet;
|
franta-hg@159
|
42 |
import java.util.HashSet;
|
franta-hg@66
|
43 |
import java.util.List;
|
franta-hg@158
|
44 |
import java.util.ServiceLoader;
|
franta-hg@159
|
45 |
import java.util.Set;
|
franta-hg@183
|
46 |
import java.util.concurrent.ExecutorService;
|
franta-hg@183
|
47 |
import java.util.concurrent.Executors;
|
franta-hg@183
|
48 |
import java.util.concurrent.TimeUnit;
|
franta-hg@17
|
49 |
import java.util.logging.Level;
|
franta-hg@17
|
50 |
import java.util.logging.Logger;
|
franta-hg@69
|
51 |
import javax.sql.rowset.RowSetMetaDataImpl;
|
franta-hg@14
|
52 |
|
franta-hg@14
|
53 |
/**
|
franta-hg@14
|
54 |
* Displays info like help, version etc.
|
franta-hg@14
|
55 |
*
|
franta-hg@14
|
56 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@14
|
57 |
*/
|
franta-hg@14
|
58 |
public class InfoLister {
|
franta-hg@14
|
59 |
|
franta-hg@17
|
60 |
private static final Logger log = Logger.getLogger(InfoLister.class.getName());
|
franta-hg@159
|
61 |
/**
|
franta-hg@159
|
62 |
* Fake database name for output formatting
|
franta-hg@159
|
63 |
*/
|
franta-hg@159
|
64 |
public static final String CONFIG_DB_NAME = "sqldk_configuration";
|
franta-hg@183
|
65 |
private final PrintStream out;
|
franta-hg@183
|
66 |
private final ConfigurationProvider configurationProvider;
|
franta-hg@183
|
67 |
private final CLIOptions options;
|
franta-hg@70
|
68 |
private Formatter formatter;
|
franta-hg@17
|
69 |
|
franta-hg@69
|
70 |
public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
|
franta-hg@17
|
71 |
this.out = out;
|
franta-hg@20
|
72 |
this.configurationProvider = configurationProvider;
|
franta-hg@69
|
73 |
this.options = options;
|
franta-hg@17
|
74 |
}
|
franta-hg@17
|
75 |
|
franta-hg@70
|
76 |
public void showInfo() throws ConfigurationException, FormatterException {
|
franta-hg@70
|
77 |
EnumSet<InfoType> commands = options.getShowInfo();
|
franta-hg@70
|
78 |
|
franta-hg@139
|
79 |
boolean formattinNeeded = false;
|
franta-hg@139
|
80 |
|
franta-hg@70
|
81 |
for (InfoType infoType : commands) {
|
franta-hg@70
|
82 |
switch (infoType) {
|
franta-hg@70
|
83 |
case CONNECTION:
|
franta-hg@158
|
84 |
case JDBC_DRIVERS:
|
franta-hg@159
|
85 |
case JDBC_PROPERTIES:
|
franta-hg@70
|
86 |
case DATABASES:
|
franta-hg@70
|
87 |
case FORMATTERS:
|
franta-hg@70
|
88 |
case TYPES:
|
franta-hg@139
|
89 |
formattinNeeded = true;
|
franta-hg@101
|
90 |
break;
|
franta-hg@70
|
91 |
}
|
franta-hg@70
|
92 |
}
|
franta-hg@139
|
93 |
|
franta-hg@139
|
94 |
if (formattinNeeded) {
|
franta-hg@139
|
95 |
try (Formatter f = getFormatter()) {
|
franta-hg@139
|
96 |
formatter = f;
|
franta-hg@139
|
97 |
formatter.writeStartBatch();
|
franta-hg@159
|
98 |
DatabaseDefinition dd = new DatabaseDefinition();
|
franta-hg@159
|
99 |
dd.setName(CONFIG_DB_NAME);
|
franta-hg@159
|
100 |
formatter.writeStartDatabase(dd);
|
franta-hg@139
|
101 |
showInfos(commands);
|
franta-hg@139
|
102 |
formatter.writeEndDatabase();
|
franta-hg@139
|
103 |
formatter.writeEndBatch();
|
franta-hg@139
|
104 |
formatter.close();
|
franta-hg@139
|
105 |
}
|
franta-hg@139
|
106 |
} else {
|
franta-hg@139
|
107 |
showInfos(commands);
|
franta-hg@139
|
108 |
}
|
franta-hg@101
|
109 |
}
|
franta-hg@70
|
110 |
|
franta-hg@101
|
111 |
private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
|
franta-hg@70
|
112 |
for (InfoType infoType : commands) {
|
franta-hg@70
|
113 |
infoType.showInfo(this);
|
franta-hg@70
|
114 |
}
|
franta-hg@70
|
115 |
}
|
franta-hg@70
|
116 |
|
franta-hg@72
|
117 |
private void listFormatters() throws ConfigurationException, FormatterException {
|
franta-hg@72
|
118 |
ColumnsHeader header = constructHeader(
|
franta-hg@72
|
119 |
new HeaderField("name", SQLType.VARCHAR),
|
franta-hg@72
|
120 |
new HeaderField("built_in", SQLType.BOOLEAN),
|
franta-hg@72
|
121 |
new HeaderField("default", SQLType.BOOLEAN),
|
franta-hg@160
|
122 |
new HeaderField("class_name", SQLType.VARCHAR),
|
franta-hg@160
|
123 |
new HeaderField("valid", SQLType.BOOLEAN));
|
franta-hg@72
|
124 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@72
|
125 |
|
franta-hg@72
|
126 |
String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
|
franta-hg@72
|
127 |
defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
|
franta-hg@72
|
128 |
|
franta-hg@69
|
129 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
|
franta-hg@160
|
130 |
data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
|
franta-hg@69
|
131 |
}
|
franta-hg@72
|
132 |
|
franta-hg@72
|
133 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
|
franta-hg@160
|
134 |
data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
|
franta-hg@69
|
135 |
}
|
franta-hg@72
|
136 |
|
franta-hg@183
|
137 |
printTable(formatter, header, "-- configured and built-in output formatters", null, data);
|
franta-hg@160
|
138 |
}
|
franta-hg@72
|
139 |
|
franta-hg@160
|
140 |
private boolean isInstantiable(FormatterDefinition fd) {
|
franta-hg@160
|
141 |
try {
|
franta-hg@160
|
142 |
try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
|
franta-hg@160
|
143 |
fd.getInstance(new FormatterContext(testStream, new Properties(0)));
|
franta-hg@160
|
144 |
return true;
|
franta-hg@160
|
145 |
}
|
franta-hg@160
|
146 |
} catch (Exception e) {
|
franta-hg@160
|
147 |
log.log(Level.SEVERE, "Unable to create an instance of formatter: " + fd.getName(), e);
|
franta-hg@160
|
148 |
return false;
|
franta-hg@160
|
149 |
}
|
franta-hg@69
|
150 |
}
|
franta-hg@67
|
151 |
|
franta-hg@70
|
152 |
public void listTypes() throws FormatterException, ConfigurationException {
|
franta-hg@70
|
153 |
ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
|
franta-hg@70
|
154 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@70
|
155 |
for (SQLType sqlType : SQLType.values()) {
|
franta-hg@70
|
156 |
data.add(new Object[]{sqlType.name(), sqlType.getCode()});
|
franta-hg@70
|
157 |
}
|
franta-hg@183
|
158 |
printTable(formatter, header, "-- data types", null, data);
|
franta-hg@93
|
159 |
log.log(Level.INFO, "Type names in --types option are case insensitive");
|
franta-hg@69
|
160 |
}
|
franta-hg@67
|
161 |
|
franta-hg@72
|
162 |
public void listDatabases() throws ConfigurationException, FormatterException {
|
franta-hg@72
|
163 |
ColumnsHeader header = constructHeader(
|
franta-hg@72
|
164 |
new HeaderField("database_name", SQLType.VARCHAR),
|
franta-hg@72
|
165 |
new HeaderField("user_name", SQLType.VARCHAR),
|
franta-hg@72
|
166 |
new HeaderField("database_url", SQLType.VARCHAR));
|
franta-hg@72
|
167 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@72
|
168 |
|
franta-hg@69
|
169 |
final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
|
franta-hg@69
|
170 |
if (configuredDatabases.isEmpty()) {
|
franta-hg@69
|
171 |
log.log(Level.WARNING, "No databases are configured.");
|
franta-hg@69
|
172 |
} else {
|
franta-hg@69
|
173 |
for (DatabaseDefinition dd : configuredDatabases) {
|
franta-hg@72
|
174 |
data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
|
franta-hg@14
|
175 |
}
|
franta-hg@14
|
176 |
}
|
franta-hg@72
|
177 |
|
franta-hg@183
|
178 |
printTable(formatter, header, "-- configured databases", null, data);
|
franta-hg@14
|
179 |
}
|
franta-hg@17
|
180 |
|
franta-hg@158
|
181 |
public void listJdbcDrivers() throws FormatterException, ConfigurationException {
|
franta-hg@158
|
182 |
ColumnsHeader header = constructHeader(
|
franta-hg@158
|
183 |
new HeaderField("class", SQLType.VARCHAR),
|
franta-hg@158
|
184 |
new HeaderField("version", SQLType.VARCHAR),
|
franta-hg@158
|
185 |
new HeaderField("major", SQLType.INTEGER),
|
franta-hg@158
|
186 |
new HeaderField("minor", SQLType.INTEGER),
|
franta-hg@158
|
187 |
new HeaderField("jdbc_compliant", SQLType.BOOLEAN));
|
franta-hg@158
|
188 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@158
|
189 |
|
franta-hg@158
|
190 |
final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
|
franta-hg@158
|
191 |
for (Driver d : drivers) {
|
franta-hg@158
|
192 |
data.add(new Object[]{
|
franta-hg@158
|
193 |
d.getClass().getName(),
|
franta-hg@158
|
194 |
d.getMajorVersion() + "." + d.getMinorVersion(),
|
franta-hg@158
|
195 |
d.getMajorVersion(),
|
franta-hg@158
|
196 |
d.getMinorVersion(),
|
franta-hg@159
|
197 |
d.jdbcCompliant()
|
franta-hg@159
|
198 |
});
|
franta-hg@158
|
199 |
}
|
franta-hg@158
|
200 |
|
franta-hg@183
|
201 |
printTable(formatter, header, "-- discovered JDBC drivers (available on the CLASSPATH)", null, data);
|
franta-hg@159
|
202 |
}
|
franta-hg@158
|
203 |
|
franta-hg@159
|
204 |
public void listJdbcProperties() throws FormatterException, ConfigurationException {
|
franta-hg@159
|
205 |
for (String dbName : options.getDatabaseNamesToListProperties()) {
|
franta-hg@159
|
206 |
ColumnsHeader header = constructHeader(
|
franta-hg@159
|
207 |
new HeaderField("property_name", SQLType.VARCHAR),
|
franta-hg@159
|
208 |
new HeaderField("required", SQLType.BOOLEAN),
|
franta-hg@159
|
209 |
new HeaderField("choices", SQLType.ARRAY),
|
franta-hg@159
|
210 |
new HeaderField("configured_value", SQLType.VARCHAR),
|
franta-hg@159
|
211 |
new HeaderField("description", SQLType.VARCHAR));
|
franta-hg@159
|
212 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@159
|
213 |
|
franta-hg@159
|
214 |
DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
|
franta-hg@159
|
215 |
|
franta-hg@159
|
216 |
Driver driver = findDriver(dd);
|
franta-hg@159
|
217 |
|
franta-hg@159
|
218 |
if (driver == null) {
|
franta-hg@159
|
219 |
log.log(Level.WARNING, "No JDBC driver was found for DB: {0} with URL: {1}", new Object[]{dd.getName(), dd.getUrl()});
|
franta-hg@159
|
220 |
} else {
|
franta-hg@159
|
221 |
log.log(Level.INFO, "For DB: {0} was found JDBC driver: {1}", new Object[]{dd.getName(), driver.getClass().getName()});
|
franta-hg@159
|
222 |
|
franta-hg@159
|
223 |
try {
|
franta-hg@159
|
224 |
DriverPropertyInfo[] propertyInfos = driver.getPropertyInfo(dd.getUrl(), dd.getProperties().getJavaProperties());
|
franta-hg@159
|
225 |
|
franta-hg@159
|
226 |
Set<String> standardProperties = new HashSet<>();
|
franta-hg@159
|
227 |
|
franta-hg@159
|
228 |
for (DriverPropertyInfo pi : propertyInfos) {
|
franta-hg@159
|
229 |
Array choices = new FakeSqlArray(pi.choices, SQLType.VARCHAR);
|
franta-hg@159
|
230 |
data.add(new Object[]{
|
franta-hg@159
|
231 |
pi.name,
|
franta-hg@159
|
232 |
pi.required,
|
franta-hg@159
|
233 |
choices.getArray() == null ? "" : choices,
|
franta-hg@159
|
234 |
pi.value == null ? "" : pi.value,
|
franta-hg@159
|
235 |
pi.description
|
franta-hg@159
|
236 |
});
|
franta-hg@159
|
237 |
standardProperties.add(pi.name);
|
franta-hg@159
|
238 |
}
|
franta-hg@159
|
239 |
|
franta-hg@159
|
240 |
for (Property p : dd.getProperties()) {
|
franta-hg@159
|
241 |
if (!standardProperties.contains(p.getName())) {
|
franta-hg@159
|
242 |
data.add(new Object[]{
|
franta-hg@159
|
243 |
p.getName(),
|
franta-hg@159
|
244 |
"",
|
franta-hg@159
|
245 |
"",
|
franta-hg@159
|
246 |
p.getValue(),
|
franta-hg@159
|
247 |
""
|
franta-hg@159
|
248 |
});
|
franta-hg@159
|
249 |
log.log(Level.WARNING, "Your configuration contains property „{0}“ not declared by the JDBC driver.", p.getName());
|
franta-hg@159
|
250 |
}
|
franta-hg@159
|
251 |
}
|
franta-hg@159
|
252 |
|
franta-hg@159
|
253 |
} catch (SQLException e) {
|
franta-hg@159
|
254 |
log.log(Level.WARNING, "Error during getting property infos.", e);
|
franta-hg@159
|
255 |
}
|
franta-hg@159
|
256 |
|
franta-hg@159
|
257 |
List<Parameter> parameters = new ArrayList<>();
|
franta-hg@159
|
258 |
parameters.add(new NamedParameter("databgase", dbName, SQLType.VARCHAR));
|
franta-hg@159
|
259 |
parameters.add(new NamedParameter("driver_class", driver.getClass().getName(), SQLType.VARCHAR));
|
franta-hg@159
|
260 |
parameters.add(new NamedParameter("driver_major_version", driver.getMajorVersion(), SQLType.INTEGER));
|
franta-hg@159
|
261 |
parameters.add(new NamedParameter("driver_minor_version", driver.getMinorVersion(), SQLType.INTEGER));
|
franta-hg@159
|
262 |
|
franta-hg@183
|
263 |
printTable(formatter, header, "-- configured and configurable JDBC driver properties", parameters, data);
|
franta-hg@159
|
264 |
}
|
franta-hg@159
|
265 |
}
|
franta-hg@159
|
266 |
|
franta-hg@159
|
267 |
}
|
franta-hg@159
|
268 |
|
franta-hg@159
|
269 |
private Driver findDriver(DatabaseDefinition dd) {
|
franta-hg@159
|
270 |
final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
|
franta-hg@159
|
271 |
for (Driver d : drivers) {
|
franta-hg@159
|
272 |
try {
|
franta-hg@159
|
273 |
if (d.acceptsURL(dd.getUrl())) {
|
franta-hg@159
|
274 |
return d;
|
franta-hg@159
|
275 |
}
|
franta-hg@159
|
276 |
} catch (SQLException e) {
|
franta-hg@159
|
277 |
log.log(Level.WARNING, "Error during finding JDBC driver for: " + dd.getName(), e);
|
franta-hg@159
|
278 |
}
|
franta-hg@159
|
279 |
}
|
franta-hg@159
|
280 |
return null;
|
franta-hg@158
|
281 |
}
|
franta-hg@158
|
282 |
|
franta-hg@183
|
283 |
/**
|
franta-hg@183
|
284 |
* Parallelism for connection testing – maximum concurrent database connections.
|
franta-hg@183
|
285 |
*/
|
franta-hg@183
|
286 |
private static final int TESTING_THREAD_COUNT = 64;
|
franta-hg@183
|
287 |
/**
|
franta-hg@183
|
288 |
* Time limit for all connection testing threads – particular timeouts per connection will be
|
franta-hg@183
|
289 |
* much smaller.
|
franta-hg@183
|
290 |
*/
|
franta-hg@183
|
291 |
private static final long TESTING_AWAIT_LIMIT = 1;
|
franta-hg@183
|
292 |
private static final TimeUnit TESTING_AWAIT_UNIT = TimeUnit.DAYS;
|
franta-hg@183
|
293 |
|
franta-hg@183
|
294 |
public void testConnections() throws FormatterException, ConfigurationException {
|
franta-hg@73
|
295 |
ColumnsHeader header = constructHeader(
|
franta-hg@73
|
296 |
new HeaderField("database_name", SQLType.VARCHAR),
|
franta-hg@74
|
297 |
new HeaderField("configured", SQLType.BOOLEAN),
|
franta-hg@74
|
298 |
new HeaderField("connected", SQLType.BOOLEAN));
|
franta-hg@73
|
299 |
|
franta-hg@183
|
300 |
log.log(Level.FINE, "Testing DB connections in {0} threads", TESTING_THREAD_COUNT);
|
franta-hg@183
|
301 |
|
franta-hg@183
|
302 |
ExecutorService es = Executors.newFixedThreadPool(TESTING_THREAD_COUNT);
|
franta-hg@183
|
303 |
|
franta-hg@183
|
304 |
final Formatter currentFormatter = formatter;
|
franta-hg@183
|
305 |
|
franta-hg@183
|
306 |
printHeader(currentFormatter, header, "-- database configuration and connectivity test", null);
|
franta-hg@183
|
307 |
|
franta-hg@183
|
308 |
for (final String dbName : options.getDatabaseNamesToTest()) {
|
franta-hg@183
|
309 |
es.submit(new Runnable() {
|
franta-hg@183
|
310 |
// TODO: Java 8 – lambda
|
franta-hg@183
|
311 |
@Override
|
franta-hg@183
|
312 |
public void run() {
|
franta-hg@183
|
313 |
final Object[] row = testConnection(dbName);
|
franta-hg@183
|
314 |
synchronized (currentFormatter) {
|
franta-hg@183
|
315 |
printRow(currentFormatter, row);
|
franta-hg@183
|
316 |
}
|
franta-hg@183
|
317 |
}
|
franta-hg@183
|
318 |
});
|
franta-hg@74
|
319 |
}
|
franta-hg@73
|
320 |
|
franta-hg@183
|
321 |
es.shutdown();
|
franta-hg@183
|
322 |
|
franta-hg@183
|
323 |
try {
|
franta-hg@183
|
324 |
log.log(Level.FINEST, "Waiting for test results: {0} {1}", new Object[]{TESTING_AWAIT_LIMIT, TESTING_AWAIT_UNIT.name()});
|
franta-hg@183
|
325 |
boolean finished = es.awaitTermination(TESTING_AWAIT_LIMIT, TESTING_AWAIT_UNIT);
|
franta-hg@183
|
326 |
if (finished) {
|
franta-hg@183
|
327 |
log.log(Level.FINEST, "All testing threads finished in time limit.");
|
franta-hg@183
|
328 |
} else {
|
franta-hg@183
|
329 |
throw new FormatterException("Exceeded total time limit for test threads – this should never happen");
|
franta-hg@183
|
330 |
}
|
franta-hg@183
|
331 |
} catch (InterruptedException e) {
|
franta-hg@183
|
332 |
throw new FormatterException("Interrupted while waiting for test results", e);
|
franta-hg@183
|
333 |
}
|
franta-hg@183
|
334 |
|
franta-hg@183
|
335 |
printFooter(currentFormatter);
|
franta-hg@73
|
336 |
}
|
franta-hg@73
|
337 |
|
franta-hg@73
|
338 |
public Object[] testConnection(String dbName) {
|
franta-hg@69
|
339 |
log.log(Level.FINE, "Testing connection to database: {0}", dbName);
|
franta-hg@73
|
340 |
|
franta-hg@73
|
341 |
boolean succesfullyConnected = false;
|
franta-hg@73
|
342 |
boolean succesfullyConfigured = false;
|
franta-hg@73
|
343 |
|
franta-hg@69
|
344 |
try {
|
franta-hg@69
|
345 |
DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
|
franta-hg@75
|
346 |
log.log(Level.FINE, "Database definition was loaded from configuration");
|
franta-hg@75
|
347 |
succesfullyConfigured = true;
|
franta-hg@106
|
348 |
try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
|
franta-hg@75
|
349 |
succesfullyConnected = dc.test();
|
franta-hg@69
|
350 |
}
|
franta-hg@75
|
351 |
log.log(Level.FINE, "Database connection test was successful");
|
franta-hg@69
|
352 |
} catch (ConfigurationException | SQLException e) {
|
franta-hg@183
|
353 |
log.log(Level.SEVERE, "Error during testing connection " + dbName, e);
|
franta-hg@69
|
354 |
}
|
franta-hg@73
|
355 |
|
franta-hg@73
|
356 |
return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
|
franta-hg@69
|
357 |
}
|
franta-hg@69
|
358 |
|
franta-hg@69
|
359 |
public void printResource(String fileName) {
|
franta-hg@18
|
360 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
|
franta-hg@17
|
361 |
while (true) {
|
franta-hg@18
|
362 |
String line = reader.readLine();
|
franta-hg@17
|
363 |
if (line == null) {
|
franta-hg@17
|
364 |
break;
|
franta-hg@17
|
365 |
} else {
|
franta-hg@17
|
366 |
println(line);
|
franta-hg@17
|
367 |
}
|
franta-hg@17
|
368 |
}
|
franta-hg@17
|
369 |
} catch (Exception e) {
|
franta-hg@18
|
370 |
log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
|
franta-hg@17
|
371 |
}
|
franta-hg@17
|
372 |
}
|
franta-hg@17
|
373 |
|
franta-hg@17
|
374 |
private void println(String line) {
|
franta-hg@17
|
375 |
out.println(line);
|
franta-hg@17
|
376 |
}
|
franta-hg@69
|
377 |
|
franta-hg@183
|
378 |
private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data) throws ConfigurationException, FormatterException {
|
franta-hg@183
|
379 |
printHeader(formatter, header, sql, parameters);
|
franta-hg@183
|
380 |
|
franta-hg@183
|
381 |
for (Object[] row : data) {
|
franta-hg@183
|
382 |
printRow(formatter, row);
|
franta-hg@183
|
383 |
}
|
franta-hg@183
|
384 |
|
franta-hg@183
|
385 |
printFooter(formatter);
|
franta-hg@183
|
386 |
}
|
franta-hg@183
|
387 |
|
franta-hg@183
|
388 |
private void printHeader(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters) {
|
franta-hg@159
|
389 |
formatter.writeStartStatement();
|
franta-hg@159
|
390 |
if (sql != null) {
|
franta-hg@159
|
391 |
formatter.writeQuery(sql);
|
franta-hg@159
|
392 |
if (parameters != null) {
|
franta-hg@159
|
393 |
formatter.writeParameters(parameters);
|
franta-hg@159
|
394 |
}
|
franta-hg@159
|
395 |
}
|
franta-hg@183
|
396 |
formatter.writeStartResultSet(header);
|
franta-hg@183
|
397 |
}
|
franta-hg@159
|
398 |
|
franta-hg@183
|
399 |
private void printRow(Formatter formatter, Object[] row) {
|
franta-hg@183
|
400 |
formatter.writeStartRow();
|
franta-hg@183
|
401 |
for (Object cell : row) {
|
franta-hg@183
|
402 |
formatter.writeColumnValue(cell);
|
franta-hg@183
|
403 |
}
|
franta-hg@183
|
404 |
formatter.writeEndRow();
|
franta-hg@183
|
405 |
}
|
franta-hg@69
|
406 |
|
franta-hg@183
|
407 |
private void printFooter(Formatter formatter) {
|
franta-hg@69
|
408 |
formatter.writeEndResultSet();
|
franta-hg@159
|
409 |
formatter.writeEndStatement();
|
franta-hg@69
|
410 |
}
|
franta-hg@69
|
411 |
|
franta-hg@69
|
412 |
private Formatter getFormatter() throws ConfigurationException, FormatterException {
|
franta-hg@89
|
413 |
String formatterName = options.getFormatterName();
|
franta-hg@89
|
414 |
formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
|
franta-hg@89
|
415 |
FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
|
franta-hg@104
|
416 |
FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
|
franta-hg@69
|
417 |
return fd.getInstance(context);
|
franta-hg@69
|
418 |
}
|
franta-hg@69
|
419 |
|
franta-hg@69
|
420 |
private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
|
franta-hg@69
|
421 |
try {
|
franta-hg@69
|
422 |
RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
|
franta-hg@69
|
423 |
metaData.setColumnCount(fields.length);
|
franta-hg@69
|
424 |
|
franta-hg@69
|
425 |
for (int i = 0; i < fields.length; i++) {
|
franta-hg@69
|
426 |
HeaderField hf = fields[i];
|
franta-hg@69
|
427 |
int sqlIndex = i + 1;
|
franta-hg@69
|
428 |
metaData.setColumnName(sqlIndex, hf.name);
|
franta-hg@69
|
429 |
metaData.setColumnLabel(sqlIndex, hf.name);
|
franta-hg@69
|
430 |
metaData.setColumnType(sqlIndex, hf.type.getCode());
|
franta-hg@69
|
431 |
metaData.setColumnTypeName(sqlIndex, hf.type.name());
|
franta-hg@69
|
432 |
}
|
franta-hg@69
|
433 |
|
franta-hg@69
|
434 |
return new ColumnsHeader(metaData);
|
franta-hg@69
|
435 |
} catch (SQLException e) {
|
franta-hg@69
|
436 |
throw new FormatterException("Error while constructing table headers", e);
|
franta-hg@69
|
437 |
}
|
franta-hg@69
|
438 |
}
|
franta-hg@69
|
439 |
|
franta-hg@69
|
440 |
private static class HeaderField {
|
franta-hg@69
|
441 |
|
franta-hg@69
|
442 |
String name;
|
franta-hg@69
|
443 |
SQLType type;
|
franta-hg@69
|
444 |
|
franta-hg@69
|
445 |
public HeaderField(String name, SQLType type) {
|
franta-hg@69
|
446 |
this.name = name;
|
franta-hg@69
|
447 |
this.type = type;
|
franta-hg@69
|
448 |
}
|
franta-hg@69
|
449 |
}
|
franta-hg@69
|
450 |
|
franta-hg@69
|
451 |
public enum InfoType {
|
franta-hg@69
|
452 |
|
franta-hg@69
|
453 |
HELP {
|
franta-hg@183
|
454 |
@Override
|
franta-hg@183
|
455 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@183
|
456 |
infoLister.printResource(Constants.HELP_FILE);
|
franta-hg@183
|
457 |
}
|
franta-hg@183
|
458 |
},
|
franta-hg@69
|
459 |
VERSION {
|
franta-hg@183
|
460 |
@Override
|
franta-hg@183
|
461 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@183
|
462 |
infoLister.printResource(Constants.VERSION_FILE);
|
franta-hg@183
|
463 |
}
|
franta-hg@183
|
464 |
},
|
franta-hg@69
|
465 |
LICENSE {
|
franta-hg@183
|
466 |
@Override
|
franta-hg@183
|
467 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@183
|
468 |
infoLister.printResource(Constants.LICENSE_FILE);
|
franta-hg@183
|
469 |
}
|
franta-hg@183
|
470 |
},
|
franta-hg@69
|
471 |
FORMATTERS {
|
franta-hg@183
|
472 |
@Override
|
franta-hg@183
|
473 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@183
|
474 |
infoLister.listFormatters();
|
franta-hg@183
|
475 |
}
|
franta-hg@183
|
476 |
},
|
franta-hg@69
|
477 |
TYPES {
|
franta-hg@183
|
478 |
@Override
|
franta-hg@183
|
479 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@183
|
480 |
infoLister.listTypes();
|
franta-hg@183
|
481 |
}
|
franta-hg@183
|
482 |
},
|
franta-hg@158
|
483 |
JDBC_DRIVERS {
|
franta-hg@183
|
484 |
@Override
|
franta-hg@183
|
485 |
public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
|
franta-hg@183
|
486 |
infoLister.listJdbcDrivers();
|
franta-hg@183
|
487 |
}
|
franta-hg@183
|
488 |
},
|
franta-hg@159
|
489 |
JDBC_PROPERTIES {
|
franta-hg@183
|
490 |
@Override
|
franta-hg@183
|
491 |
public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
|
franta-hg@183
|
492 |
infoLister.listJdbcProperties();
|
franta-hg@183
|
493 |
}
|
franta-hg@183
|
494 |
},
|
franta-hg@69
|
495 |
DATABASES {
|
franta-hg@183
|
496 |
@Override
|
franta-hg@183
|
497 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@183
|
498 |
infoLister.listDatabases();
|
franta-hg@183
|
499 |
}
|
franta-hg@183
|
500 |
},
|
franta-hg@69
|
501 |
CONNECTION {
|
franta-hg@183
|
502 |
@Override
|
franta-hg@183
|
503 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@183
|
504 |
infoLister.testConnections();
|
franta-hg@183
|
505 |
}
|
franta-hg@183
|
506 |
};
|
franta-hg@69
|
507 |
|
franta-hg@69
|
508 |
public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
|
franta-hg@69
|
509 |
}
|
franta-hg@14
|
510 |
}
|