Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate the resource bundle, but cache it as well. Remove the overri… #415

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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