franta-hg@16: /** franta-hg@16: * SQL-DK franta-hg@16: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@16: * franta-hg@16: * This program is free software: you can redistribute it and/or modify franta-hg@16: * it under the terms of the GNU General Public License as published by franta-hg@16: * the Free Software Foundation, either version 3 of the License, or franta-hg@16: * (at your option) any later version. franta-hg@16: * franta-hg@16: * This program is distributed in the hope that it will be useful, franta-hg@16: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@16: * GNU General Public License for more details. franta-hg@16: * franta-hg@16: * You should have received a copy of the GNU General Public License franta-hg@16: * along with this program. If not, see . franta-hg@16: */ franta-hg@14: package info.globalcode.sql.dk; franta-hg@14: franta-hg@67: import info.globalcode.sql.dk.configuration.Configuration; franta-hg@65: import info.globalcode.sql.dk.configuration.ConfigurationException; franta-hg@26: import info.globalcode.sql.dk.configuration.ConfigurationProvider; franta-hg@65: import info.globalcode.sql.dk.configuration.DatabaseDefinition; franta-hg@67: import info.globalcode.sql.dk.configuration.FormatterDefinition; franta-hg@67: import static info.globalcode.sql.dk.Functions.rpad; franta-hg@17: import java.io.BufferedReader; franta-hg@17: import java.io.InputStreamReader; franta-hg@14: import java.io.PrintStream; franta-hg@65: import java.sql.SQLException; franta-hg@14: import java.util.EnumSet; franta-hg@66: import java.util.List; franta-hg@17: import java.util.logging.Level; franta-hg@17: import java.util.logging.Logger; franta-hg@14: franta-hg@14: /** franta-hg@14: * Displays info like help, version etc. franta-hg@14: * franta-hg@14: * @author Ing. František Kučera (frantovo.cz) franta-hg@14: */ franta-hg@14: public class InfoLister { franta-hg@14: franta-hg@17: private static final Logger log = Logger.getLogger(InfoLister.class.getName()); franta-hg@17: private PrintStream out; franta-hg@20: private ConfigurationProvider configurationProvider; franta-hg@17: franta-hg@20: public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) { franta-hg@17: this.out = out; franta-hg@20: this.configurationProvider = configurationProvider; franta-hg@17: } franta-hg@17: franta-hg@66: public void showInfo(CLIOptions options) throws ConfigurationException { franta-hg@15: EnumSet infoTypes = options.getShowInfo(); franta-hg@14: for (CLIOptions.INFO_TYPE infoType : infoTypes) { franta-hg@14: switch (infoType) { franta-hg@14: /** franta-hg@14: * TODO: implement show info franta-hg@14: */ franta-hg@14: case FORMATTERS: franta-hg@67: for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) { franta-hg@67: log.log(Level.INFO, "Built-in formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()}); franta-hg@67: } franta-hg@67: franta-hg@67: List configuredFormatters = configurationProvider.getConfiguration().getFormatters(); franta-hg@67: for (FormatterDefinition fd : configuredFormatters) { franta-hg@67: log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()}); franta-hg@67: } franta-hg@67: if (configuredFormatters.isEmpty()) { franta-hg@67: log.log(Level.INFO, "No other formatters are configured"); franta-hg@67: } franta-hg@67: franta-hg@67: String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter(); franta-hg@67: if (configuredDefaultFormatter == null) { franta-hg@67: log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER); franta-hg@67: } else { franta-hg@67: log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter); franta-hg@67: } franta-hg@14: break; franta-hg@14: case HELP: franta-hg@19: printResource(Constants.HELP_FILE); franta-hg@14: break; franta-hg@14: case LICENSE: franta-hg@18: printResource(Constants.LICENSE_FILE); franta-hg@14: break; franta-hg@14: case TYPES: franta-hg@17: println("TODO: list supported types"); franta-hg@14: break; franta-hg@14: case VERSION: franta-hg@18: printResource(Constants.VERSION_FILE); franta-hg@14: break; franta-hg@15: case DATABASES: franta-hg@66: final List configuredDatabases = configurationProvider.getConfiguration().getDatabases(); franta-hg@66: if (configuredDatabases.isEmpty()) { franta-hg@66: log.log(Level.WARNING, "No databases are configured."); franta-hg@66: } else { franta-hg@66: for (DatabaseDefinition dd : configuredDatabases) { franta-hg@66: log.log(Level.INFO, "Configured database: {0}", dd.getName()); franta-hg@66: } franta-hg@66: } franta-hg@15: break; franta-hg@15: case CONNECTION: franta-hg@65: boolean connectionTestResult = false; franta-hg@65: String dbName = options.getDatabaseNameToTest(); franta-hg@65: log.log(Level.FINE, "Testing connection to database: {0}", dbName); franta-hg@65: try { franta-hg@65: DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName); franta-hg@65: if (dd == null) { franta-hg@65: log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName); franta-hg@65: } else { franta-hg@65: log.log(Level.FINE, "Database definition was loaded from configuration"); franta-hg@65: DatabaseConnection dc = dd.connect(); franta-hg@65: connectionTestResult = dc.test(); franta-hg@65: } franta-hg@65: } catch (ConfigurationException | SQLException e) { franta-hg@65: log.log(Level.SEVERE, "Error during testing connection", e); franta-hg@65: } franta-hg@65: log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure"); franta-hg@15: break; franta-hg@14: default: franta-hg@14: throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType); franta-hg@14: } franta-hg@14: } franta-hg@14: } franta-hg@17: franta-hg@18: private void printResource(String fileName) { franta-hg@18: try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) { franta-hg@17: while (true) { franta-hg@18: String line = reader.readLine(); franta-hg@17: if (line == null) { franta-hg@17: break; franta-hg@17: } else { franta-hg@17: println(line); franta-hg@17: } franta-hg@17: } franta-hg@17: } catch (Exception e) { franta-hg@18: log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e); franta-hg@17: } franta-hg@17: } franta-hg@17: franta-hg@17: private void println(String line) { franta-hg@17: out.println(line); franta-hg@17: } franta-hg@14: }