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