Skip to content

Commit

Permalink
Merge pull request #415 from jamezp/resource-bundle-update
Browse files Browse the repository at this point in the history
Delegate the resource bundle, but cache it as well. Remove the overri…
  • Loading branch information
jamezp authored Jul 6, 2023
2 parents 073037d + da5384f commit e94cfbb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
64 changes: 29 additions & 35 deletions src/main/java/org/jboss/logmanager/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,32 @@
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.
*/
@SuppressWarnings({ "SerializableClassWithUnconstructableAncestor" })
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<String> getKeys() {
return null;
}
};

/**
* The named logger tree node.
Expand All @@ -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<Logger, ResourceBundle> resourceBundleUpdater = AtomicReferenceFieldUpdater
.newUpdater(Logger.class, ResourceBundle.class, "resourceBundle");

private static final String LOGGER_CLASS_NAME = Logger.class.getName();

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/jboss/logmanager/LoggerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e94cfbb

Please sign in to comment.