author | František Kučera <franta-hg@frantovo.cz> |
Thu, 26 Dec 2013 01:53:15 +0100 | |
branch | v_0 |
changeset 67 | 10c9b9e54622 |
parent 66 | 6e28893eaada |
child 69 | 0befec5034c2 |
permissions | -rw-r--r-- |
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 | 18 |
package info.globalcode.sql.dk; |
19 |
||
67
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
20 |
import info.globalcode.sql.dk.configuration.Configuration; |
65
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
21 |
import info.globalcode.sql.dk.configuration.ConfigurationException; |
26
4ec8e5534eb9
configuration basics
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
22 |
import info.globalcode.sql.dk.configuration.ConfigurationProvider; |
65
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
23 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
67
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
24 |
import info.globalcode.sql.dk.configuration.FormatterDefinition; |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
25 |
import static info.globalcode.sql.dk.Functions.rpad; |
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
26 |
import java.io.BufferedReader; |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
27 |
import java.io.InputStreamReader; |
14 | 28 |
import java.io.PrintStream; |
65
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
29 |
import java.sql.SQLException; |
14 | 30 |
import java.util.EnumSet; |
66
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
31 |
import java.util.List; |
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
32 |
import java.util.logging.Level; |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
33 |
import java.util.logging.Logger; |
14 | 34 |
|
35 |
/** |
|
36 |
* Displays info like help, version etc. |
|
37 |
* |
|
38 |
* @author Ing. František Kučera (frantovo.cz) |
|
39 |
*/ |
|
40 |
public class InfoLister { |
|
41 |
||
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
42 |
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
|
43 |
private PrintStream out; |
20
e225bdcd260e
refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
44 |
private ConfigurationProvider configurationProvider; |
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
45 |
|
20
e225bdcd260e
refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
46 |
public InfoLister(PrintStream out, ConfigurationProvider configurationProvider) { |
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
47 |
this.out = out; |
20
e225bdcd260e
refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
48 |
this.configurationProvider = configurationProvider; |
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
49 |
} |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
50 |
|
66
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
51 |
public void showInfo(CLIOptions options) throws ConfigurationException { |
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
52 |
EnumSet<CLIOptions.INFO_TYPE> infoTypes = options.getShowInfo(); |
14 | 53 |
for (CLIOptions.INFO_TYPE infoType : infoTypes) { |
54 |
switch (infoType) { |
|
55 |
/** |
|
56 |
* TODO: implement show info |
|
57 |
*/ |
|
58 |
case FORMATTERS: |
|
67
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
59 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) { |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
60 |
log.log(Level.INFO, "Built-in formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()}); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
61 |
} |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
62 |
|
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
63 |
List<FormatterDefinition> configuredFormatters = configurationProvider.getConfiguration().getFormatters(); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
64 |
for (FormatterDefinition fd : configuredFormatters) { |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
65 |
log.log(Level.INFO, "Configured formatter: {0} implemented by class: {1}", new Object[]{rpad(fd.getName(), 16), fd.getClassName()}); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
66 |
} |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
67 |
if (configuredFormatters.isEmpty()) { |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
68 |
log.log(Level.INFO, "No other formatters are configured"); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
69 |
} |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
70 |
|
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
71 |
String configuredDefaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter(); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
72 |
if (configuredDefaultFormatter == null) { |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
73 |
log.log(Level.INFO, "Built-in default formatter: {0}", Configuration.DEFAULT_FORMATTER); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
74 |
} else { |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
75 |
log.log(Level.INFO, "Configured default formatter: {0}", configuredDefaultFormatter); |
10c9b9e54622
option --list-formatters – list configured and built-in formatters
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
76 |
} |
14 | 77 |
break; |
78 |
case HELP: |
|
19
873669135d97
help option: --help – basics
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
79 |
printResource(Constants.HELP_FILE); |
14 | 80 |
break; |
81 |
case LICENSE: |
|
18
7900bb1666f6
version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
82 |
printResource(Constants.LICENSE_FILE); |
14 | 83 |
break; |
84 |
case TYPES: |
|
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
85 |
println("TODO: list supported types"); |
14 | 86 |
break; |
87 |
case VERSION: |
|
18
7900bb1666f6
version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
88 |
printResource(Constants.VERSION_FILE); |
14 | 89 |
break; |
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
90 |
case DATABASES: |
66
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
91 |
final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases(); |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
92 |
if (configuredDatabases.isEmpty()) { |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
93 |
log.log(Level.WARNING, "No databases are configured."); |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
94 |
} else { |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
95 |
for (DatabaseDefinition dd : configuredDatabases) { |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
96 |
log.log(Level.INFO, "Configured database: {0}", dd.getName()); |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
97 |
} |
6e28893eaada
option --list-databases list configured databases
František Kučera <franta-hg@frantovo.cz>
parents:
65
diff
changeset
|
98 |
} |
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
99 |
break; |
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
100 |
case CONNECTION: |
65
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
101 |
boolean connectionTestResult = false; |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
102 |
String dbName = options.getDatabaseNameToTest(); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
103 |
log.log(Level.FINE, "Testing connection to database: {0}", dbName); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
104 |
try { |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
105 |
DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
106 |
if (dd == null) { |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
107 |
log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
108 |
} else { |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
109 |
log.log(Level.FINE, "Database definition was loaded from configuration"); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
110 |
DatabaseConnection dc = dd.connect(); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
111 |
connectionTestResult = dc.test(); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
112 |
} |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
113 |
} catch (ConfigurationException | SQLException e) { |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
114 |
log.log(Level.SEVERE, "Error during testing connection", e); |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
115 |
} |
f05be87239ad
option --test-connection – tests connection to given database
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
116 |
log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure"); |
15
bbd335b5410c
show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
117 |
break; |
14 | 118 |
default: |
119 |
throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
123 |
|
18
7900bb1666f6
version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
124 |
private void printResource(String fileName) { |
7900bb1666f6
version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
125 |
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
|
126 |
while (true) { |
18
7900bb1666f6
version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
127 |
String line = reader.readLine(); |
17
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
128 |
if (line == null) { |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
129 |
break; |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
130 |
} else { |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
131 |
println(line); |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
132 |
} |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
133 |
} |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
134 |
} catch (Exception e) { |
18
7900bb1666f6
version info: option --version
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
135 |
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
|
136 |
} |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
137 |
} |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
138 |
|
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
139 |
private void println(String line) { |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
140 |
out.println(line); |
d8ab8aece6f2
license option: --license
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
141 |
} |
14 | 142 |
} |