Skip to content

Commit 96fa0fd

Browse files
committed
Add default value to newCheckedInstanceOfProperty
1 parent b8a115f commit 96fa0fd

File tree

4 files changed

+52
-30
lines changed

4 files changed

+52
-30
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Enumeration;
2727
import java.util.LinkedHashSet;
2828
import java.util.Objects;
29+
import java.util.function.Supplier;
2930

3031
/**
3132
* <em>Consider this class private.</em> Utility class for ClassLoaders.
@@ -328,11 +329,37 @@ public static <T> T newInstanceOf(final String className) throws ClassNotFoundEx
328329
* @since 2.5
329330
*/
330331
public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz)
331-
throws ClassNotFoundException, InvocationTargetException, InstantiationException,
332-
IllegalAccessException, NoSuchMethodException {
332+
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException,
333+
NoSuchMethodException {
334+
return newCheckedInstanceOfProperty(propertyName, clazz, () -> null);
335+
}
336+
337+
/**
338+
* Loads and instantiates a class given by a property name.
339+
*
340+
* @param propertyName The property name to look up a class name for.
341+
* @param clazz The class to cast it to.
342+
* @param defaultSupplier Supplier of a default value if the property is not present.
343+
* @param <T> The type to cast it to.
344+
* @return new instance of the class given in the property or {@code null} if the property was unset.
345+
* @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders
346+
* @throws ExceptionInInitializerError if an exception was thrown while initializing the class
347+
* @throws LinkageError if the linkage of the class fails for any other reason
348+
* @throws ClassCastException if the class is not compatible with the generic type parameter provided
349+
* @throws NoSuchMethodException if no zero-arg constructor exists
350+
* @throws SecurityException if this class is not allowed to access declared members of the provided class
351+
* @throws IllegalAccessException if the class can't be instantiated through a public constructor
352+
* @throws InstantiationException if the provided class is abstract or an interface
353+
* @throws InvocationTargetException if an exception is thrown by the constructor
354+
* @since 2.22
355+
*/
356+
public static <T> T newCheckedInstanceOfProperty(
357+
final String propertyName, final Class<T> clazz, final Supplier<T> defaultSupplier)
358+
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException,
359+
NoSuchMethodException {
333360
final String className = PropertiesUtil.getProperties().getStringProperty(propertyName);
334361
if (className == null) {
335-
return null;
362+
return defaultSupplier.get();
336363
}
337364
return newCheckedInstanceOf(className, clazz);
338365
}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,25 @@ static int calculateRingBufferSize(final String propertyName) {
8484
}
8585

8686
static ExceptionHandler<RingBufferLogEvent> getAsyncLoggerExceptionHandler() {
87-
ExceptionHandler<RingBufferLogEvent> handler = null;
8887
try {
89-
handler =
90-
LoaderUtil.newCheckedInstanceOfProperty(LOGGER_EXCEPTION_HANDLER_PROPERTY, ExceptionHandler.class);
88+
return LoaderUtil.newCheckedInstanceOfProperty(
89+
LOGGER_EXCEPTION_HANDLER_PROPERTY, ExceptionHandler.class, AsyncLoggerDefaultExceptionHandler::new);
9190
} catch (final ReflectiveOperationException e) {
9291
LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e);
92+
return new AsyncLoggerDefaultExceptionHandler();
9393
}
94-
if (handler != null) {
95-
return handler;
96-
}
97-
return new AsyncLoggerDefaultExceptionHandler();
9894
}
9995

10096
static ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> getAsyncLoggerConfigExceptionHandler() {
101-
ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> handler = null;
10297
try {
103-
handler = LoaderUtil.newCheckedInstanceOfProperty(
104-
LOGGER_CONFIG_EXCEPTION_HANDLER_PROPERTY, ExceptionHandler.class);
98+
return LoaderUtil.newCheckedInstanceOfProperty(
99+
LOGGER_CONFIG_EXCEPTION_HANDLER_PROPERTY,
100+
ExceptionHandler.class,
101+
AsyncLoggerConfigDefaultExceptionHandler::new);
105102
} catch (final ReflectiveOperationException e) {
106103
LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e);
104+
return new AsyncLoggerConfigDefaultExceptionHandler();
107105
}
108-
if (handler != null) {
109-
return handler;
110-
}
111-
return new AsyncLoggerConfigDefaultExceptionHandler();
112106
}
113107

114108
/**

log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,18 @@ public class LoggerConfig extends AbstractFilterable implements LocationAware {
8282

8383
static {
8484
try {
85-
LOG_EVENT_FACTORY =
86-
LoaderUtil.newCheckedInstanceOfProperty(Constants.LOG4J_LOG_EVENT_FACTORY, LogEventFactory.class);
85+
LOG_EVENT_FACTORY = LoaderUtil.newCheckedInstanceOfProperty(
86+
Constants.LOG4J_LOG_EVENT_FACTORY,
87+
LogEventFactory.class,
88+
LoggerConfig::createDefaultLogEventFactory);
8789
} catch (final Exception ex) {
8890
LOGGER.error("Unable to create LogEventFactory: {}", ex.getMessage(), ex);
91+
LOG_EVENT_FACTORY = createDefaultLogEventFactory();
8992
}
90-
if (LOG_EVENT_FACTORY == null) {
91-
LOG_EVENT_FACTORY =
92-
Constants.ENABLE_THREADLOCALS ? new ReusableLogEventFactory() : new DefaultLogEventFactory();
93-
}
93+
}
94+
95+
private static LogEventFactory createDefaultLogEventFactory() {
96+
return Constants.ENABLE_THREADLOCALS ? new ReusableLogEventFactory() : new DefaultLogEventFactory();
9497
}
9598

9699
@PluginBuilderFactory

log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,15 @@ public class ContextDataInjectorFactory {
6767
* @see ContextDataInjector
6868
*/
6969
public static ContextDataInjector createInjector() {
70-
ContextDataInjector injector = null;
7170
try {
72-
injector =
73-
LoaderUtil.newCheckedInstanceOfProperty(CONTEXT_DATA_INJECTOR_PROPERTY, ContextDataInjector.class);
71+
return LoaderUtil.newCheckedInstanceOfProperty(
72+
CONTEXT_DATA_INJECTOR_PROPERTY,
73+
ContextDataInjector.class,
74+
ContextDataInjectorFactory::createDefaultInjector);
7475
} catch (final ReflectiveOperationException e) {
7576
StatusLogger.getLogger().warn("Could not create ContextDataInjector: {}", e.getMessage(), e);
77+
return createDefaultInjector();
7678
}
77-
if (injector != null) {
78-
return injector;
79-
}
80-
return createDefaultInjector();
8179
}
8280

8381
private static ContextDataInjector createDefaultInjector() {

0 commit comments

Comments
 (0)