You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2021/07/26 01:12:47 UTC

[logging-log4j2] 01/05: Add LoggerContextScoped annotation

This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch mean-bean-machine
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit b4545a4e34754630352daa0c6e8240ebdc9b2ab6
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sat Jul 24 16:25:52 2021 -0500

    Add LoggerContextScoped annotation
---
 .../log4j/core/config/di/LoggerContextScoped.java  | 35 ++++++++++++++++++++++
 .../core/config/di/impl/DefaultBeanManager.java    | 15 ++++++----
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/LoggerContextScoped.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/LoggerContextScoped.java
new file mode 100644
index 0000000..a601364
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/LoggerContextScoped.java
@@ -0,0 +1,35 @@
+/*
+ * 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.config.di;
+
+import org.apache.logging.log4j.plugins.di.ScopeType;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
+@Documented
+@Inherited
+@ScopeType
+public @interface LoggerContextScoped {
+}
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/impl/DefaultBeanManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/impl/DefaultBeanManager.java
index 7dc6921..9d9ecce 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/impl/DefaultBeanManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/di/impl/DefaultBeanManager.java
@@ -56,6 +56,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -69,10 +70,10 @@ public class DefaultBeanManager implements BeanManager {
 
     private final Injector injector = new Injector(this);
 
-    private final Collection<Bean<?>> enabledBeans = ConcurrentHashMap.newKeySet();
+    private final Collection<Bean<?>> enabledBeans = new HashSet<>();
     private final Map<Type, Collection<Bean<?>>> beansByType = new ConcurrentHashMap<>();
-    private final Collection<DisposesMethod> disposesMethods = Collections.synchronizedCollection(new ArrayList<>());
-    private final Map<Class<? extends Annotation>, ScopeContext> scopes = new ConcurrentHashMap<>();
+    private final Collection<DisposesMethod> disposesMethods = new ArrayList<>();
+    private final Map<Class<? extends Annotation>, ScopeContext> scopes = new LinkedHashMap<>();
 
     public DefaultBeanManager() {
         // TODO: need a better way to register scope contexts
@@ -409,9 +410,11 @@ public class DefaultBeanManager implements BeanManager {
         beansByType.clear();
         enabledBeans.clear();
         disposesMethods.clear();
-        // TODO: better scope closing after more scopes are supported
-        scopes.get(SingletonScoped.class).close();
-        scopes.clear();
+        final List<Class<? extends Annotation>> scopeTypes = new ArrayList<>(scopes.keySet());
+        Collections.reverse(scopeTypes);
+        for (final Class<? extends Annotation> scopeType : scopeTypes) {
+            scopes.get(scopeType).close();
+        }
     }
 
     private static class DisposesMethod {