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

68 lines
2.4 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;
2019-01-15 06:35:39 +03:00
import java.nio.file.*;
2018-09-17 10:07:32 +03:00
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
2018-09-17 10:07:32 +03:00
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
2018-09-17 10:07:32 +03:00
import java.util.jar.JarFile;
2019-01-09 11:29:54 +03:00
public final class StarterAgent {
2018-09-17 10:07:32 +03:00
2019-01-09 11:29:54 +03:00
private static final class StarterVisitor extends SimpleFileVisitor<Path> {
private static final Set<PosixFilePermission> DPERMS;
static {
Set<PosixFilePermission> perms = new HashSet<>(Arrays.asList(PosixFilePermission.values()));
perms.remove(PosixFilePermission.OTHERS_WRITE);
perms.remove(PosixFilePermission.GROUP_WRITE);
DPERMS = Collections.unmodifiableSet(perms);
}
2018-09-17 10:07:32 +03:00
private final Path filef;
private final boolean fixLib;
private StarterVisitor() {
this.filef = StarterAgent.libraries.resolve(".libraries_chmoded");
this.fixLib = !Files.exists(filef) && !Boolean.getBoolean("launcher.noLibrariesPosixPermsFix");
if (fixLib) {
try {
Files.deleteIfExists(filef);
Files.createFile(filef);
} catch (Throwable t) { }
}
2018-09-22 17:33:00 +03:00
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (fixLib && Files.getFileAttributeView(file, PosixFileAttributeView.class) != null) Files.setPosixFilePermissions(file, DPERMS);
2018-09-22 17:33:00 +03:00
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 = null;
public static Path libraries = null;
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;
libraries = Paths.get("libraries");
isStarted = true;
2018-09-22 17:33:00 +03:00
try {
Files.walkFileTree(libraries, Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor());
2018-09-22 17:33:00 +03:00
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
2018-09-17 10:07:32 +03:00
}