Skip to content

Commit

Permalink
Add default value to newCheckedInstanceOfProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Nov 14, 2023
1 parent f9188c5 commit ab48da9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.function.Supplier;

/**
* <em>Consider this class private.</em> Utility class for ClassLoaders.
Expand Down Expand Up @@ -328,11 +329,37 @@ public static <T> T newInstanceOf(final String className) throws ClassNotFoundEx
* @since 2.5
*/
public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz)
throws ClassNotFoundException, InvocationTargetException, InstantiationException,
IllegalAccessException, NoSuchMethodException {
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException,
NoSuchMethodException {
return newCheckedInstanceOfProperty(propertyName, clazz, () -> null);
}

/**
* Loads and instantiates a class given by a property name.
*
* @param propertyName The property name to look up a class name for.
* @param clazz The class to cast it to.
* @param defaultSupplier Supplier of a default value if the property is not present.
* @param <T> The type to cast it to.
* @return new instance of the class given in the property or {@code null} if the property was unset.
* @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders
* @throws ExceptionInInitializerError if an exception was thrown while initializing the class
* @throws LinkageError if the linkage of the class fails for any other reason
* @throws ClassCastException if the class is not compatible with the generic type parameter provided
* @throws NoSuchMethodException if no zero-arg constructor exists
* @throws SecurityException if this class is not allowed to access declared members of the provided class
* @throws IllegalAccessException if the class can't be instantiated through a public constructor
* @throws InstantiationException if the provided class is abstract or an interface
* @throws InvocationTargetException if an exception is thrown by the constructor
* @since 2.22
*/
public static <T> T newCheckedInstanceOfProperty(
final String propertyName, final Class<T> clazz, final Supplier<T> defaultSupplier)
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException,
NoSuchMethodException {
final String className = PropertiesUtil.getProperties().getStringProperty(propertyName);
if (className == null) {
return null;
return defaultSupplier.get();
}
return newCheckedInstanceOf(className, clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,25 @@ static int calculateRingBufferSize(final String propertyName) {
}

static ExceptionHandler<RingBufferLogEvent> getAsyncLoggerExceptionHandler() {
ExceptionHandler<RingBufferLogEvent> handler = null;
try {
handler =
LoaderUtil.newCheckedInstanceOfProperty(LOGGER_EXCEPTION_HANDLER_PROPERTY, ExceptionHandler.class);
return LoaderUtil.newCheckedInstanceOfProperty(
LOGGER_EXCEPTION_HANDLER_PROPERTY, ExceptionHandler.class, AsyncLoggerDefaultExceptionHandler::new);
} catch (final ReflectiveOperationException e) {
LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e);
return new AsyncLoggerDefaultExceptionHandler();
}
if (handler != null) {
return handler;
}
return new AsyncLoggerDefaultExceptionHandler();
}

static ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> getAsyncLoggerConfigExceptionHandler() {
ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> handler = null;
try {
handler = LoaderUtil.newCheckedInstanceOfProperty(
LOGGER_CONFIG_EXCEPTION_HANDLER_PROPERTY, ExceptionHandler.class);
return LoaderUtil.newCheckedInstanceOfProperty(
LOGGER_CONFIG_EXCEPTION_HANDLER_PROPERTY,
ExceptionHandler.class,
AsyncLoggerConfigDefaultExceptionHandler::new);
} catch (final ReflectiveOperationException e) {
LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e);
return new AsyncLoggerConfigDefaultExceptionHandler();
}
if (handler != null) {
return handler;
}
return new AsyncLoggerConfigDefaultExceptionHandler();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,18 @@ public class LoggerConfig extends AbstractFilterable implements LocationAware {

static {
try {
LOG_EVENT_FACTORY =
LoaderUtil.newCheckedInstanceOfProperty(Constants.LOG4J_LOG_EVENT_FACTORY, LogEventFactory.class);
LOG_EVENT_FACTORY = LoaderUtil.newCheckedInstanceOfProperty(
Constants.LOG4J_LOG_EVENT_FACTORY,
LogEventFactory.class,
LoggerConfig::createDefaultLogEventFactory);
} catch (final Exception ex) {
LOGGER.error("Unable to create LogEventFactory: {}", ex.getMessage(), ex);
LOG_EVENT_FACTORY = createDefaultLogEventFactory();
}
if (LOG_EVENT_FACTORY == null) {
LOG_EVENT_FACTORY =
Constants.ENABLE_THREADLOCALS ? new ReusableLogEventFactory() : new DefaultLogEventFactory();
}
}

private static LogEventFactory createDefaultLogEventFactory() {
return Constants.ENABLE_THREADLOCALS ? new ReusableLogEventFactory() : new DefaultLogEventFactory();
}

@PluginBuilderFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ public class ContextDataInjectorFactory {
* @see ContextDataInjector
*/
public static ContextDataInjector createInjector() {
ContextDataInjector injector = null;
try {
injector =
LoaderUtil.newCheckedInstanceOfProperty(CONTEXT_DATA_INJECTOR_PROPERTY, ContextDataInjector.class);
return LoaderUtil.newCheckedInstanceOfProperty(
CONTEXT_DATA_INJECTOR_PROPERTY,
ContextDataInjector.class,
ContextDataInjectorFactory::createDefaultInjector);
} catch (final ReflectiveOperationException e) {
StatusLogger.getLogger().warn("Could not create ContextDataInjector: {}", e.getMessage(), e);
return createDefaultInjector();
}
if (injector != null) {
return injector;
}
return createDefaultInjector();
}

private static ContextDataInjector createDefaultInjector() {
Expand Down

0 comments on commit ab48da9

Please sign in to comment.