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 75 43aa4625ab7e
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
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Thu Dec 26 22:39:38 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Fri Dec 27 00:57:34 2013 +0100
@@ -122,19 +122,11 @@
 
 	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
 		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
-		if (dd == null) {
-			throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
-		} else {
-			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
-			if (fd == null) {
-				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
-			} else {
-				try (DatabaseConnection c = dd.connect()) {
-					log.log(Level.FINE, "Database connected");
-					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
-					c.executeQuery(options.getSQLCommand(), f);
-				}
-			}
+		FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
+		try (DatabaseConnection c = dd.connect()) {
+			log.log(Level.FINE, "Database connected");
+			Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
+			c.executeQuery(options.getSQLCommand(), f);
 		}
 	}
 
--- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Thu Dec 26 22:39:38 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Fri Dec 27 00:57:34 2013 +0100
@@ -154,16 +154,12 @@
 
 		try {
 			DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
-			if (dd == null) {
-				log.log(Level.FINE, "No database with this name is configured: {0}", dbName);
-			} else {
-				log.log(Level.FINE, "Database definition was loaded from configuration");
-				succesfullyConfigured = true;
-				try (DatabaseConnection dc = dd.connect()) {
-					succesfullyConnected = dc.test();
-				}
-				log.log(Level.FINE, "Database connection test was successful");
+			log.log(Level.FINE, "Database definition was loaded from configuration");
+			succesfullyConfigured = true;
+			try (DatabaseConnection dc = dd.connect()) {
+				succesfullyConnected = dc.test();
 			}
+			log.log(Level.FINE, "Database connection test was successful");
 		} catch (ConfigurationException | SQLException e) {
 			log.log(Level.SEVERE, "Error during testing connection", e);
 		}
--- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Thu Dec 26 22:39:38 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Fri Dec 27 00:57:34 2013 +0100
@@ -68,8 +68,16 @@
 		this.databases = databases;
 	}
 
-	public DatabaseDefinition getDatabase(String name) {
-		return findByName(databases, name);
+	/**
+	 * @throws ConfigurationException if no database with this name is configured
+	 */
+	public DatabaseDefinition getDatabase(String name) throws ConfigurationException {
+		DatabaseDefinition dd = findByName(databases, name);
+		if (dd == null) {
+			throw new ConfigurationException("Database is not configured: " + name);
+		} else {
+			return dd;
+		}
 	}
 
 	@XmlElement(name = "formatter", namespace = CONFIGURATION)
@@ -85,18 +93,20 @@
 	 * @param name name of desired formatter. Looking for this name in user configuration, then in
 	 * buil-in formatters. If null, default from configuration or (if not configured) built-in
 	 * default is used.
-	 * @return formatter definition or null if none for this name is found
+	 * @return formatter definition
+	 * @throws ConfigurationException if no formatter with this name was found
 	 */
-	public FormatterDefinition getFormatter(String name) {
+	public FormatterDefinition getFormatter(String name) throws ConfigurationException {
 		if (name == null) {
-			if (defaultFormatter == null) {
-				return getFormatter(DEFAULT_FORMATTER);
-			} else {
-				return getFormatter(defaultFormatter);
-			}
+			return defaultFormatter == null ? getFormatter(DEFAULT_FORMATTER) : getFormatter(defaultFormatter);
 		} else {
 			FormatterDefinition fd = findByName(formatters, name);
-			return fd == null ? findByName(buildInFormatters, name) : fd;
+			fd = fd == null ? findByName(buildInFormatters, name) : fd;
+			if (fd == null) {
+				throw new ConfigurationException("Formatter is not configured: " + name);
+			} else {
+				return fd;
+			}
 		}
 	}