From 416289f7a73003d1f29f6d56562f4794e14f1e08 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 17 Sep 2018 17:14:59 +0700 Subject: [PATCH] =?UTF-8?q?sort=20=D0=B2=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D1=8F=D1=85,=20=D1=83=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20=D0=B4?= =?UTF-8?q?=D1=83=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=82=D1=8B=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manangers/ModulesManager.java | 135 +--------------- .../launchserver/modules/CoreModule.java | 5 + .../launchserver/modules/SimpleModule.java | 7 +- .../launcher/client/ClientModuleManager.java | 77 +-------- .../launcher/server/ModulesManager.java | 127 +-------------- .../ru/gravit/launcher/LauncherVersion.java | 8 +- .../ru/gravit/launcher/modules/Module.java | 1 + .../modules/ModulesManagerInterface.java | 1 + .../launcher/modules/SimpleModuleManager.java | 151 ++++++++++++++++++ .../launcher/modules/TestClientModule.java | 5 + 10 files changed, 181 insertions(+), 336 deletions(-) create mode 100644 libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java index 8277581a..086ecdf9 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ModulesManager.java @@ -15,6 +15,7 @@ import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherClassLoader; +import ru.gravit.launcher.modules.SimpleModuleManager; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.launcher.modules.Module; @@ -23,145 +24,13 @@ import ru.gravit.launchserver.modules.CoreModule; import ru.gravit.launchserver.modules.LaunchServerModuleContext; -public class ModulesManager implements AutoCloseable, ModulesManagerInterface { - private final class ModulesVisitor extends SimpleFileVisitor { - 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); - } - } - - public ArrayList modules; - public LauncherClassLoader classloader; - - private final LaunchServerModuleContext context; - +public class ModulesManager extends SimpleModuleManager { public ModulesManager(LaunchServer lsrv) { modules = new ArrayList<>(1); classloader = new LauncherClassLoader(new URL[0], ClassLoader.getSystemClassLoader()); context = new LaunchServerModuleContext(lsrv, classloader); } - - @LauncherAPI - public void autoload() throws IOException { - LogHelper.info("Load modules"); - registerCoreModule(); - Path modules = context.launchServer.dir.resolve("modules"); - if (Files.notExists(modules)) - Files.createDirectory(modules); - IOHelper.walk(modules, new ModulesVisitor(), true); - LogHelper.info("Loaded %d modules", this.modules.size()); - } - - @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 - @LauncherAPI - public void initModules() { - for (Module m : modules) { - m.init(context); - LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion()); - } - } - - @Override - @LauncherAPI - public void load(Module module) { - modules.add(module); - } - - @Override - @LauncherAPI - public void load(Module module, boolean preload) { - load(module); - if (!preload) - module.init(context); - } - - @Override - @LauncherAPI - 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(); - } - - @Override - @LauncherAPI - 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); - if (!preload) - module.init(context); - 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 - @LauncherAPI - public void preInitModules() { - for (Module m : modules) { - m.preInit(context); - LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion()); - } - } - - @Override - @LauncherAPI - public void printModules() { - for (Module m : modules) - LogHelper.info("Module %s version: %s", m.getName(), m.getVersion()); - LogHelper.info("Loaded %d modules", modules.size()); - } - private void registerCoreModule() { load(new CoreModule()); } - - @Override - @LauncherAPI - public void registerModule(Module module, boolean preload) { - load(module, preload); - LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion()); - } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/modules/CoreModule.java b/LaunchServer/src/main/java/ru/gravit/launchserver/modules/CoreModule.java index b3eff95b..bed40420 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/modules/CoreModule.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/modules/CoreModule.java @@ -20,6 +20,11 @@ public LauncherVersion getVersion() { return LauncherVersion.getVersion(); } + @Override + public int getPriority() { + return 0; + } + @Override public void init(ModuleContext context) { // nothing to do diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/modules/SimpleModule.java b/LaunchServer/src/main/java/ru/gravit/launchserver/modules/SimpleModule.java index 3af08b2c..366a4a37 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/modules/SimpleModule.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/modules/SimpleModule.java @@ -17,7 +17,12 @@ public String getName() { @Override public LauncherVersion getVersion() { - return new LauncherVersion(1,0,0); + return new LauncherVersion(1,0,0,0, LauncherVersion.Type.UNKNOWN); + } + + @Override + public int getPriority() { + return 0; } @Override diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ClientModuleManager.java b/Launcher/src/main/java/ru/gravit/launcher/client/ClientModuleManager.java index 97f788c7..bddce498 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ClientModuleManager.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ClientModuleManager.java @@ -4,94 +4,25 @@ import java.util.ArrayList; import ru.gravit.launcher.LauncherEngine; +import ru.gravit.launcher.modules.SimpleModuleManager; import ru.gravit.utils.helper.LogHelper; import ru.gravit.launcher.modules.Module; import ru.gravit.launcher.modules.ModulesManagerInterface; -public class ClientModuleManager implements ModulesManagerInterface,AutoCloseable { - public ArrayList modules; - private final ClientModuleContext context; +public class ClientModuleManager extends SimpleModuleManager { public ClientModuleManager(LauncherEngine engine) { context = new ClientModuleContext(engine); modules = new ArrayList<>(); } - @Override - public void close() throws Exception { - 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() throws Exception { - for (Module m : modules) { - m.init(context); - LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion()); - } - } @Override - public void load(Module module) throws Exception { - modules.add(module); - } - - @Override - public void load(Module module, boolean preload) throws Exception { - modules.add(module); - if(!preload) { - module.preInit(context); - module.init(context); - module.postInit(context); - } - } - - @Override - public void loadModule(URL jarpath, boolean preload) throws Exception { + public void loadModule(URL jarpath, boolean preload) { throw new SecurityException("Custom JAR's load not allowed here"); } @Override - public void loadModule(URL jarpath, String classname, boolean preload) throws Exception { + public void loadModule(URL jarpath, String classname, boolean preload) { throw new SecurityException("Custom JAR's load not allowed here"); } - - @Override - public void postInitModules() throws Exception { - for (Module m : modules) { - m.postInit(context); - LogHelper.info("Module %s version: %s post-init", m.getName(), m.getVersion()); - } - } - - @Override - public void preInitModules() throws Exception { - for (Module m : modules) { - m.preInit(context); - LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion()); - } - } - - @Override - public void printModules() throws Exception { - 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, boolean preload) throws Exception { - modules.add(module); - if(!preload) { - module.preInit(context); - module.init(context); - module.postInit(context); - } - } } diff --git a/Launcher/src/main/java/ru/gravit/launcher/server/ModulesManager.java b/Launcher/src/main/java/ru/gravit/launcher/server/ModulesManager.java index fbffaac4..8ef4d80b 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/server/ModulesManager.java +++ b/Launcher/src/main/java/ru/gravit/launcher/server/ModulesManager.java @@ -15,139 +15,16 @@ import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherClassLoader; +import ru.gravit.launcher.modules.SimpleModuleManager; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.launcher.modules.Module; import ru.gravit.launcher.modules.ModulesManagerInterface; -public class ModulesManager implements AutoCloseable, ModulesManagerInterface { - private final class ModulesVisitor extends SimpleFileVisitor { - 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); - } - } - public ArrayList modules; - public LauncherClassLoader classloader; - - private final ServerModuleContext context; - +public class ModulesManager extends SimpleModuleManager { public ModulesManager(ServerWrapper wrapper) { modules = new ArrayList<>(); classloader = new LauncherClassLoader(new URL[0], ClassLoader.getSystemClassLoader()); context = new ServerModuleContext(wrapper, classloader); } - - @LauncherAPI - public void autoload(Path dir) throws IOException { - LogHelper.info("Load modules"); - if (Files.notExists(dir)) Files.createDirectory(dir); - IOHelper.walk(dir, new ModulesVisitor(), true); - LogHelper.info("Loaded %d modules", modules.size()); - } - - @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 - @LauncherAPI - public void initModules() { - for (Module m : modules) { - m.init(context); - LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion()); - } - } - - @Override - @LauncherAPI - public void load(Module module) { - modules.add(module); - } - - @Override - @LauncherAPI - public void load(Module module, boolean preload) { - load(module); - if (!preload) module.init(context); - } - - - @Override - @LauncherAPI - 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(); - } - - @Override - @LauncherAPI - 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); - if(!preload) module.init(context); - 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 - @LauncherAPI - public void preInitModules() { - for (Module m : modules) { - m.preInit(context); - LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion()); - } - } - - @Override - @LauncherAPI - 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 - @LauncherAPI - public void registerModule(Module module,boolean preload) - { - load(module, preload); - LogHelper.info("Module %s version: %s registered",module.getName(),module.getVersion()); - } } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/LauncherVersion.java b/libLauncher/src/main/java/ru/gravit/launcher/LauncherVersion.java index f8d95c2c..153e4f84 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/LauncherVersion.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/LauncherVersion.java @@ -6,11 +6,11 @@ import ru.gravit.utils.helper.IOHelper; public class LauncherVersion { - public static int MAJOR = 3; - public static int MINOR = 18; + public static int MAJOR = 4; + public static int MINOR = 0; public static int PATCH = 0; public static int BUILD = readBuildNumber(); - public static Type RELEASE = Type.EXPERIMENTAL; + public static Type RELEASE = Type.DEV; public static LauncherVersion getVersion() { return new LauncherVersion(MAJOR,MINOR,PATCH,BUILD,RELEASE); @@ -107,7 +107,7 @@ public String getReleaseStatus() public String toString() { return String.format("%d.%d.%d-%d %s", major, minor, patch, build,getReleaseStatus()); } - enum Type + public enum Type { LTS, STABLE, diff --git a/libLauncher/src/main/java/ru/gravit/launcher/modules/Module.java b/libLauncher/src/main/java/ru/gravit/launcher/modules/Module.java index 9ece4283..51c5f960 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/modules/Module.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/modules/Module.java @@ -8,6 +8,7 @@ public interface Module extends AutoCloseable { LauncherVersion getVersion(); + int getPriority(); void init(ModuleContext context); void postInit(ModuleContext context); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/modules/ModulesManagerInterface.java b/libLauncher/src/main/java/ru/gravit/launcher/modules/ModulesManagerInterface.java index ebb4c78a..e7d8a0bc 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/modules/ModulesManagerInterface.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/modules/ModulesManagerInterface.java @@ -11,5 +11,6 @@ public interface ModulesManagerInterface { void postInitModules() throws Exception; void preInitModules() throws Exception; void printModules() throws Exception; + void sort(); void registerModule(Module module,boolean preload) throws Exception; } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java b/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java new file mode 100644 index 00000000..c53a96fc --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java @@ -0,0 +1,151 @@ +package ru.gravit.launcher.modules; + +import ru.gravit.launcher.LauncherAPI; +import ru.gravit.launcher.LauncherClassLoader; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +public class SimpleModuleManager implements ModulesManagerInterface,AutoCloseable { + protected final class ModulesVisitor extends SimpleFileVisitor { + 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); + } + } + public ArrayList modules; + public LauncherClassLoader classloader; + protected ModuleContext context; + + @LauncherAPI + public void autoload(Path dir) throws IOException { + LogHelper.info("Load modules"); + if (Files.notExists(dir)) Files.createDirectory(dir); + IOHelper.walk(dir, new ModulesVisitor(), true); + LogHelper.info("Loaded %d modules", modules.size()); + } + + public void sort() + { + modules.sort((m1,m2) -> { + int p1 = m1.getPriority(); + int p2 = m2.getPriority(); + if(p1 < p2) return 1; + else if(p1 > p2) return -1; + else return 0; + }); + } + + @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 + @LauncherAPI + public void initModules() { + for (Module m : modules) { + m.init(context); + LogHelper.info("Module %s version: %s init", m.getName(), m.getVersion()); + } + } + + @Override + @LauncherAPI + public void load(Module module) { + modules.add(module); + } + + @Override + @LauncherAPI + public void load(Module module, boolean preload) { + load(module); + if (!preload) module.init(context); + } + + + @Override + @LauncherAPI + 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(); + } + + @Override + @LauncherAPI + 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); + if(!preload) module.init(context); + 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 + @LauncherAPI + public void preInitModules() { + for (Module m : modules) { + m.preInit(context); + LogHelper.info("Module %s version: %s pre-init", m.getName(), m.getVersion()); + } + } + + @Override + @LauncherAPI + 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 + @LauncherAPI + public void registerModule(Module module,boolean preload) + { + load(module, preload); + LogHelper.info("Module %s version: %s registered",module.getName(),module.getVersion()); + } +} diff --git a/libLauncher/src/main/java/ru/gravit/launcher/modules/TestClientModule.java b/libLauncher/src/main/java/ru/gravit/launcher/modules/TestClientModule.java index 1025966a..7968f68b 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/modules/TestClientModule.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/modules/TestClientModule.java @@ -19,6 +19,11 @@ public LauncherVersion getVersion() { return LauncherVersion.getVersion(); } + @Override + public int getPriority() { + return 0; + } + @Override public void init(ModuleContext context) {