8222108: Reduce minRefreshTime for updating remote printer list on Windows
authoraivanov
Sun, 18 Aug 2019 21:36:01 +0100
changeset 57930 020f8fab32e2
parent 57929 13178f7e75d5
child 57931 39f133168348
8222108: Reduce minRefreshTime for updating remote printer list on Windows Reviewed-by: prr, serb
src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java
test/jdk/java/awt/print/RemotePrinterStatusRefresh/RemotePrinterStatusRefresh.java
--- a/src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java	Wed Aug 07 10:25:50 2019 +0200
+++ b/src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java	Sun Aug 18 21:36:01 2019 +0100
@@ -27,6 +27,8 @@
 
 import java.security.AccessController;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
 import javax.print.DocFlavor;
 import javax.print.MultiDocPrintService;
 import javax.print.PrintService;
@@ -47,9 +49,11 @@
     private PrintService defaultPrintService;
     private String[] printers; /* excludes the default printer */
     private PrintService[] printServices; /* includes the default printer */
-    private static boolean pollServices = true;
-    private static final int DEFAULT_MINREFRESH = 240;  // 4 minutes
-    private static int minRefreshTime = DEFAULT_MINREFRESH;
+
+    private static final int DEFAULT_REFRESH_TIME = 240;  // 4 minutes
+    private static final int MINIMUM_REFRESH_TIME = 120;  // 2 minutes
+    private static final boolean pollServices;
+    private static final int refreshTime;
 
     static {
         /* The system property "sun.java2d.print.polling"
@@ -58,12 +62,7 @@
          */
         String pollStr = java.security.AccessController.doPrivileged(
             new sun.security.action.GetPropertyAction("sun.java2d.print.polling"));
-
-        if (pollStr != null) {
-            if (pollStr.equalsIgnoreCase("false")) {
-                pollServices = false;
-            }
-        }
+        pollServices = !("false".equalsIgnoreCase(pollStr));
 
         /* The system property "sun.java2d.print.minRefreshTime"
          * can be used to specify minimum refresh time (in seconds)
@@ -72,17 +71,9 @@
         String refreshTimeStr = java.security.AccessController.doPrivileged(
             new sun.security.action.GetPropertyAction(
                 "sun.java2d.print.minRefreshTime"));
-
-        if (refreshTimeStr != null) {
-            try {
-                minRefreshTime = Integer.parseInt(refreshTimeStr);
-            } catch (NumberFormatException e) {
-                // ignore
-            }
-            if (minRefreshTime < DEFAULT_MINREFRESH) {
-                minRefreshTime = DEFAULT_MINREFRESH;
-            }
-        }
+        refreshTime = (refreshTimeStr != null)
+                      ? getRefreshTime(refreshTimeStr)
+                      : DEFAULT_REFRESH_TIME;
 
         java.security.AccessController.doPrivileged(
             new java.security.PrivilegedAction<Void>() {
@@ -93,6 +84,17 @@
             });
     }
 
+    private static int getRefreshTime(final String refreshTimeStr) {
+        try {
+            int minRefreshTime = Integer.parseInt(refreshTimeStr);
+            return (minRefreshTime < MINIMUM_REFRESH_TIME)
+                   ? MINIMUM_REFRESH_TIME
+                   : minRefreshTime;
+        } catch (NumberFormatException e) {
+            return DEFAULT_REFRESH_TIME;
+        }
+    }
+
     /* The singleton win32 print lookup service.
      * Code that is aware of this field and wants to use it must first
      * see if its null, and if so instantiate it by calling a method such as
@@ -398,60 +400,38 @@
        count of printer status changes(add\remove) and based on it update the printers
        list.
     */
-    class RemotePrinterChangeListener implements Runnable {
-        private String[] prevRemotePrinters;
+    class RemotePrinterChangeListener implements Comparator<String>, Runnable {
 
         RemotePrinterChangeListener() {
         }
 
-        private boolean doCompare(String[] str1, String[] str2) {
-            if (str1 == null && str2 == null) {
-                return false;
-            } else if (str1 == null || str2 == null) {
-                return true;
-            }
-
-            if (str1.length != str2.length) {
-                return true;
-            } else {
-                for (int i = 0; i < str1.length; i++) {
-                    for (int j = 0; j < str2.length; j++) {
-                        // skip if both are nulls
-                        if (str1[i] == null && str2[j] == null) {
-                            continue;
-                        }
-
-                        // return true if there is a 'difference' but
-                        // no need to access the individual string
-                        if (str1[i] == null || str2[j] == null) {
-                            return true;
-                        }
-
-                        // do comparison only if they are non-nulls
-                        if (!str1[i].equals(str2[j])) {
-                            return true;
-                        }
-                    }
-                }
-            }
-
-            return false;
+        @Override
+        public int compare(String o1, String o2) {
+            return ((o1 == null)
+                    ? ((o2 == null) ? 0 : 1)
+                    : ((o2 == null) ? -1 : o1.compareTo(o2)));
         }
 
         @Override
         public void run() {
             // Init the list of remote printers
-            prevRemotePrinters = getRemotePrintersNames();
+            String[] prevRemotePrinters = getRemotePrintersNames();
+            if (prevRemotePrinters != null) {
+                Arrays.sort(prevRemotePrinters, this);
+            }
 
             while (true) {
                 try {
-                    Thread.sleep(minRefreshTime * 1000);
+                    Thread.sleep(refreshTime * 1000);
                 } catch (InterruptedException e) {
                     break;
                 }
 
                 String[] currentRemotePrinters = getRemotePrintersNames();
-                if (doCompare(prevRemotePrinters, currentRemotePrinters)) {
+                if (currentRemotePrinters != null) {
+                    Arrays.sort(currentRemotePrinters, this);
+                }
+                if (!Arrays.equals(prevRemotePrinters, currentRemotePrinters)) {
                     // The list of remote printers got updated,
                     // so update the cached list printers which
                     // includes both local and network printers
--- a/test/jdk/java/awt/print/RemotePrinterStatusRefresh/RemotePrinterStatusRefresh.java	Wed Aug 07 10:25:50 2019 +0200
+++ b/test/jdk/java/awt/print/RemotePrinterStatusRefresh/RemotePrinterStatusRefresh.java	Sun Aug 18 21:36:01 2019 +0100
@@ -23,10 +23,10 @@
 
 /*
  * @test
- * @bug 8153732 8212202 8221263 8221412
+ * @bug 8153732 8212202 8221263 8221412 8222108
  * @requires (os.family == "Windows")
  * @summary Windows remote printer changes do not reflect in lookupPrintServices()
- * @run main/manual RemotePrinterStatusRefresh
+ * @run main/manual/othervm -Dsun.java2d.print.minRefreshTime=120 RemotePrinterStatusRefresh
  */
 
 import java.awt.BorderLayout;
@@ -63,6 +63,9 @@
 
 public class RemotePrinterStatusRefresh extends WindowAdapter {
 
+    private static final long DEFAULT_REFRESH_TIME = 240L;
+    private static final long MINIMAL_REFRESH_TIME = 120L;
+
     private static final long refreshTime = getRefreshTime();
 
     private static final long TIMEOUT = refreshTime * 4 + 60;
@@ -181,7 +184,7 @@
                     +          "configured printers.\n"
                     + "Step 1: Add or Remove a network printer using "
                     +          "Windows Control Panel.\n"
-                    + "Step 2: Wait for 4 minutes after adding or removing.\n"
+                    + "Step 2: Wait for 2\u20134 minutes after adding or removing.\n"
                     + "             \"Next printer refresh in\" gives you a "
                     +          "rough estimation on when update will happen.\n"
                     + "Step 3: Click Refresh."
@@ -195,7 +198,7 @@
                     + "Step 5: Click Pass if the list of printers is correctly "
                     +          "updated.\n"
                     + "Step 6: If the list is not updated, wait for another "
-                    +          "4 minutes, and then click Refresh again.\n"
+                    +          "2\u20134 minutes, and then click Refresh again.\n"
                     + "Step 7: If the list does not update, click Fail.\n"
                     + "\n"
                     + "You have to click Refresh to enable Pass and Fail buttons. "
@@ -215,12 +218,13 @@
 
     private static long getRefreshTime() {
         String refreshTime =
-                System.getProperty("sun.java2d.print.minRefreshTime", "240");
+                System.getProperty("sun.java2d.print.minRefreshTime",
+                                   Long.toString(DEFAULT_REFRESH_TIME));
         try {
             long value = Long.parseLong(refreshTime);
-            return value < 240L ? 240L : value;
+            return value < MINIMAL_REFRESH_TIME ? MINIMAL_REFRESH_TIME : value;
         } catch (NumberFormatException e) {
-            return 240L;
+            return DEFAULT_REFRESH_TIME;
         }
     }