-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
DatabaseFactory.java
333 lines (288 loc) · 13.6 KB
/
DatabaseFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package liquibase.database;
import liquibase.Scope;
import liquibase.database.core.UnsupportedDatabase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.DatabaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.logging.Logger;
import liquibase.resource.ResourceAccessor;
import liquibase.util.StringUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Driver;
import java.util.*;
public class DatabaseFactory {
private static final Logger LOG = Scope.getCurrentScope().getLog(DatabaseFactory.class);
private static DatabaseFactory instance;
private Map<String, SortedSet<Database>> implementedDatabases = new HashMap<>();
private Map<String, SortedSet<Database>> internalDatabases = new HashMap<>();
private DatabaseFactory() {
try {
for (Database database : Scope.getCurrentScope().getServiceLocator().findInstances(Database.class)) {
register(database);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static synchronized DatabaseFactory getInstance() {
if (instance == null) {
instance = new DatabaseFactory();
}
return instance;
}
/**
* Set singleton instance. Primarily used in testing
*/
public static synchronized void setInstance(DatabaseFactory databaseFactory) {
instance = databaseFactory;
}
public static synchronized void reset() {
instance = new DatabaseFactory();
}
/**
* Returns instances of all implemented database types.
*/
public List<Database> getImplementedDatabases() {
List<Database> returnList = new ArrayList<>();
for (SortedSet<Database> set : implementedDatabases.values()) {
returnList.add(set.iterator().next());
}
return returnList;
}
/**
* Returns instances of all "internal" database types.
*/
public List<Database> getInternalDatabases() {
List<Database> returnList = new ArrayList<>();
for (SortedSet<Database> set : internalDatabases.values()) {
returnList.add(set.iterator().next());
}
return returnList;
}
public void register(Database database) {
Map<String, SortedSet<Database>> map = null;
if (database instanceof InternalDatabase) {
map = internalDatabases;
} else {
map = implementedDatabases;
}
if (!map.containsKey(database.getShortName())) {
map.put(database.getShortName(), new TreeSet<>(new TreeSet<>(new DatabaseComparator())));
}
map.get(database.getShortName()).add(database);
}
public Database findCorrectDatabaseImplementation(DatabaseConnection connection) throws DatabaseException {
SortedSet<Database> foundDatabases = new TreeSet<>(new DatabaseComparator());
for (Database implementedDatabase : getImplementedDatabases()) {
if (connection instanceof OfflineConnection) {
if (((OfflineConnection) connection).isCorrectDatabaseImplementation(implementedDatabase)) {
foundDatabases.add(implementedDatabase);
}
} else {
if (implementedDatabase.isCorrectDatabaseImplementation(connection)) {
foundDatabases.add(implementedDatabase);
}
}
}
if (foundDatabases.isEmpty()) {
LOG.warning("Unknown database: " + connection.getDatabaseProductName());
UnsupportedDatabase unsupportedDB = new UnsupportedDatabase();
unsupportedDB.setConnection(connection);
return unsupportedDB;
}
Database returnDatabase;
try {
returnDatabase = foundDatabases.iterator().next().getClass().getConstructor().newInstance();
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
returnDatabase.setConnection(connection);
return returnDatabase;
}
public Database openDatabase(String url,
String username,
String password,
String propertyProviderClass,
ResourceAccessor resourceAccessor) throws DatabaseException {
return openDatabase(url, username, password, null, null, null, propertyProviderClass, resourceAccessor);
}
public Database openDatabase(String url,
String username,
String password,
String driver,
String databaseClass,
String driverPropertiesFile,
String propertyProviderClass,
ResourceAccessor resourceAccessor) throws DatabaseException {
return this.findCorrectDatabaseImplementation(openConnection(url, username, password, driver, databaseClass, driverPropertiesFile, propertyProviderClass, resourceAccessor));
}
public Database openDatabase(String url,
String username,
String driver,
String databaseClass,
Properties driverProperties,
ResourceAccessor resourceAccessor) throws DatabaseException {
return this.findCorrectDatabaseImplementation(openConnection(url, username, driver, databaseClass, driverProperties, resourceAccessor));
}
public DatabaseConnection openConnection(String url,
String username,
String password,
String propertyProvider,
ResourceAccessor resourceAccessor) throws DatabaseException {
return openConnection(url, username, password, null, null, null, propertyProvider, resourceAccessor);
}
public DatabaseConnection openConnection(String url,
String username,
String password,
String driver,
String databaseClass,
String driverPropertiesFile,
String propertyProviderClass,
ResourceAccessor resourceAccessor) throws DatabaseException {
Properties driverProperties;
try {
driverProperties = buildDriverProperties(username, password, driverPropertiesFile, propertyProviderClass);
} catch (Exception e) {
throw new DatabaseException(e);
}
return openConnection(url, username, driver, databaseClass, driverProperties, resourceAccessor);
}
@SuppressWarnings("java:S2095")
public DatabaseConnection openConnection(String url,
String username,
String driver,
String databaseClass,
Properties driverProperties,
ResourceAccessor resourceAccessor) throws DatabaseException {
if (url.startsWith("offline:")) {
OfflineConnection offlineConnection = new OfflineConnection(url, resourceAccessor);
offlineConnection.setConnectionUserName(username);
return offlineConnection;
}
DatabaseConnection databaseConnection;
try {
DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
if (databaseClass != null) {
databaseFactory.clearRegistry();
databaseFactory.register((Database) Class.forName(databaseClass, true, Scope.getCurrentScope().getClassLoader()).getConstructor().newInstance());
}
String selectedDriverClass = findDriverClass(url, driver, databaseFactory);
Driver driverObject = loadDriver(selectedDriverClass);
if (driverObject instanceof LiquibaseExtDriver) {
((LiquibaseExtDriver) driverObject).setResourceAccessor(resourceAccessor);
}
if(selectedDriverClass.contains("oracle")) {
driverProperties.put("remarksReporting", "true");
} else if(selectedDriverClass.contains("mysql")) {
driverProperties.put("useInformationSchema", "true");
}
LOG.fine("Connecting to the URL:'" + JdbcConnection.sanitizeUrl(url) + "' using driver:'" + driverObject.getClass().getName() + "'");
databaseConnection = ConnectionServiceFactory.getInstance().create(url, driverObject, driverProperties);
LOG.fine("Connection has been created");
} catch (Exception e) {
throw new DatabaseException(e);
}
return databaseConnection;
}
/**
* Returns the Java class name of the JDBC driver class (e.g. "org.mariadb.jdbc.Driver")
* for the specified JDBC URL, if any Database class supports that URL.
*
* @param url the JDBC URL to analyse
* @return a Database object supporting the URL. May also return null if the JDBC URL is unknown to all handlers.
*/
public String findDefaultDriver(String url) {
for (Database database : this.getImplementedDatabases()) {
String defaultDriver = database.getDefaultDriver(url);
if (defaultDriver != null) {
return defaultDriver;
}
}
return null;
}
/**
* Removes all registered databases, even built in ones. Useful for forcing a particular database implementation
*/
public void clearRegistry() {
implementedDatabases.clear();
}
public Database getDatabase(String shortName) {
if (!implementedDatabases.containsKey(shortName)) {
return null;
}
return implementedDatabases.get(shortName).iterator().next();
}
private String findDriverClass(String url, String driver, DatabaseFactory databaseFactory) {
String selectedDriverClass = StringUtil.trimToNull(driver);
if (selectedDriverClass == null) {
selectedDriverClass = databaseFactory.findDefaultDriver(url);
}
if (selectedDriverClass == null) {
throw new RuntimeException("Driver class was not specified and could not be determined from the url (" + url + ")");
}
return selectedDriverClass;
}
private Driver loadDriver(String driverClass) {
Driver driverObject;
try {
driverObject = (Driver) Class.forName(driverClass, true, Scope.getCurrentScope().getClassLoader()).getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot find database driver: " + e.getMessage());
}
return driverObject;
}
private Properties buildDriverProperties(String username, String password, String driverPropertiesFile, String propertyProviderClass) {
Properties driverProperties;
try {
if (propertyProviderClass == null) {
driverProperties = new Properties();
} else {
driverProperties = (Properties) Class.forName(propertyProviderClass, true, Scope.getCurrentScope().getClassLoader()).getConstructor().newInstance();
}
if (username != null) {
driverProperties.put("user", username);
}
if (password != null) {
driverProperties.put("password", password);
}
if (null != driverPropertiesFile) {
File propertiesFile = new File(driverPropertiesFile);
if (propertiesFile.exists()) {
LOG.fine(
"Loading properties from the file:'" + driverPropertiesFile + "'"
);
FileInputStream inputStream = new FileInputStream(propertiesFile);
try {
driverProperties.load(inputStream);
} finally {
inputStream.close();
}
} else {
throw new RuntimeException("Can't open JDBC Driver specific properties from the file: '"
+ driverPropertiesFile + "'");
}
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException | IOException e) {
throw new RuntimeException("Exception opening JDBC Driver specific properties from the file: '"
+ driverPropertiesFile + "'", e);
}
LOG.fine("Properties:");
for (Map.Entry<Object, Object> entry : driverProperties.entrySet()) {
if (entry.getKey().toString().toLowerCase().contains("password")) {
Scope.getCurrentScope().getLog(getClass()).fine("Key:'" + entry.getKey().toString() + "' Value:'**********'");
} else {
LOG.fine("Key:'" + entry.getKey().toString() + "' Value:'" + entry.getValue().toString() + "'");
}
}
return driverProperties;
}
private static class DatabaseComparator implements Comparator<Database> {
@Override
public int compare(Database o1, Database o2) {
return -1 * Integer.valueOf(o1.getPriority()).compareTo(o2.getPriority());
}
}
}