[FEATURE] Сортировка модулей по приоритету, удаление устаревших классов

This commit is contained in:
Gravit 2019-08-28 15:22:57 +07:00
parent 40b8e36961
commit 67e293d6aa
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
7 changed files with 13 additions and 200 deletions

View file

@ -2,7 +2,6 @@
import pro.gravit.launchserver.config.LaunchServerConfig; import pro.gravit.launchserver.config.LaunchServerConfig;
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
import pro.gravit.launchserver.manangers.ModulesManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager; import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.utils.command.CommandHandler; import pro.gravit.utils.command.CommandHandler;

View file

@ -14,7 +14,6 @@
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
import pro.gravit.launchserver.dao.provider.DaoProvider; import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.manangers.LaunchServerGsonManager; import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
import pro.gravit.launchserver.manangers.ModulesManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager; import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.WebSocketService; import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.utils.command.CommandHandler; import pro.gravit.utils.command.CommandHandler;

View file

@ -1,32 +0,0 @@
package pro.gravit.launchserver.manangers;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import pro.gravit.launcher.managers.SimpleModuleManager;
import pro.gravit.launcher.managers.SimpleModulesConfigManager;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.modules.CoreModule;
import pro.gravit.launchserver.modules.LaunchServerModuleContext;
import pro.gravit.utils.PublicURLClassLoader;
public class ModulesManager extends SimpleModuleManager {
public SimpleModulesConfigManager configManager;
public ModulesManager(Path configDir) {
modules = new ArrayList<>(1);
configManager = new SimpleModulesConfigManager(configDir);
classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
registerCoreModule();
}
public void initContext(LaunchServer server)
{
context = new LaunchServerModuleContext(server, classloader, configManager);
}
private void registerCoreModule() {
load(new CoreModule());
}
}

View file

@ -1,158 +0,0 @@
package pro.gravit.launcher.managers;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.jar.JarFile;
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;
@Deprecated
public class SimpleModuleManager implements ModulesManager {
protected final class ModulesVisitor extends SimpleFileVisitor<Path> {
private ModulesVisitor() {
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().endsWith(".jar"))
try (JarFile f = new JarFile(file.toFile())) {
String mainclass = f.getManifest().getMainAttributes().getValue("Main-Class");
if(mainclass == null)
{
LogHelper.error("In module %s Main-Class not found", file.toString());
return super.visitFile(file, attrs);
}
loadModule(file.toUri().toURL(), mainclass);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
LogHelper.error(e);
}
return super.visitFile(file, attrs);
}
}
public ArrayList<Module> modules;
public PublicURLClassLoader classloader;
protected ModuleContext context;
public void autoload(Path dir) throws IOException {
LogHelper.info("Load modules");
if (Files.notExists(dir)) Files.createDirectory(dir);
else {
IOHelper.walk(dir, new ModulesVisitor(), true);
}
sort();
LogHelper.info("Loaded %d modules", modules.size());
}
public void sort() {
modules.sort((m1, m2) -> {
int p1 = m1.getPriority();
int p2 = m2.getPriority();
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);
}
public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException {
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());
}
}
@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"));
}
}
@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);
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);
LogHelper.info("Module %s version: %s registered", module.getName(), module.getVersion());
}
@Override
public void finishModules() {
for (Module m : modules) {
m.finish(context);
LogHelper.info("Module %s version: %s finished initialization", m.getName(), m.getVersion());
}
}
}

View file

@ -16,14 +16,12 @@ public SimpleModulesConfigManager(Path configDir) {
} }
public Path getModuleConfig(String moduleName) { public Path getModuleConfig(String moduleName) {
if (!IOHelper.isDir(configDir)) { return getModuleConfig(moduleName, "Config");
try { }
Files.createDirectories(configDir);
} catch (IOException e) { @Override
LogHelper.error(e); public Path getModuleConfig(String moduleName, String configName) {
} return getModuleConfigDir(moduleName).resolve(moduleName.concat(configName.concat(".json")));
}
return configDir.resolve(moduleName.concat("Config.json"));
} }
public Path getModuleConfigDir(String moduleName) { public Path getModuleConfigDir(String moduleName) {

View file

@ -5,5 +5,7 @@
public interface ModulesConfigManager { public interface ModulesConfigManager {
Path getModuleConfig(String moduleName); Path getModuleConfig(String moduleName);
Path getModuleConfig(String moduleName, String configName);
Path getModuleConfigDir(String moduleName); Path getModuleConfigDir(String moduleName);
} }

View file

@ -60,6 +60,11 @@ public void initModules(LauncherInitContext initContext) {
module.preInit(); module.preInit();
} }
boolean isAnyModuleLoad = true; boolean isAnyModuleLoad = true;
modules.sort((m1, m2) -> {
int priority1 = m1.getModuleInfo().priority;
int priority2 = m2.getModuleInfo().priority;
return Integer.compare(priority1, priority2);
});
int modules_size = modules.size(); int modules_size = modules.size();
int loaded = 0; int loaded = 0;
while(isAnyModuleLoad) while(isAnyModuleLoad)