Skip to content

Commit

Permalink
Add caveat to warning msg about Java agents
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 14, 2024
1 parent c94eba9 commit 45ce159
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import me.lucko.spark.common.tick.TickReporter;
import me.lucko.spark.common.util.BytebinClient;
import me.lucko.spark.common.util.Configuration;
import me.lucko.spark.common.util.SparkStaticLogger;
import me.lucko.spark.common.util.TemporaryFiles;
import me.lucko.spark.common.ws.TrustedKeyStore;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -116,6 +117,7 @@ public class SparkPlatform {

public SparkPlatform(SparkPlugin plugin) {
this.plugin = plugin;
SparkStaticLogger.setLogger(plugin::log);

this.temporaryFiles = new TemporaryFiles(this.plugin.getPluginDirectory().resolve("tmp"));
this.configuration = new Configuration(this.plugin.getPluginDirectory().resolve("config.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.instrument.Instrumentation;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

/**
* Uses {@link Instrumentation} to find a class reference for given class names.
Expand All @@ -34,15 +35,27 @@
*/
public class ClassFinder {

private final Map<String, Class<?>> classes = new HashMap<>();
private static boolean warned = false;

public ClassFinder() {
Instrumentation instrumentation;
private static Instrumentation loadInstrumentation() {
Instrumentation instrumentation = null;
try {
instrumentation = ByteBuddyAgent.install();
if (!warned && JavaVersion.getJavaVersion() >= 21) {
warned = true;
SparkStaticLogger.log(Level.INFO, "If you see a warning above that says \"WARNING: A Java agent has been loaded dynamically\", it can be safely ignored.");
SparkStaticLogger.log(Level.INFO, "See here for more information: https://spark.lucko.me/docs/misc/Java-agent-warning");
}
} catch (Exception e) {
return;
// ignored
}
return instrumentation;
}

private final Map<String, Class<?>> classes = new HashMap<>();

public ClassFinder() {
Instrumentation instrumentation = loadInstrumentation();
if (instrumentation == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.common.util;

public class JavaVersion {
;

private static final int JAVA_VERSION;
static {
JAVA_VERSION = parseJavaVersion(System.getProperty("java.version"));
}

private static int parseJavaVersion(String version) {
if (version.startsWith("1.")) {
// Java 8 and below
return Integer.parseInt(version.substring(2, 3));
} else {
// Java 9 and above
return Integer.parseInt(version.split("\\.")[0]);
}
}

public static int getJavaVersion() {
return JAVA_VERSION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.common.util;

import java.util.logging.Level;

/**
* Special logger for use by classes that don't easily have access to a
* {@link me.lucko.spark.common.SparkPlatform} instance.
*
* <p>This avoids warnings on platforms like Paper that get upset if plugins use
* {@link System#out} or {@link System#err}.</p>
*/
public enum SparkStaticLogger {
;

private static Logger logger = null;

public synchronized static void setLogger(Logger logger) {
if (SparkStaticLogger.logger == null) {
SparkStaticLogger.logger = logger;
}
}

public static void log(Level level, String msg) {
Logger logger = SparkStaticLogger.logger;
if (logger == null) {
if (level.intValue() >= 1000) {
System.err.println(msg);
} else {
System.out.println(msg);
}
return;
}

logger.log(level, msg);
}

public interface Logger {
void log(Level level, String msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

public class SparkThreadFactory implements ThreadFactory {

public static final Thread.UncaughtExceptionHandler EXCEPTION_HANDLER = (t, e) -> {
System.err.println("Uncaught exception thrown by thread " + t.getName());
SparkStaticLogger.log(Level.SEVERE, "Uncaught exception thrown by thread " + t.getName());
e.printStackTrace();
};

Expand Down

0 comments on commit 45ce159

Please sign in to comment.