3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk;
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;
33 * Displays info like help, version etc.
35 * @author Ing. František Kučera (frantovo.cz)
37 public class InfoLister {
39 private static final Logger log = Logger.getLogger(InfoLister.class.getName());
40 private PrintStream out;
41 private ConfigurationProvider configurationProvider;
43 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
45 this.configurationProvider = configurationProvider;
48 public void showInfo(CLIOptions options) throws ConfigurationException {
49 EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
50 for (CLIOptions.INFO_TYPE infoType : infoTypes) {
53 * TODO: implement show info
56 println("TODO: list available formatters");
59 printResource(Constants.HELP_FILE);
62 printResource(Constants.LICENSE_FILE);
65 println("TODO: list supported types");
68 printResource(Constants.VERSION_FILE);
71 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
72 if (configuredDatabases.isEmpty()) {
73 log.log(Level.WARNING, "No databases are configured.");
75 for (DatabaseDefinition dd : configuredDatabases) {
76 log.log(Level.INFO, "Configured database: {0}", dd.getName());
81 boolean connectionTestResult = false;
82 String dbName = options.getDatabaseNameToTest();
83 log.log(Level.FINE, "Testing connection to database: {0}", dbName);
85 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
87 log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
89 log.log(Level.FINE, "Database definition was loaded from configuration");
90 DatabaseConnection dc = dd.connect();
91 connectionTestResult = dc.test();
93 } catch (ConfigurationException | SQLException e) {
94 log.log(Level.SEVERE, "Error during testing connection", e);
96 log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
99 throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
104 private void printResource(String fileName) {
105 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
107 String line = reader.readLine();
114 } catch (Exception e) {
115 log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
119 private void println(String line) {