diff --git a/common/src/main/java/info/mmpa/pipeblocker/PipeBlocker.java b/common/src/main/java/info/mmpa/pipeblocker/PipeBlocker.java index 9ec9d46..8351f4e 100644 --- a/common/src/main/java/info/mmpa/pipeblocker/PipeBlocker.java +++ b/common/src/main/java/info/mmpa/pipeblocker/PipeBlocker.java @@ -1,5 +1,6 @@ package info.mmpa.pipeblocker; +import info.mmpa.pipeblocker.logger.PipeBlockerJavaLogger; import info.mmpa.pipeblocker.logger.PipeBlockerLog4jLogger; import info.mmpa.pipeblocker.logger.PipeBlockerLogger; import info.mmpa.pipeblocker.logger.PipeBlockerStdoutLogger; @@ -72,13 +73,25 @@ private static void processFilter(InputStream filterStream) throws IOException { } } + public static void chooseBestLogger() { + LOGGER = PipeBlockerLogger.detectLogger(); + } + + public static void useStdOut() { + LOGGER = new PipeBlockerStdoutLogger(); + } + public static void useLog4j() { LOGGER = new PipeBlockerLog4jLogger(); } + public static void useJavaLogger() { + LOGGER = new PipeBlockerJavaLogger(); + } + private static void processLine(String line) { // ignore blank and comments - if(line.length() == 0 || line.charAt(0) == '#') + if(line.isEmpty() || line.charAt(0) == '#') return; // process glob lines String type = null; diff --git a/common/src/main/java/info/mmpa/pipeblocker/java8/FilterSetter.java b/common/src/main/java/info/mmpa/pipeblocker/java8/FilterSetter.java index b2e730d..2d67755 100644 --- a/common/src/main/java/info/mmpa/pipeblocker/java8/FilterSetter.java +++ b/common/src/main/java/info/mmpa/pipeblocker/java8/FilterSetter.java @@ -4,14 +4,18 @@ import sun.misc.ObjectInputFilter; public class FilterSetter { + private static boolean applied = false; public static void apply() { - ObjectInputFilter.Config.setSerialFilter(filterInfo -> { - switch (PipeBlocker.check(filterInfo.serialClass())) { - case UNDECIDED: return sun.misc.ObjectInputFilter.Status.UNDECIDED; - case ALLOWED: return sun.misc.ObjectInputFilter.Status.ALLOWED; - case REJECTED: return sun.misc.ObjectInputFilter.Status.REJECTED; - } - throw new AssertionError("unknown check status"); - }); + if (!applied) { + ObjectInputFilter.Config.setSerialFilter(filterInfo -> { + switch (PipeBlocker.check(filterInfo.serialClass())) { + case UNDECIDED: return ObjectInputFilter.Status.UNDECIDED; + case ALLOWED: return ObjectInputFilter.Status.ALLOWED; + case REJECTED: return ObjectInputFilter.Status.REJECTED; + } + throw new AssertionError("unknown check status"); + }); + applied = true; + } } } diff --git a/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerJavaLogger.java b/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerJavaLogger.java new file mode 100644 index 0000000..fb05280 --- /dev/null +++ b/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerJavaLogger.java @@ -0,0 +1,44 @@ +package info.mmpa.pipeblocker.logger; + +import java.lang.reflect.Method; +import java.util.logging.Logger; + +public class PipeBlockerJavaLogger implements PipeBlockerLogger { + private final Logger logger = Logger.getLogger("PipeBlocker"); + + public PipeBlockerJavaLogger() { + // Legacy FML logger injection + try { + Class fmlLog = Class.forName("cpw.mods.fml.common.FMLLog"); + Method getLogger = fmlLog.getMethod("getLogger"); + getLogger.setAccessible(true); + logger.setParent((Logger) getLogger.invoke(null)); + } catch (Throwable ignored) { + } + } + + @Override + public void info(String msg) { + logger.info(msg); + } + + @Override + public void error(String msg) { + logger.severe(msg); + } + + @Override + public void debug(String msg) { + logger.finest(msg); + } + + @Override + public void warn(String msg) { + logger.warning(msg); + } + + @Override + public void fatal(String msg) { + logger.severe(msg); + } +} diff --git a/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerLogger.java b/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerLogger.java index 114233a..88a060a 100644 --- a/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerLogger.java +++ b/common/src/main/java/info/mmpa/pipeblocker/logger/PipeBlockerLogger.java @@ -6,4 +6,22 @@ public interface PipeBlockerLogger { void warn(String msg); void fatal(String msg); void error(String msg); + + static PipeBlockerLogger detectLogger() { + try { + Class.forName("org.apache.logging.log4j.Logger"); + Class.forName("org.apache.logging.log4j.LogManager"); + return new PipeBlockerLog4jLogger(); + } catch (ClassNotFoundException ignored) { + } + + try { + Class.forName("java.util.logging.Logger"); + return new PipeBlockerJavaLogger(); + } catch (ClassNotFoundException ignored) { + } + + // Should never reach here + return new PipeBlockerStdoutLogger(); + } } diff --git a/fabric/src/main/java/info/mmpa/pipeblocker/PipeBlockerInitializer.java b/fabric/src/main/java/info/mmpa/pipeblocker/PipeBlockerInitializer.java index b566e1f..50234b4 100644 --- a/fabric/src/main/java/info/mmpa/pipeblocker/PipeBlockerInitializer.java +++ b/fabric/src/main/java/info/mmpa/pipeblocker/PipeBlockerInitializer.java @@ -5,7 +5,7 @@ public class PipeBlockerInitializer implements PreLaunchEntrypoint { @Override public void onPreLaunch() { - PipeBlocker.useLog4j(); + PipeBlocker.chooseBestLogger(); PipeBlocker.apply(); } } diff --git a/forge17_112/src/main/java/info/mmpa/pipeblocker/PipeBlockerMod.java b/forge17_112/src/main/java/info/mmpa/pipeblocker/PipeBlockerMod.java index e335ddd..f1933ae 100644 --- a/forge17_112/src/main/java/info/mmpa/pipeblocker/PipeBlockerMod.java +++ b/forge17_112/src/main/java/info/mmpa/pipeblocker/PipeBlockerMod.java @@ -1,20 +1,17 @@ package info.mmpa.pipeblocker; +import info.mmpa.pipeblocker.logger.PipeBlockerLogger; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.*; @Mod(modid = "pipeblocker", name = "PipeBlocker", version = Tags.VERSION, acceptableRemoteVersions = "*") public class PipeBlockerMod { - public static final Logger LOGGER = LogManager.getLogger("PipeBlocker"); + public static final PipeBlockerLogger LOGGER = PipeBlockerLogger.detectLogger(); @Mod.EventHandler @SuppressWarnings("unused") public void init(FMLInitializationEvent ev) { - PipeBlocker.useLog4j(); + PipeBlocker.chooseBestLogger(); } } diff --git a/java9/src/main/java/info/mmpa/pipeblocker/java9/FilterSetter.java b/java9/src/main/java/info/mmpa/pipeblocker/java9/FilterSetter.java index 002a67f..ca481d4 100644 --- a/java9/src/main/java/info/mmpa/pipeblocker/java9/FilterSetter.java +++ b/java9/src/main/java/info/mmpa/pipeblocker/java9/FilterSetter.java @@ -5,14 +5,18 @@ import info.mmpa.pipeblocker.PipeBlocker; public class FilterSetter { - public static void apply () { - ObjectInputFilter.Config.setSerialFilter(filterInfo -> { - switch (PipeBlocker.check(filterInfo.serialClass())) { + private static boolean applied = false; + public static void apply() { + if (!applied) { + ObjectInputFilter.Config.setSerialFilter(filterInfo -> { + switch (PipeBlocker.check(filterInfo.serialClass())) { case UNDECIDED: return ObjectInputFilter.Status.UNDECIDED; case ALLOWED: return ObjectInputFilter.Status.ALLOWED; case REJECTED: return ObjectInputFilter.Status.REJECTED; - } - throw new AssertionError("unknown check status"); - }); + } + throw new AssertionError("unknown check status"); + }); + applied = true; + } } } diff --git a/javaagent/src/main/java/info/mmpa/pipeblocker/PipeBlockerAgent.java b/javaagent/src/main/java/info/mmpa/pipeblocker/PipeBlockerAgent.java index ddb79d1..f921509 100644 --- a/javaagent/src/main/java/info/mmpa/pipeblocker/PipeBlockerAgent.java +++ b/javaagent/src/main/java/info/mmpa/pipeblocker/PipeBlockerAgent.java @@ -1,11 +1,10 @@ package info.mmpa.pipeblocker; -import org.apache.logging.log4j.*; - import java.lang.instrument.*; public class PipeBlockerAgent { public static void premain(String args, Instrumentation instrumentation){ + PipeBlocker.chooseBestLogger(); PipeBlocker.apply(); System.out.println("PipeBlocker Java agent loaded."); }