java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
branchv_0
changeset 160 84ea4a819fb2
parent 159 9632b23df30c
child 161 385929a50dd9
--- 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 {