Launcher/LaunchServer/src/main/java/ru/gravit/launchserver/StarterAgent.java

49 lines
1.5 KiB
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
package ru.gravit.launchserver;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
2018-12-26 15:33:49 +03:00
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
2018-09-17 10:07:32 +03:00
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.jar.JarFile;
public class StarterAgent {
2018-09-22 17:33:00 +03:00
public static final class StarterVisitor extends SimpleFileVisitor<Path> {
private Instrumentation inst;
2018-09-17 10:07:32 +03:00
2018-09-22 17:33:00 +03:00
public StarterVisitor(Instrumentation inst) {
this.inst = inst;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().endsWith(".jar"))
inst.appendToSystemClassLoaderSearch(new JarFile(file.toFile()));
return super.visitFile(file, attrs);
}
}
2018-12-20 18:45:01 +03:00
public static Instrumentation inst;
private static boolean isStarted = false;
2018-12-20 18:45:01 +03:00
public static boolean isAgentStarted() {
return isStarted;
}
2018-09-22 17:33:00 +03:00
public static void premain(String agentArgument, Instrumentation inst) {
StarterAgent.inst = inst;
isStarted = true;
2018-09-22 17:33:00 +03:00
try {
Files.walkFileTree(Paths.get("libraries"), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor(inst));
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
2018-09-17 10:07:32 +03:00
}