java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 01:53:15 +0100
branchv_0
changeset 67 10c9b9e54622
parent 66 6e28893eaada
child 69 0befec5034c2
permissions -rw-r--r--
option --list-formatters – list configured and built-in formatters
     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 static info.globalcode.sql.dk.Functions.rpad;
    26 import java.io.BufferedReader;
    27 import java.io.InputStreamReader;
    28 import java.io.PrintStream;
    29 import java.sql.SQLException;
    30 import java.util.EnumSet;
    31 import java.util.List;
    32 import java.util.logging.Level;
    33 import java.util.logging.Logger;
    34 
    35 /**
    36  * Displays info like help, version etc.
    37  *
    38  * @author Ing. František Kučera (frantovo.cz)
    39  */
    40 public class InfoLister {
    41 
    42 	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
    43 	private PrintStream out;
    44 	private ConfigurationProvider configurationProvider;
    45 
    46 	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
    47 		this.out = out;
    48 		this.configurationProvider = configurationProvider;
    49 	}
    50 
    51 	public void showInfo(CLIOptions options) throws ConfigurationException {
    52 		EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
    53 		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
    54 			switch (infoType) {
    55 				/**
    56 				 * TODO: implement show info
    57 				 */
    58 				case FORMATTERS:
    59 					for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
    60 						log.log(Level.INFO, "Built-in formatter:   {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
    61 					}
    62 
    63 					List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters();
    64 					for (FormatterDefinition fd : configuredFormatters) {
    65 						log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()});
    66 					}
    67 					if (configuredFormatters.isEmpty()) {
    68 						log.log(Level.INFO, "No other formatters are configured");
    69 					}
    70 
    71 					String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
    72 					if (configuredDefaultFormatter == null) {
    73 						log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER);
    74 					} else {
    75 						log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter);
    76 					}
    77 					break;
    78 				case HELP:
    79 					printResource(Constants.HELP_FILE);
    80 					break;
    81 				case LICENSE:
    82 					printResource(Constants.LICENSE_FILE);
    83 					break;
    84 				case TYPES:
    85 					println("TODO: list supported types");
    86 					break;
    87 				case VERSION:
    88 					printResource(Constants.VERSION_FILE);
    89 					break;
    90 				case DATABASES:
    91 					final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
    92 					if (configuredDatabases.isEmpty()) {
    93 						log.log(Level.WARNING, "No databases are configured.");
    94 					} else {
    95 						for (DatabaseDefinition dd : configuredDatabases) {
    96 							log.log(Level.INFO, "Configured database: {0}", dd.getName());
    97 						}
    98 					}
    99 					break;
   100 				case CONNECTION:
   101 					boolean connectionTestResult = false;
   102 					String dbName = options.getDatabaseNameToTest();
   103 					log.log(Level.FINE, "Testing connection to database: {0}", dbName);
   104 					try {
   105 						DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
   106 						if (dd == null) {
   107 							log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
   108 						} else {
   109 							log.log(Level.FINE, "Database definition was loaded from configuration");
   110 							DatabaseConnection dc = dd.connect();
   111 							connectionTestResult = dc.test();
   112 						}
   113 					} catch (ConfigurationException | SQLException e) {
   114 						log.log(Level.SEVERE, "Error during testing connection", e);
   115 					}
   116 					log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
   117 					break;
   118 				default:
   119 					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
   120 			}
   121 		}
   122 	}
   123 
   124 	private void printResource(String fileName) {
   125 		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
   126 			while (true) {
   127 				String line = reader.readLine();
   128 				if (line == null) {
   129 					break;
   130 				} else {
   131 					println(line);
   132 				}
   133 			}
   134 		} catch (Exception e) {
   135 			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
   136 		}
   137 	}
   138 
   139 	private void println(String line) {
   140 		out.println(line);
   141 	}
   142 }