From f9a3ddec437df3566ef9496cdc6df27b2b178579 Mon Sep 17 00:00:00 2001 From: Ryan Michela Date: Thu, 3 Jul 2014 00:18:34 -0700 Subject: [PATCH] Fix plugin for 1.7.x --- pom.xml | 7 +- .../sshd/ConsoleCommandFactory.java | 2 +- .../ryanmichela/sshd/ConsoleShellFactory.java | 17 ++-- .../java/com/ryanmichela/sshd/SshdPlugin.java | 2 +- .../sshd/StreamHandlerAppender.java | 83 +++++++++++++++++++ src/main/resources/plugin.yml | 2 +- 6 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/ryanmichela/sshd/StreamHandlerAppender.java diff --git a/pom.xml b/pom.xml index 5476a39..54f10fc 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ org.bukkit craftbukkit - 1.7.2-R0.1-SNAPSHOT + 1.7.9-R0.2 provided jar @@ -70,6 +70,7 @@ + true @@ -78,8 +79,8 @@ maven-compiler-plugin 3.0 - 1.7 - 1.7 + 1.6 + 1.6 true diff --git a/src/main/java/com/ryanmichela/sshd/ConsoleCommandFactory.java b/src/main/java/com/ryanmichela/sshd/ConsoleCommandFactory.java index 32c093b..20bd31f 100644 --- a/src/main/java/com/ryanmichela/sshd/ConsoleCommandFactory.java +++ b/src/main/java/com/ryanmichela/sshd/ConsoleCommandFactory.java @@ -53,7 +53,7 @@ public class ConsoleCommandFactory implements CommandFactory { SshdPlugin.instance.getLogger().info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command); Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); } catch (Exception e) { - SshdPlugin.instance.getLogger().severe("Error processing command from SSH"); + SshdPlugin.instance.getLogger().severe("Error processing command from SSH -" + e.getMessage()); } finally { callback.onExit(0); } diff --git a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java index 706955e..3257771 100644 --- a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java +++ b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java @@ -1,5 +1,7 @@ package com.ryanmichela.sshd; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Logger; import org.apache.sshd.common.Factory; import org.apache.sshd.server.Command; import org.apache.sshd.server.Environment; @@ -10,7 +12,6 @@ import org.bukkit.craftbukkit.libs.jline.console.ConsoleReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.logging.Logger; import java.util.logging.StreamHandler; public class ConsoleShellFactory implements Factory { @@ -28,7 +29,7 @@ public class ConsoleShellFactory implements Factory { private Environment environment; private Thread thread; - StreamHandler streamHandler; + StreamHandlerAppender streamHandlerAppender; ConsoleReader consoleReader; public InputStream getIn() { @@ -70,9 +71,10 @@ public class ConsoleShellFactory implements Factory { consoleReader.setExpandEvents(true); consoleReader.addCompleter(new ConsoleCommandCompleter()); - streamHandler = new FlushyStreamHandler(out, new ConsoleLogFormatter(), consoleReader); - Bukkit.getLogger().addHandler(streamHandler); - Logger.getLogger("").addHandler(streamHandler); + StreamHandler streamHandler = new FlushyStreamHandler(out, new ConsoleLogFormatter(), consoleReader); + streamHandlerAppender = new StreamHandlerAppender(streamHandler); + + ((Logger) LogManager.getRootLogger()).addAppender(streamHandlerAppender); environment = env; thread = new Thread(this, "SSHD ConsoleShell " + env.getEnv().get(Environment.ENV_USER)); @@ -83,8 +85,7 @@ public class ConsoleShellFactory implements Factory { } public void destroy() { - Bukkit.getLogger().removeHandler(streamHandler); - Logger.getLogger("").removeHandler(streamHandler); + ((Logger) LogManager.getRootLogger()).removeAppender(streamHandlerAppender); } public void run() { @@ -97,7 +98,7 @@ public class ConsoleShellFactory implements Factory { if (command.equals("exit")) { break; } - SshdPlugin.instance.getLogger().info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command); + SshdPlugin.instance.getLogger().info("<" + environment.getEnv().get(Environment.ENV_USER) + "> " + command); Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); } } diff --git a/src/main/java/com/ryanmichela/sshd/SshdPlugin.java b/src/main/java/com/ryanmichela/sshd/SshdPlugin.java index 30e911b..8d2c1b6 100644 --- a/src/main/java/com/ryanmichela/sshd/SshdPlugin.java +++ b/src/main/java/com/ryanmichela/sshd/SshdPlugin.java @@ -55,7 +55,7 @@ public class SshdPlugin extends JavaPlugin { public void onDisable() { try { sshd.stop(); - } catch (InterruptedException e) { + } catch (Exception e) { // do nothing } } diff --git a/src/main/java/com/ryanmichela/sshd/StreamHandlerAppender.java b/src/main/java/com/ryanmichela/sshd/StreamHandlerAppender.java new file mode 100644 index 0000000..d55f4a5 --- /dev/null +++ b/src/main/java/com/ryanmichela/sshd/StreamHandlerAppender.java @@ -0,0 +1,83 @@ +package com.ryanmichela.sshd; + +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.ErrorHandler; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LogEvent; + +import java.io.Serializable; +import java.util.UUID; +import java.util.logging.LogRecord; +import java.util.logging.StreamHandler; + +/** + * Copyright 2014 Ryan Michela + */ +public class StreamHandlerAppender implements Appender { + private StreamHandler streamHandler; + private UUID uuid; + + public StreamHandlerAppender(StreamHandler streamHandler) { + this.streamHandler = streamHandler; + uuid = UUID.randomUUID(); + } + + @Override + public void append(LogEvent logEvent) { + java.util.logging.Level level; + switch (logEvent.getLevel()) + { + case DEBUG: level = java.util.logging.Level.FINE; break; + case INFO: level = java.util.logging.Level.INFO; break; + case WARN: level = java.util.logging.Level.WARNING; break; + case ERROR: level = java.util.logging.Level.SEVERE; break; + default: level = java.util.logging.Level.INFO; break; + } + + String message = logEvent.getMessage().getFormattedMessage(); + + + LogRecord logRecord = new LogRecord(level, message); + streamHandler.publish(logRecord); + } + + @Override + public String getName() { + return "StreamHandlerAppender:" + uuid.toString(); + } + + @Override + public Layout getLayout() { + return null; + } + + @Override + public boolean ignoreExceptions() { + return false; + } + + @Override + public ErrorHandler getHandler() { + return null; + } + + @Override + public void setHandler(ErrorHandler errorHandler) { + + } + + @Override + public void start() { + + } + + @Override + public void stop() { + + } + + @Override + public boolean isStarted() { + return true; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 46fa723..de1a15e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: SSHD -version: "1.0" +version: "1.1" author: Ryan Michela main: com.ryanmichela.sshd.SshdPlugin \ No newline at end of file