java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 21 Dec 2013 20:38:50 +0100
branchv_0
changeset 26 4ec8e5534eb9
parent 20 e225bdcd260e
child 65 f05be87239ad
permissions -rw-r--r--
configuration basics
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     1
/**
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     2
 * SQL-DK
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     4
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     8
 * (at your option) any later version.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     9
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    13
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    14
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    15
 * You should have received a copy of the GNU General Public License
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    17
 */
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
26
4ec8e5534eb9 configuration basics
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    20
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    21
import java.io.BufferedReader;
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    22
import java.io.InputStreamReader;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.io.PrintStream;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.util.EnumSet;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    25
import java.util.logging.Level;
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    26
import java.util.logging.Logger;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
/**
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * Displays info like help, version etc.
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 *
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 * @author Ing. František Kučera (frantovo.cz)
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 */
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
public class InfoLister {
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    35
	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    36
	private PrintStream out;
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    37
	private ConfigurationProvider configurationProvider;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    38
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    39
	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) {
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    40
		this.out = out;
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    41
		this.configurationProvider = configurationProvider;
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    42
	}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    43
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    44
	public void showInfo(CLIOptions options) {
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    45
		EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo();
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
			switch (infoType) {
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
				/**
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
				 * TODO: implement show info
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
				 */
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
				case FORMATTERS:
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    52
					println("TODO: list available formatters");
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
				case HELP:
19
873669135d97 help option: --help – basics
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    55
					printResource(Constants.HELP_FILE);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
				case LICENSE:
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    58
					printResource(Constants.LICENSE_FILE);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
				case TYPES:
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    61
					println("TODO: list supported types");
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
				case VERSION:
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    64
					printResource(Constants.VERSION_FILE);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
					break;
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    66
				case DATABASES:
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    67
					println("TODO: list databases");
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    68
					break;
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    69
				case CONNECTION:
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    70
					println("TODO: test database connection: " + options.getDatabaseNameToTest());
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    71
					break;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				default:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			}
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		}
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	}
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    77
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    78
	private void printResource(String fileName) {
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    79
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    80
			while (true) {
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    81
				String line = reader.readLine();
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    82
				if (line == null) {
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    83
					break;
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    84
				} else {
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    85
					println(line);
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    86
				}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    87
			}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    88
		} catch (Exception e) {
18
7900bb1666f6 version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    89
			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
17
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    90
		}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    91
	}
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    92
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    93
	private void println(String line) {
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    94
		out.println(line);
d8ab8aece6f2 license option: --license
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    95
	}
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
}