[FEATURE] ComponentCommand

This commit is contained in:
Gravit 2019-03-23 11:02:45 +07:00
parent 75f69a3234
commit 4dd5b3a6b1
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 106 additions and 0 deletions

View file

@ -76,5 +76,6 @@ public static void registerCommands(ru.gravit.utils.command.CommandHandler handl
handler.registerCommand("checkInstall", new CheckInstallCommand(server));
handler.registerCommand("multi", new MultiCommand(server));
handler.registerCommand("getModulus", new GetModulusCommand(server));
handler.registerCommand("component", new ComponentCommand(server));
}
}

View file

@ -0,0 +1,105 @@
package ru.gravit.launchserver.command.service;
import ru.gravit.launcher.NeedGarbageCollection;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
import ru.gravit.launchserver.components.Component;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import java.io.Reader;
import java.nio.file.Paths;
public class ComponentCommand extends Command {
public ComponentCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[action] [component name] [more args]";
}
@Override
public String getUsageDescription() {
return "component manager";
}
public void printHelp()
{
LogHelper.info("Print help for component:");
LogHelper.subInfo("component unload [componentName]");
LogHelper.subInfo("component load [componentName] [filename]");
LogHelper.subInfo("component gc [componentName]");
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
String componentName = null;
if(args.length > 1) componentName = args[1];
switch(args[0])
{
case "unload":
{
if(componentName == null) throw new IllegalArgumentException("Must set componentName");
Component component = server.config.components.get(componentName);
if(component == null) {
LogHelper.error("Component %s not found", componentName);
return;
}
if(component instanceof AutoCloseable)
{
((AutoCloseable) component).close();
}
else
{
LogHelper.error("Component %s unload not supported", componentName);
return;
}
break;
}
case "gc":
{
if(componentName == null) throw new IllegalArgumentException("Must set componentName");
Component component = server.config.components.get(componentName);
if(component == null) {
LogHelper.error("Component %s not found", componentName);
return;
}
if(component instanceof NeedGarbageCollection)
{
((NeedGarbageCollection) component).garbageCollection();
}
else
{
LogHelper.error("Component %s gc not supported", componentName);
return;
}
break;
}
case "load":
{
if(componentName == null) throw new IllegalArgumentException("Must set componentName");
if(args.length <= 2) throw new IllegalArgumentException("Must set file");
String fileName = args[2];
try(Reader reader = IOHelper.newReader(Paths.get(fileName)))
{
Component component = LaunchServer.gson.fromJson(reader, Component.class);
component.preInit(server);
component.init(server);
component.postInit(server);
LogHelper.info("Component %s(%s) loaded", componentName, component.getClass().getName());
}
}
case "help":
{
printHelp();
}
default:
{
printHelp();
}
}
}
}