[FEATURE] Переработка Reconfigurable

This commit is contained in:
Gravit 2019-07-06 19:54:10 +07:00
parent aac9aef821
commit 198b7037d5
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
10 changed files with 82 additions and 103 deletions

View file

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

View file

@ -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<String, Command> getCommands();
}

View file

@ -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<String, Command> getCommands() {
Map<String, Command> 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;
}
}

View file

@ -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<String, pro.gravit.utils.command.Command> childCommands, LaunchServer server) {
super(childCommands);
this.server = server;
}
}

View file

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

View file

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

View file

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

View file

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

View file

@ -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<String, Reconfigurable> RECONFIGURABLE = new HashMap<>();
private class ReconfigurableVirtualCommand extends Command {
public ReconfigurableVirtualCommand(Map<String, Command> 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<String, Command> 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<String, Command> getCommands()
{
return RECONFIGURABLE;
}
}

View file

@ -6,7 +6,15 @@
import pro.gravit.utils.helper.VerifyHelper;
public abstract class Command {
public Map<String, Command> childCommands = new HashMap<>();
public Map<String, Command> childCommands;
public Command() {
childCommands = new HashMap<>();
}
public Command(Map<String, Command> childCommands) {
this.childCommands = childCommands;
}
protected static String parseUsername(String username) throws CommandException {
try {