franta-hg@16: /**
franta-hg@16: * SQL-DK
franta-hg@16: * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16: *
franta-hg@16: * This program is free software: you can redistribute it and/or modify
franta-hg@16: * it under the terms of the GNU General Public License as published by
franta-hg@16: * the Free Software Foundation, either version 3 of the License, or
franta-hg@16: * (at your option) any later version.
franta-hg@16: *
franta-hg@16: * This program is distributed in the hope that it will be useful,
franta-hg@16: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16: * GNU General Public License for more details.
franta-hg@16: *
franta-hg@16: * You should have received a copy of the GNU General Public License
franta-hg@16: * along with this program. If not, see .
franta-hg@16: */
franta-hg@14: package info.globalcode.sql.dk;
franta-hg@14:
franta-hg@203: import info.globalcode.sql.dk.configuration.CommandArgument;
franta-hg@67: import info.globalcode.sql.dk.configuration.Configuration;
franta-hg@65: import info.globalcode.sql.dk.configuration.ConfigurationException;
franta-hg@26: import info.globalcode.sql.dk.configuration.ConfigurationProvider;
franta-hg@65: import info.globalcode.sql.dk.configuration.DatabaseDefinition;
franta-hg@67: import info.globalcode.sql.dk.configuration.FormatterDefinition;
franta-hg@160: import info.globalcode.sql.dk.configuration.Properties;
franta-hg@159: import info.globalcode.sql.dk.configuration.Property;
franta-hg@203: import info.globalcode.sql.dk.configuration.TunnelDefinition;
franta-hg@69: import info.globalcode.sql.dk.formatting.ColumnsHeader;
franta-hg@159: import info.globalcode.sql.dk.formatting.FakeSqlArray;
franta-hg@69: import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@69: import info.globalcode.sql.dk.formatting.FormatterContext;
franta-hg@69: import info.globalcode.sql.dk.formatting.FormatterException;
franta-hg@17: import java.io.BufferedReader;
franta-hg@160: import java.io.ByteArrayOutputStream;
franta-hg@17: import java.io.InputStreamReader;
franta-hg@14: import java.io.PrintStream;
franta-hg@159: import java.sql.Array;
franta-hg@158: import java.sql.Driver;
franta-hg@196: import java.sql.DriverManager;
franta-hg@159: import java.sql.DriverPropertyInfo;
franta-hg@65: import java.sql.SQLException;
franta-hg@70: import java.util.ArrayList;
franta-hg@201: import java.util.Collections;
franta-hg@201: import java.util.Comparator;
franta-hg@70: import java.util.EnumSet;
franta-hg@159: import java.util.HashSet;
franta-hg@66: import java.util.List;
franta-hg@200: import java.util.Map.Entry;
franta-hg@158: import java.util.ServiceLoader;
franta-hg@159: import java.util.Set;
franta-hg@183: import java.util.concurrent.ExecutorService;
franta-hg@183: import java.util.concurrent.Executors;
franta-hg@183: import java.util.concurrent.TimeUnit;
franta-hg@17: import java.util.logging.Level;
franta-hg@196: import java.util.logging.LogRecord;
franta-hg@17: import java.util.logging.Logger;
franta-hg@69: import javax.sql.rowset.RowSetMetaDataImpl;
franta-hg@14:
franta-hg@14: /**
franta-hg@14: * Displays info like help, version etc.
franta-hg@14: *
franta-hg@14: * @author Ing. František Kučera (frantovo.cz)
franta-hg@14: */
franta-hg@14: public class InfoLister {
franta-hg@14:
franta-hg@17: private static final Logger log = Logger.getLogger(InfoLister.class.getName());
franta-hg@159: /**
franta-hg@159: * Fake database name for output formatting
franta-hg@159: */
franta-hg@159: public static final String CONFIG_DB_NAME = "sqldk_configuration";
franta-hg@183: private final PrintStream out;
franta-hg@183: private final ConfigurationProvider configurationProvider;
franta-hg@183: private final CLIOptions options;
franta-hg@70: private Formatter formatter;
franta-hg@17:
franta-hg@69: public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
franta-hg@17: this.out = out;
franta-hg@20: this.configurationProvider = configurationProvider;
franta-hg@69: this.options = options;
franta-hg@17: }
franta-hg@17:
franta-hg@70: public void showInfo() throws ConfigurationException, FormatterException {
franta-hg@70: EnumSet commands = options.getShowInfo();
franta-hg@70:
franta-hg@139: boolean formattinNeeded = false;
franta-hg@139:
franta-hg@70: for (InfoType infoType : commands) {
franta-hg@70: switch (infoType) {
franta-hg@70: case CONNECTION:
franta-hg@158: case JDBC_DRIVERS:
franta-hg@159: case JDBC_PROPERTIES:
franta-hg@70: case DATABASES:
franta-hg@70: case FORMATTERS:
franta-hg@70: case TYPES:
franta-hg@200: case JAVA_PROPERTIES:
franta-hg@200: case ENVIRONMENT_VARIABLES:
franta-hg@139: formattinNeeded = true;
franta-hg@101: break;
franta-hg@70: }
franta-hg@70: }
franta-hg@139:
franta-hg@139: if (formattinNeeded) {
franta-hg@139: try (Formatter f = getFormatter()) {
franta-hg@139: formatter = f;
franta-hg@139: formatter.writeStartBatch();
franta-hg@159: DatabaseDefinition dd = new DatabaseDefinition();
franta-hg@159: dd.setName(CONFIG_DB_NAME);
franta-hg@159: formatter.writeStartDatabase(dd);
franta-hg@139: showInfos(commands);
franta-hg@139: formatter.writeEndDatabase();
franta-hg@139: formatter.writeEndBatch();
franta-hg@139: formatter.close();
franta-hg@139: }
franta-hg@139: } else {
franta-hg@139: showInfos(commands);
franta-hg@139: }
franta-hg@101: }
franta-hg@70:
franta-hg@101: private void showInfos(EnumSet commands) throws ConfigurationException, FormatterException {
franta-hg@70: for (InfoType infoType : commands) {
franta-hg@70: infoType.showInfo(this);
franta-hg@70: }
franta-hg@70: }
franta-hg@70:
franta-hg@200: private void listJavaProperties() throws FormatterException, ConfigurationException {
franta-hg@200: ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("value", SQLType.VARCHAR));
franta-hg@200: List