]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/backend/BaseDatabaseBackend.java
Added text file
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / database / backend / BaseDatabaseBackend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.database.backend;
18
19 import java.sql.Driver;
20 import java.sql.DriverManager;
21 import java.text.MessageFormat;
22 import java.util.Enumeration;
23 import org.mxchange.addressbook.BaseFrameworkSystem;
24 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException;
25
26 /**
27  * Generall database backend
28  *
29  * @author Roland Haeder
30  */
31 public class BaseDatabaseBackend extends BaseFrameworkSystem {
32
33         /**
34          * No instances from this class
35          */
36         protected BaseDatabaseBackend () {
37         }
38
39         /**
40          * Validates driver name and throws an exception if the driver is not valid
41          *
42          * @param driverName Driver name (e.g. "mysql")
43          * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException If the given driver name cannot be solved into a driver instance
44          */
45         protected void validateDriver (final String driverName) throws UnsupportedDatabaseDriverException {
46                 // Trace message
47                 this.getLogger().trace(MessageFormat.format("driverName={0} - CALLED!", driverName)); //NOI18N
48
49                 // Try to find the driver in driver list
50                 Enumeration<Driver> drivers = DriverManager.getDrivers();
51
52                 // Default is not found
53                 boolean isFound = false;
54
55                 // Search for it
56                 while (drivers.hasMoreElements()) {
57                         // Get element
58                         Driver driver = drivers.nextElement();
59
60                         // Debug message
61                         this.getLogger().debug(MessageFormat.format("Driver {0} is installed.", driver)); //NOI18N
62
63                         // Get class name
64                         String className = driver.getClass().getName();
65
66                         // Is this wanted?
67                         if (className.contains(driverName)) {
68                                 // Debug message
69                                 this.getLogger().debug("Found driver!"); //NOI18N
70
71                                 // Found it
72                                 isFound = true;
73                                 break;
74                         }
75                 }
76
77                 // Is it found?
78                 if (!isFound) {
79                         // Throw exception
80                         throw new UnsupportedDatabaseDriverException(driverName);
81                 }
82
83                 // Trace message
84                 this.getLogger().trace("EXIT!"); //NOI18N
85         }
86 }