java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
branchv_0
changeset 55 f5ed7c4efacc
parent 48 28735e71a1da
child 56 29f45ab3b959
equal deleted inserted replaced
54:53020d0bd2e4 55:f5ed7c4efacc
    42 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    42 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    43 	private CLIOptions options;
    43 	private CLIOptions options;
    44 	private Configuration configuration;
    44 	private Configuration configuration;
    45 
    45 
    46 	public static void main(String[] args) {
    46 	public static void main(String[] args) {
       
    47 		log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
    47 
    48 
    48 		if (args.length == 0) {
    49 		if (args.length == 0) {
    49 			args = new String[]{CLIParser.Tokens.INFO_HELP};
    50 			args = new String[]{CLIParser.Tokens.INFO_HELP};
    50 		}
    51 		}
    51 
    52 
    54 			CLIOptions options = parser.parseOptions(args);
    55 			CLIOptions options = parser.parseOptions(args);
    55 			options.validate();
    56 			options.validate();
    56 			CLIStarter starter = new CLIStarter(options);
    57 			CLIStarter starter = new CLIStarter(options);
    57 			starter.installDefaultConfiguration();
    58 			starter.installDefaultConfiguration();
    58 			starter.process();
    59 			starter.process();
       
    60 			log.log(Level.FINE, "All done");
    59 		} catch (CLIParserException e) {
    61 		} catch (CLIParserException e) {
    60 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    62 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    61 		} catch (InvalidOptionsException e) {
    63 		} catch (InvalidOptionsException e) {
    62 			log.log(Level.SEVERE, "Invalid CLI options", e);
    64 			log.log(Level.SEVERE, "Invalid CLI options", e);
    63 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    65 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
   111 			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
   113 			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
   112 			if (fd == null) {
   114 			if (fd == null) {
   113 				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
   115 				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
   114 			} else {
   116 			} else {
   115 				try (DatabaseConnection c = dd.connect()) {
   117 				try (DatabaseConnection c = dd.connect()) {
       
   118 					log.log(Level.FINE, "Database connected");
   116 					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
   119 					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
   117 					c.executeQuery(options.getSQLCommand(), f);
   120 					c.executeQuery(options.getSQLCommand(), f);
   118 				}
   121 				}
   119 			}
   122 			}
   120 		}
   123 		}
   136 
   139 
   137 	private void installDefaultConfiguration() throws ConfigurationException {
   140 	private void installDefaultConfiguration() throws ConfigurationException {
   138 		Constants.DIR.mkdir();
   141 		Constants.DIR.mkdir();
   139 
   142 
   140 		if (Constants.CONFIG_FILE.exists()) {
   143 		if (Constants.CONFIG_FILE.exists()) {
   141 			log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
   144 			log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
   142 		} else {
   145 		} else {
   143 			try {
   146 			try {
   144 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
   147 				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
       
   148 				log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
   145 			} catch (IOException e) {
   149 			} catch (IOException e) {
   146 				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
   150 				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
   147 			}
   151 			}
   148 		}
   152 		}
   149 	}
   153 	}