InfoLister: option --list-formatters also tests, if formatter class can be instantiated (thus is valid) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 15 Jan 2014 21:26:15 +0100
branchv_0
changeset 160 84ea4a819fb2
parent 159 9632b23df30c
child 161 385929a50dd9
InfoLister: option --list-formatters also tests, if formatter class can be instantiated (thus is valid)
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Wed Jan 15 21:06:12 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Wed Jan 15 21:26:15 2014 +0100
@@ -22,6 +22,7 @@
 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
 import info.globalcode.sql.dk.configuration.FormatterDefinition;
+import info.globalcode.sql.dk.configuration.Properties;
 import info.globalcode.sql.dk.configuration.Property;
 import info.globalcode.sql.dk.formatting.ColumnsHeader;
 import info.globalcode.sql.dk.formatting.FakeSqlArray;
@@ -29,6 +30,8 @@
 import info.globalcode.sql.dk.formatting.FormatterContext;
 import info.globalcode.sql.dk.formatting.FormatterException;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
 import java.sql.Array;
@@ -114,23 +117,34 @@
 				new HeaderField("name", SQLType.VARCHAR),
 				new HeaderField("built_in", SQLType.BOOLEAN),
 				new HeaderField("default", SQLType.BOOLEAN),
-				new HeaderField("class_name", SQLType.VARCHAR));
+				new HeaderField("class_name", SQLType.VARCHAR),
+				new HeaderField("valid", SQLType.BOOLEAN));
 		List<Object[]> data = new ArrayList<>();
 
 		String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
 		defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
 
 		for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
-			data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
+			data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
 		}
 
 		for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
-			data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
+			data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
 		}
 
 		printTable(formatter, header, data, "-- configured and built-in output formatters", null);
+	}
 
-
+	private boolean isInstantiable(FormatterDefinition fd) {
+		try {
+			try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
+				fd.getInstance(new FormatterContext(testStream, new Properties(0)));
+				return true;
+			}
+		} catch (Exception e) {
+			log.log(Level.SEVERE, "Unable to create an instance of formatter: " + fd.getName(), e);
+			return false;
+		}
 	}
 
 	public void listTypes() throws FormatterException, ConfigurationException {
--- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Wed Jan 15 21:06:12 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Wed Jan 15 21:26:15 2014 +0100
@@ -105,7 +105,7 @@
 				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
 			}
 		} catch (ClassNotFoundException e) {
-			throw new FormatterException("No formatter class with name: " + className, e);
+			throw new FormatterException("Formatter class does not exist: " + className, e);
 		} catch (NoSuchMethodException e) {
 			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
 		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {