2019-01-12 03:13:24 +03:00
|
|
|
package ru.gravit.launcher.managers;
|
2018-09-17 13:14:59 +03:00
|
|
|
|
2019-01-12 03:13:24 +03:00
|
|
|
import ru.gravit.launcher.modules.Module;
|
|
|
|
import ru.gravit.launcher.modules.ModuleContext;
|
|
|
|
import ru.gravit.launcher.modules.ModulesManagerInterface;
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.utils.PublicURLClassLoader;
|
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
|
|
|
|
2018-09-17 13:14:59 +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.*;
|
2018-09-17 13:14:59 +03:00
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
import java.util.jar.Manifest;
|
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
public class SimpleModuleManager implements ModulesManagerInterface, AutoCloseable {
|
2018-09-17 13:14:59 +03:00
|
|
|
protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
|
|
|
|
private ModulesVisitor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
|
try {
|
|
|
|
JarFile f = new JarFile(file.toString());
|
|
|
|
Manifest m = f.getManifest();
|
|
|
|
String mainclass = m.getMainAttributes().getValue("Main-Class");
|
|
|
|
loadModule(file.toUri().toURL(), mainclass, true);
|
|
|
|
f.close();
|
|
|
|
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return result
|
|
|
|
return super.visitFile(file, attrs);
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public ArrayList<Module> modules;
|
2018-09-22 17:22:39 +03:00
|
|
|
public PublicURLClassLoader classloader;
|
2018-09-17 13:14:59 +03:00
|
|
|
protected ModuleContext context;
|
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +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();
|
2018-09-17 13:14:59 +03:00
|
|
|
LogHelper.info("Loaded %d modules", modules.size());
|
|
|
|
}
|
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
public void sort() {
|
|
|
|
modules.sort((m1, m2) -> {
|
2018-09-17 13:14:59 +03:00
|
|
|
int p1 = m1.getPriority();
|
|
|
|
int p2 = m2.getPriority();
|
2018-09-19 16:53:27 +03:00
|
|
|
return Integer.compare(p2, p1);
|
2018-09-17 13:14:59 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public void initModules() {
|
|
|
|
for (Module m : modules) {
|
|
|
|
m.init(context);
|
|
|
|
LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public void load(Module module) {
|
|
|
|
modules.add(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public void load(Module module, boolean preload) {
|
|
|
|
load(module);
|
|
|
|
if (!preload) module.init(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public void loadModule(URL jarpath, boolean preload) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
|
|
|
|
JarFile f = new JarFile(Paths.get(jarpath.toURI()).toString());
|
|
|
|
Manifest m = f.getManifest();
|
|
|
|
String mainclass = m.getMainAttributes().getValue("Main-Class");
|
|
|
|
loadModule(jarpath, mainclass, preload);
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:22:04 +03:00
|
|
|
public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
|
|
|
|
JarFile f = new JarFile(Paths.get(jarpath.toURI()).toString());
|
|
|
|
Manifest m = f.getManifest();
|
|
|
|
String mainclass = m.getMainAttributes().getValue("Main-Class");
|
|
|
|
classloader.addURL(jarpath);
|
|
|
|
Class<?> moduleclass = Class.forName(mainclass, true, classloader);
|
|
|
|
Module module = (Module) moduleclass.newInstance();
|
|
|
|
modules.add(module);
|
|
|
|
module.preInit(context);
|
|
|
|
module.init(context);
|
|
|
|
module.postInit(context);
|
|
|
|
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
|
|
|
|
f.close();
|
|
|
|
}
|
2018-12-20 18:45:01 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
@Override
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public void loadModule(URL jarpath, String classname, boolean preload) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
|
|
|
classloader.addURL(jarpath);
|
|
|
|
Class<?> moduleclass = Class.forName(classname, true, classloader);
|
|
|
|
Module module = (Module) moduleclass.newInstance();
|
|
|
|
modules.add(module);
|
|
|
|
module.preInit(context);
|
2018-09-22 17:33:00 +03:00
|
|
|
if (!preload) module.init(context);
|
|
|
|
LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion());
|
2018-09-17 13:14:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void postInitModules() {
|
|
|
|
for (Module m : modules) {
|
|
|
|
m.postInit(context);
|
|
|
|
LogHelper.info("Module %s version: %s post-init", m.getName(), m.getVersion());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
public void preInitModules() {
|
|
|
|
for (Module m : modules) {
|
|
|
|
m.preInit(context);
|
|
|
|
LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 13:14:59 +03:00
|
|
|
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
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
public void registerModule(Module module, boolean preload) {
|
2018-09-17 13:14:59 +03:00
|
|
|
load(module, preload);
|
2018-09-22 17:33:00 +03:00
|
|
|
LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion());
|
2018-09-17 13:14:59 +03:00
|
|
|
}
|
|
|
|
}
|