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
franta-hg@16
     1
/**
franta-hg@16
     2
 * SQL-DK
franta-hg@16
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16
     4
 *
franta-hg@16
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@16
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@14
    18
package info.globalcode.sql.dk;
franta-hg@14
    19
franta-hg@65
    20
import info.globalcode.sql.dk.configuration.ConfigurationException;
franta-hg@26
    21
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
franta-hg@65
    22
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
franta-hg@17
    23
import java.io.BufferedReader;
franta-hg@17
    24
import java.io.InputStreamReader;
franta-hg@14
    25
import java.io.PrintStream;
franta-hg@65
    26
import java.sql.SQLException;
franta-hg@14
    27
import java.util.EnumSet;
franta-hg@66
    28
import java.util.List;
franta-hg@17
    29
import java.util.logging.Level;
franta-hg@17
    30
import java.util.logging.Logger;
franta-hg@14
    31
franta-hg@14
    32
/**
franta-hg@14
    33
 * Displays info like help, version etc.
franta-hg@14
    34
 *
franta-hg@14
    35
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@14
    36
 */
franta-hg@14
    37
public class InfoLister {
franta-hg@14
    38
franta-hg@17
    39
	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
franta-hg@17
    40
	private PrintStream out;
franta-hg@20
    41
	private ConfigurationProvider configurationProvider;
franta-hg@17
    42
franta-hg@20
    43
	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
franta-hg@17
    44
		this.out = out;
franta-hg@20
    45
		this.configurationProvider = configurationProvider;
franta-hg@17
    46
	}
franta-hg@17
    47
franta-hg@66
    48
	public void showInfo(CLIOptions options) throws ConfigurationException {
franta-hg@15
    49
		EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
franta-hg@14
    50
		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
franta-hg@14
    51
			switch (infoType) {
franta-hg@14
    52
				/**
franta-hg@14
    53
				 * TODO: implement show info
franta-hg@14
    54
				 */
franta-hg@14
    55
				case FORMATTERS:
franta-hg@17
    56
					println("TODO: list available formatters");
franta-hg@14
    57
					break;
franta-hg@14
    58
				case HELP:
franta-hg@19
    59
					printResource(Constants.HELP_FILE);
franta-hg@14
    60
					break;
franta-hg@14
    61
				case LICENSE:
franta-hg@18
    62
					printResource(Constants.LICENSE_FILE);
franta-hg@14
    63
					break;
franta-hg@14
    64
				case TYPES:
franta-hg@17
    65
					println("TODO: list supported types");
franta-hg@14
    66
					break;
franta-hg@14
    67
				case VERSION:
franta-hg@18
    68
					printResource(Constants.VERSION_FILE);
franta-hg@14
    69
					break;
franta-hg@15
    70
				case DATABASES:
franta-hg@66
    71
					final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
franta-hg@66
    72
					if (configuredDatabases.isEmpty()) {
franta-hg@66
    73
						log.log(Level.WARNING, "No databases are configured.");
franta-hg@66
    74
					} else {
franta-hg@66
    75
						for (DatabaseDefinition dd : configuredDatabases) {
franta-hg@66
    76
							log.log(Level.INFO, "Configured database: {0}", dd.getName());
franta-hg@66
    77
						}
franta-hg@66
    78
					}
franta-hg@15
    79
					break;
franta-hg@15
    80
				case CONNECTION:
franta-hg@65
    81
					boolean connectionTestResult = false;
franta-hg@65
    82
					String dbName = options.getDatabaseNameToTest();
franta-hg@65
    83
					log.log(Level.FINE, "Testing connection to database: {0}", dbName);
franta-hg@65
    84
					try {
franta-hg@65
    85
						DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
franta-hg@65
    86
						if (dd == null) {
franta-hg@65
    87
							log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
franta-hg@65
    88
						} else {
franta-hg@65
    89
							log.log(Level.FINE, "Database definition was loaded from configuration");
franta-hg@65
    90
							DatabaseConnection dc = dd.connect();
franta-hg@65
    91
							connectionTestResult = dc.test();
franta-hg@65
    92
						}
franta-hg@65
    93
					} catch (ConfigurationException | SQLException e) {
franta-hg@65
    94
						log.log(Level.SEVERE, "Error during testing connection", e);
franta-hg@65
    95
					}
franta-hg@65
    96
					log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
franta-hg@15
    97
					break;
franta-hg@14
    98
				default:
franta-hg@14
    99
					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
franta-hg@14
   100
			}
franta-hg@14
   101
		}
franta-hg@14
   102
	}
franta-hg@17
   103
franta-hg@18
   104
	private void printResource(String fileName) {
franta-hg@18
   105
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
franta-hg@17
   106
			while (true) {
franta-hg@18
   107
				String line = reader.readLine();
franta-hg@17
   108
				if (line == null) {
franta-hg@17
   109
					break;
franta-hg@17
   110
				} else {
franta-hg@17
   111
					println(line);
franta-hg@17
   112
				}
franta-hg@17
   113
			}
franta-hg@17
   114
		} catch (Exception e) {
franta-hg@18
   115
			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
franta-hg@17
   116
		}
franta-hg@17
   117
	}
franta-hg@17
   118
franta-hg@17
   119
	private void println(String line) {
franta-hg@17
   120
		out.println(line);
franta-hg@17
   121
	}
franta-hg@14
   122
}