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.Configuration;
21 import info.globalcode.sql.dk.configuration.ConfigurationException;
22 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
23 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
24 import info.globalcode.sql.dk.configuration.FormatterDefinition;
25 import info.globalcode.sql.dk.formatting.ColumnsHeader;
26 import info.globalcode.sql.dk.formatting.Formatter;
27 import info.globalcode.sql.dk.formatting.FormatterContext;
28 import info.globalcode.sql.dk.formatting.FormatterException;
29 import java.io.BufferedReader;
30 import java.io.InputStreamReader;
31 import java.io.PrintStream;
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34 import java.util.EnumSet;
35 import java.util.List;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import javax.sql.rowset.RowSetMetaDataImpl;
41 * Displays info like help, version etc.
43 * @author Ing. František Kučera (frantovo.cz)
45 public class InfoLister {
47 private static final Logger log = Logger.getLogger(InfoLister.class.getName());
48 private PrintStream out;
49 private ConfigurationProvider configurationProvider;
50 private CLIOptions options;
51 private Formatter formatter;
53 public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
55 this.configurationProvider = configurationProvider;
56 this.options = options;
59 public void showInfo() throws ConfigurationException, FormatterException {
60 EnumSet<InfoType> commands = options.getShowInfo();
62 for (InfoType infoType : commands) {
64 // only these needs formatted output
69 formatter = getFormatter();
70 formatter.writeStartDatabase(new DatabaseDefinition());
74 for (InfoType infoType : commands) {
75 infoType.showInfo(this);
78 if (formatter != null) {
79 formatter.writeEndDatabase();
83 private void listFormatters() throws ConfigurationException, FormatterException {
84 ColumnsHeader header = constructHeader(
85 new HeaderField("name", SQLType.VARCHAR),
86 new HeaderField("built_in", SQLType.BOOLEAN),
87 new HeaderField("default", SQLType.BOOLEAN),
88 new HeaderField("class_name", SQLType.VARCHAR));
89 List<Object[]> data = new ArrayList<>();
91 String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
92 defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
94 for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
95 data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
98 for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
99 data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
102 printTable(formatter, header, data);
107 public void listTypes() throws FormatterException, ConfigurationException {
108 ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
109 List<Object[]> data = new ArrayList<>();
110 for (SQLType sqlType : SQLType.values()) {
111 data.add(new Object[]{sqlType.name(), sqlType.getCode()});
113 printTable(formatter, header, data);
116 public void listDatabases() throws ConfigurationException, FormatterException {
117 ColumnsHeader header = constructHeader(
118 new HeaderField("database_name", SQLType.VARCHAR),
119 new HeaderField("user_name", SQLType.VARCHAR),
120 new HeaderField("database_url", SQLType.VARCHAR));
121 List<Object[]> data = new ArrayList<>();
123 final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
124 if (configuredDatabases.isEmpty()) {
125 log.log(Level.WARNING, "No databases are configured.");
127 for (DatabaseDefinition dd : configuredDatabases) {
128 data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
132 printTable(formatter, header, data);
135 public void testConnection() {
136 boolean connectionTestResult = false;
137 String dbName = options.getDatabaseNameToTest();
138 log.log(Level.FINE, "Testing connection to database: {0}", dbName);
140 DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
142 log.log(Level.SEVERE, "No database with this name is configured: {0}", dbName);
144 log.log(Level.FINE, "Database definition was loaded from configuration");
145 DatabaseConnection dc = dd.connect();
146 connectionTestResult = dc.test();
148 } catch (ConfigurationException | SQLException e) {
149 log.log(Level.SEVERE, "Error during testing connection", e);
151 log.log(Level.INFO, "Connection test result: {0}", connectionTestResult ? "success" : "failure");
154 public void printResource(String fileName) {
155 try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
157 String line = reader.readLine();
164 } catch (Exception e) {
165 log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
169 private void println(String line) {
173 private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
174 formatter.writeStartResultSet();
175 formatter.writeColumnsHeader(header);
177 for (Object[] row : data) {
178 formatter.writeStartRow();
179 for (Object cell : row) {
180 formatter.writeColumnValue(cell);
182 formatter.writeEndRow();
185 formatter.writeEndResultSet();
188 private Formatter getFormatter() throws ConfigurationException, FormatterException {
189 FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(options.getFormatterName());
190 FormatterContext context = new FormatterContext(out);
191 return fd.getInstance(context);
194 private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
196 RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
197 metaData.setColumnCount(fields.length);
199 for (int i = 0; i < fields.length; i++) {
200 HeaderField hf = fields[i];
201 int sqlIndex = i + 1;
202 metaData.setColumnName(sqlIndex, hf.name);
203 metaData.setColumnLabel(sqlIndex, hf.name);
204 metaData.setColumnType(sqlIndex, hf.type.getCode());
205 metaData.setColumnTypeName(sqlIndex, hf.type.name());
208 return new ColumnsHeader(metaData);
209 } catch (SQLException e) {
210 throw new FormatterException("Error while constructing table headers", e);
214 private static class HeaderField {
219 public HeaderField(String name, SQLType type) {
225 public enum InfoType {
229 public void showInfo(InfoLister infoLister) {
230 infoLister.printResource(Constants.HELP_FILE);
235 public void showInfo(InfoLister infoLister) {
236 infoLister.printResource(Constants.VERSION_FILE);
241 public void showInfo(InfoLister infoLister) {
242 infoLister.printResource(Constants.LICENSE_FILE);
247 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
248 infoLister.listFormatters();
253 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
254 infoLister.listTypes();
259 public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
260 infoLister.listDatabases();
265 public void showInfo(InfoLister infoLister) {
266 infoLister.testConnection();
270 public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;