This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 1a7be3c8c4 Move Queued context provider logic to be the default as the
current default isn't thread safe and there is nothing in the queued
implementation specific to any logging implementation
1a7be3c8c4 is described below
commit 1a7be3c8c489728e18570cb9041c9b0abbb079d3
Author: Ralph Goers <[email protected]>
AuthorDate: Fri May 24 11:02:53 2024 -0700
Move Queued context provider logic to be the default as the current default
isn't thread safe and there is nothing in the queued implementation specific to
any logging implementation
---
.../spi/internal/DefaultScopedContextProvider.java | 38 +++++++-----
.../internal/QueuedScopedContextProviderTest.java | 57 ------------------
.../logging/log4j/core/impl/Log4jProvider.java | 14 -----
.../impl/internal/QueuedScopedContextProvider.java | 70 ----------------------
.../log4j/core/impl/internal/package-info.java | 25 --------
5 files changed, 24 insertions(+), 180 deletions(-)
diff --git
a/log4j-api/src/main/java/org/apache/logging/log4j/spi/internal/DefaultScopedContextProvider.java
b/log4j-api/src/main/java/org/apache/logging/log4j/spi/internal/DefaultScopedContextProvider.java
index 76fa0a3b33..84839f5e4c 100644
---
a/log4j-api/src/main/java/org/apache/logging/log4j/spi/internal/DefaultScopedContextProvider.java
+++
b/log4j-api/src/main/java/org/apache/logging/log4j/spi/internal/DefaultScopedContextProvider.java
@@ -16,49 +16,59 @@
*/
package org.apache.logging.log4j.spi.internal;
+import java.util.ArrayDeque;
+import java.util.Deque;
import java.util.Optional;
import org.apache.logging.log4j.spi.AbstractScopedContextProvider;
import org.apache.logging.log4j.spi.ScopedContextProvider;
/**
- * An implementation of {@link ScopedContextProvider} that uses the simplest
implementation.
+ * An implementation of {@link ScopedContextProvider}.
* @since 2.24.0
*/
public class DefaultScopedContextProvider extends
AbstractScopedContextProvider {
public static final ScopedContextProvider INSTANCE = new
DefaultScopedContextProvider();
- private final ThreadLocal<MapInstance> scopedContext = new ThreadLocal<>();
+ private final ThreadLocal<Deque<Instance>> scopedContext = new
ThreadLocal<>();
/**
* Returns an immutable Map containing all the key/value pairs as Object
objects.
- * @return The current context Instance.
+ * @return An immutable copy of the Map at the current scope.
*/
+ @Override
protected Optional<Instance> getContext() {
- return Optional.ofNullable(scopedContext.get());
+ final Deque<Instance> stack = scopedContext.get();
+ return stack != null ? Optional.of(stack.getFirst()) :
Optional.empty();
}
/**
* Add the ScopeContext.
* @param context The ScopeContext.
*/
+ @Override
protected void addScopedContext(final MapInstance context) {
- scopedContext.set(context);
+ Deque<Instance> stack = scopedContext.get();
+ if (stack == null) {
+ stack = new ArrayDeque<>();
+ scopedContext.set(stack);
+ }
+ stack.addFirst(context);
}
/**
* Remove the top ScopeContext.
*/
+ @Override
protected void removeScopedContext() {
- MapInstance current = scopedContext.get();
- if (current == null) {
- return;
- }
- MapInstance previous = current.getPrevious();
- if (previous != null) {
- scopedContext.set(previous);
- } else {
- scopedContext.remove();
+ final Deque<Instance> stack = scopedContext.get();
+ if (stack != null) {
+ if (!stack.isEmpty()) {
+ stack.removeFirst();
+ }
+ if (stack.isEmpty()) {
+ scopedContext.remove();
+ }
}
}
}
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/internal/QueuedScopedContextProviderTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/internal/QueuedScopedContextProviderTest.java
deleted file mode 100644
index 9d7e93502a..0000000000
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/internal/QueuedScopedContextProviderTest.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.logging.log4j.core.impl.internal;
-
-import org.apache.logging.log4j.test.spi.ScopedContextProviderSuite;
-import org.junit.jupiter.api.Test;
-
-class QueuedScopedContextProviderTest extends ScopedContextProviderSuite {
-
- private static QueuedScopedContextProvider createProvider() {
- return new QueuedScopedContextProvider();
- }
-
- @Test
- void testScope() {
- testScope(createProvider());
- }
-
- @Test
- void testRunWhere() {
- testRunWhere(createProvider());
- }
-
- @Test
- void testRunThreads() {
- testRunThreads(createProvider());
- }
-
- @Test
- void testThreads() throws Exception {
- testThreads(createProvider());
- }
-
- @Test
- void testThreadException() throws Exception {
- testThreadException(createProvider());
- }
-
- @Test
- void testThreadCall() throws Exception {
- testThreadCall(createProvider());
- }
-}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jProvider.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jProvider.java
index 2142cf96db..31d71cb5f4 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jProvider.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jProvider.java
@@ -19,12 +19,8 @@ package org.apache.logging.log4j.core.impl;
import aQute.bnd.annotation.Resolution;
import aQute.bnd.annotation.spi.ServiceConsumer;
import aQute.bnd.annotation.spi.ServiceProvider;
-import java.util.ServiceLoader;
-import org.apache.logging.log4j.core.impl.internal.QueuedScopedContextProvider;
import org.apache.logging.log4j.spi.Provider;
import org.apache.logging.log4j.spi.ScopedContextProvider;
-import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.ServiceLoaderUtil;
/**
* Binding for the Log4j API.
@@ -35,14 +31,4 @@ public class Log4jProvider extends Provider {
public Log4jProvider() {
super(10, CURRENT_VERSION, Log4jContextFactory.class);
}
-
- @Override
- public ScopedContextProvider getScopedContextProvider() {
- return ServiceLoaderUtil.safeStream(
- ScopedContextProvider.class,
- ServiceLoader.load(ScopedContextProvider.class),
- StatusLogger.getLogger())
- .findFirst()
- .orElse(QueuedScopedContextProvider.INSTANCE);
- }
}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/internal/QueuedScopedContextProvider.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/internal/QueuedScopedContextProvider.java
deleted file mode 100644
index 626b9c0255..0000000000
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/internal/QueuedScopedContextProvider.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.logging.log4j.core.impl.internal;
-
-import java.util.ArrayDeque;
-import java.util.Deque;
-import java.util.Optional;
-import org.apache.logging.log4j.spi.AbstractScopedContextProvider;
-import org.apache.logging.log4j.spi.ScopedContextProvider;
-
-public class QueuedScopedContextProvider extends AbstractScopedContextProvider
{
-
- public static final ScopedContextProvider INSTANCE = new
QueuedScopedContextProvider();
-
- private final ThreadLocal<Deque<Instance>> scopedContext = new
ThreadLocal<>();
-
- /**
- * Returns an immutable Map containing all the key/value pairs as Object
objects.
- * @return An immutable copy of the Map at the current scope.
- */
- @Override
- protected Optional<Instance> getContext() {
- final Deque<Instance> stack = scopedContext.get();
- return stack != null ? Optional.of(stack.getFirst()) :
Optional.empty();
- }
-
- /**
- * Add the ScopeContext.
- * @param context The ScopeContext.
- */
- @Override
- protected void addScopedContext(final MapInstance context) {
- Deque<Instance> stack = scopedContext.get();
- if (stack == null) {
- stack = new ArrayDeque<>();
- scopedContext.set(stack);
- }
- stack.addFirst(context);
- }
-
- /**
- * Remove the top ScopeContext.
- */
- @Override
- protected void removeScopedContext() {
- final Deque<Instance> stack = scopedContext.get();
- if (stack != null) {
- if (!stack.isEmpty()) {
- stack.removeFirst();
- }
- if (stack.isEmpty()) {
- scopedContext.remove();
- }
- }
- }
-}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/internal/package-info.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/internal/package-info.java
deleted file mode 100644
index 9d76948fca..0000000000
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/internal/package-info.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-/**
- * Log4j 2 private implementation classes.
- */
-@Export
-@Version("2.24.0")
-package org.apache.logging.log4j.core.impl.internal;
-
-import org.osgi.annotation.bundle.Export;
-import org.osgi.annotation.versioning.Version;