java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 01:20:57 +0100
branchv_0
changeset 48 28735e71a1da
parent 42 6fdaa4db3943
child 55 f5ed7c4efacc
permissions -rw-r--r--
print CLI options problems if any
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    21 import info.globalcode.sql.dk.CLIOptions.MODE;
    22 import info.globalcode.sql.dk.configuration.Configuration;
    23 import info.globalcode.sql.dk.configuration.ConfigurationException;
    24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
    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.IOException;
    30 import java.sql.SQLException;
    31 import java.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import javax.xml.bind.JAXBContext;
    34 import javax.xml.bind.Unmarshaller;
    35 
    36 /**
    37  *
    38  * @author Ing. František Kučera (frantovo.cz)
    39  */
    40 public class CLIStarter implements ConfigurationProvider {
    41 
    42 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    43 	private CLIOptions options;
    44 	private Configuration configuration;
    45 
    46 	public static void main(String[] args) {
    47 
    48 		if (args.length == 0) {
    49 			args = new String[]{CLIParser.Tokens.INFO_HELP};
    50 		}
    51 
    52 		try {
    53 			CLIParser parser = new CLIParser();
    54 			CLIOptions options = parser.parseOptions(args);
    55 			options.validate();
    56 			CLIStarter starter = new CLIStarter(options);
    57 			starter.installDefaultConfiguration();
    58 			starter.process();
    59 		} catch (CLIParserException e) {
    60 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    61 		} catch (InvalidOptionsException e) {
    62 			log.log(Level.SEVERE, "Invalid CLI options", e);
    63 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    64 				log.log(Level.SEVERE, "Option problem: {0}", p.getDescription());
    65 			}
    66 		} catch (ConfigurationException e) {
    67 			log.log(Level.SEVERE, "Configuration problem", e);
    68 		} catch (SQLException e) {
    69 			log.log(Level.SEVERE, "SQL problem", e);
    70 		} catch (FormatterException e) {
    71 			log.log(Level.SEVERE, "Formatting problem", e);
    72 		}
    73 	}
    74 
    75 	public CLIStarter(CLIOptions options) {
    76 		this.options = options;
    77 	}
    78 
    79 	private void process() throws ConfigurationException, SQLException, FormatterException {
    80 		/** Show info */
    81 		if (!options.getShowInfo().isEmpty()) {
    82 			InfoLister infoLister = new InfoLister(System.err, this);
    83 			infoLister.showInfo(options);
    84 		}
    85 
    86 		MODE mode = options.getMode();
    87 		switch (mode) {
    88 			case QUERY_NOW:
    89 				processQueryNow();
    90 				break;
    91 			case PREPARE_BATCH:
    92 				processPrepareBatch();
    93 				break;
    94 			case EXECUTE_BATCH:
    95 				processExecuteBatch();
    96 				break;
    97 			case JUST_SHOW_INFO:
    98 				// already done above
    99 				break;
   100 			default:
   101 				log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
   102 				break;
   103 		}
   104 	}
   105 
   106 	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
   107 		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
   108 		if (dd == null) {
   109 			throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
   110 		} else {
   111 			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
   112 			if (fd == null) {
   113 				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
   114 			} else {
   115 				try (DatabaseConnection c = dd.connect()) {
   116 					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
   117 					c.executeQuery(options.getSQLCommand(), f);
   118 				}
   119 			}
   120 		}
   121 	}
   122 
   123 	private void processPrepareBatch() {
   124 	}
   125 
   126 	private void processExecuteBatch() {
   127 	}
   128 
   129 	@Override
   130 	public Configuration getConfiguration() throws ConfigurationException {
   131 		if (configuration == null) {
   132 			configuration = loadConfiguration();
   133 		}
   134 		return configuration;
   135 	}
   136 
   137 	private void installDefaultConfiguration() throws ConfigurationException {
   138 		Constants.DIR.mkdir();
   139 
   140 		if (Constants.CONFIG_FILE.exists()) {
   141 			log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
   142 		} else {
   143 			try {
   144 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
   145 			} catch (IOException e) {
   146 				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
   147 			}
   148 		}
   149 	}
   150 
   151 	private Configuration loadConfiguration() throws ConfigurationException {
   152 		try {
   153 			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
   154 			Unmarshaller u = jaxb.createUnmarshaller();
   155 			return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
   156 		} catch (Exception e) {
   157 			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
   158 		}
   159 	}
   160 }