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