From 198b7037d5f64e8372ac2b1c5e648141e769eb68 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 6 Jul 2019 19:54:10 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9F=D0=B5=D1=80=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B0=20Reconfigurable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/launchserver/LaunchServer.java | 3 +- .../gravit/launchserver/Reconfigurable.java | 8 ++- .../auth/provider/RejectAuthProvider.java | 36 ++++-------- .../gravit/launchserver/command/Command.java | 8 +++ .../command/handler/CommandHandler.java | 2 - .../command/service/ConfigCommand.java | 9 +-- .../command/service/ConfigHelpCommand.java | 28 ---------- .../command/service/ConfigListCommand.java | 25 --------- .../manangers/ReconfigurableManager.java | 56 ++++++++++++++----- .../pro/gravit/utils/command/Command.java | 10 +++- 10 files changed, 82 insertions(+), 103 deletions(-) delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigHelpCommand.java delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigListCommand.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 4e898384..fbbe1527 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -510,7 +510,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException localCommandHandler = new StdCommandHandler(true); LogHelper.warning("JLine2 isn't in classpath, using std"); } - pro.gravit.launchserver.command.handler.CommandHandler.registerCommands(localCommandHandler, this); commandHandler = localCommandHandler; // Set key pair @@ -607,6 +606,8 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException Arrays.stream(config.mirrors).forEach(mirrorManager::addMirror); + pro.gravit.launchserver.command.handler.CommandHandler.registerCommands(localCommandHandler, this); + // init modules modulesManager.initModules(); if (config.components != null) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java b/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java index 45ee0193..acb00ba4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java @@ -1,7 +1,9 @@ package pro.gravit.launchserver; -public interface Reconfigurable { - void reconfig(String action, String[] args); +import pro.gravit.utils.command.Command; - void printConfigHelp(); +import java.util.Map; + +public interface Reconfigurable { + Map getCommands(); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java index 4a707288..f6025060 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java @@ -1,9 +1,13 @@ package pro.gravit.launchserver.auth.provider; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.utils.command.Command; +import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.SecurityHelper; @@ -36,31 +40,15 @@ public void close() { } @Override - public void reconfig(String action, String[] args) { - switch (action) { - case "message": + public Map getCommands() { + Map commands = new HashMap<>(); + commands.put("message", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { message = args[0]; LogHelper.info("New reject message: %s", message); - break; - case "whitelist.add": - if (whitelist == null) whitelist = new ArrayList<>(); - whitelist.add(args[0]); - break; - case "whitelist.remove": - if (whitelist == null) whitelist = new ArrayList<>(); - whitelist.remove(args[0]); - break; - case "whitelist.clear": - whitelist.clear(); - break; - } - } - - @Override - public void printConfigHelp() { - LogHelper.info("message [new message] - set message"); - LogHelper.info("whitelist.add [username] - add username to whitelist"); - LogHelper.info("whitelist.remove [username] - remove username into whitelist"); - LogHelper.info("whitelist.clear - clear whitelist"); + } + }); + return commands; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java index 83c28838..32ed911e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/Command.java @@ -2,6 +2,8 @@ import pro.gravit.launchserver.LaunchServer; +import java.util.Map; + public abstract class Command extends pro.gravit.utils.command.Command { @@ -9,6 +11,12 @@ public abstract class Command extends pro.gravit.utils.command.Command { protected Command(LaunchServer server) { + super(); + this.server = server; + } + + public Command(Map childCommands, LaunchServer server) { + super(childCommands); this.server = server; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index 69c16655..7af943e6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java @@ -105,8 +105,6 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand service.registerCommand("reloadAll", new ReloadAllCommand(server)); service.registerCommand("reloadList", new ReloadListCommand(server)); service.registerCommand("config", new ConfigCommand(server)); - service.registerCommand("configHelp", new ConfigHelpCommand(server)); - service.registerCommand("configList", new ConfigListCommand(server)); service.registerCommand("serverStatus", new ServerStatusCommand(server)); service.registerCommand("checkInstall", new CheckInstallCommand(server)); service.registerCommand("multi", new MultiCommand(server)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigCommand.java index 3c546609..ce6a295e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigCommand.java @@ -2,11 +2,12 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.command.Command; +import pro.gravit.launchserver.manangers.ReconfigurableManager; import pro.gravit.utils.helper.LogHelper; public class ConfigCommand extends Command { public ConfigCommand(LaunchServer server) { - super(server); + super(server.reconfigurableManager.getCommands(), server); } @Override @@ -21,10 +22,6 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { - verifyArgs(args, 2); - LogHelper.info("Call %s module %s action", args[0], args[1]); - String[] new_args = new String[args.length - 2]; - System.arraycopy(args, 2, new_args, 0, args.length - 2); - server.reconfigurableManager.call(args[0], args[1], new_args); + invokeSubcommands(args); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigHelpCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigHelpCommand.java deleted file mode 100644 index b1a0b749..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigHelpCommand.java +++ /dev/null @@ -1,28 +0,0 @@ -package pro.gravit.launchserver.command.service; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; -import pro.gravit.utils.helper.LogHelper; - -public class ConfigHelpCommand extends Command { - public ConfigHelpCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return "[name]"; - } - - @Override - public String getUsageDescription() { - return "print help for config command"; - } - - @Override - public void invoke(String... args) throws Exception { - verifyArgs(args, 1); - LogHelper.info("Help %s module", args[0]); - server.reconfigurableManager.printHelp(args[0]); - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigListCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigListCommand.java deleted file mode 100644 index 9af0aee9..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ConfigListCommand.java +++ /dev/null @@ -1,25 +0,0 @@ -package pro.gravit.launchserver.command.service; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; - -public class ConfigListCommand extends Command { - public ConfigListCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return "[name]"; - } - - @Override - public String getUsageDescription() { - return "print help for config command"; - } - - @Override - public void invoke(String... args) { - server.reconfigurableManager.printReconfigurables(); - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ReconfigurableManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ReconfigurableManager.java index 59e4d054..227ec8d0 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ReconfigurableManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/ReconfigurableManager.java @@ -1,35 +1,65 @@ package pro.gravit.launchserver.manangers; import java.util.HashMap; +import java.util.Map; import java.util.Objects; import pro.gravit.launchserver.Reconfigurable; +import pro.gravit.utils.command.Command; +import pro.gravit.utils.command.CommandException; +import pro.gravit.utils.command.basic.HelpCommand; import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.VerifyHelper; public class ReconfigurableManager { - private final HashMap RECONFIGURABLE = new HashMap<>(); + private class ReconfigurableVirtualCommand extends Command { + public ReconfigurableVirtualCommand(Map childs) { + super(childs); + } + + @Override + public String getArgsDescription() { + return null; + } + + @Override + public String getUsageDescription() { + return null; + } + + @Override + public void invoke(String... args) throws Exception { + invokeSubcommands(args); + } + } + private final HashMap RECONFIGURABLE = new HashMap<>(); public void registerReconfigurable(String name, Reconfigurable reconfigurable) { - VerifyHelper.putIfAbsent(RECONFIGURABLE, name.toLowerCase(), Objects.requireNonNull(reconfigurable, "adapter"), + VerifyHelper.putIfAbsent(RECONFIGURABLE, name.toLowerCase(), new ReconfigurableVirtualCommand(reconfigurable.getCommands()), String.format("Reloadable has been already registered: '%s'", name)); } - public Reconfigurable unregisterReconfigurable(String name) { - return RECONFIGURABLE.remove(name); + public void unregisterReconfigurable(String name) { + RECONFIGURABLE.remove(name); } - public void printHelp(String name) { - RECONFIGURABLE.get(name.toLowerCase()).printConfigHelp(); + public void call(String name, String action, String[] args) throws Exception + { + Command commands = RECONFIGURABLE.get(name); + if(commands == null) throw new CommandException(String.format("Reconfigurable %s not found", name)); + Command command = commands.childCommands.get(action); + if(command == null) throw new CommandException(String.format("Action %s.%s not found", name, action)); + command.invoke(args); } - public void call(String name, String action, String[] args) { - RECONFIGURABLE.get(name.toLowerCase()).reconfig(action.toLowerCase(), args); + public void printHelp(String name) throws CommandException + { + Command commands = RECONFIGURABLE.get(name); + if(commands == null) throw new CommandException(String.format("Reconfigurable %s not found", name)); + HelpCommand.printSubCommandsHelp(name, commands); } - - public void printReconfigurables() { - LogHelper.info("Print reconfigurables"); - RECONFIGURABLE.forEach((k, v) -> LogHelper.subInfo(k)); - LogHelper.info("Found %d reconfigurables", RECONFIGURABLE.size()); + public Map getCommands() + { + return RECONFIGURABLE; } } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java b/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java index 10d03a4f..aa1470ac 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/Command.java @@ -6,7 +6,15 @@ import pro.gravit.utils.helper.VerifyHelper; public abstract class Command { - public Map childCommands = new HashMap<>(); + public Map childCommands; + + public Command() { + childCommands = new HashMap<>(); + } + + public Command(Map childCommands) { + this.childCommands = childCommands; + } protected static String parseUsername(String username) throws CommandException { try {