8208702: javax/swing/reliability/HangDuringStaticInitialization.java may hang on macos
authorserb
Wed, 31 Oct 2018 10:01:23 -0700
changeset 52530 20e47d686221
parent 52529 755e08f972c3
child 52531 869b2c3823b7
8208702: javax/swing/reliability/HangDuringStaticInitialization.java may hang on macos Reviewed-by: prr, denis
src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java
src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m
test/jdk/javax/swing/reliability/HangDuringStaticInitialization.java
--- a/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Wed Oct 31 14:22:48 2018 +0530
+++ b/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Wed Oct 31 10:01:23 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. 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
@@ -25,11 +25,44 @@
 
 package sun.lwawt.macosx;
 
-import com.apple.laf.AquaMenuBarUI;
-import java.awt.peer.TaskbarPeer;
-import java.awt.*;
+import java.awt.AWTError;
+import java.awt.CheckboxMenuItem;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Cursor;
+import java.awt.Desktop;
+import java.awt.Dialog;
+import java.awt.Dimension;
+import java.awt.Event;
+import java.awt.EventQueue;
+import java.awt.FileDialog;
+import java.awt.Frame;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+import java.awt.HeadlessException;
+import java.awt.Image;
+import java.awt.Insets;
+import java.awt.Menu;
+import java.awt.MenuBar;
+import java.awt.MenuItem;
+import java.awt.Point;
+import java.awt.PopupMenu;
+import java.awt.RenderingHints;
+import java.awt.Robot;
+import java.awt.SystemTray;
+import java.awt.Taskbar;
+import java.awt.Toolkit;
+import java.awt.TrayIcon;
+import java.awt.Window;
 import java.awt.datatransfer.Clipboard;
-import java.awt.dnd.*;
+import java.awt.dnd.DragGestureEvent;
+import java.awt.dnd.DragGestureListener;
+import java.awt.dnd.DragGestureRecognizer;
+import java.awt.dnd.DragSource;
+import java.awt.dnd.DropTarget;
+import java.awt.dnd.InvalidDnDOperationException;
+import java.awt.dnd.MouseDragGestureRecognizer;
 import java.awt.dnd.peer.DragSourceContextPeer;
 import java.awt.event.InputEvent;
 import java.awt.event.InvocationEvent;
@@ -37,21 +70,54 @@
 import java.awt.font.TextAttribute;
 import java.awt.im.InputMethodHighlight;
 import java.awt.im.spi.InputMethodDescriptor;
-import java.awt.peer.*;
-import java.lang.reflect.*;
+import java.awt.peer.CheckboxMenuItemPeer;
+import java.awt.peer.DesktopPeer;
+import java.awt.peer.DialogPeer;
+import java.awt.peer.FileDialogPeer;
+import java.awt.peer.FontPeer;
+import java.awt.peer.MenuBarPeer;
+import java.awt.peer.MenuItemPeer;
+import java.awt.peer.MenuPeer;
+import java.awt.peer.PopupMenuPeer;
+import java.awt.peer.RobotPeer;
+import java.awt.peer.SystemTrayPeer;
+import java.awt.peer.TaskbarPeer;
+import java.awt.peer.TrayIconPeer;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.net.MalformedURLException;
 import java.net.URL;
-import java.security.*;
-import java.util.*;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.MissingResourceException;
+import java.util.Objects;
+import java.util.ResourceBundle;
 import java.util.concurrent.Callable;
-import java.net.MalformedURLException;
+
 import javax.swing.UIManager;
 
-import sun.awt.*;
+import com.apple.laf.AquaMenuBarUI;
+import sun.awt.AWTAccessor;
+import sun.awt.AppContext;
+import sun.awt.CGraphicsConfig;
+import sun.awt.CGraphicsDevice;
+import sun.awt.LightweightFrame;
+import sun.awt.SunToolkit;
 import sun.awt.datatransfer.DataTransferer;
 import sun.awt.util.ThreadGroupUtils;
 import sun.java2d.opengl.OGLRenderQueue;
-import sun.lwawt.*;
+import sun.lwawt.LWComponentPeer;
+import sun.lwawt.LWCursorManager;
+import sun.lwawt.LWToolkit;
+import sun.lwawt.LWWindowPeer;
 import sun.lwawt.LWWindowPeer.PeerType;
+import sun.lwawt.PlatformComponent;
+import sun.lwawt.PlatformDropTarget;
+import sun.lwawt.PlatformWindow;
+import sun.lwawt.SecurityWarningWindow;
 import sun.security.action.GetBooleanAction;
 
 @SuppressWarnings("serial") // JDK implementation class
@@ -97,6 +163,10 @@
             }
         });
 
+        if (!GraphicsEnvironment.isHeadless() && !isInAquaSession()) {
+            throw new AWTError("WindowServer is not available");
+        }
+
         AWTAccessor.getToolkitAccessor().setPlatformResources(platformResources);
 
         if (!GraphicsEnvironment.isHeadless()) {
@@ -794,6 +864,13 @@
      */
     public static native boolean isEmbedded();
 
+    /**
+     * Returns true if the WindowServer is available, false otherwise.
+     *
+     * @return true if the WindowServer is available, false otherwise
+     */
+    private static native boolean isInAquaSession();
+
     /*
      * Activates application ignoring other apps.
      */
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m	Wed Oct 31 14:22:48 2018 +0530
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/LWCToolkit.m	Wed Oct 31 10:01:23 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. 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
@@ -84,7 +84,7 @@
     if ([event type] != NSScrollWheel) {
         return 0;
     }
-    
+
     if ([event phase]) {
         // process a phase of manual scrolling
         switch ([event phase]) {
@@ -804,3 +804,30 @@
     return isEmbedded ? JNI_TRUE : JNI_FALSE;
 }
 
+/*
+ * Class:     sun_lwawt_macosx_LWCToolkit
+ * Method:    isInAquaSession
+ * Signature: ()Z
+ */
+JNIEXPORT jboolean JNICALL
+Java_sun_lwawt_macosx_LWCToolkit_isInAquaSession
+(JNIEnv *env, jclass klass) {
+    // copied from java.base/macosx/native/libjava/java_props_macosx.c
+    // environment variable to bypass the aqua session check
+    char *ev = getenv("AWT_FORCE_HEADFUL");
+    if (ev && (strncasecmp(ev, "true", 4) == 0)) {
+        // if "true" then tell the caller we're in an Aqua session without
+        // actually checking
+        return JNI_TRUE;
+    }
+    // Is the WindowServer available?
+    SecuritySessionId session_id;
+    SessionAttributeBits session_info;
+    OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info);
+    if (status == noErr) {
+        if (session_info & sessionHasGraphicAccess) {
+            return JNI_TRUE;
+        }
+    }
+    return JNI_FALSE;
+}
--- a/test/jdk/javax/swing/reliability/HangDuringStaticInitialization.java	Wed Oct 31 14:22:48 2018 +0530
+++ b/test/jdk/javax/swing/reliability/HangDuringStaticInitialization.java	Wed Oct 31 10:01:23 2018 -0700
@@ -32,7 +32,7 @@
 
 /**
  * @test
- * @bug 8189604
+ * @bug 8189604 8208702
  * @run main/othervm -Djava.awt.headless=false HangDuringStaticInitialization
  * @run main/othervm -Djava.awt.headless=true HangDuringStaticInitialization
  */
@@ -63,4 +63,4 @@
             }
         });
     }
-}
\ No newline at end of file
+}