http://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties

The current value is indeed the TCL version. I just migrated it. Perhaps
both properties should be supported?

On 2 September 2014 10:32, Gary Gregory <[email protected]> wrote:

> WRT:
>
>
>
> +    public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL";
>
> Should this be:
>
>
> +    public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCCL";
>
> ?
>
> Gary
>
> ---------- Forwarded message ----------
> From: <[email protected]>
> Date: Tue, Sep 2, 2014 at 12:23 AM
> Subject: [2/4] git commit: Migrate more methods from Loader to LoaderUtil.
> To: [email protected]
>
>
> Migrate more methods from Loader to LoaderUtil.
>
>
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit:
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2a962372
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2a962372
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2a962372
>
> Branch: refs/heads/LOG4J2-608
> Commit: 2a96237253823285c90f0c97e4ea4070c6522117
> Parents: 77be263
> Author: Matt Sicker <[email protected]>
> Authored: Mon Sep 1 22:40:29 2014 -0500
> Committer: Matt Sicker <[email protected]>
> Committed: Mon Sep 1 22:40:29 2014 -0500
>
> ----------------------------------------------------------------------
>  .../apache/logging/log4j/util/LoaderUtil.java   | 68 ++++++++++++++++++++
>  .../apache/logging/log4j/core/util/Loader.java  | 37 +----------
>  2 files changed, 71 insertions(+), 34 deletions(-)
> ----------------------------------------------------------------------
>
>
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2a962372/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 32b23c4..0771054 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
> @@ -16,6 +16,7 @@
>   */
>  package org.apache.logging.log4j.util;
>
> +import java.lang.reflect.InvocationTargetException;
>  import java.security.AccessController;
>  import java.security.PrivilegedAction;
>
> @@ -28,6 +29,10 @@ import java.security.PrivilegedAction;
>  public final class LoaderUtil {
>      private LoaderUtil() {}
>
> +    public static final String IGNORE_TCCL_PROPERTY = "log4j.ignoreTCL";
> +
> +    private static final boolean IGNORE_TCCL;
> +
>      private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new
> ThreadContextClassLoaderGetter();
>
>      static {
> @@ -35,6 +40,8 @@ public final class LoaderUtil {
>          if (sm != null) {
>              sm.checkPermission(new RuntimePermission("getClassLoader"));
>          }
> +        final String ignoreTccl =
> PropertiesUtil.getProperties().getStringProperty(IGNORE_TCCL_PROPERTY,
> null);
> +        IGNORE_TCCL = ignoreTccl != null &&
> !"false".equalsIgnoreCase(ignoreTccl.trim());
>      }
>
>      /**
> @@ -56,4 +63,65 @@ public final class LoaderUtil {
>              return cl == null ? ClassLoader.getSystemClassLoader() : cl;
>          }
>      }
> +
> +    /**
> +     * Loads a class by name. This method respects the {@link
> #IGNORE_TCCL_PROPERTY} Log4j property. If this property
> +     * is specified and set to anything besides {@code false}, then the
> default ClassLoader will be used.
> +     *
> +     * @param className The class name.
> +     * @return the Class for the given name.
> +     * @throws ClassNotFoundException if the specified class name could
> not be found
> +     */
> +    public static Class<?> loadClass(final String className) throws
> ClassNotFoundException {
> +        if (IGNORE_TCCL) {
> +            return Class.forName(className);
> +        }
> +        try {
> +            return getThreadContextClassLoader().loadClass(className);
> +        } catch (final Throwable e) {
> +            return Class.forName(className);
> +        }
> +    }
> +
> +    /**
> +     * Loads and instantiates a Class using the default constructor.
> +     *
> +     * @param className The class name.
> +     * @return new instance of the class.
> +     * @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
> +     */
> +    public static Object newInstanceOf(final String className)
> +        throws ClassNotFoundException, IllegalAccessException,
> InstantiationException, NoSuchMethodException,
> +        InvocationTargetException {
> +        final Class<?> clazz = loadClass(className);
> +        try {
> +            return clazz.getConstructor().newInstance();
> +        } catch (final NoSuchMethodException e) {
> +            return clazz.newInstance();
> +        }
> +    }
> +
> +    /**
> +     * Loads and instantiates a derived class using its default
> constructor.
> +     *
> +     * @param className The class name.
> +     * @param clazz The class to cast it to.
> +     * @param <T> The type of the class to check.
> +     * @return new instance of the class cast to {@code T}
> +     * @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}
> +     */
> +    public static <T> T newCheckedInstanceOf(final String className,
> final Class<T> clazz)
> +        throws ClassNotFoundException, NoSuchMethodException,
> InvocationTargetException, InstantiationException,
> +        IllegalAccessException {
> +        return clazz.cast(newInstanceOf(className));
> +    }
>  }
>
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2a962372/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
> ----------------------------------------------------------------------
> diff --git
> a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
> index 2fa70bb..9858d4e 100644
> ---
> a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
> +++
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java
> @@ -25,24 +25,17 @@ import java.net.URL;
>  import org.apache.logging.log4j.Logger;
>  import org.apache.logging.log4j.status.StatusLogger;
>  import org.apache.logging.log4j.util.LoaderUtil;
> -import org.apache.logging.log4j.util.PropertiesUtil;
>
>  /**
>   * Load resources (or images) from various sources.
>   */
>  public final class Loader {
>
> -    private static boolean ignoreTCL = false;
> -
>      private static final Logger LOGGER = StatusLogger.getLogger();
>
>      private static final String TSTR = "Caught Exception while in
> Loader.getResource. This may be innocuous.";
>
>      static {
> -        final String ignoreTCLProp =
> PropertiesUtil.getProperties().getStringProperty("log4j.ignoreTCL", null);
> -        if (ignoreTCLProp != null) {
> -            ignoreTCL = OptionConverter.toBoolean(ignoreTCLProp, true);
> -        }
>          final SecurityManager sm = System.getSecurityManager();
>          if (sm != null) {
>              sm.checkPermission(new RuntimePermission("getStackTrace"));
> @@ -55,7 +48,6 @@ public final class Loader {
>       * @return the ClassLoader.
>       */
>      public static ClassLoader getClassLoader() {
> -
>          return getClassLoader(Loader.class, null);
>      }
>
> @@ -248,23 +240,7 @@ public final class Loader {
>       * @throws ClassNotFoundException if the Class could not be found.
>       */
>      public static Class<?> loadClass(final String className) throws
> ClassNotFoundException {
> -        // Just call Class.forName(className) if we are instructed to
> ignore the TCL.
> -        if (ignoreTCL) {
> -            LOGGER.trace("Ignoring TCCL. Trying Class.forName({}).",
> className);
> -            return loadClassWithDefaultClassLoader(className);
> -        }
> -        try {
> -            LOGGER.trace("Trying TCCL for class {}.", className);
> -            // using the TCCL should work the same as the default
> ClassLoader (i.e., init or not)
> -            return Class.forName(className, true, getTcl());
> -        } catch (final Throwable e) {
> -            LOGGER.trace("TCCL didn't work for class {}: {}.", className,
> e.toString());
> -            return loadClassWithDefaultClassLoader(className);
> -        }
> -    }
> -
> -    private static Class<?> loadClassWithDefaultClassLoader(final String
> className) throws ClassNotFoundException {
> -        return Class.forName(className);
> +        return LoaderUtil.loadClass(className);
>      }
>
>      /**
> @@ -314,14 +290,7 @@ public final class Loader {
>                     InstantiationException,
>                     NoSuchMethodException,
>                     InvocationTargetException {
> -        final Class<?> clazz = loadClass(className);
> -        try {
> -            return clazz.getConstructor().newInstance();
> -        } catch (final NoSuchMethodException e) {
> -            // try the default-default constructor
> -            //noinspection ClassNewInstance
> -            return clazz.newInstance();
> -        }
> +        return LoaderUtil.newInstanceOf(className);
>      }
>
>      /**
> @@ -344,7 +313,7 @@ public final class Loader {
>                     IllegalAccessException,
>                     InvocationTargetException,
>                     InstantiationException {
> -        return clazz.cast(newInstanceOf(className));
> +        return LoaderUtil.newCheckedInstanceOf(className, clazz);
>      }
>
>      /**
>
>
>
>
> --
> E-Mail: [email protected] | [email protected]
> Java Persistence with Hibernate, Second Edition
> <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>



-- 
Matt Sicker <[email protected]>

Reply via email to