2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launcher.server;
|
2018-09-24 14:36:20 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
|
2018-09-24 14:36:20 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.instrument.Instrumentation;
|
2018-12-19 15:38:32 +03:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.MethodType;
|
2019-04-03 16:27:40 +03:00
|
|
|
import java.nio.file.FileVisitResult;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.SimpleFileVisitor;
|
2018-09-24 14:36:20 +03:00
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
|
|
public class ServerAgent {
|
2020-04-05 10:27:04 +03:00
|
|
|
public static final Boolean isAutoloadLibraries = Boolean.getBoolean(System.getProperty("serverwrapper,agentlibrariesload", "false"));
|
2019-01-13 18:51:39 +03:00
|
|
|
public static Instrumentation inst = null;
|
2020-04-05 10:27:04 +03:00
|
|
|
private static boolean isAgentStarted = false;
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-24 14:36:20 +03:00
|
|
|
public static void addJVMClassPath(String path) throws IOException {
|
2018-11-08 15:30:16 +03:00
|
|
|
LogHelper.debug("Load %s", path);
|
2018-09-24 14:36:20 +03:00
|
|
|
inst.appendToSystemClassLoaderSearch(new JarFile(path));
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-01-15 06:32:46 +03:00
|
|
|
public static void addJVMClassPath(JarFile file) {
|
2018-11-08 15:30:16 +03:00
|
|
|
LogHelper.debug("Load %s", file.getName());
|
2018-09-24 14:36:20 +03:00
|
|
|
inst.appendToSystemClassLoaderSearch(file);
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-12-19 16:02:08 +03:00
|
|
|
public static boolean isAgentStarted() {
|
2018-09-24 14:36:20 +03:00
|
|
|
return isAgentStarted;
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-24 14:36:20 +03:00
|
|
|
public static long getObjSize(Object obj) {
|
|
|
|
return inst.getObjectSize(obj);
|
|
|
|
}
|
2018-12-20 18:45:01 +03:00
|
|
|
|
2018-09-24 14:36:20 +03:00
|
|
|
public static void premain(String agentArgument, Instrumentation instrumentation) {
|
|
|
|
LogHelper.debug("Server Agent");
|
|
|
|
inst = instrumentation;
|
|
|
|
isAgentStarted = true;
|
2018-12-20 18:45:01 +03:00
|
|
|
if (isAutoloadLibraries) {
|
2018-12-19 15:38:32 +03:00
|
|
|
Path libraries = Paths.get("libraries");
|
2018-12-20 18:45:01 +03:00
|
|
|
if (IOHelper.exists(libraries)) loadLibraries(libraries);
|
2018-12-19 15:38:32 +03:00
|
|
|
}
|
2021-03-19 20:57:24 +03:00
|
|
|
String proxyClassName = System.getProperty("serverwrapper.agentproxy", null);
|
|
|
|
if (proxyClassName != null) {
|
2018-12-26 15:44:35 +03:00
|
|
|
Class<?> proxyClass;
|
2018-12-19 15:38:32 +03:00
|
|
|
try {
|
|
|
|
proxyClass = Class.forName(proxyClassName);
|
|
|
|
MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(proxyClass, "premain", MethodType.methodType(void.class, String.class, Instrumentation.class));
|
2019-06-28 11:01:18 +03:00
|
|
|
mainMethod.invoke(agentArgument, instrumentation);
|
2018-12-19 15:38:32 +03:00
|
|
|
} catch (Throwable e) {
|
2019-01-18 01:30:55 +03:00
|
|
|
LogHelper.error(e);
|
2018-12-19 15:38:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-20 18:45:01 +03:00
|
|
|
|
|
|
|
public static void loadLibraries(Path dir) {
|
2018-09-24 14:36:20 +03:00
|
|
|
try {
|
2019-01-18 01:30:55 +03:00
|
|
|
IOHelper.walk(dir, new StarterVisitor(), true);
|
2018-09-24 14:36:20 +03:00
|
|
|
} catch (IOException e) {
|
2019-01-18 01:30:55 +03:00
|
|
|
LogHelper.error(e);
|
2018-09-24 14:36:20 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
|
|
|
private static final class StarterVisitor extends SimpleFileVisitor<Path> {
|
|
|
|
@Override
|
|
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
|
if (file.toFile().getName().endsWith(".jar")) addJVMClassPath(new JarFile(file.toFile()));
|
|
|
|
return super.visitFile(file, attrs);
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 14:36:20 +03:00
|
|
|
}
|