# HG changeset patch # User mchung # Date 1368826191 25200 # Node ID 25f39684638a843d9368bb4b68eaf170f009bbed # Parent ddb2c511b3390c4c82973f8e582b241b9f474a66 8014745: Provide a switch to allow stack walk search of resource bundle Reviewed-by: alanb, jgish diff -r ddb2c511b339 -r 25f39684638a make/java/java/mapfile-vers --- a/make/java/java/mapfile-vers Mon May 20 18:51:34 2013 +0400 +++ b/make/java/java/mapfile-vers Fri May 17 14:29:51 2013 -0700 @@ -266,6 +266,7 @@ Java_sun_reflect_NativeConstructorAccessorImpl_newInstance0; Java_sun_reflect_NativeMethodAccessorImpl_invoke0; Java_sun_reflect_Reflection_getCallerClass; + Java_sun_reflect_Reflection_getCallerClass0; Java_sun_reflect_Reflection_getClassAccessFlags; Java_sun_misc_Version_getJdkVersionInfo; Java_sun_misc_Version_getJdkSpecialVersion; diff -r ddb2c511b339 -r 25f39684638a src/share/classes/java/util/logging/Logger.java --- a/src/share/classes/java/util/logging/Logger.java Mon May 20 18:51:34 2013 +0400 +++ b/src/share/classes/java/util/logging/Logger.java Fri May 17 14:29:51 2013 -0700 @@ -309,8 +309,13 @@ // null, we assume it's a system logger and add it to the system context. // These system loggers only set the resource bundle to the given // resource bundle name (rather than the default system resource bundle). - private static class SystemLoggerHelper { - static boolean disableCallerCheck = getBooleanProperty("sun.util.logging.disableCallerCheck"); + private static class LoggerHelper { + static boolean disableCallerCheck = + getBooleanProperty("sun.util.logging.disableCallerCheck"); + + // workaround to turn on the old behavior for resource bundle search + static boolean allowStackWalkSearch = + getBooleanProperty("jdk.logging.allowStackWalkSearch"); private static boolean getBooleanProperty(final String key) { String s = AccessController.doPrivileged(new PrivilegedAction() { public String run() { @@ -324,7 +329,7 @@ private static Logger demandLogger(String name, String resourceBundleName, Class caller) { LogManager manager = LogManager.getLogManager(); SecurityManager sm = System.getSecurityManager(); - if (sm != null && !SystemLoggerHelper.disableCallerCheck) { + if (sm != null && !LoggerHelper.disableCallerCheck) { if (caller.getClassLoader() == null) { return manager.demandSystemLogger(name, resourceBundleName); } @@ -1431,25 +1436,61 @@ if (useCallersClassLoader) { // Try with the caller's ClassLoader ClassLoader callersClassLoader = getCallersClassLoader(); - - if (callersClassLoader == null || callersClassLoader == cl) { - return null; + if (callersClassLoader != null && callersClassLoader != cl) { + try { + catalog = ResourceBundle.getBundle(name, currentLocale, + callersClassLoader); + catalogName = name; + catalogLocale = currentLocale; + return catalog; + } catch (MissingResourceException ex) { + } } + } - try { - catalog = ResourceBundle.getBundle(name, currentLocale, - callersClassLoader); - catalogName = name; - catalogLocale = currentLocale; - return catalog; - } catch (MissingResourceException ex) { - return null; // no luck - } + // If -Djdk.logging.allowStackWalkSearch=true is set, + // does stack walk to search for the resource bundle + if (LoggerHelper.allowStackWalkSearch) { + return findResourceBundleFromStack(name, currentLocale, cl); } else { return null; } } + /** + * This method will fail when running with a VM that enforces caller-sensitive + * methods and only allows to get the immediate caller. + */ + @CallerSensitive + private synchronized ResourceBundle findResourceBundleFromStack(String name, + Locale locale, + ClassLoader cl) + { + for (int ix = 0; ; ix++) { + Class clz = sun.reflect.Reflection.getCallerClass(ix); + if (clz == null) { + break; + } + ClassLoader cl2 = clz.getClassLoader(); + if (cl2 == null) { + cl2 = ClassLoader.getSystemClassLoader(); + } + if (cl == cl2) { + // We've already checked this classloader. + continue; + } + cl = cl2; + try { + catalog = ResourceBundle.getBundle(name, locale, cl); + catalogName = name; + catalogLocale = locale; + return catalog; + } catch (MissingResourceException ex) { + } + } + return null; + } + // Private utility method to initialize our one entry // resource bundle name cache and the callers ClassLoader // Note: for consistency reasons, we are careful to check diff -r ddb2c511b339 -r 25f39684638a src/share/classes/sun/reflect/Reflection.java --- a/src/share/classes/sun/reflect/Reflection.java Mon May 20 18:51:34 2013 +0400 +++ b/src/share/classes/sun/reflect/Reflection.java Fri May 17 14:29:51 2013 -0700 @@ -58,6 +58,21 @@ @CallerSensitive public static native Class getCallerClass(); + /** + * @deprecated No replacement. This method will be removed in the next + * JDK 7 update release. + */ + @Deprecated + @CallerSensitive + public static Class getCallerClass(int depth) { + return getCallerClass0(depth); + } + + // If the VM enforces getting caller class with @CallerSensitive, + // this will fail anyway. + @CallerSensitive + private static native Class getCallerClass0(int depth); + /** Retrieves the access flags written to the class file. For inner classes these flags may differ from those returned by Class.getModifiers(), which searches the InnerClasses diff -r ddb2c511b339 -r 25f39684638a src/share/native/sun/reflect/Reflection.c --- a/src/share/native/sun/reflect/Reflection.c Mon May 20 18:51:34 2013 +0400 +++ b/src/share/native/sun/reflect/Reflection.c Fri May 17 14:29:51 2013 -0700 @@ -34,6 +34,12 @@ return JVM_GetCallerClass(env, 2); } +JNIEXPORT jclass JNICALL Java_sun_reflect_Reflection_getCallerClass0 +(JNIEnv *env, jclass unused, jint depth) +{ + return JVM_GetCallerClass(env, depth); +} + JNIEXPORT jint JNICALL Java_sun_reflect_Reflection_getClassAccessFlags (JNIEnv *env, jclass unused, jclass cls) { diff -r ddb2c511b339 -r 25f39684638a test/java/util/logging/bundlesearch/ResourceBundleSearchTest.java --- a/test/java/util/logging/bundlesearch/ResourceBundleSearchTest.java Mon May 20 18:51:34 2013 +0400 +++ b/test/java/util/logging/bundlesearch/ResourceBundleSearchTest.java Fri May 17 14:29:51 2013 -0700 @@ -28,6 +28,7 @@ * @author Jim Gish * @build ResourceBundleSearchTest IndirectlyLoadABundle LoadItUp1 LoadItUp2 TwiceIndirectlyLoadABundle LoadItUp2Invoker * @run main/othervm ResourceBundleSearchTest + * @run main/othervm -Djdk.logging.allowStackWalkSearch=true ResourceBundleSearchTest */ import java.net.URL; import java.net.URLClassLoader; @@ -79,7 +80,15 @@ // Test 1 - can we find a Logger bundle from doing a stack search? // We shouldn't be able to - assertFalse(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch"); + // unless -Djdk.logging.allowStackWalkSearch=true is set + + boolean allowStackWalkSearch = Boolean.getBoolean("jdk.logging.allowStackWalkSearch"); + if (allowStackWalkSearch) { + assertTrue(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch"); + } else { + // default behavior + assertFalse(testGetBundleFromStackSearch(), "1-testGetBundleFromStackSearch"); + } // Test 2 - can we find a Logger bundle off of the Thread context class // loader? We should be able to. @@ -111,8 +120,10 @@ // Test 6 - first call getLogger("myLogger"). // Then call getLogger("myLogger","bundleName") from a different ClassLoader // Make sure we find the bundle - assertTrue(testGetBundleFromSecondCallersClassLoader(), - "6-testGetBundleFromSecondCallersClassLoader"); + if (!allowStackWalkSearch) { + assertTrue(testGetBundleFromSecondCallersClassLoader(), + "6-testGetBundleFromSecondCallersClassLoader"); + } report(); }