From c54cc68775fcc629fa33992e517b3a5b94cae22d Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Mon, 25 Nov 2024 11:31:22 -0600 Subject: [PATCH] Add smart service provider method This allows a service loader to fully initialize the log manager without worrying about class init loops. --- src/main/java/module-info.java | 2 +- .../java/org/jboss/logmanager/LogManager.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index b37483c8..5cf99267 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -29,7 +29,7 @@ exports org.jboss.logmanager.formatters; exports org.jboss.logmanager.handlers; - provides java.util.logging.LogManager with LogManager; + provides java.util.logging.LogManager with LogManager.Provider; provides System.LoggerFinder with JBossLoggerFinder; provides ConfiguratorFactory with DefaultConfiguratorFactory; diff --git a/src/main/java/org/jboss/logmanager/LogManager.java b/src/main/java/org/jboss/logmanager/LogManager.java index 5961095d..f50b5a01 100644 --- a/src/main/java/org/jboss/logmanager/LogManager.java +++ b/src/main/java/org/jboss/logmanager/LogManager.java @@ -222,4 +222,25 @@ public boolean addLogger(java.util.logging.Logger logger) { public Logger getLogger(String name) { return LogContext.getLogContext().getLogger(name); } + + public static final class Provider { + /** + * The service provider method. + * This method is used when the log manager service is loaded from a modular application. + * + * @return the log manager instance (not {@code null}) + * @throws ClassCastException if the log manager is not initialized to this class + */ + public static LogManager provider() { + Thread ct = Thread.currentThread(); + ClassLoader old = ct.getContextClassLoader(); + ct.setContextClassLoader(Provider.class.getClassLoader()); + try { + System.setProperty("java.util.logging.manager", LogManager.class.getName()); + return (LogManager) getLogManager(); + } finally { + ct.setContextClassLoader(old); + } + } + } }