throw ConfigurationException instead of returning null, if database or formatter of given name is not configured v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 00:57:34 +0100
branchv_0
changeset 7543aa4625ab7e
parent 74 a8444f6a54f3
child 76 fe23cea7542f
throw ConfigurationException instead of returning null, if database or formatter of given name is not configured
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Thu Dec 26 22:39:38 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Fri Dec 27 00:57:34 2013 +0100
     1.3 @@ -122,19 +122,11 @@
     1.4  
     1.5  	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
     1.6  		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
     1.7 -		if (dd == null) {
     1.8 -			throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
     1.9 -		} else {
    1.10 -			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
    1.11 -			if (fd == null) {
    1.12 -				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
    1.13 -			} else {
    1.14 -				try (DatabaseConnection c = dd.connect()) {
    1.15 -					log.log(Level.FINE, "Database connected");
    1.16 -					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
    1.17 -					c.executeQuery(options.getSQLCommand(), f);
    1.18 -				}
    1.19 -			}
    1.20 +		FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
    1.21 +		try (DatabaseConnection c = dd.connect()) {
    1.22 +			log.log(Level.FINE, "Database connected");
    1.23 +			Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
    1.24 +			c.executeQuery(options.getSQLCommand(), f);
    1.25  		}
    1.26  	}
    1.27  
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 22:39:38 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Fri Dec 27 00:57:34 2013 +0100
     2.3 @@ -154,16 +154,12 @@
     2.4  
     2.5  		try {
     2.6  			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
     2.7 -			if (dd == null) {
     2.8 -				log.log(Level.FINE, "No database with this name is configured: {0}", dbName);
     2.9 -			} else {
    2.10 -				log.log(Level.FINE, "Database definition was loaded from configuration");
    2.11 -				succesfullyConfigured = true;
    2.12 -				try (DatabaseConnection dc = dd.connect()) {
    2.13 -					succesfullyConnected = dc.test();
    2.14 -				}
    2.15 -				log.log(Level.FINE, "Database connection test was successful");
    2.16 +			log.log(Level.FINE, "Database definition was loaded from configuration");
    2.17 +			succesfullyConfigured = true;
    2.18 +			try (DatabaseConnection dc = dd.connect()) {
    2.19 +				succesfullyConnected = dc.test();
    2.20  			}
    2.21 +			log.log(Level.FINE, "Database connection test was successful");
    2.22  		} catch (ConfigurationException | SQLException e) {
    2.23  			log.log(Level.SEVERE, "Error during testing connection", e);
    2.24  		}
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Thu Dec 26 22:39:38 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Fri Dec 27 00:57:34 2013 +0100
     3.3 @@ -68,8 +68,16 @@
     3.4  		this.databases = databases;
     3.5  	}
     3.6  
     3.7 -	public DatabaseDefinition getDatabase(String name) {
     3.8 -		return findByName(databases, name);
     3.9 +	/**
    3.10 +	 * @throws ConfigurationException if no database with this name is configured
    3.11 +	 */
    3.12 +	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
    3.13 +		DatabaseDefinition dd = findByName(databases, name);
    3.14 +		if (dd == null) {
    3.15 +			throw new ConfigurationException("Database is not configured: " + name);
    3.16 +		} else {
    3.17 +			return dd;
    3.18 +		}
    3.19  	}
    3.20  
    3.21  	@XmlElement(name = "formatter", namespace = CONFIGURATION)
    3.22 @@ -85,18 +93,20 @@
    3.23  	 * @param name name of desired formatter. Looking for this name in user configuration, then in
    3.24  	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
    3.25  	 * default is used.
    3.26 -	 * @return formatter definition or null if none for this name is found
    3.27 +	 * @return formatter definition
    3.28 +	 * @throws ConfigurationException if no formatter with this name was found
    3.29  	 */
    3.30 -	public FormatterDefinition getFormatter(String name) {
    3.31 +	public FormatterDefinition getFormatter(String name) throws ConfigurationException {
    3.32  		if (name == null) {
    3.33 -			if (defaultFormatter == null) {
    3.34 -				return getFormatter(DEFAULT_FORMATTER);
    3.35 -			} else {
    3.36 -				return getFormatter(defaultFormatter);
    3.37 -			}
    3.38 +			return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
    3.39  		} else {
    3.40  			FormatterDefinition fd = findByName(formatters, name);
    3.41 -			return fd == null ? findByName(buildInFormatters, name) : fd;
    3.42 +			fd = fd == null ? findByName(buildInFormatters, name) : fd;
    3.43 +			if (fd == null) {
    3.44 +				throw new ConfigurationException("Formatter is not configured: " + name);
    3.45 +			} else {
    3.46 +				return fd;
    3.47 +			}
    3.48  		}
    3.49  	}
    3.50