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 16084ea4a819fb2
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
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Wed Jan 15 21:06:12 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Wed Jan 15 21:26:15 2014 +0100
     1.3 @@ -22,6 +22,7 @@
     1.4  import info.globalcode.sql.dk.configuration.ConfigurationProvider;
     1.5  import info.globalcode.sql.dk.configuration.DatabaseDefinition;
     1.6  import info.globalcode.sql.dk.configuration.FormatterDefinition;
     1.7 +import info.globalcode.sql.dk.configuration.Properties;
     1.8  import info.globalcode.sql.dk.configuration.Property;
     1.9  import info.globalcode.sql.dk.formatting.ColumnsHeader;
    1.10  import info.globalcode.sql.dk.formatting.FakeSqlArray;
    1.11 @@ -29,6 +30,8 @@
    1.12  import info.globalcode.sql.dk.formatting.FormatterContext;
    1.13  import info.globalcode.sql.dk.formatting.FormatterException;
    1.14  import java.io.BufferedReader;
    1.15 +import java.io.ByteArrayInputStream;
    1.16 +import java.io.ByteArrayOutputStream;
    1.17  import java.io.InputStreamReader;
    1.18  import java.io.PrintStream;
    1.19  import java.sql.Array;
    1.20 @@ -114,23 +117,34 @@
    1.21  				new HeaderField("name", SQLType.VARCHAR),
    1.22  				new HeaderField("built_in", SQLType.BOOLEAN),
    1.23  				new HeaderField("default", SQLType.BOOLEAN),
    1.24 -				new HeaderField("class_name", SQLType.VARCHAR));
    1.25 +				new HeaderField("class_name", SQLType.VARCHAR),
    1.26 +				new HeaderField("valid", SQLType.BOOLEAN));
    1.27  		List<Object[]> data = new ArrayList<>();
    1.28  
    1.29  		String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
    1.30  		defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
    1.31  
    1.32  		for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
    1.33 -			data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
    1.34 +			data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
    1.35  		}
    1.36  
    1.37  		for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
    1.38 -			data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
    1.39 +			data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
    1.40  		}
    1.41  
    1.42  		printTable(formatter, header, data, "-- configured and built-in output formatters", null);
    1.43 +	}
    1.44  
    1.45 -
    1.46 +	private boolean isInstantiable(FormatterDefinition fd) {
    1.47 +		try {
    1.48 +			try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
    1.49 +				fd.getInstance(new FormatterContext(testStream, new Properties(0)));
    1.50 +				return true;
    1.51 +			}
    1.52 +		} catch (Exception e) {
    1.53 +			log.log(Level.SEVERE, "Unable to create an instance of formatter: " + fd.getName(), e);
    1.54 +			return false;
    1.55 +		}
    1.56  	}
    1.57  
    1.58  	public void listTypes() throws FormatterException, ConfigurationException {
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Wed Jan 15 21:06:12 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/FormatterDefinition.java	Wed Jan 15 21:26:15 2014 +0100
     2.3 @@ -105,7 +105,7 @@
     2.4  				throw new FormatterException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface");
     2.5  			}
     2.6  		} catch (ClassNotFoundException e) {
     2.7 -			throw new FormatterException("No formatter class with name: " + className, e);
     2.8 +			throw new FormatterException("Formatter class does not exist: " + className, e);
     2.9  		} catch (NoSuchMethodException e) {
    2.10  			throw new FormatterException("Formatter class with no valid constructor: " + className, e);
    2.11  		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {