package org.apache.logging.log4j.util;

import java.security.AccessController;
import java.security.PrivilegedAction;

public final class LoaderUtil {
   private static final PrivilegedAction<ClassLoader> TCCL_GETTER = new LoaderUtil.ThreadContextClassLoaderGetter();

   private LoaderUtil() {
   }

   public static ClassLoader getThreadContextClassLoader() {
      return System.getSecurityManager() == null ? (ClassLoader)TCCL_GETTER.run() : (ClassLoader)AccessController.doPrivileged(TCCL_GETTER);
   }

   static {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
         sm.checkPermission(new RuntimePermission("getClassLoader"));
      }

   }

   private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader> {
      private ThreadContextClassLoaderGetter() {
      }

      public ClassLoader run() {
         ClassLoader cl = Thread.currentThread().getContextClassLoader();
         return cl == null ? ClassLoader.getSystemClassLoader() : cl;
      }

      // $FF: synthetic method
      ThreadContextClassLoaderGetter(Object x0) {
         this();
      }
   }
}
