Improved exception handing
This commit is contained in:
parent
4f6f7286c0
commit
8aa55ddbff
|
@ -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<TPQManager> createManagerBuilder() throws JAXBException {
|
||||
public static TPQConfigBuilder<TPQManager> 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<TPQManager> createManagerBuilder(TPQConfig config) throws JAXBException {
|
||||
public static TPQConfigBuilder<TPQManager> createManagerBuilder(TPQConfig config) {
|
||||
return new TPQConfigBuilder<TPQManager>(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<TPQConfig> createConfigBuilder() throws JAXBException {
|
||||
public static TPQConfigBuilder<TPQConfig> 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<TPQConfig> createConfigBuilder(TPQConfig config) throws JAXBException {
|
||||
public static TPQConfigBuilder<TPQConfig> createConfigBuilder(TPQConfig config) {
|
||||
return new TPQConfigBuilder<TPQConfig>(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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<T> {
|
|||
* @param resultBuilder the result of this builder.
|
||||
* @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(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<T> {
|
|||
* @return The builder.
|
||||
* @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");
|
||||
try {
|
||||
return readQuerySet(new FileInputStream(file),file.getAbsolutePath());
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new JAXBException(e);
|
||||
}
|
||||
return readQuerySet(new FileInputStream(file),file.getAbsolutePath());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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 -> {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String,TPQueryParameterMetaData> 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) {
|
||||
|
|
|
@ -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)
|
||||
public void testValueTypeAliasAdd() throws Exception {
|
||||
TPQConfig c = TPQFactory.createConfig();
|
||||
|
|
|
@ -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<String,Object>());
|
||||
manager.getQueryExecutorContext().prepareQuery("junit.test", new HashMap<String,Object>());
|
||||
}
|
||||
|
||||
@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<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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue