src/java.desktop/windows/classes/sun/print/PrintServiceLookupProvider.java
author sveerabhadra
Mon, 25 Feb 2019 11:02:53 +0530
changeset 53936 03163eb3b2d4
parent 50838 732a3b600098
child 54400 31c35004f300
permissions -rw-r--r--
8212202: [Windows] Exception if no printers are installed. Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
     2
 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.print.MultiDocPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.print.PrintServiceLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.HashPrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.PrintRequestAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.print.attribute.PrintServiceAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.print.attribute.PrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
27269
1ef2879dc7ad 8055705: Rename UnixPrintServiceLookup and Win32PrintServiceLookup as a platform neutral class name
prr
parents: 25859
diff changeset
    50
public class PrintServiceLookupProvider extends PrintServiceLookup {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private PrintService defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String[] printers; /* excludes the default printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private PrintService[] printServices; /* includes the default printer */
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    56
    private static boolean pollServices = true;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    57
    private static final int DEFAULT_MINREFRESH = 240;  // 4 minutes
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    58
    private static int minRefreshTime = DEFAULT_MINREFRESH;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static {
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    61
        /* The system property "sun.java2d.print.polling"
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    62
         * can be used to force the printing code to poll or not poll
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    63
         * for PrintServices.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    64
         */
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    65
        String pollStr = java.security.AccessController.doPrivileged(
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    66
            new sun.security.action.GetPropertyAction("sun.java2d.print.polling"));
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    67
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    68
        if (pollStr != null) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    69
            if (pollStr.equalsIgnoreCase("false")) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    70
                pollServices = false;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    71
            }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    72
        }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    73
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    74
        /* The system property "sun.java2d.print.minRefreshTime"
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    75
         * can be used to specify minimum refresh time (in seconds)
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    76
         * for polling PrintServices.  The default is 240.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    77
         */
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    78
        String refreshTimeStr = java.security.AccessController.doPrivileged(
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    79
            new sun.security.action.GetPropertyAction(
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    80
                "sun.java2d.print.minRefreshTime"));
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    81
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    82
        if (refreshTimeStr != null) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    83
            try {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    84
                minRefreshTime = (Integer.valueOf(refreshTimeStr)).intValue();
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    85
            } catch (NumberFormatException e) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    86
            }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    87
            if (minRefreshTime < DEFAULT_MINREFRESH) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    88
                minRefreshTime = DEFAULT_MINREFRESH;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    89
            }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    90
        }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    93
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    94
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    95
                    System.loadLibrary("awt");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    96
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    97
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    98
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* The singleton win32 print lookup service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Code that is aware of this field and wants to use it must first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * see if its null, and if so instantiate it by calling a method such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * javax.print.PrintServiceLookup.defaultPrintService() so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * same instance is stored there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
27269
1ef2879dc7ad 8055705: Rename UnixPrintServiceLookup and Win32PrintServiceLookup as a platform neutral class name
prr
parents: 25859
diff changeset
   107
    private static PrintServiceLookupProvider win32PrintLUS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* Think carefully before calling this. Preferably don't call it. */
27269
1ef2879dc7ad 8055705: Rename UnixPrintServiceLookup and Win32PrintServiceLookup as a platform neutral class name
prr
parents: 25859
diff changeset
   110
    public static PrintServiceLookupProvider getWin32PrintLUS() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (win32PrintLUS == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            /* This call is internally synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             * When it returns an instance of this class will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             * been instantiated - else there's a JDK internal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            PrintServiceLookup.lookupDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return win32PrintLUS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
27269
1ef2879dc7ad 8055705: Rename UnixPrintServiceLookup and Win32PrintServiceLookup as a platform neutral class name
prr
parents: 25859
diff changeset
   121
    public PrintServiceLookupProvider() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (win32PrintLUS == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            win32PrintLUS = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            String osName = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                new sun.security.action.GetPropertyAction("os.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // There's no capability for Win98 to refresh printers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // See "OpenPrinter" for more info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (osName != null && osName.startsWith("Windows 98")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   133
            // start the local printer listener thread
37550
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
   134
            Thread thr = new Thread(null, new PrinterChangeListener(),
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
   135
                                    "PrinterListener", 0, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            thr.start();
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   138
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   139
            if (pollServices) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   140
                // start the remote printer listener thread
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   141
                Thread remThr = new Thread(null, new RemotePrinterChangeListener(),
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   142
                                        "RemotePrinterListener", 0, false);
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   143
                remThr.setDaemon(true);
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   144
                remThr.start();
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   145
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } /* else condition ought to never happen! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /* Want the PrintService which is default print service to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * equality of reference with the equivalent in list of print services
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * This isn't required by the API and there's a risk doing this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * lead people to assume its guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public synchronized PrintService[] getPrintServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return printServices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private synchronized void refreshServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        printers = getAllPrinterNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (printers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // In Windows it is safe to assume no default if printers == null so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // don't get the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            printServices = new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        PrintService[] newServices = new PrintService[printers.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        PrintService defService = getDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int p = 0; p < printers.length; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (defService != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                printers[p].equals(defService.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                newServices[p] = defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    newServices[p] = new Win32PrintService(printers[p]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    for (j = 0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        if ((printServices[j]!= null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            (printers[p].equals(printServices[j].getName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                            newServices[p] = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            printServices[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    if (j == printServices.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        newServices[p] = new Win32PrintService(printers[p]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // Look for deleted services and invalidate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            for (int j=0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if ((printServices[j] instanceof Win32PrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    (!printServices[j].equals(defaultPrintService))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    ((Win32PrintService)printServices[j]).invalidateService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        printServices = newServices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public synchronized PrintService getPrintServiceByName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            /* getPrintServices() is now very fast. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            PrintService[] printServices = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            for (int i=0; i<printServices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (printServices[i].getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    return printServices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 14342
diff changeset
   229
    @SuppressWarnings("unchecked") // Cast to Class<PrintServiceAttribute>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    boolean matchingService(PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            PrintServiceAttributeSet serviceSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (serviceSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            Attribute [] attrs =  serviceSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            Attribute serviceAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                serviceAttr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public PrintService[] getPrintServices(DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                           AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        PrintRequestAttributeSet requestSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        PrintServiceAttributeSet serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (attributes != null && !attributes.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            requestSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            serviceSet = new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            Attribute[] attrs = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    requestSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                } else if (attrs[i] instanceof PrintServiceAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    serviceSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * Special case: If client is asking for a particular printer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * (by name) then we can save time by getting just that service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * to check against the rest of the specified attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        PrintService[] services = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            PrintService service = getPrintServiceByName(name.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (service == null || !matchingService(service, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                services = new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                services[0] = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (services.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } else {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 14342
diff changeset
   293
            ArrayList<PrintService> matchingServices = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            for (int i=0; i<services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    if (services[i].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        getUnsupportedAttributes(flavor, requestSet) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        matchingServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            services = new PrintService[matchingServices.size()];
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 14342
diff changeset
   304
            return matchingServices.toArray(services);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * return empty array as don't support multi docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public MultiDocPrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        getMultiDocPrintServices(DocFlavor[] flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                 AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return new MultiDocPrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public synchronized PrintService getDefaultPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // Windows does not have notification for a change in default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // so we always get the latest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        defaultPrinter = getDefaultPrinterName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (defaultPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if ((defaultPrintService != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            defaultPrintService.getName().equals(defaultPrinter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         // Not the same as default so proceed to get new PrintService.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // clear defaultPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            for (int j=0; j<printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (defaultPrinter.equals(printServices[j].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    defaultPrintService = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (defaultPrintService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            defaultPrintService = new Win32PrintService(defaultPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 27269
diff changeset
   361
    class PrinterChangeListener implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        long chgObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        PrinterChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            chgObj = notifyFirstPrinterChange(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 27269
diff changeset
   367
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (chgObj != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    // wait for configuration to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if (notifyPrinterChange(chgObj) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        notifyClosePrinterChange(chgObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   387
    /* Windows provides *PrinterChangeNotification* functions that provides
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   388
       information about printer status changes of the local printers but not
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   389
       network printers.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   390
       Alternatively, Windows provides a way thro' which one can get the
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   391
       network printer status changes by using WMI, RegistryKeyChange combination,
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   392
       which is a slightly complex mechanism.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   393
       The Windows WMI offers an async and sync method to read thro' registry
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   394
       via the WQL query. The async method is considered dangerous as it leaves
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   395
       open a channel until we close it. But the async method has the advantage of
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   396
       being notified of a change in registry by calling callback without polling for it.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   397
       The sync method uses the polling mechanism to notify.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   398
       RegistryValueChange cannot be used in combination with WMI to get registry
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   399
       value change notification because of an error that may be generated because the
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   400
       scope of the query would be too big to handle(at times).
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   401
       Hence an alternative mechanism is choosen via the EnumPrinters by polling for the
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   402
       count of printer status changes(add\remove) and based on it update the printers
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   403
       list.
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   404
    */
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   405
    class RemotePrinterChangeListener implements Runnable {
53936
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   406
        private String[] prevRemotePrinters = null;
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   407
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   408
        RemotePrinterChangeListener() {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   409
            prevRemotePrinters = getRemotePrintersNames();
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   410
        }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   411
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   412
        boolean doCompare(String[] str1, String[] str2) {
53936
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   413
            if (str1 == null && str2 == null) {
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   414
                return false;
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   415
            } else if (str1 == null || str2 == null) {
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   416
                return true;
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   417
            }
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   418
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   419
            if (str1.length != str2.length) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   420
                return true;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   421
            } else {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   422
                for (int i = 0;i < str1.length;i++) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   423
                    for (int j = 0;j < str2.length;j++) {
53936
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   424
                        // skip if both are nulls
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   425
                        if (str1[i] == null && str2[j] == null) {
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   426
                            continue;
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   427
                        }
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   428
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   429
                        // return true if there is a 'difference' but
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   430
                        // no need to access the individual string
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   431
                        if (str1[i] == null || str2[j] == null) {
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   432
                            return true;
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   433
                        }
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   434
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   435
                        // do comparison only if they are non-nulls
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   436
                        if (!str1[i].equals(str2[j])) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   437
                            return true;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   438
                        }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   439
                    }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   440
                }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   441
            }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   442
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   443
            return false;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   444
        }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   445
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   446
        @Override
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   447
        public void run() {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   448
            while (true) {
53936
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   449
                if (prevRemotePrinters != null && prevRemotePrinters.length > 0) {
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   450
                    String[] currentRemotePrinters = getRemotePrintersNames();
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   451
                    if (doCompare(prevRemotePrinters, currentRemotePrinters)) {
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   452
53936
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   453
                        // updated the printers data
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   454
                        // printers list now contains both local and network printer data
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   455
                        refreshServices();
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   456
53936
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   457
                        // store the current data for next comparison
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   458
                        prevRemotePrinters = currentRemotePrinters;
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   459
                    }
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   460
                } else {
03163eb3b2d4 8212202: [Windows] Exception if no printers are installed.
sveerabhadra
parents: 50838
diff changeset
   461
                    prevRemotePrinters = getRemotePrintersNames();
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   462
                }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   463
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   464
                try {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   465
                    Thread.sleep(minRefreshTime * 1000);
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   466
                } catch (InterruptedException e) {
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   467
                    break;
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   468
                }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   469
            }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   470
        }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   471
    }
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   472
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    private native String getDefaultPrinterName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private native String[] getAllPrinterNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    private native long notifyFirstPrinterChange(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private native void notifyClosePrinterChange(long chgObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    private native int notifyPrinterChange(long chgObj);
50838
732a3b600098 8153732: Windows remote printer changes do not reflect in lookupPrintServices()
sveerabhadra
parents: 47216
diff changeset
   478
    private native String[] getRemotePrintersNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
}