sort в модулях, убраны дубликаты кода

This commit is contained in:
Gravit 2018-09-17 17:14:59 +07:00
parent 10e40035ee
commit 416289f7a7
10 changed files with 181 additions and 336 deletions

View file

@ -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<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);
}
}
public ArrayList<Module> 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());
}
}

View file

@ -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

View file

@ -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

View file

@ -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<Module> 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);
}
}
}

View file

@ -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<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);
}
}
public ArrayList<Module> 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());
}
}

View file

@ -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,

View file

@ -8,6 +8,7 @@ public interface Module extends AutoCloseable {
LauncherVersion getVersion();
int getPriority();
void init(ModuleContext context);
void postInit(ModuleContext context);

View file

@ -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;
}

View file

@ -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<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);
}
}
public ArrayList<Module> 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());
}
}

View file

@ -19,6 +19,11 @@ public LauncherVersion getVersion() {
return LauncherVersion.getVersion();
}
@Override
public int getPriority() {
return 0;
}
@Override
public void init(ModuleContext context) {