From da5384fc3d8fc96725235613e721eb1493aee860 Mon Sep 17 00:00:00 2001 From: "James R. Perkins" Date: Thu, 6 Jul 2023 09:01:01 -0700 Subject: [PATCH] Delegate the resource bundle, but cache it as well. Remove the overriding of the getResourceBundleName(). Signed-off-by: James R. Perkins --- .../java/org/jboss/logmanager/Logger.java | 64 +++++++++---------- .../org/jboss/logmanager/LoggerTests.java | 15 +++++ 2 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/jboss/logmanager/Logger.java b/src/main/java/org/jboss/logmanager/Logger.java index 526b6180..3290bb7a 100644 --- a/src/main/java/org/jboss/logmanager/Logger.java +++ b/src/main/java/org/jboss/logmanager/Logger.java @@ -22,16 +22,14 @@ import java.io.ObjectStreamException; import java.io.Serializable; import java.util.Arrays; +import java.util.Enumeration; import java.util.Locale; import java.util.ResourceBundle; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; import java.util.logging.Filter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; -import io.smallrye.common.constraint.Assert; - /** * An actual logger instance. This is the end-user interface into the logging system. */ @@ -39,6 +37,17 @@ public final class Logger extends java.util.logging.Logger implements Serializable { private static final long serialVersionUID = 5093333069125075416L; + private static final ResourceBundle TOMBSTONE = new ResourceBundle() { + @Override + protected Object handleGetObject(final String key) { + return null; + } + + @Override + public Enumeration getKeys() { + return null; + } + }; /** * The named logger tree node. @@ -50,12 +59,6 @@ public final class Logger extends java.util.logging.Logger implements Serializab */ private volatile ResourceBundle resourceBundle; - /** - * Atomic updater for {@link #resourceBundle}. - */ - private static final AtomicReferenceFieldUpdater resourceBundleUpdater = AtomicReferenceFieldUpdater - .newUpdater(Logger.class, ResourceBundle.class, "resourceBundle"); - private static final String LOGGER_CLASS_NAME = Logger.class.getName(); /** @@ -880,45 +883,36 @@ public void logRaw(final ExtLogRecord record) { } /** - * Set the resource bundle for this logger. Unlike {@link java.util.logging.Logger#setResourceBundle(ResourceBundle)}, - * there is no parent search performed for resource bundles by this implementation. + * Set the resource bundle for this logger. * * @param resourceBundle the resource bundle (must not be {@code null}) */ @Override public void setResourceBundle(ResourceBundle resourceBundle) { - Assert.checkNotNullParam("resourceBundle", resourceBundle); - LogContext.checkAccess(); - ResourceBundle old; - do { - old = this.resourceBundle; - if (old != null && !old.getBaseBundleName().equals(resourceBundle.getBaseBundleName())) { - throw new IllegalArgumentException("Bundle base name does not match existing bundle"); - } - } while (!resourceBundleUpdater.compareAndSet(this, old, resourceBundle)); + super.setResourceBundle(resourceBundle); + synchronized (this) { + this.resourceBundle = resourceBundle; + } } /** - * Get the resource bundle for this logger. Unlike {@link java.util.logging.Logger#getResourceBundle()}, - * there is no parent search performed for resource bundles by this implementation. + * Get the resource bundle for this logger. * * @return the resource bundle, or {@code null} if none is configured for this logger */ @Override public ResourceBundle getResourceBundle() { - return resourceBundle; - } - - /** - * Get the resource bundle name for this logger. Unlike {@link java.util.logging.Logger#getResourceBundleName()}, - * there is no parent search performed for resource bundles by this implementation. - * - * @return the resource bundle, or {@code null} if none is configured for this logger - */ - @Override - public String getResourceBundleName() { - final ResourceBundle resourceBundle = getResourceBundle(); - return resourceBundle == null ? null : resourceBundle.getBaseBundleName(); + if (resourceBundle == null) { + synchronized (this) { + if (resourceBundle == null) { + resourceBundle = super.getResourceBundle(); + if (resourceBundle == null) { + resourceBundle = TOMBSTONE; + } + } + } + } + return resourceBundle == TOMBSTONE ? null : resourceBundle; } /** diff --git a/src/test/java/org/jboss/logmanager/LoggerTests.java b/src/test/java/org/jboss/logmanager/LoggerTests.java index 524a44ad..07176ad4 100644 --- a/src/test/java/org/jboss/logmanager/LoggerTests.java +++ b/src/test/java/org/jboss/logmanager/LoggerTests.java @@ -202,6 +202,21 @@ public void testResourceBundle() { assertEquals("Test message", handler.messages.get(1)); } + @Test + public void testJulResourceBundle() { + final ListHandler handler = new ListHandler(); + final java.util.logging.Logger logger = java.util.logging.Logger.getLogger("rbLogger", getClass().getName()); + assertNotNull(logger.getResourceBundleName(), "Resource bundle name was expected"); + assertNotNull(logger.getResourceBundle(), "Resource bundle was expected"); + logger.setLevel(Level.INFO); + handler.setLevel(Level.INFO); + logger.addHandler(handler); + logger.log(Level.INFO, null, new IllegalArgumentException()); + logger.log(Level.INFO, "test", new IllegalArgumentException()); + assertNull(handler.messages.get(0)); + assertEquals("Test message", handler.messages.get(1)); + } + @Test public void testInheritedFilter() { final ListHandler handler = new ListHandler();