Launcher/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java

147 lines
5 KiB
Java
Raw Normal View History

package pro.gravit.launcher.managers;
import pro.gravit.launcher.modules.Module;
import pro.gravit.launcher.modules.ModuleContext;
import pro.gravit.launcher.modules.ModulesManager;
import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
2018-12-20 18:45:01 +03:00
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
2018-12-20 18:45:01 +03:00
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.jar.JarFile;
public class SimpleModuleManager implements ModulesManager {
protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
private ModulesVisitor() {
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
2019-04-03 16:27:40 +03:00
if (file.toFile().getName().endsWith(".jar"))
try (JarFile f = new JarFile(file.toFile())) {
loadModule(file.toUri().toURL(), f.getManifest().getMainAttributes().getValue("Main-Class"));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
LogHelper.error(e);
}
return super.visitFile(file, attrs);
}
}
2018-09-22 17:33:00 +03:00
public ArrayList<Module> modules;
public PublicURLClassLoader classloader;
protected ModuleContext context;
2018-10-13 11:01:10 +03:00
public void autoload(Path dir) throws IOException {
LogHelper.info("Load modules");
if (Files.notExists(dir)) Files.createDirectory(dir);
IOHelper.walk(dir, new ModulesVisitor(), true);
2018-09-22 14:21:52 +03:00
sort();
LogHelper.info("Loaded %d modules", modules.size());
}
2018-09-22 17:33:00 +03:00
public void sort() {
modules.sort((m1, m2) -> {
int p1 = m1.getPriority();
int p2 = m2.getPriority();
2018-09-19 16:53:27 +03:00
return Integer.compare(p2, p1);
});
}
@Override
public void close() {
for (Module m : modules)
try {
m.close();
} catch (Throwable t) {
if (m.getName() != null) LogHelper.error("Error in stopping module: %s", m.getName());
else LogHelper.error("Error in stopping one of modules");
LogHelper.error(t);
}
}
@Override
public void initModules() {
for (Module m : modules) {
m.init(context);
LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion());
}
}
@Override
public void load(Module module) {
modules.add(module);
}
2018-12-17 19:22:04 +03:00
public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
2019-04-03 16:27:40 +03:00
try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) {
classloader.addURL(jarpath);
Module module = (Module) Class.forName(f.getManifest().getMainAttributes().getValue("Main-Class"), true, classloader).newInstance();
modules.add(module);
module.preInit(context);
module.init(context);
module.postInit(context);
module.finish(context);
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
}
2018-12-17 19:22:04 +03:00
}
2018-12-20 18:45:01 +03:00
2019-04-03 16:27:40 +03:00
@Override
public void loadModule(URL jarpath) throws Exception {
try (JarFile f = new JarFile(Paths.get(jarpath.toURI()).toFile())) {
loadModule(jarpath, f.getManifest().getMainAttributes().getValue("Main-Class"));
}
2019-04-03 16:27:40 +03:00
}
2018-10-13 11:01:10 +03:00
@Override
public void loadModule(URL jarpath, String classname) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
classloader.addURL(jarpath);
Module module = (Module) Class.forName(classname, true, classloader).newInstance();
modules.add(module);
2018-09-22 17:33:00 +03:00
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
}
@Override
public void postInitModules() {
for (Module m : modules) {
m.postInit(context);
LogHelper.info("Module %s version: %s post-init", m.getName(), m.getVersion());
}
}
@Override
public void preInitModules() {
for (Module m : modules) {
m.preInit(context);
LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion());
}
}
@Override
public void printModules() {
for (Module m : modules)
LogHelper.info("Module %s version: %s", m.getName(), m.getVersion());
LogHelper.info("Loaded %d modules", modules.size());
}
@Override
public void registerModule(Module module) {
modules.add(module);
2018-09-22 17:33:00 +03:00
LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion());
}
2019-04-03 16:27:40 +03:00
@Override
public void finishModules() {
for (Module m : modules) {
m.finish(context);
LogHelper.info("Module %s version: %s finished initialization", m.getName(), m.getVersion());
}
2019-04-03 16:27:40 +03:00
}
}