[FEATURE] LaunchServerConsole

This commit is contained in:
Gravit 2019-03-08 19:43:26 +07:00
parent ec5ef7af4f
commit 57231d948d
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
8 changed files with 113 additions and 9 deletions

View file

@ -0,0 +1,20 @@
String mainClassName = "ru.gravit.launchserver.console.ConsoleMain"
repositories {
maven {
url "http://repo.spring.io/plugins-release/"
}
}
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
jar {
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
manifest.attributes("Main-Class": mainClassName)
}
dependencies {
compileOnly project(':ServerWrapper')
}

View file

@ -0,0 +1,35 @@
package ru.gravit.launchserver.console;
import ru.gravit.launcher.server.ServerWrapper;
import ru.gravit.utils.command.CommandHandler;
import ru.gravit.utils.command.JLineCommandHandler;
import ru.gravit.utils.command.StdCommandHandler;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException;
public class ConsoleMain {
public static CommandHandler commandHandler;
public static void main(String[] args) throws IOException {
if(ServerWrapper.config == null)
{
LogHelper.warning("ServerWrapper not found");
}
if(!ServerWrapper.permissions.canAdmin)
{
LogHelper.warning("Permission canAdmin not found");
}
try {
Class.forName("jline.Terminal");
// JLine2 available
commandHandler = new RemoteJLineCommandHandler();
LogHelper.info("JLine2 terminal enabled");
} catch (ClassNotFoundException ignored) {
commandHandler = new RemoteStdCommandHandler(true);
LogHelper.warning("JLine2 isn't in classpath, using std");
}
LogHelper.info("CommandHandler started. Use 'exit' to exit this console");
commandHandler.run();
}
}

View file

@ -0,0 +1,24 @@
package ru.gravit.launchserver.console;
import ru.gravit.launcher.request.admin.ExecCommandRequest;
import ru.gravit.utils.command.Command;
import ru.gravit.utils.command.JLineCommandHandler;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException;
public class RemoteJLineCommandHandler extends JLineCommandHandler {
public RemoteJLineCommandHandler() throws IOException {
}
@Override
public void eval(String line, boolean bell)
{
if(line.equals("exit")) System.exit(0);
ExecCommandRequest request = new ExecCommandRequest(System.out::println, line);
try {
request.request();
} catch (Exception e) {
LogHelper.error(e);
}
}
}

View file

@ -0,0 +1,24 @@
package ru.gravit.launchserver.console;
import ru.gravit.launcher.request.admin.ExecCommandRequest;
import ru.gravit.utils.command.Command;
import ru.gravit.utils.command.CommandHandler;
import ru.gravit.utils.command.StdCommandHandler;
import ru.gravit.utils.helper.LogHelper;
public class RemoteStdCommandHandler extends StdCommandHandler {
public RemoteStdCommandHandler(boolean readCommands) {
super(readCommands);
}
@Override
public void eval(String line, boolean bell)
{
if(line.equals("exit")) System.exit(0);
ExecCommandRequest request = new ExecCommandRequest(System.out::println, line);
try {
request.request();
} catch (Exception e) {
LogHelper.error(e);
}
}
}

View file

@ -16,7 +16,7 @@
public abstract class CommandHandler implements Runnable {
private final Map<String, Command> commands = new ConcurrentHashMap<>(32);
public final void eval(String line, boolean bell) {
public void eval(String line, boolean bell) {
LogHelper.info("Command '%s'", line);
// Parse line to tokens
@ -33,7 +33,7 @@ public final void eval(String line, boolean bell) {
}
public final void eval(String[] args, boolean bell) {
public void eval(String[] args, boolean bell) {
if (args.length == 0)
return;
@ -56,7 +56,7 @@ public final void eval(String[] args, boolean bell) {
}
public final Command lookup(String name) throws CommandException {
public Command lookup(String name) throws CommandException {
Command command = commands.get(name);
if (command == null)
throw new CommandException(String.format("Unknown command: '%s'", name));
@ -72,14 +72,14 @@ private void readLoop() throws IOException {
}
public final void registerCommand(String name, Command command) {
public void registerCommand(String name, Command command) {
VerifyHelper.verifyIDName(name);
VerifyHelper.putIfAbsent(commands, name, Objects.requireNonNull(command, "command"),
String.format("Command has been already registered: '%s'", name));
}
@Override
public final void run() {
public void run() {
try {
readLoop();
} catch (IOException e) {
@ -95,7 +95,7 @@ public final void run() {
public abstract void clear() throws IOException;
public final Map<String, Command> commandsMap() {
public Map<String, Command> commandsMap() {
return Collections.unmodifiableMap(commands);
}

View file

@ -6,7 +6,7 @@
import java.io.IOException;
public final class JLineCommandHandler extends CommandHandler {
public class JLineCommandHandler extends CommandHandler {
private final class JLineOutput implements Output {
@Override
public void println(String message) {

View file

@ -5,7 +5,7 @@
import java.io.BufferedReader;
import java.io.IOException;
public final class StdCommandHandler extends CommandHandler {
public class StdCommandHandler extends CommandHandler {
private final BufferedReader reader;
public StdCommandHandler(boolean readCommands) {

View file

@ -5,6 +5,7 @@
include 'LauncherAPI'
include 'ServerWrapper'
include 'LaunchServer'
include 'LaunchServerConsole'
include 'modules'
file('modules').eachDir { sub ->
if (sub.name.endsWith('_module')) include 'modules:' + sub.name