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