Skip to content

Commit

Permalink
Update h2 driver. Improve legacy compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Feb 28, 2019
1 parent 14a95d9 commit fc94d36
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/mengcraft/simpleorm/EbeanHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ public void reflect() {
plugin.getLogger().warning("自定义连接不能注入到端");
return;
}
try {
if (Reflect.isLegacy()) {
PluginDescriptionFile desc = plugin.getDescription();
if (!((boolean) desc.getClass().getMethod("isDatabaseEnabled").invoke(desc))) {
desc.getClass().getMethod("setDatabaseEnabled", boolean.class).invoke(desc, true);
}
Reflect.replace(plugin, server);
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "注入失败了,1.12服务端不再支持注入方式|" + e.toString());
Reflect.setDatabaseEnabled(desc, true);
Reflect.setEbeanServer(plugin, server);
} else {
plugin.getLogger().log(Level.WARNING, "注入失败了,1.12服务端不再支持注入方式");
}
}

@Deprecated
public void define(Class<?> clz) {
add(clz);
}
Expand All @@ -128,7 +127,7 @@ public <T> T find(Class<T> in, Object id) {
public void uninstall() {
validInitialized();
try {
SpiEbeanServer spi = SpiEbeanServer.class.cast(server);
SpiEbeanServer spi = (SpiEbeanServer) server;
DdlGenerator gen = spi.getDdlGenerator();
gen.runScript(true, gen.generateDropDdl());
} catch (Exception e) {
Expand All @@ -151,7 +150,7 @@ public void install(boolean ignore) {
} catch (Exception e) {
plugin.getLogger().info(e.getMessage());
plugin.getLogger().info("Start create tables, wait...");
DdlGenerator gen = SpiEbeanServer.class.cast(server).getDdlGenerator();
DdlGenerator gen = ((SpiEbeanServer) server).getDdlGenerator();
gen.runScript(ignore, gen.generateCreateDdl());
plugin.getLogger().info("Create tables done!");
}
Expand Down
42 changes: 37 additions & 5 deletions src/main/java/com/mengcraft/simpleorm/Reflect.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
package com.mengcraft.simpleorm;

import com.avaje.ebean.EbeanServer;
import lombok.SneakyThrows;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static com.mengcraft.simpleorm.lib.Reflector.getField;

public final class Reflect {

public static void replace(Plugin proxy, EbeanServer in) throws Exception {
Field server = JavaPlugin.class.getDeclaredField("ebean");
server.setAccessible(true);
server.set(proxy, in);
private static final Method SET_DATABASE_ENABLED = INIT_SET_DATABASE_ENABLED();
private static final Field EBEAN = INIT_EBEAN();
private static boolean legacy;

private static Method INIT_SET_DATABASE_ENABLED() {
try {
return PluginDescriptionFile.class.getMethod("setDatabaseEnabled", boolean.class);
} catch (NoSuchMethodException ignored) {
}
return null;
}

private static Field INIT_EBEAN() {
try {
Field ebean = JavaPlugin.class.getDeclaredField("ebean");
ebean.setAccessible(true);
legacy = true;
} catch (NoSuchFieldException ignored) {
}
return null;
}

@SneakyThrows
public static void setDatabaseEnabled(PluginDescriptionFile descriptionFile, boolean update) {
SET_DATABASE_ENABLED.invoke(descriptionFile, update);
}

@SneakyThrows
public static void setEbeanServer(Plugin proxy, EbeanServer in) {
EBEAN.set(proxy, in);
}

public static ClassLoader getLoader(Plugin plugin) throws Exception {
public static ClassLoader getLoader(Plugin plugin) {
return getField(plugin, "classLoader");
}

public static boolean isLegacy() {
return legacy;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/mengcraft/simpleorm/driver/H2Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ protected String protocol() {

@Override
protected String description() {
return "com.h2database:h2:1.4.197";
return "com.h2database:h2:1.4.198";
}
}

0 comments on commit fc94d36

Please sign in to comment.