27
27
28
28
import java .security .AccessController ;
29
29
import java .util .ArrayList ;
30
+ import java .util .Arrays ;
31
+ import java .util .Comparator ;
30
32
import javax .print .DocFlavor ;
31
33
import javax .print .MultiDocPrintService ;
32
34
import javax .print .PrintService ;
@@ -47,9 +49,11 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
47
49
private PrintService defaultPrintService ;
48
50
private String [] printers ; /* excludes the default printer */
49
51
private PrintService [] printServices ; /* includes the default printer */
50
- private static boolean pollServices = true ;
51
- private static final int DEFAULT_MINREFRESH = 240 ; // 4 minutes
52
- private static int minRefreshTime = DEFAULT_MINREFRESH ;
52
+
53
+ private static final int DEFAULT_REFRESH_TIME = 240 ; // 4 minutes
54
+ private static final int MINIMUM_REFRESH_TIME = 120 ; // 2 minutes
55
+ private static final boolean pollServices ;
56
+ private static final int refreshTime ;
53
57
54
58
static {
55
59
/* The system property "sun.java2d.print.polling"
@@ -58,12 +62,7 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
58
62
*/
59
63
String pollStr = java .security .AccessController .doPrivileged (
60
64
new sun .security .action .GetPropertyAction ("sun.java2d.print.polling" ));
61
-
62
- if (pollStr != null ) {
63
- if (pollStr .equalsIgnoreCase ("false" )) {
64
- pollServices = false ;
65
- }
66
- }
65
+ pollServices = !("false" .equalsIgnoreCase (pollStr ));
67
66
68
67
/* The system property "sun.java2d.print.minRefreshTime"
69
68
* can be used to specify minimum refresh time (in seconds)
@@ -72,17 +71,9 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
72
71
String refreshTimeStr = java .security .AccessController .doPrivileged (
73
72
new sun .security .action .GetPropertyAction (
74
73
"sun.java2d.print.minRefreshTime" ));
75
-
76
- if (refreshTimeStr != null ) {
77
- try {
78
- minRefreshTime = Integer .parseInt (refreshTimeStr );
79
- } catch (NumberFormatException e ) {
80
- // ignore
81
- }
82
- if (minRefreshTime < DEFAULT_MINREFRESH ) {
83
- minRefreshTime = DEFAULT_MINREFRESH ;
84
- }
85
- }
74
+ refreshTime = (refreshTimeStr != null )
75
+ ? getRefreshTime (refreshTimeStr )
76
+ : DEFAULT_REFRESH_TIME ;
86
77
87
78
java .security .AccessController .doPrivileged (
88
79
new java .security .PrivilegedAction <Void >() {
@@ -93,6 +84,17 @@ public Void run() {
93
84
});
94
85
}
95
86
87
+ private static int getRefreshTime (final String refreshTimeStr ) {
88
+ try {
89
+ int minRefreshTime = Integer .parseInt (refreshTimeStr );
90
+ return (minRefreshTime < MINIMUM_REFRESH_TIME )
91
+ ? MINIMUM_REFRESH_TIME
92
+ : minRefreshTime ;
93
+ } catch (NumberFormatException e ) {
94
+ return DEFAULT_REFRESH_TIME ;
95
+ }
96
+ }
97
+
96
98
/* The singleton win32 print lookup service.
97
99
* Code that is aware of this field and wants to use it must first
98
100
* see if its null, and if so instantiate it by calling a method such as
@@ -398,60 +400,38 @@ scope of the query would be too big to handle(at times).
398
400
count of printer status changes(add\remove) and based on it update the printers
399
401
list.
400
402
*/
401
- class RemotePrinterChangeListener implements Runnable {
402
- private String [] prevRemotePrinters ;
403
+ class RemotePrinterChangeListener implements Comparator <String >, Runnable {
403
404
404
405
RemotePrinterChangeListener () {
405
406
}
406
407
407
- private boolean doCompare (String [] str1 , String [] str2 ) {
408
- if (str1 == null && str2 == null ) {
409
- return false ;
410
- } else if (str1 == null || str2 == null ) {
411
- return true ;
412
- }
413
-
414
- if (str1 .length != str2 .length ) {
415
- return true ;
416
- } else {
417
- for (int i = 0 ; i < str1 .length ; i ++) {
418
- for (int j = 0 ; j < str2 .length ; j ++) {
419
- // skip if both are nulls
420
- if (str1 [i ] == null && str2 [j ] == null ) {
421
- continue ;
422
- }
423
-
424
- // return true if there is a 'difference' but
425
- // no need to access the individual string
426
- if (str1 [i ] == null || str2 [j ] == null ) {
427
- return true ;
428
- }
429
-
430
- // do comparison only if they are non-nulls
431
- if (!str1 [i ].equals (str2 [j ])) {
432
- return true ;
433
- }
434
- }
435
- }
436
- }
437
-
438
- return false ;
408
+ @ Override
409
+ public int compare (String o1 , String o2 ) {
410
+ return ((o1 == null )
411
+ ? ((o2 == null ) ? 0 : 1 )
412
+ : ((o2 == null ) ? -1 : o1 .compareTo (o2 )));
439
413
}
440
414
441
415
@ Override
442
416
public void run () {
443
417
// Init the list of remote printers
444
- prevRemotePrinters = getRemotePrintersNames ();
418
+ String [] prevRemotePrinters = getRemotePrintersNames ();
419
+ if (prevRemotePrinters != null ) {
420
+ Arrays .sort (prevRemotePrinters , this );
421
+ }
445
422
446
423
while (true ) {
447
424
try {
448
- Thread .sleep (minRefreshTime * 1000 );
425
+ Thread .sleep (refreshTime * 1000 );
449
426
} catch (InterruptedException e ) {
450
427
break ;
451
428
}
452
429
453
430
String [] currentRemotePrinters = getRemotePrintersNames ();
454
- if (doCompare (prevRemotePrinters , currentRemotePrinters )) {
431
+ if (currentRemotePrinters != null ) {
432
+ Arrays .sort (currentRemotePrinters , this );
433
+ }
434
+ if (!Arrays .equals (prevRemotePrinters , currentRemotePrinters )) {
455
435
// The list of remote printers got updated,
456
436
// so update the cached list printers which
457
437
// includes both local and network printers
0 commit comments