mcMMO/src/main/java/com/gmail/nossr50/database/DatabaseManagerFactory.java

91 lines
3.2 KiB
Java
Raw Normal View History

package com.gmail.nossr50.database;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.mcMMO;
public class DatabaseManagerFactory {
private static Class<? extends DatabaseManager> customManager = null;
public static DatabaseManager getDatabaseManager() {
if (customManager != null) {
try {
2013-10-09 05:47:20 +02:00
return createDefaultCustomDatabaseManager();
}
catch (Exception e) {
mcMMO.p.debug("Could not create custom database manager");
e.printStackTrace();
}
catch (Throwable e) {
mcMMO.p.debug("Failed to create custom database manager");
e.printStackTrace();
}
mcMMO.p.debug("Falling back on " + (Config.getInstance().getUseMySQL() ? "SQL" : "Flatfile") + " database");
}
return Config.getInstance().getUseMySQL() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
}
/**
* Sets the custom DatabaseManager class for mcMMO to use. This should be
* called prior to mcMMO enabling.
2013-10-09 05:47:20 +02:00
* <p/>
* 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/>
* 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
*
* @throws IllegalArgumentException if the provided class does not have
2013-10-09 05:47:20 +02:00
* an empty constructor
*/
public static void setCustomDatabaseManagerClass(Class<? extends DatabaseManager> clazz) {
try {
clazz.getConstructor();
2013-07-01 06:34:54 +02:00
customManager = clazz;
}
catch (Throwable e) {
throw new IllegalArgumentException("Provided database manager class must have an empty constructor", e);
}
}
public static Class<? extends DatabaseManager> getCustomDatabaseManagerClass() {
return customManager;
}
public static DatabaseManager createDatabaseManager(DatabaseType type) {
switch (type) {
case FLATFILE:
2020-05-06 04:51:53 +02:00
mcMMO.p.getLogger().info("Using FlatFile Database");
return new FlatfileDatabaseManager();
case SQL:
2020-05-06 04:51:53 +02:00
mcMMO.p.getLogger().info("Using SQL Database");
return new SQLDatabaseManager();
2013-10-09 05:47:20 +02:00
case CUSTOM:
try {
2020-05-06 04:51:53 +02:00
mcMMO.p.getLogger().info("Attempting to use Custom Database");
2013-10-09 05:47:20 +02:00
return createDefaultCustomDatabaseManager();
}
catch (Throwable e) {
e.printStackTrace();
}
default:
return null;
}
}
2013-10-09 05:47:20 +02:00
public static DatabaseManager createDefaultCustomDatabaseManager() throws Throwable {
return customManager.getConstructor().newInstance();
2013-10-09 05:47:20 +02:00
}
public static DatabaseManager createCustomDatabaseManager(Class<? extends DatabaseManager> clazz) throws Throwable {
return clazz.getConstructor().newInstance();
}
}