InfoLister: new listings: --list-java-properties and --list-environment-variables
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.Configuration;
21 import info.globalcode.sql.dk.configuration.ConfigurationException;
22 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
23 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
24 import info.globalcode.sql.dk.configuration.FormatterDefinition;
25 import info.globalcode.sql.dk.configuration.Properties;
26 import info.globalcode.sql.dk.configuration.Property;
27 import info.globalcode.sql.dk.formatting.ColumnsHeader;
28 import info.globalcode.sql.dk.formatting.FakeSqlArray;
29 import info.globalcode.sql.dk.formatting.Formatter;
30 import info.globalcode.sql.dk.formatting.FormatterContext;
31 import info.globalcode.sql.dk.formatting.FormatterException;
32 import java.io.BufferedReader;
33 import java.io.ByteArrayOutputStream;
34 import java.io.InputStreamReader;
35 import java.io.PrintStream;
36 import java.sql.Array;
37 import java.sql.Driver;
38 import java.sql.DriverManager;
39 import java.sql.DriverPropertyInfo;
40 import java.sql.SQLException;
41 import java.util.ArrayList;
42 import java.util.EnumSet;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Map.Entry;
46 import java.util.ServiceLoader;
48 import java.util.concurrent.ExecutorService;
49 import java.util.concurrent.Executors;
50 import java.util.concurrent.TimeUnit;
51 import java.util.logging.Level;
52 import java.util.logging.LogRecord;
53 import java.util.logging.Logger;
54 import javax.sql.rowset.RowSetMetaDataImpl;
57 * Displays info like help, version etc.
59 * @author Ing. František Kučera (frantovo.cz)
61 public class InfoLister {
63 private static final Logger log = Logger.getLogger(InfoLister.class.getName());
65 * Fake database name for output formatting
67 public static final String CONFIG_DB_NAME = "sqldk_configuration";
68 private final PrintStream out;
69 private final ConfigurationProvider configurationProvider;
70 private final CLIOptions options;
71 private Formatter formatter;
73 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
75 this.configurationProvider = configurationProvider;
76 this.options = options;
79 public void showInfo() throws ConfigurationException, FormatterException {
80 EnumSet<InfoType> commands = options.getShowInfo();
82 boolean formattinNeeded = false;
84 for (InfoType infoType : commands) {
93 case ENVIRONMENT_VARIABLES:
94 formattinNeeded = true;
99 if (formattinNeeded) {
100 try (Formatter f = getFormatter()) {
102 formatter.writeStartBatch();
103 DatabaseDefinition dd = new DatabaseDefinition();
104 dd.setName(CONFIG_DB_NAME);
105 formatter.writeStartDatabase(dd);
107 formatter.writeEndDatabase();
108 formatter.writeEndBatch();
116 private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
117 for (InfoType infoType : commands) {
118 infoType.showInfo(this);
122 private void listJavaProperties() throws FormatterException, ConfigurationException {
123 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("value", SQLType.VARCHAR));
124 List<Object[]> data = new ArrayList<>();
125 for (Entry<Object, Object> e : System.getProperties().entrySet()) {
126 data.add(new Object[]{e.getKey(), e.getValue()});
128 printTable(formatter, header, "-- Java system properties", null, data);
131 private void listEnvironmentVariables() throws FormatterException, ConfigurationException {
132 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("value", SQLType.VARCHAR));
133 List<Object[]> data = new ArrayList<>();
134 for (Entry<String, String> e : System.getenv().entrySet()) {
135 data.add(new Object[]{e.getKey(), e.getValue()});
137 printTable(formatter, header, "-- environment variables", null, data);
140 private void listFormatters() throws ConfigurationException, FormatterException {
141 ColumnsHeader header = constructHeader(
142 new HeaderField("name", SQLType.VARCHAR),
143 new HeaderField("built_in", SQLType.BOOLEAN),
144 new HeaderField("default", SQLType.BOOLEAN),
145 new HeaderField("class_name", SQLType.VARCHAR),
146 new HeaderField("valid", SQLType.BOOLEAN));
147 List<Object[]> data = new ArrayList<>();
149 String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
150 defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
152 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
153 data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
156 for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
157 data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
160 printTable(formatter, header, "-- configured and built-in output formatters", null, data);
163 private boolean isInstantiable(FormatterDefinition fd) {
165 try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
166 fd.getInstance(new FormatterContext(testStream, new Properties(0)));
169 } catch (Exception e) {
170 log.log(Level.SEVERE, "Unable to create an instance of formatter: " + fd.getName(), e);
175 public void listTypes() throws FormatterException, ConfigurationException {
176 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
177 List<Object[]> data = new ArrayList<>();
178 for (SQLType sqlType : SQLType.values()) {
179 data.add(new Object[]{sqlType.name(), sqlType.getCode()});
181 printTable(formatter, header, "-- data types", null, data);
182 log.log(Level.INFO, "Type names in --types option are case insensitive");
185 public void listDatabases() throws ConfigurationException, FormatterException {
186 ColumnsHeader header = constructHeader(
187 new HeaderField("database_name", SQLType.VARCHAR),
188 new HeaderField("user_name", SQLType.VARCHAR),
189 new HeaderField("database_url", SQLType.VARCHAR));
190 List<Object[]> data = new ArrayList<>();
192 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
193 if (configuredDatabases.isEmpty()) {
194 log.log(Level.WARNING, "No databases are configured.");
196 for (DatabaseDefinition dd : configuredDatabases) {
197 data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
201 printTable(formatter, header, "-- configured databases", null, data);
204 public void listJdbcDrivers() throws FormatterException, ConfigurationException {
205 ColumnsHeader header = constructHeader(
206 new HeaderField("class", SQLType.VARCHAR),
207 new HeaderField("version", SQLType.VARCHAR),
208 new HeaderField("major", SQLType.INTEGER),
209 new HeaderField("minor", SQLType.INTEGER),
210 new HeaderField("jdbc_compliant", SQLType.BOOLEAN));
211 List<Object[]> data = new ArrayList<>();
213 final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
214 for (Driver d : drivers) {
215 data.add(new Object[]{
216 d.getClass().getName(),
217 d.getMajorVersion() + "." + d.getMinorVersion(),
224 printTable(formatter, header, "-- discovered JDBC drivers (available on the CLASSPATH)", null, data);
227 public void listJdbcProperties() throws FormatterException, ConfigurationException {
228 for (String dbName : options.getDatabaseNamesToListProperties()) {
229 ColumnsHeader header = constructHeader(
230 new HeaderField("property_name", SQLType.VARCHAR),
231 new HeaderField("required", SQLType.BOOLEAN),
232 new HeaderField("choices", SQLType.ARRAY),
233 new HeaderField("configured_value", SQLType.VARCHAR),
234 new HeaderField("description", SQLType.VARCHAR));
235 List<Object[]> data = new ArrayList<>();
237 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
239 Driver driver = findDriver(dd);
241 if (driver == null) {
242 log.log(Level.WARNING, "No JDBC driver was found for DB: {0} with URL: {1}", new Object[]{dd.getName(), dd.getUrl()});
244 log.log(Level.INFO, "For DB: {0} was found JDBC driver: {1}", new Object[]{dd.getName(), driver.getClass().getName()});
247 DriverPropertyInfo[] propertyInfos = driver.getPropertyInfo(dd.getUrl(), dd.getProperties().getJavaProperties());
249 Set<String> standardProperties = new HashSet<>();
251 for (DriverPropertyInfo pi : propertyInfos) {
252 Array choices = new FakeSqlArray(pi.choices, SQLType.VARCHAR);
253 data.add(new Object[]{
256 choices.getArray() == null ? "" : choices,
257 pi.value == null ? "" : pi.value,
260 standardProperties.add(pi.name);
263 for (Property p : dd.getProperties()) {
264 if (!standardProperties.contains(p.getName())) {
265 data.add(new Object[]{
272 log.log(Level.WARNING, "Your configuration contains property „{0}“ not declared by the JDBC driver.", p.getName());
276 } catch (SQLException e) {
277 log.log(Level.WARNING, "Error during getting property infos.", e);
280 List<Parameter> parameters = new ArrayList<>();
281 parameters.add(new NamedParameter("database", dbName, SQLType.VARCHAR));
282 parameters.add(new NamedParameter("driver_class", driver.getClass().getName(), SQLType.VARCHAR));
283 parameters.add(new NamedParameter("driver_major_version", driver.getMajorVersion(), SQLType.INTEGER));
284 parameters.add(new NamedParameter("driver_minor_version", driver.getMinorVersion(), SQLType.INTEGER));
286 printTable(formatter, header, "-- configured and configurable JDBC driver properties", parameters, data);
292 private Driver findDriver(DatabaseDefinition dd) {
293 final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
294 for (Driver d : drivers) {
296 if (d.acceptsURL(dd.getUrl())) {
299 } catch (SQLException e) {
300 log.log(Level.WARNING, "Error during finding JDBC driver for: " + dd.getName(), e);
307 * Parallelism for connection testing – maximum concurrent database connections.
309 private static final int TESTING_THREAD_COUNT = 64;
311 * Time limit for all connection testing threads – particular timeouts per connection will be
314 private static final long TESTING_AWAIT_LIMIT = 1;
315 private static final TimeUnit TESTING_AWAIT_UNIT = TimeUnit.DAYS;
317 public void testConnections() throws FormatterException, ConfigurationException {
318 ColumnsHeader header = constructHeader(
319 new HeaderField("database_name", SQLType.VARCHAR),
320 new HeaderField("configured", SQLType.BOOLEAN),
321 new HeaderField("connected", SQLType.BOOLEAN));
323 log.log(Level.FINE, "Testing DB connections in {0} threads", TESTING_THREAD_COUNT);
325 ExecutorService es = Executors.newFixedThreadPool(TESTING_THREAD_COUNT);
327 final Formatter currentFormatter = formatter;
329 printHeader(currentFormatter, header, "-- database configuration and connectivity test", null);
331 for (final String dbName : options.getDatabaseNamesToTest()) {
332 preloadDriver(dbName);
335 for (final String dbName : options.getDatabaseNamesToTest()) {
336 es.submit(new Runnable() {
337 // TODO: Java 8 – lambda
340 final Object[] row = testConnection(dbName);
341 synchronized (currentFormatter) {
342 printRow(currentFormatter, row);
351 log.log(Level.FINEST, "Waiting for test results: {0} {1}", new Object[]{TESTING_AWAIT_LIMIT, TESTING_AWAIT_UNIT.name()});
352 boolean finished = es.awaitTermination(TESTING_AWAIT_LIMIT, TESTING_AWAIT_UNIT);
354 log.log(Level.FINEST, "All testing threads finished in time limit.");
356 throw new FormatterException("Exceeded total time limit for test threads – this should never happen");
358 } catch (InterruptedException e) {
359 throw new FormatterException("Interrupted while waiting for test results", e);
362 printFooter(currentFormatter);
366 * JDBC driver classes should be preloaded in single thread to avoid deadlocks while doing
367 * {@linkplain DriverManager#registerDriver(java.sql.Driver)} during parallel connections.
371 private void preloadDriver(String dbName) {
373 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
374 Driver driver = findDriver(dd);
375 if (driver == null) {
376 log.log(Level.WARNING, "No Driver found for DB: {0}", dbName);
378 log.log(Level.FINEST, "Driver preloading for DB: {0} was successfull", dbName);
380 } catch (Exception e) {
381 LogRecord r = new LogRecord(Level.WARNING, "Failed to preload the Driver for DB: {0}");
382 r.setParameters(new Object[]{dbName});
388 public Object[] testConnection(String dbName) {
389 log.log(Level.FINE, "Testing connection to database: {0}", dbName);
391 boolean succesfullyConnected = false;
392 boolean succesfullyConfigured = false;
395 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
396 log.log(Level.FINE, "Database definition was loaded from configuration");
397 succesfullyConfigured = true;
398 try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
399 succesfullyConnected = dc.test();
401 log.log(Level.FINE, "Database connection test was successful");
402 } catch (ConfigurationException | SQLException | RuntimeException e) {
403 log.log(Level.SEVERE, "Error during testing connection " + dbName, e);
406 return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
409 public void printResource(String fileName) {
410 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
412 String line = reader.readLine();
419 } catch (Exception e) {
420 log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
424 private void println(String line) {
428 private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data) throws ConfigurationException, FormatterException {
429 printHeader(formatter, header, sql, parameters);
431 for (Object[] row : data) {
432 printRow(formatter, row);
435 printFooter(formatter);
438 private void printHeader(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters) {
439 formatter.writeStartStatement();
441 formatter.writeQuery(sql);
442 if (parameters != null) {
443 formatter.writeParameters(parameters);
446 formatter.writeStartResultSet(header);
449 private void printRow(Formatter formatter, Object[] row) {
450 formatter.writeStartRow();
451 for (Object cell : row) {
452 formatter.writeColumnValue(cell);
454 formatter.writeEndRow();
457 private void printFooter(Formatter formatter) {
458 formatter.writeEndResultSet();
459 formatter.writeEndStatement();
462 private Formatter getFormatter() throws ConfigurationException, FormatterException {
463 String formatterName = options.getFormatterName();
464 formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
465 FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
466 FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
467 return fd.getInstance(context);
470 private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
472 RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
473 metaData.setColumnCount(fields.length);
475 for (int i = 0; i < fields.length; i++) {
476 HeaderField hf = fields[i];
477 int sqlIndex = i + 1;
478 metaData.setColumnName(sqlIndex, hf.name);
479 metaData.setColumnLabel(sqlIndex, hf.name);
480 metaData.setColumnType(sqlIndex, hf.type.getCode());
481 metaData.setColumnTypeName(sqlIndex, hf.type.name());
484 return new ColumnsHeader(metaData);
485 } catch (SQLException e) {
486 throw new FormatterException("Error while constructing table headers", e);
490 private static class HeaderField {
495 public HeaderField(String name, SQLType type) {
501 public enum InfoType {
505 public void showInfo(InfoLister infoLister) {
506 infoLister.printResource(Constants.HELP_FILE);
511 public void showInfo(InfoLister infoLister) {
512 infoLister.printResource(Constants.VERSION_FILE);
517 public void showInfo(InfoLister infoLister) {
518 infoLister.printResource(Constants.LICENSE_FILE);
523 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
524 infoLister.listJavaProperties();
527 ENVIRONMENT_VARIABLES {
529 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
530 infoLister.listEnvironmentVariables();
535 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
536 infoLister.listFormatters();
541 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
542 infoLister.listTypes();
547 public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
548 infoLister.listJdbcDrivers();
553 public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
554 infoLister.listJdbcProperties();
559 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
560 infoLister.listDatabases();
565 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
566 infoLister.testConnections();
570 public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;