-/*\r
- * Copyright (C) 2015 Roland Haeder\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package org.mxchange.addressbook.database.backend;\r
-\r
-import org.mxchange.addressbook.BaseFrameworkSystem;\r
-\r
-/**\r
- * Generall database backend\r
- * \r
- * @author Roland Haeder\r
- */\r
-public class BaseDatabaseBackend extends BaseFrameworkSystem {\r
- /**\r
- * No instances from this class\r
- */\r
- protected BaseDatabaseBackend () {\r
- }\r
-}\r
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+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
+ *
+ * @author Roland Haeder
+ */
+public class BaseDatabaseBackend extends BaseFrameworkSystem {
+
+ /**
+ * No instances from this class
+ */
+ 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
+ }
+}