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