2018-09-24 14:36:20 +03:00
|
|
|
package ru.gravit.launcher.server;
|
|
|
|
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.instrument.Instrumentation;
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
|
|
|
public class ServerAgent {
|
2018-11-08 15:30:16 +03:00
|
|
|
private static boolean isAgentStarted = false;
|
2018-09-24 14:36:20 +03:00
|
|
|
public static Instrumentation inst;
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-24 14:36:20 +03:00
|
|
|
public 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-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
|
|
|
|
2018-09-24 14:36:20 +03:00
|
|
|
public static void addJVMClassPath(JarFile file) throws IOException {
|
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
|
|
|
|
|
|
|
public 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void premain(String agentArgument, Instrumentation instrumentation) {
|
|
|
|
LogHelper.debug("Server Agent");
|
|
|
|
inst = instrumentation;
|
|
|
|
isAgentStarted = true;
|
|
|
|
|
|
|
|
try {
|
2018-11-11 09:01:19 +03:00
|
|
|
Files.walkFileTree(Paths.get("libraries"), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor());
|
2018-09-24 14:36:20 +03:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace(System.err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|