2013-06-04 18:14:43 +02:00
|
|
|
package com.gmail.nossr50.database;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.config.Config;
|
2013-08-22 15:11:33 +02:00
|
|
|
import com.gmail.nossr50.datatypes.database.DatabaseType;
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-06-04 18:14:43 +02:00
|
|
|
|
|
|
|
public class DatabaseManagerFactory {
|
2013-06-29 00:02:58 +02:00
|
|
|
private static Class<? extends DatabaseManager> customManager = null;
|
|
|
|
|
2013-06-04 18:14:43 +02:00
|
|
|
public static DatabaseManager getDatabaseManager() {
|
2013-06-29 00:02:58 +02:00
|
|
|
if (customManager != null) {
|
|
|
|
try {
|
2013-10-09 05:47:20 +02:00
|
|
|
return createDefaultCustomDatabaseManager();
|
2013-08-22 15:11:33 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2013-06-29 00:02:58 +02:00
|
|
|
mcMMO.p.debug("Could not create custom database manager");
|
|
|
|
e.printStackTrace();
|
2013-08-22 15:11:33 +02:00
|
|
|
}
|
|
|
|
catch (Throwable e) {
|
2013-06-29 00:02:58 +02:00
|
|
|
mcMMO.p.debug("Failed to create custom database manager");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
mcMMO.p.debug("Falling back on " + (Config.getInstance().getUseMySQL() ? "SQL" : "Flatfile") + " database");
|
|
|
|
}
|
2013-08-22 15:11:33 +02:00
|
|
|
|
2013-06-04 18:14:43 +02:00
|
|
|
return Config.getInstance().getUseMySQL() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
|
|
|
|
}
|
2013-06-29 00:02:58 +02:00
|
|
|
|
|
|
|
/**
|
2013-08-22 15:11:33 +02:00
|
|
|
* Sets the custom DatabaseManager class for mcMMO to use. This should be
|
2013-06-29 00:02:58 +02:00
|
|
|
* called prior to mcMMO enabling.
|
2013-10-09 05:47:20 +02:00
|
|
|
* <p/>
|
2013-06-29 00:02:58 +02:00
|
|
|
* The provided class must have an empty constructor, which is the one
|
|
|
|
* that will be used.
|
2013-10-09 05:47:20 +02:00
|
|
|
* <p/>
|
2013-06-29 00:02:58 +02:00
|
|
|
* This method is intended for API use, but it should not be considered
|
|
|
|
* stable. This method is subject to change and/or removal in future
|
|
|
|
* versions.
|
|
|
|
*
|
2013-08-10 20:10:45 +02:00
|
|
|
* @param clazz the DatabaseManager class to use
|
2013-10-09 05:47:20 +02:00
|
|
|
*
|
2013-06-29 00:02:58 +02:00
|
|
|
* @throws IllegalArgumentException if the provided class does not have
|
2013-10-09 05:47:20 +02:00
|
|
|
* an empty constructor
|
2013-06-29 00:02:58 +02:00
|
|
|
*/
|
|
|
|
public static void setCustomDatabaseManagerClass(Class<? extends DatabaseManager> clazz) {
|
|
|
|
try {
|
2015-01-22 13:14:14 +01:00
|
|
|
clazz.getConstructor();
|
2013-07-01 06:34:54 +02:00
|
|
|
customManager = clazz;
|
2013-08-22 15:11:33 +02:00
|
|
|
}
|
|
|
|
catch (Throwable e) {
|
2013-06-29 00:02:58 +02:00
|
|
|
throw new IllegalArgumentException("Provided database manager class must have an empty constructor", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Class<? extends DatabaseManager> getCustomDatabaseManagerClass() {
|
|
|
|
return customManager;
|
|
|
|
}
|
|
|
|
|
2013-08-22 15:11:33 +02:00
|
|
|
public static DatabaseManager createDatabaseManager(DatabaseType type) {
|
|
|
|
switch (type) {
|
|
|
|
case FLATFILE:
|
|
|
|
return new FlatfileDatabaseManager();
|
2013-06-29 00:02:58 +02:00
|
|
|
|
2013-08-22 15:11:33 +02:00
|
|
|
case SQL:
|
|
|
|
return new SQLDatabaseManager();
|
2013-06-29 00:02:58 +02:00
|
|
|
|
2013-10-09 05:47:20 +02:00
|
|
|
case CUSTOM:
|
|
|
|
try {
|
|
|
|
return createDefaultCustomDatabaseManager();
|
|
|
|
}
|
|
|
|
catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2013-08-22 15:11:33 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-29 00:02:58 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 05:47:20 +02:00
|
|
|
public static DatabaseManager createDefaultCustomDatabaseManager() throws Throwable {
|
2015-01-22 13:14:14 +01:00
|
|
|
return customManager.getConstructor().newInstance();
|
2013-10-09 05:47:20 +02:00
|
|
|
}
|
|
|
|
|
2013-06-29 00:02:58 +02:00
|
|
|
public static DatabaseManager createCustomDatabaseManager(Class<? extends DatabaseManager> clazz) throws Throwable {
|
2015-01-22 13:14:14 +01:00
|
|
|
return clazz.getConstructor().newInstance();
|
2013-06-29 00:02:58 +02:00
|
|
|
}
|
2013-06-04 18:14:43 +02:00
|
|
|
}
|