Skip to content

Commit

Permalink
IHandlerInitializer.java
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Jan 6, 2021
1 parent 5bc0a80 commit fb0a0f3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
69 changes: 43 additions & 26 deletions src/main/java/com/mengcraft/simpleorm/EbeanManager.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mengcraft.simpleorm;

import com.mengcraft.simpleorm.provider.IHandlerInitializer;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
Expand All @@ -12,6 +14,7 @@ public class EbeanManager {

public static final EbeanManager DEFAULT = new EbeanManager();

private static final IHandlerInitializer STANDARD_HANDLER_INITIALIZER = new StandardHandlerInitializer();
private static String url = "jdbc:mysql://localhost/db";
private static String user = "user";
private static String password = "passwd";
Expand All @@ -26,13 +29,21 @@ public EbeanHandler getHandler(JavaPlugin plugin) {
}

public EbeanHandler getHandler(JavaPlugin plugin, boolean shared) {
return getHandler(plugin, shared, STANDARD_HANDLER_INITIALIZER);
}

public EbeanHandler getHandler(JavaPlugin plugin, IHandlerInitializer initializer) {
return getHandler(plugin, false, initializer);
}

public EbeanHandler getHandler(JavaPlugin plugin, boolean shared, IHandlerInitializer initializer) {
if (!ORM.isFullyEnabled()) {
plugin.getLogger().warning("Try register db handler while ORM not fully enabled(Not depend on or register at onLoad?).");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + plugin.getName() + " try register db handler while ORM not fully enabled. This may cause unknown issues.");
}
EbeanHandler out = map.get(plugin.getName());
if (out == null) {
map.put(plugin.getName(), out = build(plugin, shared));
map.put(plugin.getName(), out = build(plugin, shared, initializer));
}
return out;
}
Expand All @@ -45,36 +56,13 @@ public EbeanHandler getHandler(String name) {
return map.get(name);
}

private EbeanHandler build(JavaPlugin plugin, boolean shared) {
private EbeanHandler build(JavaPlugin plugin, boolean shared, IHandlerInitializer initializer) {
if (shared || plugin.getConfig().getBoolean("dataSource.disabled", false)) {
return new EbeanHandler(plugin, true, ORM.getSharedDs());
}

EbeanHandler handler = new EbeanHandler(plugin, true, null);
String url = plugin.getConfig().getString("dataSource.url");
String user = plugin.getConfig().getString("dataSource.user");
if (user == null) {
user = plugin.getConfig().getString("dataSource.userName");
}
String password = plugin.getConfig().getString("dataSource.password");

if (url == null) {
plugin.getConfig().set("dataSource.disabled", false);
plugin.getConfig().set("dataSource.url", url = EbeanManager.getUrl());
plugin.getConfig().set("dataSource.user", user = EbeanManager.getUser());
plugin.getConfig().set("dataSource.password", password = EbeanManager.getPassword());
plugin.saveConfig();
} else {
String driver = plugin.getConfig().getString("dataSource.driver");
if (driver != null) {
handler.setDriver(driver);
}
}

handler.setUrl(url);
handler.setUser(user);
handler.setPassword(password);

initializer.initialize(plugin, handler);
return handler;
}

Expand Down Expand Up @@ -114,4 +102,33 @@ public static void shutdown(JavaPlugin plugin) throws DatabaseException {
}
}

private static class StandardHandlerInitializer implements IHandlerInitializer {

@Override
public void initialize(Plugin plugin, EbeanHandler handler) {
String url = plugin.getConfig().getString("dataSource.url");
String user = plugin.getConfig().getString("dataSource.user");
if (user == null) {
user = plugin.getConfig().getString("dataSource.userName");
}
String password = plugin.getConfig().getString("dataSource.password");

if (url == null) {
plugin.getConfig().set("dataSource.disabled", false);
plugin.getConfig().set("dataSource.url", url = EbeanManager.getUrl());
plugin.getConfig().set("dataSource.user", user = EbeanManager.getUser());
plugin.getConfig().set("dataSource.password", password = EbeanManager.getPassword());
plugin.saveConfig();
} else {
String driver = plugin.getConfig().getString("dataSource.driver");
if (driver != null) {
handler.setDriver(driver);
}
}

handler.setUrl(url);
handler.setUser(user);
handler.setPassword(password);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/mengcraft/simpleorm/ORM.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mengcraft.simpleorm.lib.LibraryLoader;
import com.mengcraft.simpleorm.lib.MavenLibrary;
import com.mengcraft.simpleorm.provider.IDataSourceProvider;
import com.mengcraft.simpleorm.provider.IHandlerInitializer;
import com.mengcraft.simpleorm.provider.IRedisProvider;
import com.mengcraft.simpleorm.redis.RedisProviders;
import com.mengcraft.simpleorm.serializable.SerializableTypes;
Expand Down Expand Up @@ -138,6 +139,10 @@ public static EbeanHandler getDataHandler(JavaPlugin plugin, boolean shared) {
return EbeanManager.DEFAULT.getHandler(plugin, shared);
}

public static EbeanHandler getDataHandler(JavaPlugin plugin, IHandlerInitializer initializer) {
return EbeanManager.DEFAULT.getHandler(plugin, initializer);
}

public static GenericTrigger getGenericTrigger() {
return GENERIC_TRIGGER;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mengcraft.simpleorm.provider;

import com.mengcraft.simpleorm.EbeanHandler;
import org.bukkit.plugin.Plugin;

public interface IHandlerInitializer {

void initialize(Plugin plugin, EbeanHandler handler);
}

0 comments on commit fb0a0f3

Please sign in to comment.