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