Skip to content

Commit

Permalink
Better library loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenicos committed Apr 17, 2022
1 parent 38c5d4c commit 0be3ad3
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 465 deletions.
8 changes: 4 additions & 4 deletions bridge/velocity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ dependencies {
exclude group: 'aopalliance', module: 'aopalliance'
exclude group: 'javax.inject', module: 'javax.inject'
}
implementation 'com.github.Osiris-Team:Dyml:9.4.0'
implementation 'com.saicone.ezlib:ezlib:1.1'
compileOnly 'com.github.Osiris-Team:Dyml:9.4.0'
}

jar {
Expand All @@ -50,7 +51,6 @@ jar {
shadowJar {
archiveBaseName.set('UClanSync-Velocity')
archiveClassifier.set('')
relocate 'com.osiris.dyml', 'com.saicone.uclansync.libs.dyml'
relocate 'org.jetbrains.annotations', 'com.saicone.uclansync.libs.annotations'
minimize()
relocate 'com.saicone.ezlib', project.group + '.lib.ezlib'
relocate 'com.osiris.dyml', project.group + '.lib.dyml'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.saicone.uclansync;

import com.google.inject.Inject;
import com.saicone.ezlib.Ezlib;
import com.saicone.uclansync.module.MainCommand;
import com.saicone.uclansync.module.Settings;
import com.saicone.uclansync.module.VelocityListener;
Expand All @@ -12,9 +13,12 @@
import com.velocitypowered.api.proxy.ProxyServer;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Plugin(id = "UClanSync", name = "Ultimate Clans Sync", version = "${version}", authors = "Rubenicos")
public class UClanSync {
Expand All @@ -30,6 +34,7 @@ public static Settings getSettings() {
return settings;
}

private Ezlib ezlib;
private final ProxyServer proxy;
private final Logger logger;
private final Path dataDirectory;
Expand All @@ -40,6 +45,10 @@ public static Settings getSettings() {
@Inject
public UClanSync(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
instance = this;
Map<String, String> relocations = new HashMap<>();
relocations.put(new StringBuilder("com").append(".osiris.dyml").toString(), "com.saicone.uclansync.lib.dyml");
ezlib = new Ezlib(new File(dataDirectory.toFile(), "libs"));
ezlib.load("com.github.Osiris-Team:Dyml:9.4.0", "https://jitpack.io", relocations, true);
settings = new Settings(dataDirectory.toFile(), "settings.yml", "${version}", true);
this.proxy = server;
this.logger = logger;
Expand Down
31 changes: 26 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2' apply false
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

allprojects {
Expand All @@ -11,6 +11,7 @@ allprojects {
url 'http://ptms.ink:8081/repository/releases/'
allowInsecureProtocol true
}
maven { url 'https://jitpack.io' }
mavenCentral()
}

Expand All @@ -34,12 +35,32 @@ dependencies {
compileOnly 'ink.ptms.core:v11802:11802:universal'
compileOnly 'ink.ptms.core:v11701:11701:universal'

implementation 'com.saicone.ezlib:ezlib:1.1'

compileOnly ('redis.clients:jedis:4.2.0') {
exclude group: 'com.google.code.gson', module: 'gson'
exclude group: 'org.json', module: 'json'
exclude group: 'org.slf4j', module: 'slf4j-api'
}
compileOnly ('com.rabbitmq:amqp-client:5.14.2') {
exclude group: 'org.slf4j', module: 'slf4j-api'
}

compileOnly fileTree(dir: 'libs', includes: ['*.jar'])
}

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
jar {
enabled = false
dependsOn (shadowJar)
}

test {
useJUnitPlatform()
shadowJar {
archiveClassifier.set('')
relocate 'com.saicone.ezlib', project.group + '.lib.ezlib'
relocate 'com.google.gson', project.group + '.lib.gson'
relocate 'org.apache.commons.pool2', project.group + '.lib.pool2'
relocate 'org.json', project.group + '.lib.json'
relocate 'org.slf4j', project.group + '.lib.slf4j'
relocate 'redis.clients.jedis', project.group + '.lib.jedis'
relocate 'com.rabbitmq', project.group + '.lib.rabbitmq'
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=com.saicone.uclansync
version=1.1.3
version=1.2.0-SNAPSHOT
25 changes: 0 additions & 25 deletions jar/build.gradle

This file was deleted.

22 changes: 0 additions & 22 deletions messenger/rabbitmq/build.gradle

This file was deleted.

24 changes: 0 additions & 24 deletions messenger/redis/build.gradle

This file was deleted.

8 changes: 1 addition & 7 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
rootProject.name = 'UClanSync'

include(
'bridge:bungee',
'bridge:velocity',
'messenger:rabbitmq',
'messenger:redis',
'jar'
)
include('bridge:bungee', 'bridge:velocity')
79 changes: 13 additions & 66 deletions src/main/java/com/saicone/uclansync/UClanSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@

import com.saicone.uclansync.core.ClanUpdater;
import com.saicone.uclansync.core.messenger.Messenger;
import com.saicone.uclansync.core.messenger.type.RabbitMQMessenger;
import com.saicone.uclansync.core.messenger.type.RedisMessenger;
import com.saicone.uclansync.module.Locale;
import com.saicone.uclansync.module.SettingsFile;
import com.saicone.uclansync.module.command.CommandLoader;
import com.saicone.uclansync.module.library.LibraryLoader;
import com.saicone.uclansync.module.LibraryLoader;
import com.saicone.uclansync.module.listener.BukkitListener;
import com.saicone.uclansync.util.Proxy;
import me.ulrich.clans.Clans;
import me.ulrich.clans.data.Addon;
import me.ulrich.clans.data.ClanEnum;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

public class UClanSync extends Addon {

private static UClanSync instance;
Expand Down Expand Up @@ -51,19 +44,17 @@ public void onLoad() {
SETTINGS.load(this).onReload();
Locale.INSTANCE.load(this);
if (isEnabled()) {
libraryLoader = new LibraryLoader(
new File(getAddonDataFolder(), "libs"),
getMessengers("rabbitmq", "redis"),
getClassLoader());
if (libraryLoader.load(SETTINGS.getConfigurationSection("Libraries.Redis"))) {
libraryLoader.init("com.saicone.uclansync.core.messenger.type.RedisMessenger");
} else {
Locale.log(2, "Redis messenger will not be loaded");
libraryLoader = new LibraryLoader();
libraryLoader.load();
try {
Messenger.PROVIDERS.put("redis", RedisMessenger.class);
} catch (Throwable t) {
Messenger.PROVIDERS.remove("redis");
}
if (libraryLoader.load(SETTINGS.getConfigurationSection("Libraries.RabbitMQ"))) {
libraryLoader.init("com.saicone.uclansync.core.messenger.type.RabbitMQMessenger");
} else {
Locale.log(2, "RabbitMQ messenger will not be loaded");
try {
Messenger.PROVIDERS.put("rabbitmq", RabbitMQMessenger.class);
} catch (Throwable t) {
Messenger.PROVIDERS.remove("rabbitmq");
}
} else {
return;
Expand Down Expand Up @@ -105,9 +96,6 @@ public void onDisable() {
if (clanUpdater != null) {
clanUpdater.onDisable();
}
if (libraryLoader != null) {
libraryLoader.close();
}
}

public void onReload() {
Expand All @@ -122,45 +110,4 @@ public void onReload() {
public ClanUpdater getClanUpdater() {
return clanUpdater;
}

private URL[] getMessengers(String... names) {
List<URL> urls = new ArrayList<>();
for (String name : names) {
URL url = getMessenger(name);
if (url != null) {
urls.add(url);
}
}
return urls.toArray(new URL[0]);
}

private URL getMessenger(String name) {
URL url = getClassLoader().getResource(name + "-messenger.compiled");
if (url == null) {
Bukkit.getConsoleSender().sendMessage("[UClanSync] The " + name + " addon was not found inside .jar");
return null;
}

Path path;
try {
path = Files.createTempFile("uclansync-" + name + "-messenger", ".jar.tmp");
} catch (Exception e) {
e.printStackTrace();
return null;
}
path.toFile().deleteOnExit();

try (InputStream in = url.openStream()) {
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
return null;
}

try {
return path.toUri().toURL();
} catch (Exception e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

public class RabbitMQMessenger extends Messenger implements DeliverCallback {

static {
Messenger.PROVIDERS.put("rabbitmq", RabbitMQMessenger.class);
}

private String exchange = UClanSync.SETTINGS.getString("Messenger.RabbitMQ.exchange", "uclansync");
private ConnectionFactory factory = null;
private Connection connection = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

public class RedisMessenger extends Messenger {

static {
Messenger.PROVIDERS.put("redis", RedisMessenger.class);
}

private final Bridge bridge;
private JedisPool pool = null;
private boolean enabled = false;
Expand Down
Loading

0 comments on commit 0be3ad3

Please sign in to comment.