// Figure out desired JDBC compatibility level
int compatibilityLevel = JdbcCompatibility.parseLevel(ps.get(PARAM_JDBC_COMPATIBILITY));
// Try to create the connection
JenaConnection conn = null;
boolean abort = false;
try {
// Attempt connection
conn = this.connect(ps, compatibilityLevel);
// Prepare reduced properties for initializing any pre and
// post-processors with
Properties initProps = new Properties(ps);
initProps.remove(PARAM_PASSWORD);
// Attempt registration of command pre-processors
Object ppObj = ps.get(PARAM_PRE_PROCESSOR);
List<String> preProcessors;
if (ppObj != null) {
if (ppObj instanceof String) {
// Single instance to try and register
preProcessors = new ArrayList<String>();
preProcessors.add(ppObj.toString());
} else if (ppObj instanceof List<?>) {
// Multiple instances to try and register
preProcessors = (List<String>) ppObj;
} else {
// Parameter set to some unexpected type
LOGGER.error("Driver Parameter " + PARAM_PRE_PROCESSOR + " has unexpected invalid value");
throw new SQLException("Parameter " + PARAM_PRE_PROCESSOR + " was set to a value of unexpected type "
+ ppObj.getClass().getCanonicalName()
+ ", expected either a String or List<String> as the parameter value");
}
// Try and create each pre-processor
for (String ppClassName : preProcessors) {
// Ignore null values
if (ppClassName == null)
continue;
try {
LOGGER.info("Attempting to initialize pre-processor " + ppClassName);
Class<?> c = Class.forName(ppClassName);
Object i = c.newInstance();
if (i instanceof CommandPreProcessor) {
// If it implements the right interface initialize
// and
// register it
CommandPreProcessor pp = (CommandPreProcessor) i;
pp.initialize(initProps);
conn.addPreProcessor(pp);
LOGGER.info("Initialized pre-processor " + ppClassName + " successfully");
} else {
// Otherwise throw an error
LOGGER.error("Invalid value for " + PARAM_PRE_PROCESSOR
+ " parameter, references a class that exists but does not implement the required interface");
throw new SQLException(
"Parameter "
+ PARAM_PRE_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that does not implement the expected CommandPreProcessor interface, please ensure that the class name is corect and that the class implements the required interface");
}
} catch (ClassNotFoundException e) {
// Unable to find the referenced class
LOGGER.error("Invalid value for " + PARAM_PRE_PROCESSOR
+ " parameter, references a class that did not exist", e);
throw new SQLException(
"Parameter "
+ PARAM_PRE_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that could not be found, please ensure that the class name is correct and the JAR containing this class is on your class path",
e);
} catch (InstantiationException e) {
// Unable to instantiate the referenced class
LOGGER.error("Invalid value for " + PARAM_PRE_PROCESSOR
+ " parameter, references a class that exists but does not have an appropriate constructor", e);
throw new SQLException(
"Parameter "
+ PARAM_PRE_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that could not be sucessfully instantiated, this class must have an unparameterized constructor to be usable with this parameter. If this is not possible try calling addPreProcessor() on the returned JenaConnection instead",
e);
} catch (IllegalAccessException e) {
// Referenced class is not accessible
LOGGER.error("Invalid value for " + PARAM_PRE_PROCESSOR
+ " parameter, references a class that exists but is inaccessible", e);
throw new SQLException(
"Parameter "
+ PARAM_PRE_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that could not be sucessfully instantiated, this class must have a publicly accessible unparameterized constructor to be usable with this parameter. If this is not possible try calling addPreProcessor() on the returned JenaConnection instead",
e);
} catch (SQLException e) {
// Throw as-is
throw e;
} catch (Exception e) {
// Unexpected error
LOGGER.error(
"Invalid value for "
+ PARAM_PRE_PROCESSOR
+ " parameter, references a class that attempting to initialize produced an unexpected exception",
e);
throw new SQLException(
"Parameter "
+ PARAM_PRE_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which caused an unexpected exception when trying to instantiate it, see the inner exception for details",
e);
}
}
}
// Attempt registration of results post-processors
ppObj = ps.get(PARAM_POST_PROCESSOR);
List<String> postProcessors;
if (ppObj != null) {
if (ppObj instanceof String) {
// Single instance to try and register
postProcessors = new ArrayList<String>();
postProcessors.add(ppObj.toString());
} else if (ppObj instanceof List<?>) {
// Multiple instances to try and register
postProcessors = (List<String>) ppObj;
} else {
// Parameter set to some unexpected type
LOGGER.error("Driver Parameter " + PARAM_POST_PROCESSOR + " has unexpected invalid value");
throw new SQLException("Parameter " + PARAM_POST_PROCESSOR + " was set to a value of unexpected type "
+ ppObj.getClass().getCanonicalName()
+ ", expected either a String or List<String> as the parameter value");
}
// Try and create each pre-processor
for (String ppClassName : postProcessors) {
// Ignore null values
if (ppClassName == null)
continue;
try {
LOGGER.info("Attempting to initialize post-processor " + ppClassName);
Class<?> c = Class.forName(ppClassName);
Object i = c.newInstance();
if (i instanceof ResultsPostProcessor) {
// If it implements the right interface initialize
// and
// register it
ResultsPostProcessor pp = (ResultsPostProcessor) i;
pp.initialize(initProps);
conn.addPostProcessor(pp);
LOGGER.info("Initialized post-processor " + ppClassName + " successfully");
} else {
// Otherwise throw an error
LOGGER.error("Invalid value for " + PARAM_POST_PROCESSOR
+ " parameter, references a class that exists but does not implement the required interface");
throw new SQLException(
"Parameter "
+ PARAM_POST_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that does not implement the expected ResultsPostProcessor interface, please ensure that the class name is corect and that the class implements the required interface");
}
} catch (ClassNotFoundException e) {
// Unable to find the referenced class
LOGGER.error("Invalid value for " + PARAM_POST_PROCESSOR
+ " parameter, references a class that did not exist", e);
throw new SQLException(
"Parameter "
+ PARAM_POST_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that could not be found, please ensure that the class name is correct and the JAR containing this class is on your class path",
e);
} catch (InstantiationException e) {
// Unable to instantiate the referenced class
LOGGER.error("Invalid value for " + PARAM_POST_PROCESSOR
+ " parameter, references a class that exists but does not have an appropriate constructor", e);
throw new SQLException(
"Parameter "
+ PARAM_POST_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that could not be sucessfully instantiated, this class must have an unparameterized constructor to be usable with this parameter. If this is not possible try calling addPostProcessor() on the returned JenaConnection instead",
e);
} catch (IllegalAccessException e) {
// Referenced class is not accessible
LOGGER.error("Invalid value for " + PARAM_POST_PROCESSOR
+ " parameter, references a class that exists but is inaccessible", e);
throw new SQLException(
"Parameter "
+ PARAM_POST_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which references a class that could not be sucessfully instantiated, this class must have a publicly accessible unparameterized constructor to be usable with this parameter. If this is not possible try calling addPostProcessor() on the returned JenaConnection instead",
e);
} catch (SQLException e) {
// Throw as-is
throw e;
} catch (Exception e) {
// Unexpected error
LOGGER.error(
"Invalid value for "
+ PARAM_POST_PROCESSOR
+ " parameter, references a class that attempting to initialize produced an unexpected exception",
e);
throw new SQLException(
"Parameter "
+ PARAM_POST_PROCESSOR
+ " includes the value "
+ ppClassName
+ " which caused an unexpected exception when trying to instantiate it, see the inner exception for details",
e);
}
}
}
// All pre and post processors successfully registered, return the
// connection
return conn;
} catch (SQLException e) {
abort = true;
throw e;
} catch (Exception e) {
abort = true;
LOGGER.error("Unexpected exception while establishing a connection", e);
throw new SQLException("Unexpected exception while establishing a connection, see inner exception for details", e);
} finally {
// If something has gone badly wrong close the connection
if (abort && conn != null) {
conn.close();
}
}
}