Improved exception handing

This commit is contained in:
Willem 2016-05-13 00:46:33 +02:00
parent 4f6f7286c0
commit 8aa55ddbff
9 changed files with 163 additions and 72 deletions

View file

@ -38,7 +38,6 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.tpquery.config.TPQConfig; import net.forwardfire.tpquery.config.TPQConfig;
@ -122,7 +121,7 @@ public final class TPQFactory {
* @return The builder. * @return The builder.
* @throws JAXBException If the jaxb marshaller throws an error. * @throws JAXBException If the jaxb marshaller throws an error.
*/ */
public static TPQConfigBuilder<TPQManager> createManagerBuilder() throws JAXBException { public static TPQConfigBuilder<TPQManager> createManagerBuilder() {
return createManagerBuilder(createConfig()); return createManagerBuilder(createConfig());
} }
@ -132,7 +131,7 @@ public final class TPQFactory {
* @return The builder. * @return The builder.
* @throws JAXBException If the jaxb marshaller throws an error. * @throws JAXBException If the jaxb marshaller throws an error.
*/ */
public static TPQConfigBuilder<TPQManager> createManagerBuilder(TPQConfig config) throws JAXBException { public static TPQConfigBuilder<TPQManager> createManagerBuilder(TPQConfig config) {
return new TPQConfigBuilder<TPQManager>(config,() -> createManager(config)); return new TPQConfigBuilder<TPQManager>(config,() -> createManager(config));
} }
@ -141,7 +140,7 @@ public final class TPQFactory {
* @return The builder. * @return The builder.
* @throws JAXBException If the jaxb marshaller throws an error. * @throws JAXBException If the jaxb marshaller throws an error.
*/ */
public static TPQConfigBuilder<TPQConfig> createConfigBuilder() throws JAXBException { public static TPQConfigBuilder<TPQConfig> createConfigBuilder() {
return createConfigBuilder(createConfig()); return createConfigBuilder(createConfig());
} }
@ -151,7 +150,7 @@ public final class TPQFactory {
* @return The builder. * @return The builder.
* @throws JAXBException If the jaxb marshaller throws an error. * @throws JAXBException If the jaxb marshaller throws an error.
*/ */
public static TPQConfigBuilder<TPQConfig> createConfigBuilder(TPQConfig config) throws JAXBException { public static TPQConfigBuilder<TPQConfig> createConfigBuilder(TPQConfig config) {
return new TPQConfigBuilder<TPQConfig>(config,() -> config); return new TPQConfigBuilder<TPQConfig>(config,() -> config);
} }
@ -252,38 +251,30 @@ public final class TPQFactory {
public void initializeConfig(TPQConfig config) { public void initializeConfig(TPQConfig config) {
// pure java types // pure java types
addValueType(config, JAVA_OBJECT); config.addValueType(JAVA_OBJECT);
addValueType(config, BIGDECIMAL); config.addValueType(BIGDECIMAL);
addValueType(config, BOOLEAN); config.addValueType(BOOLEAN);
addValueType(config, INTEGER); config.addValueType(INTEGER);
addValueType(config, STRING); config.addValueType(STRING);
addValueType(config, DOUBLE); config.addValueType(DOUBLE);
addValueType(config, LONG); config.addValueType(LONG);
addValueType(config, FLOAT); config.addValueType(FLOAT);
addValueType(config, SHORT); config.addValueType(SHORT);
addValueType(config, URL); config.addValueType(URL);
addValueType(config, BYTE_DATA); config.addValueType(BYTE_DATA);
// jdbc sql types // jdbc sql types
addValueType(config, SQL_ARRAY); config.addValueType(SQL_ARRAY);
addValueType(config, SQL_BLOB); config.addValueType(SQL_BLOB);
addValueType(config, SQL_CLOB); config.addValueType(SQL_CLOB);
addValueType(config, SQL_DATE); config.addValueType(SQL_DATE);
addValueType(config, SQL_NCLOB); config.addValueType(SQL_NCLOB);
addValueType(config, SQL_REF); config.addValueType(SQL_REF);
addValueType(config, SQL_ROWID); config.addValueType(SQL_ROWID);
addValueType(config, SQL_STRUCT); config.addValueType(SQL_STRUCT);
addValueType(config, SQL_XML); config.addValueType(SQL_XML);
addValueType(config, SQL_TIME); config.addValueType(SQL_TIME);
addValueType(config, SQL_TIMESTAMP); config.addValueType(SQL_TIMESTAMP);
}
private void addValueType(TPQConfig config,String className) {
try {
config.addValueType(ClassUtils.getClass(className));
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e.getMessage(),e);
}
} }
} }

View file

@ -26,6 +26,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.tpquery.config.validate.TPQConfigValidator; 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.")); 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. * Removes a value type.
* @param valueType The value type to remove. * @param valueType The value type to remove.

View file

@ -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);
}
}

View file

@ -33,6 +33,7 @@ import javax.xml.bind.JAXBException;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import net.forwardfire.tpquery.config.TPQConfig; import net.forwardfire.tpquery.config.TPQConfig;
import net.forwardfire.tpquery.config.TPQConfigException;
import net.forwardfire.tpquery.model.TPQuerySet; import net.forwardfire.tpquery.model.TPQuerySet;
import net.forwardfire.tpquery.model.ModelXMLMarshaller; import net.forwardfire.tpquery.model.ModelXMLMarshaller;
import net.forwardfire.tpquery.statement.TPQStatementMarshaller; import net.forwardfire.tpquery.statement.TPQStatementMarshaller;
@ -57,12 +58,16 @@ public class TPQConfigBuilder<T> {
* @param resultBuilder the result of this builder. * @param resultBuilder the result of this builder.
* @throws JAXBException If the jaxb marshaller throws an error. * @throws JAXBException If the jaxb marshaller throws an error.
*/ */
public TPQConfigBuilder(TPQConfig storeConfig,Supplier<T> resultBuilder) throws JAXBException { public TPQConfigBuilder(TPQConfig storeConfig,Supplier<T> resultBuilder) {
Validate.notNull(storeConfig,"Can't build null config."); Validate.notNull(storeConfig,"Can't build null config.");
Validate.notNull(resultBuilder,"Can't build with null builder."); Validate.notNull(resultBuilder,"Can't build with null builder.");
this.storeConfig = storeConfig; this.storeConfig = storeConfig;
this.resultBuilder = resultBuilder; this.resultBuilder = resultBuilder;
this.xmlDriver = new ModelXMLMarshaller(); try {
this.xmlDriver = new ModelXMLMarshaller();
} catch (JAXBException e) {
throw new TPQConfigException(e);
}
this.statementMarshaller = new TPQStatementMarshaller(); this.statementMarshaller = new TPQStatementMarshaller();
} }
@ -111,13 +116,9 @@ public class TPQConfigBuilder<T> {
* @return The builder. * @return The builder.
* @throws JAXBException When marshall error. * @throws JAXBException When marshall error.
*/ */
public TPQConfigBuilder<T> readQuerySet(File file) throws JAXBException { public TPQConfigBuilder<T> readQuerySet(File file) throws JAXBException, FileNotFoundException {
Validate.notNull(file, "Can't read null File"); Validate.notNull(file, "Can't read null File");
try { return readQuerySet(new FileInputStream(file),file.getAbsolutePath());
return readQuerySet(new FileInputStream(file),file.getAbsolutePath());
} catch (FileNotFoundException e) {
throw new JAXBException(e);
}
} }
/** /**

View file

@ -26,6 +26,7 @@ import javax.script.CompiledScript;
import net.forwardfire.tpquery.TPQManager; import net.forwardfire.tpquery.TPQManager;
import net.forwardfire.tpquery.config.TPQConfig; import net.forwardfire.tpquery.config.TPQConfig;
import net.forwardfire.tpquery.config.TPQConfigException;
import net.forwardfire.tpquery.statement.TPQStatementContext; import net.forwardfire.tpquery.statement.TPQStatementContext;
import net.forwardfire.tpquery.store.TPQueryStore; import net.forwardfire.tpquery.store.TPQueryStore;
import net.forwardfire.tpquery.store.TPQueryStoreScriptEngineException; import net.forwardfire.tpquery.store.TPQueryStoreScriptEngineException;
@ -54,15 +55,18 @@ public class TPQStoreManager implements TPQManager {
this(new TPQStoreManagerConfig(config)); this(new TPQStoreManagerConfig(config));
} }
private TPQStoreManager(TPQStoreManagerConfig config) { protected TPQStoreManager(TPQStoreManagerConfig config) {
this.queryStore = new TPQStoreManagerEntryView(config); this.queryStore = new TPQStoreManagerEntryView(config);
TPQStatementContext statementContext = new TPQStoreManagerStatementContext(config); TPQStatementContext statementContext = new TPQStoreManagerStatementContext(config);
this.executorContext = new TPQStoreManagerExecutorContext(config,statementContext); this.executorContext = new TPQStoreManagerExecutorContext(config,statementContext);
try {
initStore(config,statementContext); 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()); LOG.debug("init value scripts: {}",config.getParameterValueScripts().size());
config.getParameterValueScripts().forEach(scriptBody -> { config.getParameterValueScripts().forEach(scriptBody -> {

View file

@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
import net.forwardfire.tpquery.config.AbstractTPQConfig; import net.forwardfire.tpquery.config.AbstractTPQConfig;
import net.forwardfire.tpquery.config.TPQConfig; import net.forwardfire.tpquery.config.TPQConfig;
import net.forwardfire.tpquery.config.TPQConfigException;
import net.forwardfire.tpquery.model.AbstractTPQueryNode; import net.forwardfire.tpquery.model.AbstractTPQueryNode;
import net.forwardfire.tpquery.model.TPQuery; import net.forwardfire.tpquery.model.TPQuery;
import net.forwardfire.tpquery.model.TPQueryParameter; import net.forwardfire.tpquery.model.TPQueryParameter;
@ -210,7 +211,7 @@ public final class TPQStoreManagerConfig extends AbstractTPQConfig {
try { try {
return ClassUtils.getClass(valueType); return ClassUtils.getClass(valueType);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e); throw new TPQConfigException(e);
} }
} }
} }

View file

@ -39,7 +39,7 @@ import net.forwardfire.tpquery.model.TPQueryParameter;
import net.forwardfire.tpquery.store.TPQueryParameterMetaData; 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 * @author Willem Cazander
* @version 1.0 May 25, 2015 * @version 1.0 May 25, 2015
@ -81,25 +81,16 @@ public final class TPQStoreManagerEntry {
/** /**
* Init extra data. * Init extra data.
*/ */
public void initEntry() { public void initEntry() throws ClassNotFoundException {
Map<String,TPQueryParameterMetaData> parameterMetaDataBuild = new HashMap<>(); Map<String,TPQueryParameterMetaData> parameterMetaDataBuild = new HashMap<>();
getQuery().getQueryParameters().forEach(p -> { for (TPQueryParameter p:getQuery().getQueryParameters()) {
Class<?> valueType = loadValueType(p.getValueType()); // check if we van get this from config ? Class<?> valueType = ClassUtils.getClass(p.getValueType());
parameterMetaDataBuild.put(p.getName(), TPQStoreManagerEntryParameterMetaData md = new TPQStoreManagerEntryParameterMetaData(
new TPQStoreManagerEntryParameterMetaData( p.getName(),valueType,p.getNullable(),defineRequired(p)
p.getName(),valueType,p.getNullable(),defineRequired(p) );
)); parameterMetaDataBuild.put(p.getName(),md);
}
);
this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild);
}
private Class<?> loadValueType(String valueType) {
try {
return ClassUtils.getClass(valueType);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
} }
this.parameterMetaData=Collections.unmodifiableMap(parameterMetaDataBuild);
} }
private boolean defineRequired(TPQueryParameter p) { private boolean defineRequired(TPQueryParameter p) {

View file

@ -70,6 +70,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) @Test(expected=IllegalArgumentException.class)
public void testValueTypeAliasAdd() throws Exception { public void testValueTypeAliasAdd() throws Exception {
TPQConfig c = TPQFactory.createConfig(); TPQConfig c = TPQFactory.createConfig();

View file

@ -5,6 +5,7 @@ import java.util.HashMap;
import net.forwardfire.tpquery.TPQManager; import net.forwardfire.tpquery.TPQManager;
import net.forwardfire.tpquery.TPQFactory; import net.forwardfire.tpquery.TPQFactory;
import net.forwardfire.tpquery.config.TPQConfig; import net.forwardfire.tpquery.config.TPQConfig;
import net.forwardfire.tpquery.config.TPQConfigException;
import net.forwardfire.tpquery.store.TPQueryStoreScriptEngineException; import net.forwardfire.tpquery.store.TPQueryStoreScriptEngineException;
import org.junit.Test; import org.junit.Test;
@ -31,7 +32,7 @@ public class TPQStoreManagerTest {
@Test(expected=TPQueryStoreScriptEngineException.class) @Test(expected=TPQueryStoreScriptEngineException.class)
public void testCompileScriptError() throws Exception { public void testCompileScriptError() throws Exception {
TPQManager store = TPQFactory TPQManager manager = TPQFactory
.createManagerBuilder() .createManagerBuilder()
.createQuerySet("junit", "jar:junit:mem") .createQuerySet("junit", "jar:junit:mem")
.setLanguage(TPQFactory.StatementLanguage.SQL) .setLanguage(TPQFactory.StatementLanguage.SQL)
@ -39,16 +40,16 @@ public class TPQStoreManagerTest {
.parseStatement("select * from foobar where a=$$a$$") .parseStatement("select * from foobar where a=$$a$$")
.createQueryParameter("a", TPQFactory.ParameterValueType.SQL_TIME) .createQueryParameter("a", TPQFactory.ParameterValueType.SQL_TIME)
.setDefaultValue("js:+++++++-------------=========----------+++++++++") .setDefaultValue("js:+++++++-------------=========----------+++++++++")
.build()
.build() .build()
.build()
.build() .build()
.build(); .build();
store.getQueryExecutorContext().prepareQuery("junit.test", new HashMap<String,Object>()); manager.getQueryExecutorContext().prepareQuery("junit.test", new HashMap<String,Object>());
} }
@Test(expected=Exception.class) @Test(expected=Exception.class)
public void testRuntimeScriptError() throws Exception { public void testRuntimeScriptError() throws Exception {
TPQManager store = TPQFactory TPQManager manager = TPQFactory
.createManagerBuilder() .createManagerBuilder()
.createQuerySet("junit", "jar:junit:mem") .createQuerySet("junit", "jar:junit:mem")
.setLanguage(TPQFactory.StatementLanguage.SQL) .setLanguage(TPQFactory.StatementLanguage.SQL)
@ -56,10 +57,44 @@ public class TPQStoreManagerTest {
.parseStatement("select * from foobar where a=$$a$$") .parseStatement("select * from foobar where a=$$a$$")
.createQueryParameter("a", TPQFactory.ParameterValueType.SQL_TIME) .createQueryParameter("a", TPQFactory.ParameterValueType.SQL_TIME)
.setDefaultValue("js:throw createObject(java.lang.String,'error')") .setDefaultValue("js:throw createObject(java.lang.String,'error')")
.build()
.build() .build()
.build()
.build() .build()
.build(); .build();
store.getQueryExecutorContext().prepareQuery("junit.test", new HashMap<String,Object>()); manager.getQueryExecutorContext().prepareQuery("junit.test", new HashMap<String,Object>());
}
@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);
} }
} }