*/
package org.mxchange.addressbook.database.backend;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.text.MessageFormat;
+import java.util.Enumeration;
import org.mxchange.addressbook.BaseFrameworkSystem;
+import org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException;
/**
* Generall database backend
*/
protected BaseDatabaseBackend () {
}
+
+ /**
+ * Validates driver name and throws an exception if the driver is not valid
+ *
+ * @param driverName Driver name (e.g. "mysql")
+ * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException If the given driver name cannot be solved into a driver instance
+ */
+ protected void validateDriver (final String driverName) throws UnsupportedDatabaseDriverException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("driverName={0} - CALLED!", driverName)); //NOI18N
+
+ // Try to find the driver in driver list
+ Enumeration<Driver> drivers = DriverManager.getDrivers();
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Search for it
+ while (drivers.hasMoreElements()) {
+ // Get element
+ Driver driver = drivers.nextElement();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Driver {0} is installed.", driver)); //NOI18N
+
+ // Get class name
+ String className = driver.getClass().getName();
+
+ // Is this wanted?
+ if (className.contains(driverName)) {
+ // Debug message
+ this.getLogger().debug("Found driver!"); //NOI18N
+
+ // Found it
+ isFound = true;
+ break;
+ }
+ }
+
+ // Is it found?
+ if (!isFound) {
+ // Throw exception
+ throw new UnsupportedDatabaseDriverException(driverName);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
}