From f625cfc738aa1a277171b46115265c1c05f0d171 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Thu, 13 Jan 2022 19:31:41 +0800 Subject: [PATCH] 1. fix debug info (thank RayGicEFL) 2. split conf and plugins directories by javaagent args Signed-off-by: pengzhile --- README.md | 6 ++++- pom.xml | 2 +- .../com/janetfilter/core/Environment.java | 27 +++++++++++++++++-- .../com/janetfilter/core/Initializer.java | 2 ++ .../java/com/janetfilter/core/Launcher.java | 6 ++--- .../janetfilter/core/commons/DebugInfo.java | 15 ++++++++--- ...9c353c626d6510ca855ab6907ed7f64400257.txt} | 0 7 files changed, 47 insertions(+), 11 deletions(-) rename src/main/resources/{5a1666cf298cd1d4fa64d62d123af55f5f39024f.txt => 4cc9c353c626d6510ca855ab6907ed7f64400257.txt} (100%) diff --git a/README.md b/README.md index 8c0a5a6..302c5e3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v2.0.1 +# ja-netfilter v2.1.0 ### A javaagent framework @@ -11,6 +11,10 @@ * **WARNING: DO NOT put some unnecessary whitespace characters!** * edit your plugin config files: `${lower plugin name}.conf` file in the `conf` dir where `ja-netfilter.jar` is located. +* the `conf` and `plugins` directory can be specified through **the javaagent args**. + * eg: `-javaagent:/path/to/ja-netfilter.jar=appName`, your config and plugins directories will be `conf-appname` and `plugins-appname`. + * if no javaagent args, they default to `conf` and `plugins`. + * this mechanism will avoid extraneous and bloated `conf` and `plugins`. * run your java application and enjoy~ diff --git a/pom.xml b/pom.xml index 16cbe1c..2ef86a3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ja-netfilter ja-netfilter - 2.0.1 + 2.1.0 ja-netfilter A javaagent framework diff --git a/src/main/java/com/janetfilter/core/Environment.java b/src/main/java/com/janetfilter/core/Environment.java index 312fdd2..aa4ae89 100644 --- a/src/main/java/com/janetfilter/core/Environment.java +++ b/src/main/java/com/janetfilter/core/Environment.java @@ -1,5 +1,7 @@ package com.janetfilter.core; +import com.janetfilter.core.utils.StringUtils; + import java.io.File; public final class Environment { @@ -9,10 +11,21 @@ public final class Environment { private final File pluginsDir; public Environment(File agentFile) { + this(agentFile, null); + } + + public Environment(File agentFile, String app) { this.agentFile = agentFile; baseDir = agentFile.getParentFile(); - configDir = new File(baseDir, "config"); - pluginsDir = new File(baseDir, "plugins"); + + if (StringUtils.isEmpty(app)) { + configDir = new File(baseDir, "config"); + pluginsDir = new File(baseDir, "plugins"); + } else { + app = app.toLowerCase(); + configDir = new File(baseDir, "config-" + app); + pluginsDir = new File(baseDir, "plugins-" + app); + } } public File getBaseDir() { @@ -30,4 +43,14 @@ public final class Environment { public File getPluginsDir() { return pluginsDir; } + + @Override + public String toString() { + return "Environment: {" + + "\n\tbaseDir=" + baseDir + + ", \n\tagentFile=" + agentFile + + ", \n\tconfigDir=" + configDir + + ", \n\tpluginsDir=" + pluginsDir + + "\n}"; + } } diff --git a/src/main/java/com/janetfilter/core/Initializer.java b/src/main/java/com/janetfilter/core/Initializer.java index e827a5c..f6d100b 100644 --- a/src/main/java/com/janetfilter/core/Initializer.java +++ b/src/main/java/com/janetfilter/core/Initializer.java @@ -8,6 +8,8 @@ import java.util.Set; public class Initializer { public static void init(Instrumentation inst, Environment environment) { + DebugInfo.output(environment.toString()); + Dispatcher dispatcher = new Dispatcher(); new PluginManager(inst, dispatcher, environment).loadPlugins(); diff --git a/src/main/java/com/janetfilter/core/Launcher.java b/src/main/java/com/janetfilter/core/Launcher.java index 724dad2..66ed0e9 100644 --- a/src/main/java/com/janetfilter/core/Launcher.java +++ b/src/main/java/com/janetfilter/core/Launcher.java @@ -9,7 +9,7 @@ import java.net.URL; import java.util.jar.JarFile; public class Launcher { - private static final String VERSION = "v2.0.1"; + private static final String VERSION = "v2.1.0"; public static void main(String[] args) { printUsage(); @@ -34,7 +34,7 @@ public class Launcher { return; } - Initializer.init(inst, new Environment(agentFile)); // for some custom UrlLoaders + Initializer.init(inst, new Environment(agentFile, args)); // for some custom UrlLoaders } private static void printUsage() { @@ -58,7 +58,7 @@ public class Launcher { return url.toURI(); } - String resourcePath = "/5a1666cf298cd1d4fa64d62d123af55f5f39024f.txt"; + String resourcePath = "/4cc9c353c626d6510ca855ab6907ed7f64400257.txt"; url = Launcher.class.getResource(resourcePath); if (null == url) { throw new Exception("Can not locate resource file."); diff --git a/src/main/java/com/janetfilter/core/commons/DebugInfo.java b/src/main/java/com/janetfilter/core/commons/DebugInfo.java index 0f54b5d..7127313 100644 --- a/src/main/java/com/janetfilter/core/commons/DebugInfo.java +++ b/src/main/java/com/janetfilter/core/commons/DebugInfo.java @@ -4,6 +4,8 @@ import com.janetfilter.core.utils.DateUtils; public class DebugInfo { private static final boolean DEBUG = "1".equals(System.getenv("JANF_DEBUG")) || "1".equals(System.getProperty("janf.debug")); + private static final String CLASS_NAME = DebugInfo.class.getName(); + private static final String LOG_TEMPLATE = "[%s] %s DEBUG : %s%n"; public static void output(String content) { output(content, null); @@ -14,12 +16,17 @@ public class DebugInfo { return; } - String template = "[%s] %s DEBUG : %s%n"; - + String caller = "UNKNOWN"; StackTraceElement[] traces = new Throwable().getStackTrace(); - String caller = traces.length < 2 ? "UNKNOWN" : traces[1].toString(); + for (int i = 1, l = traces.length; i < l; i++) { // thank RayGicEFL + StackTraceElement element = traces[i]; + if (!CLASS_NAME.equals(element.getClassName())) { + caller = element.toString(); + break; + } + } - String outContent = String.format(template, DateUtils.formatNow(), caller, content); + String outContent = String.format(LOG_TEMPLATE, DateUtils.formatNow(), caller, content); if (null == e) { System.out.print(outContent); return; diff --git a/src/main/resources/5a1666cf298cd1d4fa64d62d123af55f5f39024f.txt b/src/main/resources/4cc9c353c626d6510ca855ab6907ed7f64400257.txt similarity index 100% rename from src/main/resources/5a1666cf298cd1d4fa64d62d123af55f5f39024f.txt rename to src/main/resources/4cc9c353c626d6510ca855ab6907ed7f64400257.txt