3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.configuration.CommandArgument;
21 import info.globalcode.sql.dk.configuration.Configuration;
22 import info.globalcode.sql.dk.configuration.ConfigurationException;
23 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
26 import info.globalcode.sql.dk.configuration.Properties;
27 import info.globalcode.sql.dk.configuration.Property;
28 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
29 import info.globalcode.sql.dk.configuration.PropertyDeclarations;
30 import info.globalcode.sql.dk.configuration.TunnelDefinition;
31 import info.globalcode.sql.dk.formatting.ColumnsHeader;
32 import info.globalcode.sql.dk.formatting.CommonProperties;
33 import info.globalcode.sql.dk.formatting.FakeSqlArray;
34 import info.globalcode.sql.dk.formatting.Formatter;
35 import info.globalcode.sql.dk.formatting.FormatterContext;
36 import info.globalcode.sql.dk.formatting.FormatterException;
37 import java.io.BufferedReader;
38 import java.io.ByteArrayOutputStream;
39 import java.io.InputStreamReader;
40 import java.io.PrintStream;
41 import java.sql.Array;
42 import java.sql.Driver;
43 import java.sql.DriverManager;
44 import java.sql.DriverPropertyInfo;
45 import java.sql.SQLException;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Comparator;
49 import java.util.EnumSet;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.List;
54 import java.util.Map.Entry;
55 import java.util.ServiceLoader;
57 import java.util.concurrent.ExecutorService;
58 import java.util.concurrent.Executors;
59 import java.util.concurrent.TimeUnit;
60 import java.util.logging.Level;
61 import java.util.logging.LogRecord;
62 import java.util.logging.Logger;
63 import javax.sql.rowset.RowSetMetaDataImpl;
66 * Displays info like help, version etc.
68 * @author Ing. František Kučera (frantovo.cz)
70 public class InfoLister {
72 private static final Logger log = Logger.getLogger(InfoLister.class.getName());
74 * Fake database name for output formatting
76 public static final String CONFIG_DB_NAME = "sqldk_configuration";
77 private final PrintStream out;
78 private final ConfigurationProvider configurationProvider;
79 private final CLIOptions options;
80 private Formatter formatter;
82 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
84 this.configurationProvider = configurationProvider;
85 this.options = options;
88 public void showInfo() throws ConfigurationException, FormatterException {
89 EnumSet<InfoType> commands = options.getShowInfo();
91 boolean formattinNeeded = false;
93 for (InfoType infoType : commands) {
100 case FORMATTER_PROPERTIES:
102 case JAVA_PROPERTIES:
103 case ENVIRONMENT_VARIABLES:
104 formattinNeeded = true;
109 if (formattinNeeded) {
110 try (Formatter f = getFormatter()) {
112 formatter.writeStartBatch();
113 DatabaseDefinition dd = new DatabaseDefinition();
114 dd.setName(CONFIG_DB_NAME);
115 formatter.writeStartDatabase(dd);
117 formatter.writeEndDatabase();
118 formatter.writeEndBatch();
126 private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
127 for (InfoType infoType : commands) {
128 infoType.showInfo(this);
132 private void listJavaProperties() throws FormatterException, ConfigurationException {
133 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("value", SQLType.VARCHAR));
134 List<Object[]> data = new ArrayList<>();
135 for (Entry<Object, Object> e : System.getProperties().entrySet()) {
136 data.add(new Object[]{e.getKey(), e.getValue()});
138 printTable(formatter, header, "-- Java system properties", null, data, 0);
141 private void listEnvironmentVariables() throws FormatterException, ConfigurationException {
142 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("value", SQLType.VARCHAR));
143 List<Object[]> data = new ArrayList<>();
144 for (Entry<String, String> e : System.getenv().entrySet()) {
145 data.add(new Object[]{e.getKey(), e.getValue()});
147 printTable(formatter, header, "-- environment variables", null, data, 0);
150 private void listFormatters() throws ConfigurationException, FormatterException {
151 ColumnsHeader header = constructHeader(
152 new HeaderField("name", SQLType.VARCHAR),
153 new HeaderField("built_in", SQLType.BOOLEAN),
154 new HeaderField("default", SQLType.BOOLEAN),
155 new HeaderField("class_name", SQLType.VARCHAR),
156 new HeaderField("valid", SQLType.BOOLEAN));
157 List<Object[]> data = new ArrayList<>();
159 String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
160 defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
162 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
163 data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
166 for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
167 data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
170 printTable(formatter, header, "-- configured and built-in output formatters", null, data);
173 private boolean isInstantiable(FormatterDefinition fd) {
175 try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
176 fd.getInstance(new FormatterContext(testStream, new Properties(0)));
179 } catch (Exception e) {
180 log.log(Level.SEVERE, "Unable to create an instance of formatter: " + fd.getName(), e);
185 private void listFormatterProperties() throws FormatterException, ConfigurationException {
186 for (String formatterName : options.getFormatterNamesToListProperties()) {
187 listFormatterProperties(formatterName);
191 private PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
192 PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
194 if (properties == null) {
195 PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
196 return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
198 return properties.value();
202 private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
203 FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
206 // currently only for debugging purposes
207 // TODO: introduce --info-lister-property or generic filtering capability in printTable() ?
208 boolean printDeclaredIn = options.getFormatterProperties().getBoolean("InfoLister:print:declared_in", false);
210 List<HeaderField> headerFields = new ArrayList<>();
211 headerFields.add(new HeaderField("name", SQLType.VARCHAR));
212 headerFields.add(new HeaderField("type", SQLType.VARCHAR));
213 headerFields.add(new HeaderField("default", SQLType.VARCHAR));
214 headerFields.add(new HeaderField("description", SQLType.VARCHAR));
215 if (printDeclaredIn) {
216 headerFields.add(new HeaderField("declared_in", SQLType.VARCHAR));
219 ColumnsHeader header = constructHeader(headerFields.toArray(new HeaderField[0]));
221 Map<String, Object[]> data = new HashMap<>();
222 Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
223 List<Class<? extends Formatter>> hierarchy = Functions.getClassHierarchy(formatterClass, Formatter.class);
224 Collections.reverse(hierarchy);
225 hierarchy.stream().forEach((c) -> {
226 for (PropertyDeclaration p : getPropertyDeclarations(c)) {
227 data.put(p.name(), propertyDeclarationToRow(p, c, printDeclaredIn));
231 List<Parameter> parameters = new ArrayList<>();
232 parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
234 printTable(formatter, header, "-- formatter properties", parameters, new ArrayList<>(data.values()));
235 } catch (ClassNotFoundException e) {
236 throw new ConfigurationException("Unable to find class " + fd.getClassName() + " of formatter" + fd.getName(), e);
240 private static Object[] propertyDeclarationToRow(PropertyDeclaration p, Class formatterClass, boolean printDeclaredIn) {
241 List list = new ArrayList();
244 list.add(CommonProperties.getSimpleTypeName(p.type()));
245 list.add(p.defaultValue());
246 list.add(p.description());
247 if (printDeclaredIn) {
248 list.add(formatterClass.getName());
251 return list.toArray();
254 private void listTypes() throws FormatterException, ConfigurationException {
255 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
256 List<Object[]> data = new ArrayList<>();
257 for (SQLType sqlType : SQLType.values()) {
258 data.add(new Object[]{sqlType.name(), sqlType.getCode()});
260 printTable(formatter, header, "-- data types", null, data);
261 log.log(Level.INFO, "Type names in --types option are case insensitive");
264 private void listDatabases() throws ConfigurationException, FormatterException {
265 ColumnsHeader header = constructHeader(
266 new HeaderField("database_name", SQLType.VARCHAR),
267 new HeaderField("user_name", SQLType.VARCHAR),
268 new HeaderField("database_url", SQLType.VARCHAR));
269 List<Object[]> data = new ArrayList<>();
271 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
272 if (configuredDatabases.isEmpty()) {
273 log.log(Level.WARNING, "No databases are configured.");
275 for (DatabaseDefinition dd : configuredDatabases) {
276 data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
278 final TunnelDefinition tunnel = dd.getTunnel();
279 if (tunnel != null) {
280 log.log(Level.INFO, "Tunnel command: {0}", tunnel.getCommand());
281 for (CommandArgument ca : Functions.notNull(tunnel.getArguments())) {
282 log.log(Level.INFO, "\targument: {0}/{1}", new Object[]{ca.getType(), ca.getValue()});
289 printTable(formatter, header, "-- configured databases", null, data);
292 private void listJdbcDrivers() throws FormatterException, ConfigurationException {
293 ColumnsHeader header = constructHeader(
294 new HeaderField("class", SQLType.VARCHAR),
295 new HeaderField("version", SQLType.VARCHAR),
296 new HeaderField("major", SQLType.INTEGER),
297 new HeaderField("minor", SQLType.INTEGER),
298 new HeaderField("jdbc_compliant", SQLType.BOOLEAN));
299 List<Object[]> data = new ArrayList<>();
301 final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
302 for (Driver d : drivers) {
303 data.add(new Object[]{
304 d.getClass().getName(),
305 d.getMajorVersion() + "." + d.getMinorVersion(),
312 printTable(formatter, header, "-- discovered JDBC drivers (available on the CLASSPATH)", null, data);
315 private void listJdbcProperties() throws FormatterException, ConfigurationException {
316 for (String dbName : options.getDatabaseNamesToListProperties()) {
317 ColumnsHeader header = constructHeader(
318 new HeaderField("property_name", SQLType.VARCHAR),
319 new HeaderField("required", SQLType.BOOLEAN),
320 new HeaderField("choices", SQLType.ARRAY),
321 new HeaderField("configured_value", SQLType.VARCHAR),
322 new HeaderField("description", SQLType.VARCHAR));
323 List<Object[]> data = new ArrayList<>();
325 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
327 Driver driver = findDriver(dd);
329 if (driver == null) {
330 log.log(Level.WARNING, "No JDBC driver was found for DB: {0} with URL: {1}", new Object[]{dd.getName(), dd.getUrl()});
332 log.log(Level.INFO, "For DB: {0} was found JDBC driver: {1}", new Object[]{dd.getName(), driver.getClass().getName()});
335 DriverPropertyInfo[] propertyInfos = driver.getPropertyInfo(dd.getUrl(), dd.getProperties().getJavaProperties());
337 Set<String> standardProperties = new HashSet<>();
339 for (DriverPropertyInfo pi : propertyInfos) {
340 Array choices = new FakeSqlArray(pi.choices, SQLType.VARCHAR);
341 data.add(new Object[]{
344 choices.getArray() == null ? "" : choices,
345 pi.value == null ? "" : pi.value,
348 standardProperties.add(pi.name);
351 for (Property p : dd.getProperties()) {
352 if (!standardProperties.contains(p.getName())) {
353 data.add(new Object[]{
360 log.log(Level.WARNING, "Your configuration contains property „{0}“ not declared by the JDBC driver.", p.getName());
364 } catch (SQLException e) {
365 log.log(Level.WARNING, "Error during getting property infos.", e);
368 List<Parameter> parameters = new ArrayList<>();
369 parameters.add(new NamedParameter("database", dbName, SQLType.VARCHAR));
370 parameters.add(new NamedParameter("driver_class", driver.getClass().getName(), SQLType.VARCHAR));
371 parameters.add(new NamedParameter("driver_major_version", driver.getMajorVersion(), SQLType.INTEGER));
372 parameters.add(new NamedParameter("driver_minor_version", driver.getMinorVersion(), SQLType.INTEGER));
374 printTable(formatter, header, "-- configured and configurable JDBC driver properties", parameters, data);
380 private Driver findDriver(DatabaseDefinition dd) {
381 final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
382 for (Driver d : drivers) {
384 if (d.acceptsURL(dd.getUrl())) {
387 } catch (SQLException e) {
388 log.log(Level.WARNING, "Error during finding JDBC driver for: " + dd.getName(), e);
395 * Parallelism for connection testing – maximum concurrent database connections.
397 private static final int TESTING_THREAD_COUNT = 64;
399 * Time limit for all connection testing threads – particular timeouts per connection will be
402 private static final long TESTING_AWAIT_LIMIT = 1;
403 private static final TimeUnit TESTING_AWAIT_UNIT = TimeUnit.DAYS;
405 private void testConnections() throws FormatterException, ConfigurationException {
406 ColumnsHeader header = constructHeader(
407 new HeaderField("database_name", SQLType.VARCHAR),
408 new HeaderField("configured", SQLType.BOOLEAN),
409 new HeaderField("connected", SQLType.BOOLEAN));
411 log.log(Level.FINE, "Testing DB connections in {0} threads", TESTING_THREAD_COUNT);
413 ExecutorService es = Executors.newFixedThreadPool(TESTING_THREAD_COUNT);
415 final Formatter currentFormatter = formatter;
417 printHeader(currentFormatter, header, "-- database configuration and connectivity test", null);
419 for (final String dbName : options.getDatabaseNamesToTest()) {
420 preloadDriver(dbName);
423 for (final String dbName : options.getDatabaseNamesToTest()) {
425 final Object[] row = testConnection(dbName);
426 synchronized (currentFormatter) {
427 printRow(currentFormatter, row);
436 log.log(Level.FINEST, "Waiting for test results: {0} {1}", new Object[]{TESTING_AWAIT_LIMIT, TESTING_AWAIT_UNIT.name()});
437 boolean finished = es.awaitTermination(TESTING_AWAIT_LIMIT, TESTING_AWAIT_UNIT);
439 log.log(Level.FINEST, "All testing threads finished in time limit.");
441 throw new FormatterException("Exceeded total time limit for test threads – this should never happen");
443 } catch (InterruptedException e) {
444 throw new FormatterException("Interrupted while waiting for test results", e);
447 printFooter(currentFormatter);
451 * JDBC driver classes should be preloaded in single thread to avoid deadlocks while doing
452 * {@linkplain DriverManager#registerDriver(java.sql.Driver)} during parallel connections.
456 private void preloadDriver(String dbName) {
458 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
459 Driver driver = findDriver(dd);
460 if (driver == null) {
461 log.log(Level.WARNING, "No Driver found for DB: {0}", dbName);
463 log.log(Level.FINEST, "Driver preloading for DB: {0} was successfull", dbName);
465 } catch (Exception e) {
466 LogRecord r = new LogRecord(Level.WARNING, "Failed to preload the Driver for DB: {0}");
467 r.setParameters(new Object[]{dbName});
473 private Object[] testConnection(String dbName) {
474 log.log(Level.FINE, "Testing connection to database: {0}", dbName);
476 boolean succesfullyConnected = false;
477 boolean succesfullyConfigured = false;
480 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
481 log.log(Level.FINE, "Database definition was loaded from configuration");
482 succesfullyConfigured = true;
483 try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
484 succesfullyConnected = dc.test();
486 log.log(Level.FINE, "Database connection test was successful");
487 } catch (ConfigurationException | SQLException | RuntimeException e) {
488 log.log(Level.SEVERE, "Error during testing connection " + dbName, e);
491 return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
494 private void printResource(String fileName) {
495 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
497 String line = reader.readLine();
504 } catch (Exception e) {
505 log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
509 private void println(String line) {
513 private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data) throws ConfigurationException, FormatterException {
514 printTable(formatter, header, sql, parameters, data, null);
517 private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data, final Integer sortByColumn) throws ConfigurationException, FormatterException {
518 printHeader(formatter, header, sql, parameters);
520 if (sortByColumn != null) {
521 Collections.sort(data, new Comparator<Object[]>() {
524 public int compare(Object[] o1, Object[] o2) {
525 String s1 = String.valueOf(o1[sortByColumn]);
526 String s2 = String.valueOf(o2[sortByColumn]);
527 return s1.compareTo(s2);
532 for (Object[] row : data) {
533 printRow(formatter, row);
536 printFooter(formatter);
539 private void printHeader(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters) {
540 formatter.writeStartStatement();
542 formatter.writeQuery(sql);
543 if (parameters != null) {
544 formatter.writeParameters(parameters);
547 formatter.writeStartResultSet(header);
550 private void printRow(Formatter formatter, Object[] row) {
551 formatter.writeStartRow();
552 for (Object cell : row) {
553 formatter.writeColumnValue(cell);
555 formatter.writeEndRow();
558 private void printFooter(Formatter formatter) {
559 formatter.writeEndResultSet();
560 formatter.writeEndStatement();
563 private Formatter getFormatter() throws ConfigurationException, FormatterException {
564 String formatterName = options.getFormatterName();
565 formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
566 FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
567 FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
568 return fd.getInstance(context);
571 private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
573 RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
574 metaData.setColumnCount(fields.length);
576 for (int i = 0; i < fields.length; i++) {
577 HeaderField hf = fields[i];
578 int sqlIndex = i + 1;
579 metaData.setColumnName(sqlIndex, hf.name);
580 metaData.setColumnLabel(sqlIndex, hf.name);
581 metaData.setColumnType(sqlIndex, hf.type.getCode());
582 metaData.setColumnTypeName(sqlIndex, hf.type.name());
585 return new ColumnsHeader(metaData);
586 } catch (SQLException e) {
587 throw new FormatterException("Error while constructing table headers", e);
591 private static class HeaderField {
596 public HeaderField(String name, SQLType type) {
602 public enum InfoType {
606 public void showInfo(InfoLister infoLister) {
607 infoLister.printResource(Constants.HELP_FILE);
612 public void showInfo(InfoLister infoLister) {
613 infoLister.printResource(Constants.VERSION_FILE);
618 public void showInfo(InfoLister infoLister) {
619 infoLister.printResource(Constants.LICENSE_FILE);
624 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
625 infoLister.listJavaProperties();
628 ENVIRONMENT_VARIABLES {
630 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
631 infoLister.listEnvironmentVariables();
636 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
637 infoLister.listFormatters();
640 FORMATTER_PROPERTIES {
642 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
643 infoLister.listFormatterProperties();
648 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
649 infoLister.listTypes();
654 public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
655 infoLister.listJdbcDrivers();
660 public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
661 infoLister.listJdbcProperties();
666 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
667 infoLister.listDatabases();
672 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
673 infoLister.testConnections();
677 public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;