Repository: logging-log4j2
Updated Branches:
  refs/heads/master 6028540ed -> a6c5ff217


Refactor common idiom into LoaderUtil from TODO.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a6c5ff21
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a6c5ff21
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a6c5ff21

Branch: refs/heads/master
Commit: a6c5ff2172c05eeaa1984a701dfed7e25a9b81e1
Parents: 6028540
Author: Matt Sicker <boa...@gmail.com>
Authored: Fri Oct 30 22:36:35 2015 -0500
Committer: Matt Sicker <boa...@gmail.com>
Committed: Fri Oct 30 22:36:35 2015 -0500

----------------------------------------------------------------------
 .../apache/logging/log4j/util/LoaderUtil.java   | 27 +++++++++++++++-
 .../log4j/core/impl/Log4jContextFactory.java    | 33 ++++++++++----------
 2 files changed, 42 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a6c5ff21/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
index e881a3a..bf08c53 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java
@@ -89,7 +89,7 @@ public final class LoaderUtil {
     }
 
     /**
-     * 
+     *
      */
     private static class ThreadContextClassLoaderGetter implements 
PrivilegedAction<ClassLoader> {
         @Override
@@ -167,6 +167,31 @@ public final class LoaderUtil {
         return clazz.cast(newInstanceOf(className));
     }
 
+    /**
+     * 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 <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 IllegalAccessException    if the class can't be instantiated 
through a public constructor
+     * @throws InstantiationException    if there was an exception whilst 
instantiating the class
+     * @throws NoSuchMethodException     if there isn't a no-args constructor 
on the class
+     * @throws InvocationTargetException if there was an exception whilst 
constructing the class
+     * @throws ClassCastException        if the constructed object isn't type 
compatible with {@code T}
+     * @since 2.5
+     */
+    public static <T> T newCheckedInstanceOfProperty(final String 
propertyName, final Class<T> clazz)
+        throws ClassNotFoundException, NoSuchMethodException, 
InvocationTargetException, InstantiationException,
+        IllegalAccessException {
+        final String property = 
PropertiesUtil.getProperties().getStringProperty(propertyName);
+        if (property == null) {
+            return null;
+        }
+        return newCheckedInstanceOf(propertyName, clazz);
+    }
+
     private static boolean isIgnoreTccl() {
         // we need to lazily initialize this, but concurrent access is not an 
issue
         if (ignoreTCCL == null) {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a6c5ff21/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
----------------------------------------------------------------------
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 f54fa2d..eaef598 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
@@ -29,10 +29,10 @@ import 
org.apache.logging.log4j.core.selector.ContextSelector;
 import org.apache.logging.log4j.core.util.Cancellable;
 import org.apache.logging.log4j.core.util.Constants;
 import org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry;
-import org.apache.logging.log4j.core.util.Loader;
 import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
 import org.apache.logging.log4j.spi.LoggerContextFactory;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LoaderUtil;
 import org.apache.logging.log4j.util.PropertiesUtil;
 
 /**
@@ -89,29 +89,28 @@ public class Log4jContextFactory implements 
LoggerContextFactory, ShutdownCallba
     }
 
     private static ContextSelector createContextSelector() {
-        final String sel = 
PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
-        if (sel != null) {
-            try {
-                return Loader.newCheckedInstanceOf(sel, ContextSelector.class);
-            } catch (final Exception ex) {
-                LOGGER.error("Unable to create context {}", sel, ex);
+        try {
+            final ContextSelector selector = 
LoaderUtil.newCheckedInstanceOfProperty(Constants.LOG4J_CONTEXT_SELECTOR,
+                ContextSelector.class);
+            if (selector != null) {
+                return selector;
             }
+        } catch (final Exception e) {
+            LOGGER.error("Unable to create custom ContextSelector. Falling 
back to default.", e);
         }
         return new ClassLoaderContextSelector();
     }
 
     private static ShutdownCallbackRegistry createShutdownCallbackRegistry() {
-        // TODO: this is such a common idiom it really deserves a utility 
method somewhere
-        final String registry = 
PropertiesUtil.getProperties().getStringProperty(
-            ShutdownCallbackRegistry.SHUTDOWN_CALLBACK_REGISTRY);
-        if (registry != null) {
-            try {
-                return Loader.newCheckedInstanceOf(registry, 
ShutdownCallbackRegistry.class);
-            } catch (final Exception e) {
-                LOGGER.error(SHUTDOWN_HOOK_MARKER,
-                    "There was an error loading the ShutdownCallbackRegistry 
[{}]. "
-                        + "Falling back to DefaultShutdownCallbackRegistry.", 
registry, e);
+        try {
+            final ShutdownCallbackRegistry registry = 
LoaderUtil.newCheckedInstanceOfProperty(
+                ShutdownCallbackRegistry.SHUTDOWN_CALLBACK_REGISTRY, 
ShutdownCallbackRegistry.class
+            );
+            if (registry != null) {
+                return registry;
             }
+        } catch (final Exception e) {
+            LOGGER.error("Unable to create custom ShutdownCallbackRegistry. 
Falling back to default.", e);
         }
         return new DefaultShutdownCallbackRegistry();
     }

Reply via email to