[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); localCommandHandler = new StdCommandHandler(true);
LogHelper.warning("JLine2 isn't in classpath, using std"); LogHelper.warning("JLine2 isn't in classpath, using std");
} }
pro.gravit.launchserver.command.handler.CommandHandler.registerCommands(localCommandHandler, this);
commandHandler = localCommandHandler; commandHandler = localCommandHandler;
// Set key pair // Set key pair
@ -607,6 +606,8 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException
Arrays.stream(config.mirrors).forEach(mirrorManager::addMirror); Arrays.stream(config.mirrors).forEach(mirrorManager::addMirror);
pro.gravit.launchserver.command.handler.CommandHandler.registerCommands(localCommandHandler, this);
// init modules // init modules
modulesManager.initModules(); modulesManager.initModules();
if (config.components != null) { if (config.components != null) {

View file

@ -1,7 +1,9 @@
package pro.gravit.launchserver; package pro.gravit.launchserver;
public interface Reconfigurable { import pro.gravit.utils.command.Command;
void reconfig(String action, String[] args);
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; package pro.gravit.launchserver.auth.provider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.Reconfigurable;
import pro.gravit.launchserver.auth.AuthException; 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.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
@ -36,31 +40,15 @@ public void close() {
} }
@Override @Override
public void reconfig(String action, String[] args) { public Map<String, Command> getCommands() {
switch (action) { Map<String, Command> commands = new HashMap<>();
case "message": commands.put("message", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
message = args[0]; message = args[0];
LogHelper.info("New reject message: %s", message); LogHelper.info("New reject message: %s", message);
break; }
case "whitelist.add": });
if (whitelist == null) whitelist = new ArrayList<>(); return commands;
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");
} }
} }

View file

@ -2,6 +2,8 @@
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import java.util.Map;
public abstract class Command extends pro.gravit.utils.command.Command { 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) { 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; 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("reloadAll", new ReloadAllCommand(server));
service.registerCommand("reloadList", new ReloadListCommand(server)); service.registerCommand("reloadList", new ReloadListCommand(server));
service.registerCommand("config", new ConfigCommand(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("serverStatus", new ServerStatusCommand(server));
service.registerCommand("checkInstall", new CheckInstallCommand(server)); service.registerCommand("checkInstall", new CheckInstallCommand(server));
service.registerCommand("multi", new MultiCommand(server)); service.registerCommand("multi", new MultiCommand(server));

View file

@ -2,11 +2,12 @@
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command; import pro.gravit.launchserver.command.Command;
import pro.gravit.launchserver.manangers.ReconfigurableManager;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
public class ConfigCommand extends Command { public class ConfigCommand extends Command {
public ConfigCommand(LaunchServer server) { public ConfigCommand(LaunchServer server) {
super(server); super(server.reconfigurableManager.getCommands(), server);
} }
@Override @Override
@ -21,10 +22,6 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
verifyArgs(args, 2); invokeSubcommands(args);
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);
} }
} }

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; package pro.gravit.launchserver.manangers;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import pro.gravit.launchserver.Reconfigurable; 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.LogHelper;
import pro.gravit.utils.helper.VerifyHelper; import pro.gravit.utils.helper.VerifyHelper;
public class ReconfigurableManager { 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) { 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)); String.format("Reloadable has been already registered: '%s'", name));
} }
public Reconfigurable unregisterReconfigurable(String name) { public void unregisterReconfigurable(String name) {
return RECONFIGURABLE.remove(name); RECONFIGURABLE.remove(name);
} }
public void printHelp(String name) { public void call(String name, String action, String[] args) throws Exception
RECONFIGURABLE.get(name.toLowerCase()).printConfigHelp(); {
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) { public void printHelp(String name) throws CommandException
RECONFIGURABLE.get(name.toLowerCase()).reconfig(action.toLowerCase(), args); {
Command commands = RECONFIGURABLE.get(name);
if(commands == null) throw new CommandException(String.format("Reconfigurable %s not found", name));
HelpCommand.printSubCommandsHelp(name, commands);
} }
public Map<String, Command> getCommands()
public void printReconfigurables() { {
LogHelper.info("Print reconfigurables"); return RECONFIGURABLE;
RECONFIGURABLE.forEach((k, v) -> LogHelper.subInfo(k));
LogHelper.info("Found %d reconfigurables", RECONFIGURABLE.size());
} }
} }

View file

@ -6,7 +6,15 @@
import pro.gravit.utils.helper.VerifyHelper; import pro.gravit.utils.helper.VerifyHelper;
public abstract class Command { 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 { protected static String parseUsername(String username) throws CommandException {
try { try {