Initial pool
This commit is contained in:
@@ -3,6 +3,7 @@ package com.ilummc.tlib;
|
||||
import com.ilummc.tlib.annotations.Dependency;
|
||||
import com.ilummc.tlib.compat.PlaceholderHook;
|
||||
import com.ilummc.tlib.config.TLibConfig;
|
||||
import com.ilummc.tlib.db.Pool;
|
||||
import com.ilummc.tlib.filter.TLoggerFilter;
|
||||
import com.ilummc.tlib.inject.TConfigWatcher;
|
||||
import com.ilummc.tlib.inject.TDependencyInjector;
|
||||
@@ -25,6 +26,16 @@ import java.nio.charset.Charset;
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.ow2.asm:asm:6.1.1")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "com.zaxxer:HikariCP:3.1.0")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.slf4j:slf4j-api:1.7.25")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.javalite:activejdbc:2.0")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.javalite:javalite-common:2.0")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.javalite:app-config:2.0")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.codehaus.jackson:jackson-mapper-asl:1.9.13")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.codehaus.jackson:jackson-core-asl:1.9.13")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "jaxen:jaxen:1.1.6")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "dom4j:dom4j:1.6.1")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "xml-apis:xml-apis:1.0.b2")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.ehcache:ehcache:3.5.2")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "com.h2database:h2:1.4.197")
|
||||
public class TLib {
|
||||
|
||||
@Getter
|
||||
@@ -34,7 +45,7 @@ public class TLib {
|
||||
private TLogger logger = new TLogger("§8[§3§lTabooLib§8][§r{1}§8] §f{2}", Main.getInst(), TLogger.FINE);
|
||||
|
||||
@Getter
|
||||
private TLibConfig config;
|
||||
private TLibConfig config = new TLibConfig();
|
||||
|
||||
@Getter
|
||||
private TConfigWatcher configWatcher = new TConfigWatcher();
|
||||
@@ -66,9 +77,18 @@ public class TLib {
|
||||
PlaceholderHook.init();
|
||||
TLocaleLoader.load(Main.getInst(), false);
|
||||
TDependencyInjector.inject(Main.getInst(), tLib);
|
||||
|
||||
// init database
|
||||
try {
|
||||
Pool.init();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void unload() {
|
||||
Pool.unload();
|
||||
tLib.getConfigWatcher().unregisterAll();
|
||||
TDependencyInjector.eject(Main.getInst(), tLib);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.ilummc.tlib.config;
|
||||
|
||||
import com.ilummc.tlib.annotations.Config;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
@@ -9,9 +13,28 @@ import com.ilummc.tlib.annotations.Config;
|
||||
@Config(name = "tlib.yml", listenChanges = true, readOnly = false)
|
||||
public class TLibConfig {
|
||||
|
||||
private boolean enablePlaceholderHookByDefault = false;
|
||||
@Getter
|
||||
private String dataSourceClassName;
|
||||
|
||||
@Getter
|
||||
private String jdbcUrl = "jdbc:h2:file:~/plugins/TabooLib/h2";
|
||||
|
||||
@Getter
|
||||
private String driverClassName;
|
||||
|
||||
@Getter
|
||||
private String username = "";
|
||||
|
||||
@Getter
|
||||
private String password = "";
|
||||
|
||||
@Getter
|
||||
private int maximumPoolSize = 4;
|
||||
|
||||
@Getter
|
||||
private Map<String, Object> settings = new HashMap<String, Object>() {{
|
||||
put("cachePrepStmts", true);
|
||||
put("useServerPrepStmts", true);
|
||||
}};
|
||||
|
||||
public boolean isEnablePlaceholderHookByDefault() {
|
||||
return enablePlaceholderHookByDefault;
|
||||
}
|
||||
}
|
||||
|
||||
61
src/main/java/com/ilummc/tlib/db/Pool.java
Normal file
61
src/main/java/com/ilummc/tlib/db/Pool.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package com.ilummc.tlib.db;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import org.javalite.activejdbc.Base;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public final class Pool extends ThreadPoolExecutor {
|
||||
|
||||
private static final AtomicInteger number = new AtomicInteger(1);
|
||||
|
||||
private static final Pool singleton = new Pool();
|
||||
|
||||
private final TLibDataSource dataSource;
|
||||
|
||||
private Pool() {
|
||||
super(TLib.getTLib().getConfig().getMaximumPoolSize(),
|
||||
TLib.getTLib().getConfig().getMaximumPoolSize(),
|
||||
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
|
||||
try {
|
||||
dataSource = new TLibDataSource();
|
||||
this.setThreadFactory(r -> new Thread(() -> {
|
||||
Base.open(dataSource.getDataSource());
|
||||
r.run();
|
||||
}, "TabooLib-DbPool-" + number.getAndIncrement()));
|
||||
prestartAllCoreThreads();
|
||||
TLocale.sendToConsole("DATABASE.CONNECTION-ESTABLISHED", dataSource.getDataSource().getConnection().getMetaData().getDatabaseProductName(),
|
||||
String.valueOf(TLib.getTLib().getConfig().getMaximumPoolSize()));
|
||||
} catch (Exception e) {
|
||||
TLocale.sendToConsole("DATABASE.CONNECTION-ERROR", e.toString());
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void run(Runnable runnable) {
|
||||
instance().execute(runnable);
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
|
||||
}
|
||||
|
||||
public static void unload() {
|
||||
instance().dataSource.disconnect();
|
||||
instance().shutdown();
|
||||
}
|
||||
|
||||
public static Pool instance() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterExecute(Runnable r, Throwable t) {
|
||||
if (t != null) Base.close();
|
||||
}
|
||||
|
||||
}
|
||||
35
src/main/java/com/ilummc/tlib/db/TLibDataSource.java
Normal file
35
src/main/java/com/ilummc/tlib/db/TLibDataSource.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.ilummc.tlib.db;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.javalite.activejdbc.Base;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
public class TLibDataSource {
|
||||
|
||||
private final HikariDataSource dataSource;
|
||||
|
||||
TLibDataSource() {
|
||||
Properties properties = new Properties();
|
||||
properties.put("jdbcUrl", TLib.getTLib().getConfig().getJdbcUrl());
|
||||
properties.put("username", TLib.getTLib().getConfig().getUsername());
|
||||
properties.put("password", TLib.getTLib().getConfig().getPassword());
|
||||
properties.put("dataSourceClassName", TLib.getTLib().getConfig().getDataSourceClassName());
|
||||
properties.put("driverClassName", TLib.getTLib().getConfig().getDriverClassName());
|
||||
TLib.getTLib().getConfig().getSettings().forEach((k, v) -> properties.put("dataSource." + k, v));
|
||||
dataSource = new HikariDataSource(new HikariConfig(properties));
|
||||
Base.open(dataSource);
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
Base.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.ilummc.tlib.inject;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.ilummc.tlib.annotations.Config;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
@@ -14,6 +16,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TConfigInjector {
|
||||
@@ -55,7 +58,8 @@ public class TConfigInjector {
|
||||
File file = new File(plugin.getDataFolder(), config.name());
|
||||
if (!file.exists()) if (config.fromJar()) plugin.saveResource(config.name(), true);
|
||||
else saveConfig(plugin, clazz.newInstance());
|
||||
Object obj = unserialize(plugin, clazz);
|
||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
Object obj = gson.fromJson(gson.toJson(new Yaml().load(Files.toString(file, Charset.forName(config.charset())))), clazz);
|
||||
if (!config.readOnly()) saveConfig(plugin, obj);
|
||||
return obj;
|
||||
} catch (NullPointerException e) {
|
||||
@@ -106,7 +110,7 @@ public class TConfigInjector {
|
||||
try {
|
||||
Config config = object.getClass().getAnnotation(Config.class);
|
||||
Validate.notNull(config);
|
||||
return ConfigUtils.objToConf(object).getValues(false);
|
||||
return ConfigUtils.objToMap(ConfigUtils.objToConf(object).getValues(false), config.excludeModifiers());
|
||||
} catch (NullPointerException e) {
|
||||
TLocale.Logger.warn("CONFIG.SAVE-FAIL-NO-ANNOTATION", plugin.toString(), object.getClass().getSimpleName());
|
||||
} catch (Exception e) {
|
||||
@@ -118,16 +122,12 @@ public class TConfigInjector {
|
||||
public static void saveConfig(Plugin plugin, Object object) throws IOException, NullPointerException {
|
||||
Config config = object.getClass().getAnnotation(Config.class);
|
||||
Validate.notNull(config);
|
||||
Object obj = serialize(plugin, object);
|
||||
Validate.notNull(obj);
|
||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
Map map = gson.fromJson(gson.toJson(object), HashMap.class);
|
||||
YamlConfiguration configuration = (YamlConfiguration) ConfigUtils.mapToConf(map);
|
||||
File target = new File(plugin.getDataFolder(), config.name());
|
||||
if (!target.exists()) target.createNewFile();
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
options.setAllowUnicode(false);
|
||||
Yaml yaml = new Yaml(options);
|
||||
String str = yaml.dump(obj);
|
||||
byte[] arr = str.getBytes(config.charset());
|
||||
byte[] arr = configuration.saveToString().getBytes(config.charset());
|
||||
Files.write(arr, target);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ public class TDependencyInjector {
|
||||
injectConfig(plugin, o);
|
||||
injectPluginInstance(plugin, o);
|
||||
TLocaleLoader.load(plugin, true);
|
||||
if (o != TLib.getTLib())
|
||||
injectDatabase(plugin, o);
|
||||
}
|
||||
|
||||
static void injectOnEnable(Plugin plugin) {
|
||||
@@ -55,6 +57,10 @@ public class TDependencyInjector {
|
||||
}
|
||||
}
|
||||
|
||||
private static void injectDatabase(Plugin plugin, Object o) {
|
||||
|
||||
}
|
||||
|
||||
private static void injectConfig(Plugin plugin, Object o) {
|
||||
for (Field field : Ref.getDeclaredFields(o.getClass())) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user