From 1b3374bccebd8eb4134f7bc80a2659ba7fe8c1f0 Mon Sep 17 00:00:00 2001 From: Roman Kennke Date: Wed, 22 Jul 2009 15:52:41 +0200 Subject: [PATCH] 6857358: Two testcases are failing in jdk7 b64 pit build with java.security.AccessControlException Try to load GraphicsEnvironment with bootclassloader first, only then try app classloader. Reviewed-by: prr, tdv, igor --- .../classes/java/awt/GraphicsEnvironment.java | 66 ++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/jdk/src/share/classes/java/awt/GraphicsEnvironment.java b/jdk/src/share/classes/java/awt/GraphicsEnvironment.java index d77b2f22b8b..6b6afb353d4 100644 --- a/jdk/src/share/classes/java/awt/GraphicsEnvironment.java +++ b/jdk/src/share/classes/java/awt/GraphicsEnvironment.java @@ -27,9 +27,11 @@ package java.awt; import java.awt.image.BufferedImage; +import java.security.AccessController; import java.util.Locale; import sun.java2d.HeadlessGraphicsEnvironment; import sun.java2d.SunGraphicsEnvironment; +import sun.security.action.GetPropertyAction; /** * @@ -73,34 +75,52 @@ public abstract class GraphicsEnvironment { */ public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() { if (localEnv == null) { - String nm = (String) java.security.AccessController.doPrivileged - (new sun.security.action.GetPropertyAction - ("java.awt.graphicsenv", null)); - - try { -// long t0 = System.currentTimeMillis(); - ClassLoader cl = ClassLoader.getSystemClassLoader(); - Class geCls = Class.forName(nm, true, cl); - localEnv = (GraphicsEnvironment)geCls.newInstance(); -// long t1 = System.currentTimeMillis(); -// System.out.println("GE creation took " + (t1-t0)+ "ms."); - if (isHeadless()) { - localEnv = new HeadlessGraphicsEnvironment(localEnv); - } - } catch (ClassNotFoundException e) { - throw new Error("Could not find class: "+nm); - } catch (InstantiationException e) { - throw new Error("Could not instantiate Graphics Environment: " - + nm); - } catch (IllegalAccessException e) { - throw new Error ("Could not access Graphics Environment: " - + nm); - } + localEnv = createGE(); } return localEnv; } + /** + * Creates and returns the GraphicsEnvironment, according to the + * system property 'java.awt.graphicsenv'. + * + * @return the graphics environment + */ + private static GraphicsEnvironment createGE() { + GraphicsEnvironment ge; + String nm = AccessController.doPrivileged(new GetPropertyAction("java.awt.graphicsenv", null)); + try { +// long t0 = System.currentTimeMillis(); + Class geCls; + try { + // First we try if the bootclassloader finds the requested + // class. This way we can avoid to run in a privileged block. + geCls = Class.forName(nm); + } catch (ClassNotFoundException ex) { + // If the bootclassloader fails, we try again with the + // application classloader. + ClassLoader cl = ClassLoader.getSystemClassLoader(); + geCls = Class.forName(nm, true, cl); + } + ge = (GraphicsEnvironment) geCls.newInstance(); +// long t1 = System.currentTimeMillis(); +// System.out.println("GE creation took " + (t1-t0)+ "ms."); + if (isHeadless()) { + localEnv = new HeadlessGraphicsEnvironment(localEnv); + } + } catch (ClassNotFoundException e) { + throw new Error("Could not find class: "+nm); + } catch (InstantiationException e) { + throw new Error("Could not instantiate Graphics Environment: " + + nm); + } catch (IllegalAccessException e) { + throw new Error ("Could not access Graphics Environment: " + + nm); + } + return ge; + } + /** * Tests whether or not a display, keyboard, and mouse can be * supported in this environment. If this method returns true,