This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 27a7f3760f Prevent overwriting of constructor parameters
27a7f3760f is described below
commit 27a7f3760f242cce28b8f4499eb803d3b117c5a5
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Fri Jul 15 23:08:33 2022 +0200
Prevent overwriting of constructor parameters
If the context selector binding is registered before the call to
`init()`, the default context selector overwrites the one given as
parameter to the Log4jContextFactory constructor.
---
.../log4j/core/impl/Log4jContextFactoryTest.java | 40 ++++++++++++++++++++++
.../logging/log4j/core/impl/DefaultBundle.java | 13 +++++++
.../log4j/core/impl/Log4jContextFactory.java | 10 +++---
3 files changed, 59 insertions(+), 4 deletions(-)
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/Log4jContextFactoryTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/Log4jContextFactoryTest.java
new file mode 100644
index 0000000000..74b2a5522a
--- /dev/null
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/Log4jContextFactoryTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.logging.log4j.core.selector.BasicContextSelector;
+import org.junit.jupiter.api.Test;
+
+public class Log4jContextFactoryTest {
+
+ /**
+ * Tests whether the constructor parameters take priority over the default
+ * injector bindings.
+ */
+ @Test
+ public void testParameterPriority() {
+ Log4jContextFactory factory = new Log4jContextFactory(new
BasicContextSelector());
+ assertEquals(BasicContextSelector.class,
factory.getSelector().getClass());
+ factory = new Log4jContextFactory(factory);
+ assertEquals(Log4jContextFactory.class,
factory.getShutdownCallbackRegistry().getClass());
+ factory = new Log4jContextFactory(new BasicContextSelector(), factory);
+ assertEquals(BasicContextSelector.class,
factory.getSelector().getClass());
+ assertEquals(Log4jContextFactory.class,
factory.getShutdownCallbackRegistry().getClass());
+ }
+}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/DefaultBundle.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/DefaultBundle.java
index 42d27378d6..77888906ef 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/DefaultBundle.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/DefaultBundle.java
@@ -93,6 +93,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = Constants.LOG4J_CONTEXT_SELECTOR)
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-100)
public ContextSelector systemPropertyContextSelector() throws
ClassNotFoundException {
@@ -106,6 +107,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name =
ShutdownCallbackRegistry.SHUTDOWN_CALLBACK_REGISTRY)
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-100)
public ShutdownCallbackRegistry systemPropertyShutdownCallbackRegistry()
throws ClassNotFoundException {
@@ -119,6 +121,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME, value =
"SystemClock")
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-200)
public Clock systemClock() {
@@ -126,6 +129,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME, value =
"SystemMillisClock")
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-200)
public Clock systemMillisClock() {
@@ -133,6 +137,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME, value =
"CachedClock")
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-200)
public Clock cachedClock() {
@@ -140,6 +145,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME, value =
"org.apache.logging.log4j.core.time.internal.CachedClock")
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-200)
public Clock cachedClockFqcn() {
@@ -147,6 +153,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME, value =
"CoarseCachedClock")
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-200)
public Clock coarseCachedClock() {
@@ -154,6 +161,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME, value =
"org.apache.logging.log4j.core.time.internal.CoarseCachedClock")
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-200)
public Clock coarseCachedClockFqcn() {
@@ -161,6 +169,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = ClockFactory.PROPERTY_NAME)
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-100)
public Clock systemPropertyClock() throws ClassNotFoundException {
@@ -180,6 +189,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = "log4j2.ContextDataInjector")
+ @ConditionalOnMissingBinding
@Factory
@Ordered(-100)
public ContextDataInjector systemPropertyContextDataInjector() throws
ClassNotFoundException {
@@ -203,6 +213,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = Constants.LOG4J_LOG_EVENT_FACTORY)
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-100)
public LogEventFactory systemPropertyLogEventFactory() throws
ClassNotFoundException {
@@ -241,6 +252,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name =
CompositeConfiguration.MERGE_STRATEGY_PROPERTY)
+ @ConditionalOnMissingBinding
@SingletonFactory
@Ordered(-100)
public MergeStrategy systemPropertyMergeStrategy() throws
ClassNotFoundException {
@@ -254,6 +266,7 @@ public class DefaultBundle {
}
@ConditionalOnProperty(name = Constants.LOG4J_DEFAULT_STATUS_LEVEL)
+ @ConditionalOnMissingBinding
@SingletonFactory
@Named("StatusLogger")
@Ordered(-100)
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
index b9502685f6..573cb12225 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
@@ -75,7 +75,8 @@ public class Log4jContextFactory implements
LoggerContextFactory, ShutdownCallba
public Log4jContextFactory(final ContextSelector selector) {
Objects.requireNonNull(selector, "No ContextSelector provided");
injector = DI.createInjector();
- injector.registerBinding(ContextSelector.KEY, () -> selector).init();
+ injector.init();
+ injector.registerBinding(ContextSelector.KEY, () -> selector);
this.selector = injector.getInstance(ContextSelector.KEY);
this.shutdownCallbackRegistry =
injector.getInstance(ShutdownCallbackRegistry.KEY);
LOGGER.debug("Using ShutdownCallbackRegistry {}",
this.shutdownCallbackRegistry.getClass());
@@ -92,7 +93,8 @@ public class Log4jContextFactory implements
LoggerContextFactory, ShutdownCallba
public Log4jContextFactory(final ShutdownCallbackRegistry
shutdownCallbackRegistry) {
Objects.requireNonNull(shutdownCallbackRegistry, "No
ShutdownCallbackRegistry provided");
injector = DI.createInjector();
- injector.registerBinding(ShutdownCallbackRegistry.KEY, () ->
shutdownCallbackRegistry).init();
+ injector.init();
+ injector.registerBinding(ShutdownCallbackRegistry.KEY, () ->
shutdownCallbackRegistry);
this.selector = injector.getInstance(ContextSelector.KEY);
this.shutdownCallbackRegistry =
injector.getInstance(ShutdownCallbackRegistry.KEY);
LOGGER.debug("Using ShutdownCallbackRegistry {}",
this.shutdownCallbackRegistry.getClass());
@@ -111,9 +113,9 @@ public class Log4jContextFactory implements
LoggerContextFactory, ShutdownCallba
Objects.requireNonNull(selector, "No ContextSelector provided");
Objects.requireNonNull(shutdownCallbackRegistry, "No
ShutdownCallbackRegistry provided");
injector = DI.createInjector();
+ injector.init();
injector.registerBinding(ContextSelector.KEY, () -> selector)
- .registerBinding(ShutdownCallbackRegistry.KEY, () ->
shutdownCallbackRegistry)
- .init();
+ .registerBinding(ShutdownCallbackRegistry.KEY, () ->
shutdownCallbackRegistry);
this.selector = injector.getInstance(ContextSelector.KEY);
this.shutdownCallbackRegistry =
injector.getInstance(ShutdownCallbackRegistry.KEY);
LOGGER.debug("Using ShutdownCallbackRegistry {}",
this.shutdownCallbackRegistry.getClass());