changeset 1714:a4f31836660a

6854954: Eliminate static dependency on java.awt.AWTPermission Reviewed-by: mullan, mchung, anthony
author alanb
date Mon, 05 Oct 2009 16:45:55 +0100
parents 97f17e6d0560
children 54118c8e0ebe
files src/share/classes/java/awt/Dialog.java src/share/classes/java/awt/MouseInfo.java src/share/classes/java/awt/Robot.java src/share/classes/java/awt/SystemTray.java src/share/classes/java/awt/Toolkit.java src/share/classes/java/awt/Window.java src/share/classes/java/lang/SecurityManager.java src/share/classes/javax/swing/JPopupMenu.java src/share/classes/sun/awt/AWTPermissionFactory.java src/share/classes/sun/security/provider/PolicyFile.java src/share/classes/sun/security/util/PermissionFactory.java src/share/classes/sun/security/util/SecurityConstants.java
diffstat 12 files changed, 200 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/Dialog.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/awt/Dialog.java	Mon Oct 05 16:45:55 2009 +0100
@@ -856,7 +856,7 @@
         if (type == ModalityType.TOOLKIT_MODAL) {
             SecurityManager sm = System.getSecurityManager();
             if (sm != null) {
-                sm.checkPermission(SecurityConstants.TOOLKIT_MODALITY_PERMISSION);
+                sm.checkPermission(SecurityConstants.AWT.TOOLKIT_MODALITY_PERMISSION);
             }
         }
         modalityType = type;
--- a/src/share/classes/java/awt/MouseInfo.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/awt/MouseInfo.java	Mon Oct 05 16:45:55 2009 +0100
@@ -76,7 +76,7 @@
 
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.WATCH_MOUSE_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.WATCH_MOUSE_PERMISSION);
         }
 
         Point point = new Point(0, 0);
--- a/src/share/classes/java/awt/Robot.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/awt/Robot.java	Mon Oct 05 16:45:55 2009 +0100
@@ -167,7 +167,7 @@
     private void checkRobotAllowed() {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.CREATE_ROBOT_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.CREATE_ROBOT_PERMISSION);
         }
     }
 
@@ -466,7 +466,7 @@
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
             security.checkPermission(
-                SecurityConstants.READ_DISPLAY_PIXELS_PERMISSION);
+                SecurityConstants.AWT.READ_DISPLAY_PIXELS_PERMISSION);
         }
     }
 
--- a/src/share/classes/java/awt/SystemTray.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/awt/SystemTray.java	Mon Oct 05 16:45:55 2009 +0100
@@ -490,7 +490,7 @@
     static void checkSystemTrayAllowed() {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.ACCESS_SYSTEM_TRAY_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.ACCESS_SYSTEM_TRAY_PERMISSION);
         }
     }
 
--- a/src/share/classes/java/awt/Toolkit.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/awt/Toolkit.java	Mon Oct 05 16:45:55 2009 +0100
@@ -2025,7 +2025,7 @@
         }
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-          security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
+          security.checkPermission(SecurityConstants.AWT.ALL_AWT_EVENTS_PERMISSION);
         }
         synchronized (this) {
             SelectiveAWTEventListener selectiveListener =
@@ -2094,7 +2094,7 @@
         }
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.ALL_AWT_EVENTS_PERMISSION);
         }
 
         synchronized (this) {
@@ -2165,7 +2165,7 @@
     public AWTEventListener[] getAWTEventListeners() {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.ALL_AWT_EVENTS_PERMISSION);
         }
         synchronized (this) {
             EventListener[] la = ToolkitEventMulticaster.getListeners(eventListener,AWTEventListener.class);
@@ -2217,7 +2217,7 @@
     public AWTEventListener[] getAWTEventListeners(long eventMask) {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.ALL_AWT_EVENTS_PERMISSION);
         }
         synchronized (this) {
             EventListener[] la = ToolkitEventMulticaster.getListeners(eventListener,AWTEventListener.class);
--- a/src/share/classes/java/awt/Window.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/awt/Window.java	Mon Oct 05 16:45:55 2009 +0100
@@ -1581,7 +1581,7 @@
         if (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE) {
             SecurityManager sm = System.getSecurityManager();
             if (sm != null) {
-                sm.checkPermission(SecurityConstants.TOOLKIT_MODALITY_PERMISSION);
+                sm.checkPermission(SecurityConstants.AWT.TOOLKIT_MODALITY_PERMISSION);
             }
         }
         modalExclusionType = exclusionType;
@@ -2129,7 +2129,7 @@
     public final void setAlwaysOnTop(boolean alwaysOnTop) throws SecurityException {
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
-            security.checkPermission(SecurityConstants.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
+            security.checkPermission(SecurityConstants.AWT.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
         }
 
         boolean oldAlwaysOnTop;
--- a/src/share/classes/java/lang/SecurityManager.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/java/lang/SecurityManager.java	Mon Oct 05 16:45:55 2009 +0100
@@ -1341,7 +1341,7 @@
             throw new NullPointerException("window can't be null");
         }
         try {
-            checkPermission(SecurityConstants.TOPLEVEL_WINDOW_PERMISSION);
+            checkPermission(SecurityConstants.AWT.TOPLEVEL_WINDOW_PERMISSION);
             return true;
         } catch (SecurityException se) {
             // just return false
@@ -1391,7 +1391,7 @@
      * @see        #checkPermission(java.security.Permission) checkPermission
      */
     public void checkSystemClipboardAccess() {
-        checkPermission(SecurityConstants.ACCESS_CLIPBOARD_PERMISSION);
+        checkPermission(SecurityConstants.AWT.ACCESS_CLIPBOARD_PERMISSION);
     }
 
     /**
@@ -1412,7 +1412,7 @@
      * @see        #checkPermission(java.security.Permission) checkPermission
      */
     public void checkAwtEventQueueAccess() {
-        checkPermission(SecurityConstants.CHECK_AWT_EVENTQUEUE_PERMISSION);
+        checkPermission(SecurityConstants.AWT.CHECK_AWT_EVENTQUEUE_PERMISSION);
     }
 
     /*
--- a/src/share/classes/javax/swing/JPopupMenu.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/javax/swing/JPopupMenu.java	Mon Oct 05 16:45:55 2009 +0100
@@ -412,7 +412,7 @@
             SecurityManager sm = System.getSecurityManager();
             if (sm != null) {
                 sm.checkPermission(
-                        SecurityConstants.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
+                    SecurityConstants.AWT.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
             }
         } catch (SecurityException se) {
             // There is no permission to show popups over the task bar
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/awt/AWTPermissionFactory.java	Mon Oct 05 16:45:55 2009 +0100
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package sun.awt;
+
+import java.awt.AWTPermission;
+import sun.security.util.PermissionFactory;
+
+/**
+ * A factory object for AWTPermission objects.
+ */
+
+public class AWTPermissionFactory
+    implements PermissionFactory<AWTPermission>
+{
+    @Override
+    public AWTPermission newPermission(String name) {
+        return new AWTPermission(name);
+    }
+}
--- a/src/share/classes/sun/security/provider/PolicyFile.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/sun/security/provider/PolicyFile.java	Mon Oct 05 16:45:55 2009 +0100
@@ -54,7 +54,6 @@
 import java.net.NetPermission;
 import java.util.PropertyPermission;
 import java.util.concurrent.atomic.AtomicReference;
-import java.awt.AWTPermission;
 /*
 import javax.security.auth.AuthPermission;
 import javax.security.auth.kerberos.ServicePermission;
@@ -1023,8 +1022,6 @@
             return new NetPermission(name, actions);
         } else if (claz.equals(AllPermission.class)) {
             return SecurityConstants.ALL_PERMISSION;
-        } else if (claz.equals(AWTPermission.class)) {
-            return new AWTPermission(name, actions);
 /*
         } else if (claz.equals(ReflectPermission.class)) {
             return new ReflectPermission(name, actions);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/sun/security/util/PermissionFactory.java	Mon Oct 05 16:45:55 2009 +0100
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package sun.security.util;
+
+import java.security.Permission;
+
+/**
+ * A factory object that creates Permission objects.
+ */
+
+public interface PermissionFactory<T extends Permission> {
+    T newPermission(String name);
+}
--- a/src/share/classes/sun/security/util/SecurityConstants.java	Sun Oct 04 15:42:00 2009 +0100
+++ b/src/share/classes/sun/security/util/SecurityConstants.java	Mon Oct 05 16:45:55 2009 +0100
@@ -25,12 +25,12 @@
 
 package sun.security.util;
 
-import java.io.FilePermission;
-import java.awt.AWTPermission;
-import java.util.PropertyPermission;
-import java.lang.RuntimePermission;
 import java.net.SocketPermission;
 import java.net.NetPermission;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.Permission;
+import java.security.BasicPermission;
 import java.security.SecurityPermission;
 import java.security.AllPermission;
 import javax.security.auth.AuthPermission;
@@ -71,45 +71,118 @@
     // sun.security.provider.PolicyFile
     public static final AllPermission ALL_PERMISSION = new AllPermission();
 
-    // java.lang.SecurityManager
-    public static final AWTPermission TOPLEVEL_WINDOW_PERMISSION =
-        new AWTPermission("showWindowWithoutWarningBanner");
+    /**
+     * Permission type used when AWT is not present.
+     */
+    private static class FakeAWTPermission extends BasicPermission {
+        private static final long serialVersionUID = -1L;
+        public FakeAWTPermission(String name) {
+            super(name);
+        }
+        public String toString() {
+            return "(\"java.awt.AWTPermission\" \"" + getName() + "\")";
+        }
+    }
 
-    // java.lang.SecurityManager
-    public static final AWTPermission ACCESS_CLIPBOARD_PERMISSION =
-        new AWTPermission("accessClipboard");
+    /**
+     * Permission factory used when AWT is not present.
+     */
+    private static class FakeAWTPermissionFactory
+        implements PermissionFactory<FakeAWTPermission>
+    {
+        @Override
+        public FakeAWTPermission newPermission(String name) {
+            return new FakeAWTPermission(name);
+        }
+    }
 
-    // java.lang.SecurityManager
-    public static final AWTPermission CHECK_AWT_EVENTQUEUE_PERMISSION =
-        new AWTPermission("accessEventQueue");
+    /**
+     * AWT Permissions used in the JDK.
+     */
+    public static class AWT {
+        private AWT() { }
 
-    // java.awt.Dialog
-    public static final AWTPermission TOOLKIT_MODALITY_PERMISSION =
-        new AWTPermission("toolkitModality");
+        /**
+         * The class name of the factory to create java.awt.AWTPermission objects.
+         */
+        private static final String AWTFactory = "sun.awt.AWTPermissionFactory";
 
-    // java.awt.Robot
-    public static final AWTPermission READ_DISPLAY_PIXELS_PERMISSION =
-        new AWTPermission("readDisplayPixels");
+        /**
+         * The PermissionFactory to create AWT permissions (or fake permissions
+         * if AWT is not present).
+         */
+        private static final PermissionFactory<?> factory = permissionFactory();
 
-    // java.awt.Robot
-    public static final AWTPermission CREATE_ROBOT_PERMISSION =
-        new AWTPermission("createRobot");
+        private static PermissionFactory<?> permissionFactory() {
+            Class<?> c = AccessController
+                .doPrivileged(new PrivilegedAction<Class<?>>() {
+                    public Class<?> run() {
+                        try {
+                           return Class.forName(AWTFactory, true, null);
+                        } catch (ClassNotFoundException e) {
+                            // not available
+                            return null;
+                        }
+                    }});
+            if (c != null) {
+                // AWT present
+                try {
+                    return (PermissionFactory<?>)c.newInstance();
+                } catch (InstantiationException x) {
+                    throw new InternalError(x.getMessage());
+                } catch (IllegalAccessException x) {
+                    throw new InternalError(x.getMessage());
+                }
+            } else {
+                // AWT not present
+                return new FakeAWTPermissionFactory();
+            }
+        }
 
-    // java.awt.MouseInfo
-    public static final AWTPermission WATCH_MOUSE_PERMISSION =
-        new AWTPermission("watchMousePointer");
+        private static Permission newAWTPermission(String name) {
+            return factory.newPermission(name);
+        }
 
-    // java.awt.Window
-    public static final AWTPermission SET_WINDOW_ALWAYS_ON_TOP_PERMISSION =
-        new AWTPermission("setWindowAlwaysOnTop");
+        // java.lang.SecurityManager
+        public static final Permission TOPLEVEL_WINDOW_PERMISSION =
+            newAWTPermission("showWindowWithoutWarningBanner");
 
-    // java.awt.Toolkit
-    public static final AWTPermission ALL_AWT_EVENTS_PERMISSION =
-        new AWTPermission("listenToAllAWTEvents");
+        // java.lang.SecurityManager
+        public static final Permission ACCESS_CLIPBOARD_PERMISSION =
+            newAWTPermission("accessClipboard");
 
-    // java.awt.SystemTray
-    public static final AWTPermission ACCESS_SYSTEM_TRAY_PERMISSION =
-        new AWTPermission("accessSystemTray");
+        // java.lang.SecurityManager
+        public static final Permission CHECK_AWT_EVENTQUEUE_PERMISSION =
+            newAWTPermission("accessEventQueue");
+
+        // java.awt.Dialog
+        public static final Permission TOOLKIT_MODALITY_PERMISSION =
+            newAWTPermission("toolkitModality");
+
+        // java.awt.Robot
+        public static final Permission READ_DISPLAY_PIXELS_PERMISSION =
+            newAWTPermission("readDisplayPixels");
+
+        // java.awt.Robot
+        public static final Permission CREATE_ROBOT_PERMISSION =
+            newAWTPermission("createRobot");
+
+        // java.awt.MouseInfo
+        public static final Permission WATCH_MOUSE_PERMISSION =
+            newAWTPermission("watchMousePointer");
+
+        // java.awt.Window
+        public static final Permission SET_WINDOW_ALWAYS_ON_TOP_PERMISSION =
+            newAWTPermission("setWindowAlwaysOnTop");
+
+        // java.awt.Toolkit
+        public static final Permission ALL_AWT_EVENTS_PERMISSION =
+            newAWTPermission("listenToAllAWTEvents");
+
+        // java.awt.SystemTray
+        public static final Permission ACCESS_SYSTEM_TRAY_PERMISSION =
+            newAWTPermission("accessSystemTray");
+    }
 
     // java.net.URL
     public static final NetPermission SPECIFY_HANDLER_PERMISSION =