From 8aa55ddbff886c69565f3d765eabcb0dbdec8ba5 Mon Sep 17 00:00:00 2001 From: Willem Date: Fri, 13 May 2016 00:46:33 +0200 Subject: [PATCH] Improved exception handing --- .../net/forwardfire/tpquery/TPQFactory.java | 61 ++++++++----------- .../forwardfire/tpquery/config/TPQConfig.java | 13 ++++ .../tpquery/config/TPQConfigException.java | 37 +++++++++++ .../config/builder/TPQConfigBuilder.java | 17 +++--- .../store/manager/TPQStoreManager.java | 12 ++-- .../store/manager/TPQStoreManagerConfig.java | 3 +- .../store/manager/TPQStoreManagerEntry.java | 27 +++----- .../config/TPQueryStoreConfigTest.java | 18 ++++++ .../store/manager/TPQStoreManagerTest.java | 47 ++++++++++++-- 9 files changed, 163 insertions(+), 72 deletions(-) create mode 100644 ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfigException.java diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/TPQFactory.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/TPQFactory.java index 92f2f93..a69c6a9 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/TPQFactory.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/TPQFactory.java @@ -38,7 +38,6 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.xml.bind.JAXBException; -import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.config.TPQConfig; @@ -122,7 +121,7 @@ public final class TPQFactory { * @return The builder. * @throws JAXBException If the jaxb marshaller throws an error. */ - public static TPQConfigBuilder createManagerBuilder() throws JAXBException { + public static TPQConfigBuilder createManagerBuilder() { return createManagerBuilder(createConfig()); } @@ -132,7 +131,7 @@ public final class TPQFactory { * @return The builder. * @throws JAXBException If the jaxb marshaller throws an error. */ - public static TPQConfigBuilder createManagerBuilder(TPQConfig config) throws JAXBException { + public static TPQConfigBuilder createManagerBuilder(TPQConfig config) { return new TPQConfigBuilder(config,() -> createManager(config)); } @@ -141,7 +140,7 @@ public final class TPQFactory { * @return The builder. * @throws JAXBException If the jaxb marshaller throws an error. */ - public static TPQConfigBuilder createConfigBuilder() throws JAXBException { + public static TPQConfigBuilder createConfigBuilder() { return createConfigBuilder(createConfig()); } @@ -151,7 +150,7 @@ public final class TPQFactory { * @return The builder. * @throws JAXBException If the jaxb marshaller throws an error. */ - public static TPQConfigBuilder createConfigBuilder(TPQConfig config) throws JAXBException { + public static TPQConfigBuilder createConfigBuilder(TPQConfig config) { return new TPQConfigBuilder(config,() -> config); } @@ -252,38 +251,30 @@ public final class TPQFactory { public void initializeConfig(TPQConfig config) { // pure java types - addValueType(config, JAVA_OBJECT); - addValueType(config, BIGDECIMAL); - addValueType(config, BOOLEAN); - addValueType(config, INTEGER); - addValueType(config, STRING); - addValueType(config, DOUBLE); - addValueType(config, LONG); - addValueType(config, FLOAT); - addValueType(config, SHORT); - addValueType(config, URL); - addValueType(config, BYTE_DATA); + config.addValueType(JAVA_OBJECT); + config.addValueType(BIGDECIMAL); + config.addValueType(BOOLEAN); + config.addValueType(INTEGER); + config.addValueType(STRING); + config.addValueType(DOUBLE); + config.addValueType(LONG); + config.addValueType(FLOAT); + config.addValueType(SHORT); + config.addValueType(URL); + config.addValueType(BYTE_DATA); // jdbc sql types - addValueType(config, SQL_ARRAY); - addValueType(config, SQL_BLOB); - addValueType(config, SQL_CLOB); - addValueType(config, SQL_DATE); - addValueType(config, SQL_NCLOB); - addValueType(config, SQL_REF); - addValueType(config, SQL_ROWID); - addValueType(config, SQL_STRUCT); - addValueType(config, SQL_XML); - addValueType(config, SQL_TIME); - addValueType(config, SQL_TIMESTAMP); - } - - private void addValueType(TPQConfig config,String className) { - try { - config.addValueType(ClassUtils.getClass(className)); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(e.getMessage(),e); - } + config.addValueType(SQL_ARRAY); + config.addValueType(SQL_BLOB); + config.addValueType(SQL_CLOB); + config.addValueType(SQL_DATE); + config.addValueType(SQL_NCLOB); + config.addValueType(SQL_REF); + config.addValueType(SQL_ROWID); + config.addValueType(SQL_STRUCT); + config.addValueType(SQL_XML); + config.addValueType(SQL_TIME); + config.addValueType(SQL_TIMESTAMP); } } diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfig.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfig.java index df46435..c4ae953 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfig.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfig.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Objects; +import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.config.validate.TPQConfigValidator; @@ -215,6 +216,18 @@ public class TPQConfig extends AbstractTPQConfig { valueTypes.add(Validate.notNull(valueType,"Can't add null valueType.")); } + /** + * Adds a value type. + * @param valueTypeClass The value type className to add. + */ + public void addValueType(String valueTypeClass) { + try { + addValueType(ClassUtils.getClass(valueTypeClass)); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e.getMessage(),e); + } + } + /** * Removes a value type. * @param valueType The value type to remove. diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfigException.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfigException.java new file mode 100644 index 0000000..26c7afb --- /dev/null +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/TPQConfigException.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2013-2015, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.forwardfire.tpquery.config; + +/** + * None recoverable config start exception. + * + * @author Willem Cazander + * @version 1.0 May 13, 2016 + */ +@SuppressWarnings("serial") +public class TPQConfigException extends RuntimeException { + + public TPQConfigException(Throwable cause) { + super(cause); + } +} diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java index 625955d..9921ffb 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java @@ -33,6 +33,7 @@ import javax.xml.bind.JAXBException; import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.config.TPQConfig; +import net.forwardfire.tpquery.config.TPQConfigException; import net.forwardfire.tpquery.model.TPQuerySet; import net.forwardfire.tpquery.model.ModelXMLMarshaller; import net.forwardfire.tpquery.statement.TPQStatementMarshaller; @@ -57,12 +58,16 @@ public class TPQConfigBuilder { * @param resultBuilder the result of this builder. * @throws JAXBException If the jaxb marshaller throws an error. */ - public TPQConfigBuilder(TPQConfig storeConfig,Supplier resultBuilder) throws JAXBException { + public TPQConfigBuilder(TPQConfig storeConfig,Supplier resultBuilder) { Validate.notNull(storeConfig,"Can't build null config."); Validate.notNull(resultBuilder,"Can't build with null builder."); this.storeConfig = storeConfig; this.resultBuilder = resultBuilder; - this.xmlDriver = new ModelXMLMarshaller(); + try { + this.xmlDriver = new ModelXMLMarshaller(); + } catch (JAXBException e) { + throw new TPQConfigException(e); + } this.statementMarshaller = new TPQStatementMarshaller(); } @@ -111,13 +116,9 @@ public class TPQConfigBuilder { * @return The builder. * @throws JAXBException When marshall error. */ - public TPQConfigBuilder readQuerySet(File file) throws JAXBException { + public TPQConfigBuilder readQuerySet(File file) throws JAXBException, FileNotFoundException { Validate.notNull(file, "Can't read null File"); - try { - return readQuerySet(new FileInputStream(file),file.getAbsolutePath()); - } catch (FileNotFoundException e) { - throw new JAXBException(e); - } + return readQuerySet(new FileInputStream(file),file.getAbsolutePath()); } /** diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java index 870b080..2cdfb08 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java @@ -26,6 +26,7 @@ import javax.script.CompiledScript; import net.forwardfire.tpquery.TPQManager; import net.forwardfire.tpquery.config.TPQConfig; +import net.forwardfire.tpquery.config.TPQConfigException; import net.forwardfire.tpquery.statement.TPQStatementContext; import net.forwardfire.tpquery.store.TPQueryStore; import net.forwardfire.tpquery.store.TPQueryStoreScriptEngineException; @@ -54,15 +55,18 @@ public class TPQStoreManager implements TPQManager { this(new TPQStoreManagerConfig(config)); } - private TPQStoreManager(TPQStoreManagerConfig config) { + protected TPQStoreManager(TPQStoreManagerConfig config) { this.queryStore = new TPQStoreManagerEntryView(config); TPQStatementContext statementContext = new TPQStoreManagerStatementContext(config); this.executorContext = new TPQStoreManagerExecutorContext(config,statementContext); - - initStore(config,statementContext); + try { + initStore(config,statementContext); + } catch (ClassNotFoundException e) { + throw new TPQConfigException(e); + } } - protected void initStore(TPQStoreManagerConfig config,TPQStatementContext statementContext) { + protected void initStore(TPQStoreManagerConfig config,TPQStatementContext statementContext) throws ClassNotFoundException { LOG.debug("init value scripts: {}",config.getParameterValueScripts().size()); config.getParameterValueScripts().forEach(scriptBody -> { diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerConfig.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerConfig.java index 068ef18..6f966b2 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerConfig.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerConfig.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import net.forwardfire.tpquery.config.AbstractTPQConfig; import net.forwardfire.tpquery.config.TPQConfig; +import net.forwardfire.tpquery.config.TPQConfigException; import net.forwardfire.tpquery.model.AbstractTPQueryNode; import net.forwardfire.tpquery.model.TPQuery; import net.forwardfire.tpquery.model.TPQueryParameter; @@ -210,7 +211,7 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig { try { return ClassUtils.getClass(valueType); } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(e); + throw new TPQConfigException(e); } } } diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java index 391eabb..8a673c6 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java @@ -39,7 +39,7 @@ import net.forwardfire.tpquery.model.TPQueryParameter; import net.forwardfire.tpquery.store.TPQueryParameterMetaData; /** - * Hold al extra objects to prepare query the query. + * Holds all extra objects to prepare the query. * * @author Willem Cazander * @version 1.0 May 25, 2015 @@ -81,25 +81,16 @@ public final class TPQStoreManagerEntry { /** * Init extra data. */ - public void initEntry() { + public void initEntry() throws ClassNotFoundException { Map parameterMetaDataBuild = new HashMap<>(); - getQuery().getQueryParameters().forEach(p -> { - Class valueType = loadValueType(p.getValueType()); // check if we van get this from config ? - parameterMetaDataBuild.put(p.getName(), - new TPQStoreManagerEntryParameterMetaData( - p.getName(),valueType,p.getNullable(),defineRequired(p) - )); - } - ); - this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild); - } - - private Class loadValueType(String valueType) { - try { - return ClassUtils.getClass(valueType); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(e); + for (TPQueryParameter p:getQuery().getQueryParameters()) { + Class valueType = ClassUtils.getClass(p.getValueType()); + TPQStoreManagerEntryParameterMetaData md = new TPQStoreManagerEntryParameterMetaData( + p.getName(),valueType,p.getNullable(),defineRequired(p) + ); + parameterMetaDataBuild.put(p.getName(),md); } + this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild); } private boolean defineRequired(TPQueryParameter p) { diff --git a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigTest.java b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigTest.java index 976276a..843cdf8 100644 --- a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigTest.java +++ b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigTest.java @@ -69,6 +69,24 @@ public class TPQueryStoreConfigTest { } // --------- + + @Test() + public void testAddValueType() throws Exception { + TPQConfig c = TPQFactory.createConfig(); + c.addValueType(this.getClass()); + } + + @Test() + public void testAddValueTypeClassName() throws Exception { + TPQConfig c = TPQFactory.createConfig(); + c.addValueType(this.getClass().getName()); + } + + @Test(expected=IllegalArgumentException.class) + public void testAddValueTypeClassNameError() throws Exception { + TPQConfig c = TPQFactory.createConfig(); + c.addValueType("net.foo.bar.NoneExcistingClassName"); + } @Test(expected=IllegalArgumentException.class) public void testValueTypeAliasAdd() throws Exception { diff --git a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java index d1a8a23..fb26b7c 100644 --- a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java +++ b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java @@ -5,6 +5,7 @@ import java.util.HashMap; import net.forwardfire.tpquery.TPQManager; import net.forwardfire.tpquery.TPQFactory; import net.forwardfire.tpquery.config.TPQConfig; +import net.forwardfire.tpquery.config.TPQConfigException; import net.forwardfire.tpquery.store.TPQueryStoreScriptEngineException; import org.junit.Test; @@ -31,7 +32,7 @@ public class TPQStoreManagerTest { @Test(expected=TPQueryStoreScriptEngineException.class) public void testCompileScriptError() throws Exception { - TPQManager store = TPQFactory + TPQManager manager = TPQFactory .createManagerBuilder() .createQuerySet("junit", "jar:junit:mem") .setLanguage(TPQFactory.StatementLanguage.SQL) @@ -39,16 +40,16 @@ public class TPQStoreManagerTest { .parseStatement("select * from foobar where a=$$a$$") .createQueryParameter("a", TPQFactory.ParameterValueType.SQL_TIME) .setDefaultValue("js:+++++++-------------=========----------+++++++++") - .build() .build() + .build() .build() .build(); - store.getQueryExecutorContext().prepareQuery("junit.test", new HashMap()); + manager.getQueryExecutorContext().prepareQuery("junit.test", new HashMap()); } @Test(expected=Exception.class) public void testRuntimeScriptError() throws Exception { - TPQManager store = TPQFactory + TPQManager manager = TPQFactory .createManagerBuilder() .createQuerySet("junit", "jar:junit:mem") .setLanguage(TPQFactory.StatementLanguage.SQL) @@ -56,10 +57,44 @@ public class TPQStoreManagerTest { .parseStatement("select * from foobar where a=$$a$$") .createQueryParameter("a", TPQFactory.ParameterValueType.SQL_TIME) .setDefaultValue("js:throw createObject(java.lang.String,'error')") - .build() .build() + .build() .build() .build(); - store.getQueryExecutorContext().prepareQuery("junit.test", new HashMap()); + manager.getQueryExecutorContext().prepareQuery("junit.test", new HashMap()); + } + + @Test(expected=TPQConfigException.class) + public void testConfigErrorByTPQStoreManagerConfig() throws Exception { + TPQFactory.createManagerBuilder() + .createQuerySet("junit", "jar:junit:mem") + .setLanguage(TPQFactory.StatementLanguage.SQL) + .createQuery("test") + .parseStatement("select * from foobar where a=$$a$$") + .createQueryParameter("a", "net.foo.bar.NoneExcistingClass") + .build() + .build() + .build() + .build(); + } + + @Test(expected=TPQConfigException.class) + public void testConfigErrorByTPQStoreManager() throws Exception { + + TPQConfig c = TPQFactory.createConfigBuilder() + .createQuerySet("junit", "jar:junit:mem") + .setLanguage(TPQFactory.StatementLanguage.SQL) + .createQuery("test") + .parseStatement("select * from foobar where a=$$a$$") + .createQueryParameter("a", "java.lang.String") + .build() + .build() + .build() + .build(); + TPQStoreManagerConfig smc = new TPQStoreManagerConfig(c); + + smc.getEntries().get("junit.test").getQuery().getQueryParameters().get(0).setValueType("net.foo.bar.NoneExcistingClass"); + + new TPQStoreManager(smc); } }