java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
branchv_0
changeset 33 04db6ccd6c48
parent 26 4ec8e5534eb9
child 34 9335cf31c0f2
equal deleted inserted replaced
32:5e412dbd9362 33:04db6ccd6c48
    18 package info.globalcode.sql.dk;
    18 package info.globalcode.sql.dk;
    19 
    19 
    20 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    20 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
    21 import info.globalcode.sql.dk.CLIOptions.MODE;
    21 import info.globalcode.sql.dk.CLIOptions.MODE;
    22 import info.globalcode.sql.dk.configuration.Configuration;
    22 import info.globalcode.sql.dk.configuration.Configuration;
       
    23 import info.globalcode.sql.dk.configuration.ConfigurationException;
       
    24 import java.io.IOException;
    23 import java.util.logging.Level;
    25 import java.util.logging.Level;
    24 import java.util.logging.Logger;
    26 import java.util.logging.Logger;
       
    27 import javax.xml.bind.JAXBContext;
       
    28 import javax.xml.bind.Unmarshaller;
    25 
    29 
    26 /**
    30 /**
    27  *
    31  *
    28  * @author Ing. František Kučera (frantovo.cz)
    32  * @author Ing. František Kučera (frantovo.cz)
    29  */
    33  */
    80 				break;
    84 				break;
    81 		}
    85 		}
    82 	}
    86 	}
    83 
    87 
    84 	@Override
    88 	@Override
    85 	public Configuration getConfiguration() {
    89 	public Configuration getConfiguration() throws ConfigurationException {
    86 		if (configuration == null) {
    90 		if (configuration == null) {
    87 			configuration = loadConfiguration();
    91 			configuration = loadConfiguration();
    88 		}
    92 		}
    89 		return configuration;
    93 		return configuration;
    90 	}
    94 	}
    91 
    95 
    92 	private void installDefaultConfiguration() {
    96 	private void installDefaultConfiguration() {
    93 		/**
    97 		Constants.DIR.mkdir();
    94 		 * TODO: check config folder/file and create it if missing
    98 
    95 		 */
    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 
    96 	}
   109 	}
    97 
   110 
    98 	private Configuration loadConfiguration() {
   111 	private Configuration loadConfiguration() throws ConfigurationException {
    99 		/**
   112 		try {
   100 		 * TODO: load configuration from XML
   113 			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
   101 		 */
   114 			Unmarshaller u = jaxb.createUnmarshaller();
   102 		return null;
   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 		}
   103 	}
   119 	}
   104 }
   120 }