jdk/src/share/classes/java/util/logging/Logger.java
changeset 16906 44dfee24cb71
parent 16483 443a7e5f9b91
child 17487 77566e5979d1
equal deleted inserted replaced
16905:0419f45c7761 16906:44dfee24cb71
    34 import java.util.Locale;
    34 import java.util.Locale;
    35 import java.util.MissingResourceException;
    35 import java.util.MissingResourceException;
    36 import java.util.ResourceBundle;
    36 import java.util.ResourceBundle;
    37 import java.util.concurrent.CopyOnWriteArrayList;
    37 import java.util.concurrent.CopyOnWriteArrayList;
    38 import java.util.function.Supplier;
    38 import java.util.function.Supplier;
       
    39 import sun.reflect.CallerSensitive;
       
    40 import sun.reflect.Reflection;
    39 
    41 
    40 /**
    42 /**
    41  * A Logger object is used to log messages for a specific
    43  * A Logger object is used to log messages for a specific
    42  * system or application component.  Loggers are normally named,
    44  * system or application component.  Loggers are normally named,
    43  * using a hierarchical dot-separated namespace.  Logger names
    45  * using a hierarchical dot-separated namespace.  Logger names
   331             });
   333             });
   332             return Boolean.valueOf(s);
   334             return Boolean.valueOf(s);
   333         }
   335         }
   334     }
   336     }
   335 
   337 
   336     private static Logger demandLogger(String name, String resourceBundleName) {
   338     private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
   337         LogManager manager = LogManager.getLogManager();
   339         LogManager manager = LogManager.getLogManager();
   338         SecurityManager sm = System.getSecurityManager();
   340         SecurityManager sm = System.getSecurityManager();
   339         if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
   341         if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
   340             // 0: Reflection 1: Logger.demandLogger 2: Logger.getLogger 3: caller
       
   341             final int SKIP_FRAMES = 3;
       
   342             Class<?> caller = sun.reflect.Reflection.getCallerClass(SKIP_FRAMES);
       
   343             if (caller.getClassLoader() == null) {
   342             if (caller.getClassLoader() == null) {
   344                 return manager.demandSystemLogger(name, resourceBundleName);
   343                 return manager.demandSystemLogger(name, resourceBundleName);
   345             }
   344             }
   346         }
   345         }
   347         return manager.demandLogger(name, resourceBundleName);
   346         return manager.demandLogger(name, resourceBundleName);
   375      * @throws NullPointerException if the name is null.
   374      * @throws NullPointerException if the name is null.
   376      */
   375      */
   377 
   376 
   378     // Synchronization is not required here. All synchronization for
   377     // Synchronization is not required here. All synchronization for
   379     // adding a new Logger object is handled by LogManager.addLogger().
   378     // adding a new Logger object is handled by LogManager.addLogger().
       
   379     @CallerSensitive
   380     public static Logger getLogger(String name) {
   380     public static Logger getLogger(String name) {
   381         // This method is intentionally not a wrapper around a call
   381         // This method is intentionally not a wrapper around a call
   382         // to getLogger(name, resourceBundleName). If it were then
   382         // to getLogger(name, resourceBundleName). If it were then
   383         // this sequence:
   383         // this sequence:
   384         //
   384         //
   386         //     getLogger("Foo");
   386         //     getLogger("Foo");
   387         //
   387         //
   388         // would throw an IllegalArgumentException in the second call
   388         // would throw an IllegalArgumentException in the second call
   389         // because the wrapper would result in an attempt to replace
   389         // because the wrapper would result in an attempt to replace
   390         // the existing "resourceBundleForFoo" with null.
   390         // the existing "resourceBundleForFoo" with null.
   391         return demandLogger(name, null);
   391         return demandLogger(name, null, Reflection.getCallerClass());
   392     }
   392     }
   393 
   393 
   394     /**
   394     /**
   395      * Find or create a logger for a named subsystem.  If a logger has
   395      * Find or create a logger for a named subsystem.  If a logger has
   396      * already been created with the given name it is returned.  Otherwise
   396      * already been created with the given name it is returned.  Otherwise
   432      * @throws NullPointerException if the name is null.
   432      * @throws NullPointerException if the name is null.
   433      */
   433      */
   434 
   434 
   435     // Synchronization is not required here. All synchronization for
   435     // Synchronization is not required here. All synchronization for
   436     // adding a new Logger object is handled by LogManager.addLogger().
   436     // adding a new Logger object is handled by LogManager.addLogger().
       
   437     @CallerSensitive
   437     public static Logger getLogger(String name, String resourceBundleName) {
   438     public static Logger getLogger(String name, String resourceBundleName) {
   438         Logger result = demandLogger(name, resourceBundleName);
   439         Logger result = demandLogger(name, resourceBundleName, Reflection.getCallerClass());
   439 
   440 
   440         // MissingResourceException or IllegalArgumentException can be
   441         // MissingResourceException or IllegalArgumentException can be
   441         // thrown by setupResourceInfo().
   442         // thrown by setupResourceInfo().
   442         result.setupResourceInfo(resourceBundleName);
   443         result.setupResourceInfo(resourceBundleName);
   443         return result;
   444         return result;