jvz commented on code in PR #2419:
URL: https://github.com/apache/logging-log4j2/pull/2419#discussion_r1544765859


##########
log4j-api/src/main/java/org/apache/logging/log4j/ScopedContext.java:
##########
@@ -0,0 +1,558 @@
+/*
+ * 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;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.function.Supplier;
+import org.apache.logging.log4j.internal.ScopedContextAnchor;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * Context that can be used for data to be logged in a block of code.
+ *
+ * While this is influenced by ScopedValues from Java 21 it does not share the 
same API. While it can perform a
+ * similar function as a set of ScopedValues it is really meant to allow a 
block of code to include a set of keys and
+ * values in all the log events within that block. The underlying 
implementation must provide support for
+ * logging the ScopedContext for that to happen.
+ *
+ * The ScopedContext will not be bound to the current thread until either a 
run or call method is invoked. The
+ * contexts are nested so creating and running or calling via a second 
ScopedContext will result in the first
+ * ScopedContext being hidden until the call is returned. Thus the values from 
the first ScopedContext need to
+ * be added to the second to be included.
+ *
+ * The ScopedContext can be passed to child threads by including the 
ExecutorService to be used to manage the
+ * run or call methods. The caller should interact with the ExecutorService as 
if they were submitting their
+ * run or call methods directly to it. The ScopedContext performs no error 
handling other than to ensure the
+ * ThreadContext and ScopedContext are cleaned up from the executed Thread.
+ *
+ * @since 2.24.0
+ */
+public class ScopedContext {
+
+    public static final Logger LOGGER = StatusLogger.getLogger();
+
+    /**
+     * @hidden
+     * Returns an unmodifiable copy of the current ScopedContext Map. This 
method should
+     * only be used by implementations of Log4j API.
+     * @return the Map of Renderable objects.
+     */
+    public static Map<String, Renderable> getContextMap() {
+        Optional<Instance> context = ScopedContextAnchor.getContext();
+        if (context.isPresent()
+                && context.get().contextMap != null
+                && !context.get().contextMap.isEmpty()) {
+            return Collections.unmodifiableMap(context.get().contextMap);
+        }
+        return Collections.emptyMap();
+    }
+
+    /**
+     * Return the key from the current ScopedContext, if there is one and the 
key exists.
+     * @param key The key.
+     * @return The value of the key in the current ScopedContext.
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T get(String key) {
+        Optional<Instance> context = ScopedContextAnchor.getContext();
+        if (context.isPresent()) {
+            Renderable renderable = context.get().contextMap.get(key);
+            if (renderable != null) {
+                return (T) renderable.getObject();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Creates a ScopedContext Instance with a key/value pair.
+     *
+     * @param key   the key to add.
+     * @param value the value associated with the key.
+     * @return the Instance constructed if a valid key and value were 
provided. Otherwise, either the
+     * current Instance is returned or a new Instance is created if there is 
no current Instance.
+     */
+    public static Instance where(String key, Object value) {
+        if (value != null) {
+            Renderable renderable = value instanceof Renderable ? (Renderable) 
value : new ObjectRenderable(value);
+            Instance parent = current().isPresent() ? current().get() : null;
+            return new Instance(parent, key, renderable);
+        } else {
+            if (current().isPresent()) {
+                Map<String, Renderable> map = getContextMap();
+                map.remove(key);
+                return new Instance(map);
+            }
+        }
+        return current().isPresent() ? current().get() : new Instance();
+    }
+
+    /**
+     * Adds a key/value pair to the ScopedContext being constructed.
+     *
+     * @param key      the key to add.
+     * @param supplier the function to generate the value.
+     * @return the ScopedContext being constructed.
+     */
+    public static Instance where(String key, Supplier<Object> supplier) {
+        return where(key, supplier.get());
+    }
+
+    /**
+     * Creates a ScopedContext Instance with a Map of keys and values.
+     * @param map the Map.
+     * @return the ScopedContext Instance constructed.
+     */
+    public static Instance where(Map<String, ?> map) {
+        if (map != null && !map.isEmpty()) {
+            Map<String, Renderable> renderableMap = new HashMap<>();
+            if (current().isPresent()) {
+                renderableMap.putAll(current().get().contextMap);
+            }
+            map.forEach((key, value) -> {
+                if (value == null || (value instanceof String && ((String) 
value).isEmpty())) {
+                    renderableMap.remove(key);
+                } else {
+                    renderableMap.put(
+                            key, value instanceof Renderable ? (Renderable) 
value : new ObjectRenderable(value));
+                }
+            });
+            return new Instance(renderableMap);
+        } else {
+            return current().isPresent() ? current().get() : new Instance();
+        }
+    }
+
+    /**
+     * Creates a ScopedContext with a single key/value pair and calls a method.
+     * @param key the key.
+     * @param obj the value associated with the key.
+     * @param op the Runnable to call.
+     */
+    public static void runWhere(String key, Object obj, Runnable op) {
+        if (obj != null) {
+            Renderable renderable = obj instanceof Renderable ? (Renderable) 
obj : new ObjectRenderable(obj);
+            Map<String, Renderable> map = new HashMap<>();
+            if (current().isPresent()) {
+                map.putAll(current().get().contextMap);
+            }
+            map.put(key, renderable);
+            new Instance(map).run(op);
+        } else {
+            Map<String, Renderable> map = new HashMap<>();
+            if (current().isPresent()) {
+                map.putAll(current().get().contextMap);
+            }
+            map.remove(key);
+            new Instance(map).run(op);
+        }
+    }
+
+    /**
+     * Creates a ScopedContext with a single key/value pair and calls a method 
on a separate Thread.
+     * @param key the key.
+     * @param obj the value associated with the key.
+     * @param executorService the ExecutorService to dispatch the work.
+     * @param op the Runnable to call.
+     */
+    public static Future<?> runWhere(String key, Object obj, ExecutorService 
executorService, Runnable op) {
+        if (obj != null) {
+            Renderable renderable = obj instanceof Renderable ? (Renderable) 
obj : new ObjectRenderable(obj);
+            Map<String, Renderable> map = new HashMap<>();
+            if (current().isPresent()) {
+                map.putAll(current().get().contextMap);
+            }
+            map.put(key, renderable);
+            if (executorService != null) {
+                return executorService.submit(new Runner(
+                        new Instance(map), ThreadContext.getContext(), 
ThreadContext.getImmutableStack(), op));
+            } else {
+                new Instance(map).run(op);
+                return CompletableFuture.completedFuture(0);
+            }
+        } else {
+            Map<String, Renderable> map = new HashMap<>();
+            if (current().isPresent()) {
+                map.putAll(current().get().contextMap);
+            }
+            map.remove(key);
+            if (executorService != null) {
+                return executorService.submit(new Runner(
+                        new Instance(map), ThreadContext.getContext(), 
ThreadContext.getImmutableStack(), op));
+            } else {
+                new Instance(map).run(op);
+                return CompletableFuture.completedFuture(0);
+            }
+        }
+    }
+
+    /**
+     * Creates a ScopedContext with a Map of keys and values and calls a 
method.
+     * @param map the Map.
+     * @param op the Runnable to call.
+     */
+    public static void runWhere(Map<String, ?> map, Runnable op) {
+        if (map != null && !map.isEmpty()) {
+            Map<String, Renderable> renderableMap = new HashMap<>();
+            if (current().isPresent()) {
+                renderableMap.putAll(current().get().contextMap);
+            }
+            map.forEach((key, value) -> {
+                renderableMap.put(key, value instanceof Renderable ? 
(Renderable) value : new ObjectRenderable(value));
+            });
+            new Instance(renderableMap).run(op);
+        } else {
+            op.run();
+        }
+    }
+
+    /**
+     * Creates a ScopedContext with a single key/value pair and calls a method.
+     * @param key the key.
+     * @param obj the value associated with the key.
+     * @param op the Runnable to call.
+     */
+    public static <R> R callWhere(String key, Object obj, Callable<R> op) 
throws Exception {
+        if (obj != null) {
+            Renderable renderable = obj instanceof Renderable ? (Renderable) 
obj : new ObjectRenderable(obj);
+            Map<String, Renderable> map = new HashMap<>();
+            if (current().isPresent()) {
+                map.putAll(current().get().contextMap);
+            }
+            map.put(key, renderable);
+            return new Instance(map).call(op);
+        } else {
+            Map<String, Renderable> map = new HashMap<>();
+            if (current().isPresent()) {
+                map.putAll(current().get().contextMap);
+            }
+            map.remove(key);
+            return new Instance(map).call(op);
+        }
+    }
+
+    /**
+     * Creates a ScopedContext with a single key/value pair and calls a method 
on a separate Thread.
+     * @param key the key.
+     * @param obj the value associated with the key.
+     * @param executorService the ExecutorService to dispatch the work.
+     * @param op the Callable to call.
+     */
+    public static <R> Future<R> callWhere(String key, Object obj, 
ExecutorService executorService, Callable<R> op)

Review Comment:
   It implements Future, yes, but it's also a CompletionStage which is the part 
of the API I'm more interested in.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to