Launcher/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java

392 lines
17 KiB
Java
Raw Normal View History

package pro.gravit.launcher;
import pro.gravit.launcher.client.ClientModuleManager;
2019-06-03 10:58:10 +03:00
import pro.gravit.launcher.client.DirBridge;
import pro.gravit.launcher.utils.DirWatcher;
2019-06-03 10:58:10 +03:00
import pro.gravit.utils.helper.EnvHelper;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
2019-10-19 19:46:04 +03:00
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
2019-10-19 19:46:04 +03:00
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
2019-10-19 19:46:04 +03:00
public class ClientLauncherWrapper {
2019-04-03 16:27:40 +03:00
public static final String MAGIC_ARG = "-Djdk.attach.allowAttachSelf";
public static final String WAIT_PROCESS_PROPERTY = "launcher.waitProcess";
public static final String NO_JAVA_CHECK_PROPERTY = "launcher.noJavaCheck";
public static boolean noJavaCheck = Boolean.getBoolean(NO_JAVA_CHECK_PROPERTY);
2020-04-05 10:27:04 +03:00
public static boolean waitProcess = Boolean.getBoolean(WAIT_PROCESS_PROPERTY);
@LauncherInject("launcher.memory")
public static int launcherMemoryLimit;
2020-09-25 18:48:33 +03:00
public static void main(String[] arguments) throws IOException, InterruptedException {
LogHelper.printVersion("Launcher");
LogHelper.printLicense("Launcher");
JVMHelper.checkStackTrace(ClientLauncherWrapper.class);
JVMHelper.verifySystemProperties(Launcher.class, true);
2018-10-26 17:42:20 +03:00
EnvHelper.checkDangerousParams();
LauncherConfig config = Launcher.getConfig();
LauncherEngine.modulesManager = new ClientModuleManager();
LauncherConfig.initModules(LauncherEngine.modulesManager);
2019-10-19 19:52:57 +03:00
LogHelper.info("Launcher for project %s", config.projectName);
2019-05-15 14:11:22 +03:00
if (config.environment.equals(LauncherConfig.LauncherEnvironment.PROD)) {
if (System.getProperty(LogHelper.DEBUG_PROPERTY) != null) {
LogHelper.warning("Found -Dlauncher.debug=true");
}
2019-05-15 14:11:22 +03:00
if (System.getProperty(LogHelper.STACKTRACE_PROPERTY) != null) {
LogHelper.warning("Found -Dlauncher.stacktrace=true");
}
LogHelper.info("Debug mode disabled (found env PRODUCTION)");
2019-05-15 14:11:22 +03:00
} else {
LogHelper.info("If need debug output use -Dlauncher.debug=true");
LogHelper.info("If need stacktrace output use -Dlauncher.stacktrace=true");
2019-05-15 14:11:22 +03:00
if (LogHelper.isDebugEnabled()) waitProcess = true;
}
LogHelper.info("Restart Launcher with JavaAgent...");
ClientLauncherWrapperContext context = new ClientLauncherWrapperContext();
context.processBuilder = new ProcessBuilder();
if (waitProcess) context.processBuilder.inheritIO();
2021-05-31 01:51:40 +03:00
context.javaVersion = null;
try {
2021-05-28 23:14:03 +03:00
if (!noJavaCheck) {
List<JavaVersion> javaVersions = findJava();
for (JavaVersion version : javaVersions) {
2021-05-31 04:25:33 +03:00
LogHelper.debug("Found Java %d b%d in %s javafx %s", version.version, version.build, version.jvmDir.toString(), version.enabledJavaFX ? "supported" : "not supported");
2021-05-28 23:14:03 +03:00
if (context.javaVersion == null) {
context.javaVersion = version;
continue;
}
if (version.enabledJavaFX && !context.javaVersion.enabledJavaFX) {
context.javaVersion = version;
continue;
}
if (version.enabledJavaFX) {
if (context.javaVersion.version < version.version) {
context.javaVersion = version;
} else if (context.javaVersion.version == version.version && context.javaVersion.build < version.build) {
context.javaVersion = version;
}
}
}
}
} catch (Throwable e) {
LogHelper.error(e);
}
2021-05-31 04:25:33 +03:00
if (context.javaVersion == null) {
context.javaVersion = JavaVersion.getCurrentJavaVersion();
}
context.executePath = IOHelper.resolveJavaBin(context.javaVersion.jvmDir);
//List<String> args = new LinkedList<>();
//args.add(javaBin.toString());
String pathLauncher = IOHelper.getCodeSource(LauncherEngine.class).toString();
context.mainClass = LauncherEngine.class.getName();
context.memoryLimit = launcherMemoryLimit;
context.classpath.add(pathLauncher);
context.jvmProperties.put(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled()));
context.jvmProperties.put(LogHelper.STACKTRACE_PROPERTY, Boolean.toString(LogHelper.isStacktraceEnabled()));
context.jvmProperties.put(LogHelper.DEV_PROPERTY, Boolean.toString(LogHelper.isDevEnabled()));
context.addSystemProperty(DirBridge.CUSTOMDIR_PROPERTY);
context.addSystemProperty(DirBridge.USE_CUSTOMDIR_PROPERTY);
context.addSystemProperty(DirBridge.USE_OPTDIR_PROPERTY);
context.addSystemProperty(DirWatcher.IGN_OVERFLOW);
context.jvmModules.add("javafx.base");
context.jvmModules.add("javafx.graphics");
context.jvmModules.add("javafx.fxml");
context.jvmModules.add("javafx.controls");
context.jvmModules.add("javafx.swing");
2021-05-29 00:42:09 +03:00
context.jvmModules.add("javafx.media");
context.jvmModules.add("javafx.web");
context.args.add(MAGIC_ARG);
context.args.add("-XX:+DisableAttachMechanism");
EnvHelper.addEnv(context.processBuilder);
LauncherEngine.modulesManager.callWrapper(context);
// ---------
List<String> args = new ArrayList<>(16);
args.add(context.executePath.toAbsolutePath().toString());
args.addAll(context.args);
2021-05-25 12:17:29 +03:00
context.jvmProperties.forEach((key, value) -> args.add(String.format("-D%s=%s", key, value)));
if (context.javaVersion.version >= 9) {
context.javaFXPaths.add(context.javaVersion.jvmDir);
context.javaFXPaths.add(context.javaVersion.jvmDir.resolve("jre"));
Path openjfxPath = tryGetOpenJFXPath(context.javaVersion.jvmDir);
2021-05-25 12:17:29 +03:00
if (openjfxPath != null) {
context.javaFXPaths.add(openjfxPath);
}
StringBuilder modulesPath = new StringBuilder();
StringBuilder modulesAdd = new StringBuilder();
2021-05-25 12:17:29 +03:00
for (String moduleName : context.jvmModules) {
boolean success = tryAddModule(context.javaFXPaths, moduleName, modulesPath);
2021-05-25 12:17:29 +03:00
if (success) {
if (modulesAdd.length() > 0) modulesAdd.append(",");
modulesAdd.append(moduleName);
}
}
2021-05-25 12:17:29 +03:00
if (modulesAdd.length() > 0) {
args.add("--add-modules");
args.add(modulesAdd.toString());
}
2021-05-25 12:17:29 +03:00
if (modulesPath.length() > 0) {
args.add("--module-path");
args.add(modulesPath.toString());
}
}
if (context.memoryLimit != 0) {
args.add(String.format("-Xmx%dM", context.memoryLimit));
2021-03-20 11:50:32 +03:00
}
args.add("-cp");
args.add(String.join(IOHelper.PLATFORM_SEPARATOR, context.classpath));
args.add(context.mainClass);
args.addAll(context.clientArgs);
2018-10-24 16:40:51 +03:00
LogHelper.debug("Commandline: " + args);
context.processBuilder.command(args);
Process process = context.processBuilder.start();
if (!waitProcess) {
Thread.sleep(3000);
if (!process.isAlive()) {
int errorcode = process.exitValue();
2018-12-20 18:45:01 +03:00
if (errorcode != 0)
LogHelper.error("Process exit with error code: %d", errorcode);
else
LogHelper.info("Process exit with code 0");
} else {
LogHelper.debug("Process started success");
}
2018-11-08 15:30:16 +03:00
} else {
process.waitFor();
}
}
2019-10-19 19:46:04 +03:00
public static Path tryGetOpenJFXPath(Path jvmDir) {
String dirName = jvmDir.getFileName().toString();
Path parent = jvmDir.getParent();
2021-05-25 12:17:29 +03:00
if (parent == null) return null;
Path archJFXPath = parent.resolve(dirName.replace("openjdk", "openjfx"));
2021-05-25 12:17:29 +03:00
if (Files.isDirectory(archJFXPath)) {
return archJFXPath;
}
Path arch2JFXPath = parent.resolve(dirName.replace("jdk", "openjfx"));
2021-05-25 12:17:29 +03:00
if (Files.isDirectory(arch2JFXPath)) {
return arch2JFXPath;
}
2021-05-25 12:17:29 +03:00
if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) {
Path debianJfxPath = Paths.get("/usr/share/openjfx");
2021-05-25 12:17:29 +03:00
if (Files.isDirectory(debianJfxPath)) {
return debianJfxPath;
}
}
return null;
}
2019-10-19 19:46:04 +03:00
public static Path tryFindModule(Path path, String moduleName) {
Path result = path.resolve(moduleName.concat(".jar"));
2019-10-19 19:46:04 +03:00
if (!IOHelper.isFile(result))
result = path.resolve("lib").resolve(moduleName.concat(".jar"));
else return result;
2019-10-19 19:46:04 +03:00
if (!IOHelper.isFile(result))
return null;
else return result;
}
2019-10-19 19:46:04 +03:00
public static boolean tryAddModule(List<Path> paths, String moduleName, StringBuilder args) {
2019-10-19 19:46:04 +03:00
for (Path path : paths) {
if (path == null) continue;
Path result = tryFindModule(path, moduleName);
2019-10-19 19:46:04 +03:00
if (result != null) {
if (args.length() != 0) args.append(File.pathSeparatorChar);
args.append(result.toAbsolutePath().toString());
return true;
}
}
return false;
}
2021-05-28 23:14:03 +03:00
public static List<JavaVersion> findJava() {
List<String> javaPaths = new ArrayList<>(4);
List<JavaVersion> result = new ArrayList<>(4);
try {
tryAddJava(javaPaths, result, ClientLauncherWrapper.JavaVersion.getCurrentJavaVersion());
} catch (IOException e) {
LogHelper.error(e);
}
String[] path = System.getenv("PATH").split(JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE ? ";" : ":");
for (String p : path) {
try {
2021-05-28 23:14:03 +03:00
Path p1 = Paths.get(p);
Path javaExecPath = JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE ? p1.resolve("java.exe") : p1.resolve("java");
if (Files.exists(javaExecPath)) {
if (Files.isSymbolicLink(javaExecPath)) {
javaExecPath = javaExecPath.toRealPath();
}
p1 = javaExecPath.getParent().getParent();
tryAddJava(javaPaths, result, ClientLauncherWrapper.JavaVersion.getByPath(p1));
trySearchJava(javaPaths, result, p1.getParent());
}
} catch (IOException e) {
LogHelper.error(e);
}
}
2021-05-28 23:14:03 +03:00
if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE) {
Path rootDrive = Paths.get(System.getProperty("java.home"));
try {
trySearchJava(javaPaths, result, rootDrive.resolve("Program Files").resolve("Java"));
trySearchJava(javaPaths, result, rootDrive.resolve("Program Files").resolve("AdoptOpenJDK"));
} catch (IOException e) {
LogHelper.error(e);
}
} else if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) {
try {
trySearchJava(javaPaths, result, Paths.get("/usr/lib/jvm"));
} catch (IOException e) {
LogHelper.error(e);
}
}
2021-05-28 23:14:03 +03:00
return result;
}
2021-05-28 23:14:03 +03:00
public static boolean tryAddJava(List<String> javaPaths, List<JavaVersion> result, ClientLauncherWrapper.JavaVersion version) throws IOException {
if (version == null) return false;
String path = version.jvmDir.toAbsolutePath().toString();
if (javaPaths.contains(path)) return false;
javaPaths.add(path);
result.add(version);
return true;
}
public static void trySearchJava(List<String> javaPaths, List<JavaVersion> result, Path path) throws IOException {
if (!Files.isDirectory(path)) return;
Files.list(path).filter(p -> Files.exists(p.resolve("bin").resolve(JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE ? "java.exe" : "java"))).forEach(e -> {
try {
tryAddJava(javaPaths, result, JavaVersion.getByPath(e));
if (Files.exists(e.resolve("jre"))) {
tryAddJava(javaPaths, result, JavaVersion.getByPath(e.resolve("jre")));
}
2021-05-28 23:14:03 +03:00
} catch (IOException ioException) {
LogHelper.error(ioException);
}
2021-05-28 23:14:03 +03:00
});
}
public static class JavaVersionAndBuild {
public int version;
public int build;
public JavaVersionAndBuild(int version, int build) {
this.version = version;
this.build = build;
}
public JavaVersionAndBuild() {
}
}
2021-05-28 23:14:03 +03:00
public static JavaVersionAndBuild getJavaVersion(String version) {
JavaVersionAndBuild result = new JavaVersionAndBuild();
if (version.startsWith("1.")) {
2021-05-28 23:14:03 +03:00
result.version = Integer.parseInt(version.substring(2, 3));
result.build = Integer.parseInt(version.substring(version.indexOf('_') + 1));
} else {
int dot = version.indexOf(".");
if (dot != -1) {
2021-05-28 23:14:03 +03:00
result.version = Integer.parseInt(version.substring(0, dot));
dot = version.lastIndexOf(".");
result.build = Integer.parseInt(version.substring(dot + 1));
}
}
2021-05-28 23:14:03 +03:00
return result;
}
2021-03-20 11:53:22 +03:00
public static class JavaVersion {
public final Path jvmDir;
public final int version;
2021-05-22 19:51:46 +03:00
public final int build;
2021-03-20 11:53:22 +03:00
public boolean enabledJavaFX;
public JavaVersion(Path jvmDir, int version) {
this.jvmDir = jvmDir;
this.version = version;
2021-05-22 19:51:46 +03:00
this.build = 0;
2021-03-20 11:53:22 +03:00
this.enabledJavaFX = true;
}
2021-05-22 19:51:46 +03:00
public JavaVersion(Path jvmDir, int version, int build, boolean enabledJavaFX) {
this.jvmDir = jvmDir;
this.version = version;
this.build = build;
this.enabledJavaFX = enabledJavaFX;
}
2021-03-20 11:53:22 +03:00
public static JavaVersion getCurrentJavaVersion() {
2021-05-31 04:25:33 +03:00
return new JavaVersion(Paths.get(System.getProperty("java.home")), JVMHelper.getVersion(), 0, isCurrentJavaSupportJavaFX());
}
private static boolean isCurrentJavaSupportJavaFX() {
try {
Class.forName("javafx.application.Application");
return true;
} catch (ClassNotFoundException e) {
if (JVMHelper.getVersion() > 8) {
Path jvmDir = Paths.get(System.getProperty("java.home"));
return tryFindModule(jvmDir, "javafx.base") != null;
}
2021-05-31 04:25:33 +03:00
return false;
}
2021-03-20 11:53:22 +03:00
}
public static JavaVersion getByPath(Path jvmDir) throws IOException {
Path releaseFile = jvmDir.resolve("release");
2021-05-28 23:14:03 +03:00
JavaVersionAndBuild versionAndBuild;
if (IOHelper.isFile(releaseFile)) {
Properties properties = new Properties();
properties.load(IOHelper.newReader(releaseFile));
versionAndBuild = getJavaVersion(properties.getProperty("JAVA_VERSION").replaceAll("\"", ""));
} else {
versionAndBuild = new JavaVersionAndBuild(isExistExtJavaLibrary(jvmDir, "jfxrt") ? 8 : 9, 0);
}
JavaVersion resultJavaVersion = new JavaVersion(jvmDir, versionAndBuild.version, versionAndBuild.build, false);
if (versionAndBuild.version <= 8) {
2021-03-20 11:53:22 +03:00
resultJavaVersion.enabledJavaFX = isExistExtJavaLibrary(jvmDir, "jfxrt");
} else {
resultJavaVersion.enabledJavaFX = tryFindModule(jvmDir, "javafx.base") != null;
if (!resultJavaVersion.enabledJavaFX)
resultJavaVersion.enabledJavaFX = tryFindModule(jvmDir.resolve("jre"), "javafx.base") != null;
}
return resultJavaVersion;
}
public static boolean isExistExtJavaLibrary(Path jvmDir, String name) {
Path jrePath = jvmDir.resolve("lib").resolve("ext").resolve(name.concat(".jar"));
Path jdkPath = jvmDir.resolve("jre").resolve("lib").resolve("ext").resolve(name.concat(".jar"));
return IOHelper.isFile(jrePath) || IOHelper.isFile(jdkPath);
}
}
2021-05-25 12:17:29 +03:00
public static class ClientLauncherWrapperContext {
public JavaVersion javaVersion;
public Path executePath;
public String mainClass;
public int memoryLimit;
public ProcessBuilder processBuilder;
public List<String> args = new ArrayList<>(8);
public Map<String, String> jvmProperties = new HashMap<>();
public List<String> classpath = new ArrayList<>();
public List<String> jvmModules = new ArrayList<>();
public List<String> clientArgs = new ArrayList<>();
public List<Path> javaFXPaths = new ArrayList<>();
2021-05-25 12:17:29 +03:00
public void addSystemProperty(String name) {
String property = System.getProperty(name);
if (property != null)
jvmProperties.put(name, property);
}
}
}