java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 22:02:44 +0100
branchv_0
changeset 33 04db6ccd6c48
parent 26 4ec8e5534eb9
child 34 9335cf31c0f2
permissions -rw-r--r--
configuration loading from XML
     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 java.io.IOException;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 import javax.xml.bind.JAXBContext;
    28 import javax.xml.bind.Unmarshaller;
    29 
    30 /**
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 public class CLIStarter implements ConfigurationProvider {
    35 
    36 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    37 	private CLIOptions options;
    38 	private Configuration configuration;
    39 
    40 	public static void main(String[] args) {
    41 
    42 		if (args.length == 0) {
    43 			args = new String[]{CLIParser.Tokens.INFO_HELP};
    44 		}
    45 
    46 		try {
    47 			CLIParser parser = new CLIParser();
    48 			CLIOptions options = parser.parseOptions(args);
    49 			options.validate();
    50 			CLIStarter starter = new CLIStarter(options);
    51 			starter.installDefaultConfiguration();
    52 			starter.process();
    53 		} catch (CLIParserException e) {
    54 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    55 		} catch (InvalidOptionsException e) {
    56 			log.log(Level.SEVERE, "Invalid CLI options", e);
    57 		}
    58 	}
    59 
    60 	public CLIStarter(CLIOptions options) {
    61 		this.options = options;
    62 	}
    63 
    64 	private void process() {
    65 		/** Show info */
    66 		if (!options.getShowInfo().isEmpty()) {
    67 			InfoLister infoLister = new InfoLister(System.err, this);
    68 			infoLister.showInfo(options);
    69 		}
    70 
    71 		MODE mode = options.getMode();
    72 		switch (mode) {
    73 			case QUERY_NOW:
    74 				break;
    75 			case PREPARE_BATCH:
    76 				break;
    77 			case EXECUTE_BATCH:
    78 				break;
    79 			case JUST_SHOW_INFO:
    80 				// already done above
    81 				break;
    82 			default:
    83 				log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
    84 				break;
    85 		}
    86 	}
    87 
    88 	@Override
    89 	public Configuration getConfiguration() throws ConfigurationException {
    90 		if (configuration == null) {
    91 			configuration = loadConfiguration();
    92 		}
    93 		return configuration;
    94 	}
    95 
    96 	private void installDefaultConfiguration() {
    97 		Constants.DIR.mkdir();
    98 
    99 		if (Constants.CONFIG_FILE.exists()) {
   100 			log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
   101 		} else {
   102 			try {
   103 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
   104 			} catch (IOException e) {
   105 				log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
   106 			}
   107 		}
   108 
   109 	}
   110 
   111 	private Configuration loadConfiguration() throws ConfigurationException {
   112 		try {
   113 			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
   114 			Unmarshaller u = jaxb.createUnmarshaller();
   115 			return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
   116 		} catch (Exception e) {
   117 			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
   118 		}
   119 	}
   120 }