java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 01 Jan 2014 01:06:38 +0100
branchv_0
changeset 106 e9c3583580c8
parent 104 245f1b88a3e6
child 139 5c0e344c3b60
permissions -rw-r--r--
use database properties
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@69
    25
import info.globalcode.sql.dk.formatting.ColumnsHeader;
franta-hg@69
    26
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@69
    27
import info.globalcode.sql.dk.formatting.FormatterContext;
franta-hg@69
    28
import info.globalcode.sql.dk.formatting.FormatterException;
franta-hg@17
    29
import java.io.BufferedReader;
franta-hg@17
    30
import java.io.InputStreamReader;
franta-hg@14
    31
import java.io.PrintStream;
franta-hg@65
    32
import java.sql.SQLException;
franta-hg@70
    33
import java.util.ArrayList;
franta-hg@70
    34
import java.util.EnumSet;
franta-hg@66
    35
import java.util.List;
franta-hg@17
    36
import java.util.logging.Level;
franta-hg@17
    37
import java.util.logging.Logger;
franta-hg@69
    38
import javax.sql.rowset.RowSetMetaDataImpl;
franta-hg@14
    39
franta-hg@14
    40
/**
franta-hg@14
    41
 * Displays info like help, version etc.
franta-hg@14
    42
 *
franta-hg@14
    43
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@14
    44
 */
franta-hg@14
    45
public class InfoLister {
franta-hg@14
    46
franta-hg@17
    47
	private static final Logger log = Logger.getLogger(InfoLister.class.getName());
franta-hg@17
    48
	private PrintStream out;
franta-hg@20
    49
	private ConfigurationProvider configurationProvider;
franta-hg@69
    50
	private CLIOptions options;
franta-hg@70
    51
	private Formatter formatter;
franta-hg@17
    52
franta-hg@69
    53
	public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
franta-hg@17
    54
		this.out = out;
franta-hg@20
    55
		this.configurationProvider = configurationProvider;
franta-hg@69
    56
		this.options = options;
franta-hg@17
    57
	}
franta-hg@17
    58
franta-hg@70
    59
	public void showInfo() throws ConfigurationException, FormatterException {
franta-hg@70
    60
		EnumSet<InfoType> commands = options.getShowInfo();
franta-hg@70
    61
franta-hg@70
    62
		for (InfoType infoType : commands) {
franta-hg@70
    63
			switch (infoType) {
franta-hg@74
    64
				// only these need formatted output
franta-hg@70
    65
				case CONNECTION:
franta-hg@70
    66
				case DATABASES:
franta-hg@70
    67
				case FORMATTERS:
franta-hg@70
    68
				case TYPES:
franta-hg@101
    69
					try (Formatter f = getFormatter()) {
franta-hg@101
    70
						formatter = f;
franta-hg@101
    71
						formatter.writeStartBatch();
franta-hg@101
    72
						formatter.writeStartDatabase(new DatabaseDefinition());
franta-hg@101
    73
						showInfos(commands);
franta-hg@101
    74
						formatter.writeEndDatabase();
franta-hg@101
    75
						formatter.writeEndBatch();
franta-hg@101
    76
						formatter.close();
franta-hg@101
    77
					}
franta-hg@101
    78
					break;
franta-hg@101
    79
				default:
franta-hg@101
    80
					showInfos(commands);
franta-hg@70
    81
			}
franta-hg@70
    82
		}
franta-hg@101
    83
	}
franta-hg@70
    84
franta-hg@101
    85
	private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
franta-hg@70
    86
		for (InfoType infoType : commands) {
franta-hg@70
    87
			infoType.showInfo(this);
franta-hg@70
    88
		}
franta-hg@70
    89
	}
franta-hg@70
    90
franta-hg@72
    91
	private void listFormatters() throws ConfigurationException, FormatterException {
franta-hg@72
    92
		ColumnsHeader header = constructHeader(
franta-hg@72
    93
				new HeaderField("name", SQLType.VARCHAR),
franta-hg@72
    94
				new HeaderField("built_in", SQLType.BOOLEAN),
franta-hg@72
    95
				new HeaderField("default", SQLType.BOOLEAN),
franta-hg@72
    96
				new HeaderField("class_name", SQLType.VARCHAR));
franta-hg@72
    97
		List<Object[]> data = new ArrayList<>();
franta-hg@72
    98
franta-hg@72
    99
		String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
franta-hg@72
   100
		defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
franta-hg@72
   101
franta-hg@69
   102
		for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
franta-hg@72
   103
			data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
franta-hg@69
   104
		}
franta-hg@72
   105
franta-hg@72
   106
		for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
franta-hg@72
   107
			data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
franta-hg@69
   108
		}
franta-hg@72
   109
franta-hg@72
   110
		printTable(formatter, header, data);
franta-hg@72
   111
franta-hg@72
   112
franta-hg@69
   113
	}
franta-hg@67
   114
franta-hg@70
   115
	public void listTypes() throws FormatterException, ConfigurationException {
franta-hg@70
   116
		ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
franta-hg@70
   117
		List<Object[]> data = new ArrayList<>();
franta-hg@70
   118
		for (SQLType sqlType : SQLType.values()) {
franta-hg@70
   119
			data.add(new Object[]{sqlType.name(), sqlType.getCode()});
franta-hg@70
   120
		}
franta-hg@70
   121
		printTable(formatter, header, data);
franta-hg@93
   122
		log.log(Level.INFO, "Type names in --types option are case insensitive");
franta-hg@69
   123
	}
franta-hg@67
   124
franta-hg@72
   125
	public void listDatabases() throws ConfigurationException, FormatterException {
franta-hg@72
   126
		ColumnsHeader header = constructHeader(
franta-hg@72
   127
				new HeaderField("database_name", SQLType.VARCHAR),
franta-hg@72
   128
				new HeaderField("user_name", SQLType.VARCHAR),
franta-hg@72
   129
				new HeaderField("database_url", SQLType.VARCHAR));
franta-hg@72
   130
		List<Object[]> data = new ArrayList<>();
franta-hg@72
   131
franta-hg@69
   132
		final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
franta-hg@69
   133
		if (configuredDatabases.isEmpty()) {
franta-hg@69
   134
			log.log(Level.WARNING, "No databases are configured.");
franta-hg@69
   135
		} else {
franta-hg@69
   136
			for (DatabaseDefinition dd : configuredDatabases) {
franta-hg@72
   137
				data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
franta-hg@14
   138
			}
franta-hg@14
   139
		}
franta-hg@72
   140
franta-hg@72
   141
		printTable(formatter, header, data);
franta-hg@14
   142
	}
franta-hg@17
   143
franta-hg@73
   144
	public void testConnection() throws FormatterException, ConfigurationException {
franta-hg@73
   145
		ColumnsHeader header = constructHeader(
franta-hg@73
   146
				new HeaderField("database_name", SQLType.VARCHAR),
franta-hg@74
   147
				new HeaderField("configured", SQLType.BOOLEAN),
franta-hg@74
   148
				new HeaderField("connected", SQLType.BOOLEAN));
franta-hg@73
   149
		List<Object[]> data = new ArrayList<>();
franta-hg@73
   150
franta-hg@74
   151
		for (String dbName : options.getDatabaseNameToTest()) {
franta-hg@74
   152
			data.add(testConnection(dbName));
franta-hg@74
   153
		}
franta-hg@73
   154
franta-hg@73
   155
		printTable(formatter, header, data);
franta-hg@73
   156
	}
franta-hg@73
   157
franta-hg@73
   158
	public Object[] testConnection(String dbName) {
franta-hg@69
   159
		log.log(Level.FINE, "Testing connection to database: {0}", dbName);
franta-hg@73
   160
franta-hg@73
   161
		boolean succesfullyConnected = false;
franta-hg@73
   162
		boolean succesfullyConfigured = false;
franta-hg@73
   163
franta-hg@69
   164
		try {
franta-hg@69
   165
			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
franta-hg@75
   166
			log.log(Level.FINE, "Database definition was loaded from configuration");
franta-hg@75
   167
			succesfullyConfigured = true;
franta-hg@106
   168
			try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
franta-hg@75
   169
				succesfullyConnected = dc.test();
franta-hg@69
   170
			}
franta-hg@75
   171
			log.log(Level.FINE, "Database connection test was successful");
franta-hg@69
   172
		} catch (ConfigurationException | SQLException e) {
franta-hg@69
   173
			log.log(Level.SEVERE, "Error during testing connection", e);
franta-hg@69
   174
		}
franta-hg@73
   175
franta-hg@73
   176
		return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
franta-hg@69
   177
	}
franta-hg@69
   178
franta-hg@69
   179
	public void printResource(String fileName) {
franta-hg@18
   180
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
franta-hg@17
   181
			while (true) {
franta-hg@18
   182
				String line = reader.readLine();
franta-hg@17
   183
				if (line == null) {
franta-hg@17
   184
					break;
franta-hg@17
   185
				} else {
franta-hg@17
   186
					println(line);
franta-hg@17
   187
				}
franta-hg@17
   188
			}
franta-hg@17
   189
		} catch (Exception e) {
franta-hg@18
   190
			log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
franta-hg@17
   191
		}
franta-hg@17
   192
	}
franta-hg@17
   193
franta-hg@17
   194
	private void println(String line) {
franta-hg@17
   195
		out.println(line);
franta-hg@17
   196
	}
franta-hg@69
   197
franta-hg@69
   198
	private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
franta-hg@69
   199
		formatter.writeStartResultSet();
franta-hg@69
   200
		formatter.writeColumnsHeader(header);
franta-hg@69
   201
franta-hg@69
   202
		for (Object[] row : data) {
franta-hg@69
   203
			formatter.writeStartRow();
franta-hg@69
   204
			for (Object cell : row) {
franta-hg@69
   205
				formatter.writeColumnValue(cell);
franta-hg@69
   206
			}
franta-hg@69
   207
			formatter.writeEndRow();
franta-hg@69
   208
		}
franta-hg@69
   209
franta-hg@69
   210
		formatter.writeEndResultSet();
franta-hg@69
   211
	}
franta-hg@69
   212
franta-hg@69
   213
	private Formatter getFormatter() throws ConfigurationException, FormatterException {
franta-hg@89
   214
		String formatterName = options.getFormatterName();
franta-hg@89
   215
		formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
franta-hg@89
   216
		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
franta-hg@104
   217
		FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
franta-hg@69
   218
		return fd.getInstance(context);
franta-hg@69
   219
	}
franta-hg@69
   220
franta-hg@69
   221
	private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
franta-hg@69
   222
		try {
franta-hg@69
   223
			RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
franta-hg@69
   224
			metaData.setColumnCount(fields.length);
franta-hg@69
   225
franta-hg@69
   226
			for (int i = 0; i < fields.length; i++) {
franta-hg@69
   227
				HeaderField hf = fields[i];
franta-hg@69
   228
				int sqlIndex = i + 1;
franta-hg@69
   229
				metaData.setColumnName(sqlIndex, hf.name);
franta-hg@69
   230
				metaData.setColumnLabel(sqlIndex, hf.name);
franta-hg@69
   231
				metaData.setColumnType(sqlIndex, hf.type.getCode());
franta-hg@69
   232
				metaData.setColumnTypeName(sqlIndex, hf.type.name());
franta-hg@69
   233
			}
franta-hg@69
   234
franta-hg@69
   235
			return new ColumnsHeader(metaData);
franta-hg@69
   236
		} catch (SQLException e) {
franta-hg@69
   237
			throw new FormatterException("Error while constructing table headers", e);
franta-hg@69
   238
		}
franta-hg@69
   239
	}
franta-hg@69
   240
franta-hg@69
   241
	private static class HeaderField {
franta-hg@69
   242
franta-hg@69
   243
		String name;
franta-hg@69
   244
		SQLType type;
franta-hg@69
   245
franta-hg@69
   246
		public HeaderField(String name, SQLType type) {
franta-hg@69
   247
			this.name = name;
franta-hg@69
   248
			this.type = type;
franta-hg@69
   249
		}
franta-hg@69
   250
	}
franta-hg@69
   251
franta-hg@69
   252
	public enum InfoType {
franta-hg@69
   253
franta-hg@69
   254
		HELP {
franta-hg@69
   255
			@Override
franta-hg@69
   256
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   257
				infoLister.printResource(Constants.HELP_FILE);
franta-hg@69
   258
			}
franta-hg@69
   259
		},
franta-hg@69
   260
		VERSION {
franta-hg@69
   261
			@Override
franta-hg@69
   262
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   263
				infoLister.printResource(Constants.VERSION_FILE);
franta-hg@69
   264
			}
franta-hg@69
   265
		},
franta-hg@69
   266
		LICENSE {
franta-hg@69
   267
			@Override
franta-hg@69
   268
			public void showInfo(InfoLister infoLister) {
franta-hg@69
   269
				infoLister.printResource(Constants.LICENSE_FILE);
franta-hg@69
   270
			}
franta-hg@69
   271
		},
franta-hg@69
   272
		FORMATTERS {
franta-hg@69
   273
			@Override
franta-hg@70
   274
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   275
				infoLister.listFormatters();
franta-hg@69
   276
			}
franta-hg@69
   277
		},
franta-hg@69
   278
		TYPES {
franta-hg@69
   279
			@Override
franta-hg@70
   280
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   281
				infoLister.listTypes();
franta-hg@69
   282
			}
franta-hg@69
   283
		},
franta-hg@69
   284
		DATABASES {
franta-hg@69
   285
			@Override
franta-hg@70
   286
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   287
				infoLister.listDatabases();
franta-hg@69
   288
			}
franta-hg@69
   289
		},
franta-hg@69
   290
		CONNECTION {
franta-hg@69
   291
			@Override
franta-hg@73
   292
			public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
franta-hg@69
   293
				infoLister.testConnection();
franta-hg@69
   294
			}
franta-hg@69
   295
		};
franta-hg@69
   296
franta-hg@69
   297
		public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
franta-hg@69
   298
	}
franta-hg@14
   299
}