Launcher/LauncherClient/src/main/java/pro/gravit/launcher/client/LauncherAgent.java

60 lines
1.9 KiB
Java
Raw Normal View History

2023-12-23 08:05:23 +03:00
package pro.gravit.launcher.client;
2023-12-23 08:05:23 +03:00
import pro.gravit.launcher.client.runtime.utils.NativeJVMHalt;
2019-10-19 19:46:04 +03:00
import pro.gravit.utils.helper.LogHelper;
2019-08-23 13:35:36 +03:00
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
2019-08-23 13:35:36 +03:00
import java.nio.file.Path;
import java.util.jar.JarFile;
public final class LauncherAgent {
public static Instrumentation inst;
2020-04-05 10:27:04 +03:00
private static boolean isAgentStarted = false;
public static void addJVMClassPath(String path) throws IOException {
LogHelper.debug("Launcher Agent addJVMClassPath");
2019-08-23 13:35:36 +03:00
inst.appendToSystemClassLoaderSearch(new JarFile(new File(path)));
}
2019-10-19 19:46:04 +03:00
2019-08-23 13:35:36 +03:00
public static void addJVMClassPath(Path path) throws IOException {
LogHelper.debug("Launcher Agent addJVMClassPath");
inst.appendToSystemClassLoaderSearch(new JarFile(path.toFile()));
}
public static void premain(String agentArgument, Instrumentation instrumentation) {
2019-05-15 14:11:22 +03:00
System.out.println("Launcher Agent");
checkAgentStacktrace();
inst = instrumentation;
NativeJVMHalt.initFunc();
isAgentStarted = true;
}
2019-10-19 19:46:04 +03:00
public static void checkAgentStacktrace() {
RuntimeException ex = new SecurityException("Error check agent stacktrace");
boolean isFoundNative = false;
boolean foundPreMain = false;
2019-10-19 19:46:04 +03:00
for (StackTraceElement e : ex.getStackTrace()) {
if (e.isNativeMethod()) {
if (!isFoundNative) isFoundNative = true;
else throw ex;
}
2019-10-19 19:46:04 +03:00
if (e.getMethodName().equals("premain")) {
if (!foundPreMain) foundPreMain = true;
else throw ex;
}
}
2019-10-19 19:46:04 +03:00
if (!isFoundNative || !foundPreMain) throw ex;
}
public static boolean isStarted() {
return isAgentStarted;
}
2020-04-05 10:27:04 +03:00
public boolean isAgentStarted() {
return isAgentStarted;
}
}