diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpEntryCacheCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpEntryCacheCommand.java index 4fe70d76..fba4e1ff 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpEntryCacheCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpEntryCacheCommand.java @@ -11,12 +11,59 @@ import pro.gravit.launchserver.auth.AuthProviderPair; import pro.gravit.launchserver.auth.handler.CachedAuthHandler; import pro.gravit.launchserver.command.Command; +import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; public class DumpEntryCacheCommand extends Command { public DumpEntryCacheCommand(LaunchServer server) { super(server); + childCommands.put("load", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 2); + AuthProviderPair pair = server.config.getAuthProviderPair(args[0]); + if (pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[0])); + if (!(pair.handler instanceof CachedAuthHandler)) + throw new UnsupportedOperationException("This command used only CachedAuthHandler"); + CachedAuthHandler authHandler = (CachedAuthHandler) pair.handler; + + LogHelper.info("CachedAuthHandler read from %s", args[0]); + int size_entry; + int size_username; + try (Reader reader = IOHelper.newReader(Paths.get(args[1]))) { + EntryAndUsername entryAndUsername = Launcher.gsonManager.configGson.fromJson(reader, EntryAndUsername.class); + size_entry = entryAndUsername.entryCache.size(); + size_username = entryAndUsername.usernameCache.size(); + authHandler.loadEntryCache(entryAndUsername.entryCache); + authHandler.loadUsernameCache(entryAndUsername.usernameCache); + + } + LogHelper.subInfo("Readed %d entryCache %d usernameCache", size_entry, size_username); + } + }); + childCommands.put("unload", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 2); + AuthProviderPair pair = server.config.getAuthProviderPair(args[0]); + if (pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[0])); + if (!(pair.handler instanceof CachedAuthHandler)) + throw new UnsupportedOperationException("This command used only CachedAuthHandler"); + CachedAuthHandler authHandler = (CachedAuthHandler) pair.handler; + + LogHelper.info("CachedAuthHandler write to %s", args[1]); + Map entryCache = authHandler.getEntryCache(); + Map usernamesCache = authHandler.getUsernamesCache(); + EntryAndUsername serializable = new EntryAndUsername(); + serializable.entryCache = entryCache; + serializable.usernameCache = usernamesCache; + try (Writer writer = IOHelper.newWriter(Paths.get(args[1]))) { + Launcher.gsonManager.configGson.toJson(serializable, writer); + } + LogHelper.subInfo("Write %d entryCache, %d usernameCache", entryCache.size(), usernamesCache.size()); + } + }); } @Override @@ -31,37 +78,7 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { - verifyArgs(args, 3); - AuthProviderPair pair = server.config.getAuthProviderPair(args[1]); - if (pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[1])); - if (!(pair.handler instanceof CachedAuthHandler)) - throw new UnsupportedOperationException("This command used only CachedAuthHandler"); - CachedAuthHandler authHandler = (CachedAuthHandler) pair.handler; - if (args[0].equals("unload")) { - LogHelper.info("CachedAuthHandler write to %s", args[2]); - Map entryCache = authHandler.getEntryCache(); - Map usernamesCache = authHandler.getUsernamesCache(); - EntryAndUsername serializable = new EntryAndUsername(); - serializable.entryCache = entryCache; - serializable.usernameCache = usernamesCache; - try (Writer writer = IOHelper.newWriter(Paths.get(args[1]))) { - Launcher.gsonManager.configGson.toJson(serializable, writer); - } - LogHelper.subInfo("Write %d entryCache, %d usernameCache", entryCache.size(), usernamesCache.size()); - } else if (args[0].equals("load")) { - LogHelper.info("CachedAuthHandler read from %s", args[1]); - int size_entry; - int size_username; - try (Reader reader = IOHelper.newReader(Paths.get(args[1]))) { - EntryAndUsername entryAndUsername = Launcher.gsonManager.configGson.fromJson(reader, EntryAndUsername.class); - size_entry = entryAndUsername.entryCache.size(); - size_username = entryAndUsername.usernameCache.size(); - authHandler.loadEntryCache(entryAndUsername.entryCache); - authHandler.loadUsernameCache(entryAndUsername.usernameCache); - - } - LogHelper.subInfo("Readed %d entryCache %d usernameCache", size_entry, size_username); - } + invokeSubcommands(args); } public class EntryAndUsername { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpSessionsCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpSessionsCommand.java index 234b72d2..350e7515 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpSessionsCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dump/DumpSessionsCommand.java @@ -13,12 +13,41 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.command.Command; import pro.gravit.launchserver.socket.Client; +import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; public class DumpSessionsCommand extends Command { public DumpSessionsCommand(LaunchServer server) { super(server); + childCommands.put("load", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + LogHelper.info("Sessions read from %s", args[0]); + int size; + try (Reader reader = IOHelper.newReader(Paths.get(args[0]))) { + Type setType = new TypeToken>() { + }.getType(); + Set clientSet = Launcher.gsonManager.configGson.fromJson(reader, setType); + size = clientSet.size(); + server.sessionManager.loadSessions(clientSet); + } + LogHelper.subInfo("Readed %d sessions", size); + } + }); + childCommands.put("unload", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + LogHelper.info("Sessions write to %s", args[0]); + Set clientSet = server.sessionManager.getSessions(); + try (Writer writer = IOHelper.newWriter(Paths.get(args[0]))) { + Launcher.gsonManager.configGson.toJson(clientSet, writer); + } + LogHelper.subInfo("Write %d sessions", clientSet.size()); + } + }); } @Override @@ -33,25 +62,6 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { - verifyArgs(args, 2); - if (args[0].equals("unload")) { - LogHelper.info("Sessions write to %s", args[1]); - Set clientSet = server.sessionManager.getSessions(); - try (Writer writer = IOHelper.newWriter(Paths.get(args[1]))) { - Launcher.gsonManager.configGson.toJson(clientSet, writer); - } - LogHelper.subInfo("Write %d sessions", clientSet.size()); - } else if (args[0].equals("load")) { - LogHelper.info("Sessions read from %s", args[1]); - int size; - try (Reader reader = IOHelper.newReader(Paths.get(args[1]))) { - Type setType = new TypeToken>() { - }.getType(); - Set clientSet = Launcher.gsonManager.configGson.fromJson(reader, setType); - size = clientSet.size(); - server.sessionManager.loadSessions(clientSet); - } - LogHelper.subInfo("Readed %d sessions", size); - } + invokeSubcommands(args); } }