jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java
author jgodinez
Wed, 10 Jul 2013 11:49:04 -0700
changeset 18752 2a4f599c6bff
parent 18115 dbd9791b69d2
child 19357 7780d9ec205c
permissions -rw-r--r--
8016737: After clicking on "Print UNCOLLATED" button, the print out come in order 'Page 1', 'Page 2', 'Page 1' Reviewed-by: jchen, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.print.DocFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.print.MultiDocPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.print.PrintServiceLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.HashPrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.HashPrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.print.attribute.PrintRequestAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.print.attribute.PrintServiceAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.print.attribute.PrintServiceAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.io.FileReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.net.URL;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 12047
diff changeset
    54
import java.nio.file.Files;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Remind: This class uses solaris commands. We also need a linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class UnixPrintServiceLookup extends PrintServiceLookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    implements BackgroundServiceLookup, Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* Remind: the current implementation is static, as its assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * its preferable to minimise creation of PrintService instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Later we should add logic to add/remove services on the fly which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * will take a hit of needing to regather the list of services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private String defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private PrintService defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private PrintService[] printServices; /* includes the default printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private Vector lookupListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static String debugPrefix = "UnixPrintServiceLookup>> ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static boolean pollServices = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final int DEFAULT_MINREFRESH = 120;  // 2 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static int minRefreshTime = DEFAULT_MINREFRESH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static String osname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* The system property "sun.java2d.print.polling"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         * can be used to force the printing code to poll or not poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * for PrintServices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String pollStr = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            new sun.security.action.GetPropertyAction("sun.java2d.print.polling"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (pollStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (pollStr.equalsIgnoreCase("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                pollServices = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } else if (pollStr.equalsIgnoreCase("false")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                pollServices = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* The system property "sun.java2d.print.minRefreshTime"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * can be used to specify minimum refresh time (in seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * for polling PrintServices.  The default is 120.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String refreshTimeStr = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            new sun.security.action.GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                "sun.java2d.print.minRefreshTime"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (refreshTimeStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                minRefreshTime = (new Integer(refreshTimeStr)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (minRefreshTime < DEFAULT_MINREFRESH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                minRefreshTime = DEFAULT_MINREFRESH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        osname = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            new sun.security.action.GetPropertyAction("os.name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   118
    static boolean isMac() {
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   119
        return osname.startsWith("Mac");
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   120
    }
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static boolean isSysV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return osname.equals("SunOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
18752
2a4f599c6bff 8016737: After clicking on "Print UNCOLLATED" button, the print out come in order 'Page 1', 'Page 2', 'Page 1'
jgodinez
parents: 18115
diff changeset
   126
    static boolean isLinux() {
2a4f599c6bff 8016737: After clicking on "Print UNCOLLATED" button, the print out come in order 'Page 1', 'Page 2', 'Page 1'
jgodinez
parents: 18115
diff changeset
   127
        return (osname.equals("Linux"));
2a4f599c6bff 8016737: After clicking on "Print UNCOLLATED" button, the print out come in order 'Page 1', 'Page 2', 'Page 1'
jgodinez
parents: 18115
diff changeset
   128
    }
2a4f599c6bff 8016737: After clicking on "Print UNCOLLATED" button, the print out come in order 'Page 1', 'Page 2', 'Page 1'
jgodinez
parents: 18115
diff changeset
   129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static boolean isBSD() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   131
        return (osname.equals("Linux") ||
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
   132
                osname.contains("OS X"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static final int UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static final int BSD_LPD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static final int BSD_LPD_NG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static int cmdIndex = UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    String[] lpcFirstCom = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        "/usr/sbin/lpc status | grep : | sed -ne '1,1 s/://p'",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        "/usr/sbin/lpc status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    String[] lpcAllCom = {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   147
        "/usr/sbin/lpc status all | grep : | sed -e 's/://'",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   148
        "/usr/sbin/lpc status all | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}' | sort"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    String[] lpcNameCom = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        "| grep : | sed -ne 's/://p'",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        "| grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    static int getBSDCommandIndex() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   158
        String command  = "/usr/sbin/lpc status all";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        String[] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if ((names == null) || (names.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return BSD_LPD_NG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        for (int i=0; i<names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (names[i].indexOf('@') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                return BSD_LPD_NG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return BSD_LPD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public UnixPrintServiceLookup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // start the printer listener thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (pollServices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            PrinterChangeListener thr = new PrinterChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            IPPPrintService.debug_println(debugPrefix+"polling turned on");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /* Want the PrintService which is default print service to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * equality of reference with the equivalent in list of print services
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * This isn't required by the API and there's a risk doing this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * lead people to assume its guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public synchronized PrintService[] getPrintServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (printServices == null || !pollServices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
10779
5037da29fa26 6604109: javax.print.PrintServiceLookup.lookupPrintServices fails SOMETIMES for Cups
jgodinez
parents: 5506
diff changeset
   202
            return (PrintService[])printServices.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    // refreshes "printServices"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public synchronized void refreshServices() {
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   209
        /* excludes the default printer */
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   210
        String[] printers = null; // array of printer names
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   211
        String[] printerURIs = null; //array of printer URIs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        getDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (CUPSPrinter.isCupsRunning()) {
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   215
            printerURIs = CUPSPrinter.getAllPrinters();
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   216
            if ((printerURIs != null) && (printerURIs.length > 0)) {
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   217
                printers = new String[printerURIs.length];
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   218
                for (int i=0; i<printerURIs.length; i++) {
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   219
                    int lastIndex = printerURIs[i].lastIndexOf("/");
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   220
                    printers[i] = printerURIs[i].substring(lastIndex+1);
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   221
                }
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   222
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else {
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   224
            if (isMac() || isSysV()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                printers = getAllPrinterNamesSysV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } else { //BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                printers = getAllPrinterNamesBSD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (printers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (defaultPrintService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                printServices = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                printServices[0] = defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                printServices = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        ArrayList printerList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int defaultIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int p=0; p<printers.length; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (printers[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if ((defaultPrintService != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                && printers[p].equals(defaultPrintService.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                printerList.add(defaultPrintService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                defaultIndex = printerList.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                                  "total# of printers = "+printers.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        try {
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   258
                            printerList.add(new IPPPrintService(printers[p],
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   259
                                                                printerURIs[p],
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   260
                                                                true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                            IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                                          " getAllPrinters Exception "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                                          e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        printerList.add(new UnixPrintService(printers[p]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    for (j=0; j<printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        if ((printServices[j] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            (printers[p].equals(printServices[j].getName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            printerList.add(printServices[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                            printServices[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    if (j == printServices.length) {      // not found?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                            try {
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   284
                                printerList.add(new IPPPrintService(
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   285
                                                               printers[p],
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   286
                                                               printerURIs[p],
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   287
                                                               true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                              " getAllPrinters Exception "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                                              e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            printerList.add(new UnixPrintService(printers[p]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // Look for deleted services and invalidate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            for (int j=0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if ((printServices[j] instanceof UnixPrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    (!printServices[j].equals(defaultPrintService))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    ((UnixPrintService)printServices[j]).invalidateService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        //if defaultService is not found in printerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (defaultIndex == -1 && defaultPrintService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            //add default to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            printerList.add(defaultPrintService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            defaultIndex = printerList.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        printServices = (PrintService[])printerList.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                      new PrintService[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // swap default with the first in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (defaultIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            PrintService saveService = printServices[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            printServices[0] = printServices[defaultIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            printServices[defaultIndex] = saveService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private boolean matchesAttributes(PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                      PrintServiceAttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Attribute [] attrs =  attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Attribute serviceAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            serviceAttr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
      /* This checks for validity of the printer name before passing as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
       * parameter to a shell command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
      private boolean checkPrinterName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        for (int i=0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
          c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
          if (Character.isLetterOrDigit(c) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
              c == '-' || c == '_' || c == '.' || c == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /* On a network with many (hundreds) of network printers, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * can save several seconds if you know all you want is a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * printer, to ask for that printer rather than retrieving all printers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private PrintService getServiceByName(PrinterName nameAttr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        String name = nameAttr.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (name == null || name.equals("") || !checkPrinterName(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
18115
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   372
        /* check is all printers are already available */
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   373
        if (printServices != null) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   374
            for (PrintService printService : printServices) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   375
                if (printService.getName().equals(name)) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   376
                    return printService;
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   377
                }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   378
            }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   379
        }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   380
        /* take CUPS into account first */
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   381
        if (CUPSPrinter.isCupsRunning()) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   382
            try {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   383
                return new IPPPrintService(name,
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   384
                                           new URL("http://"+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   385
                                                   CUPSPrinter.getServer()+":"+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   386
                                                   CUPSPrinter.getPort()+"/"+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   387
                                                   name));
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   388
            } catch (Exception e) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   389
                IPPPrintService.debug_println(debugPrefix+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   390
                                              " getServiceByName Exception "+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   391
                                              e);
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   392
            }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   393
        }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   394
        /* fallback if nothing not having a printer at this point */
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   395
        PrintService printer = null;
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   396
        if (isMac() || isSysV()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            printer = getNamedPrinterNameSysV(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            printer = getNamedPrinterNameBSD(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private PrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        getPrintServices(PrintServiceAttributeSet serviceSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (serviceSet == null || serviceSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        /* Typically expect that if a service attribute is specified that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * its a printer name and there ought to be only one match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * Directly retrieve that service and confirm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * that it meets the other requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * If printer name isn't mentioned then go a slow path checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         * all printers if they meet the reqiremements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        PrintService[] services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        PrintService defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (name != null && (defService = getDefaultPrintService()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            /* To avoid execing a unix command  see if the client is asking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
             * for the default printer by name, since we already have that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
             * initialised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            PrinterName defName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                (PrinterName)defService.getAttribute(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (defName != null && name.equals(defName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (matchesAttributes(defService, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    services[0] = defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                /* Its not the default service */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                PrintService service = getServiceByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (service != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    matchesAttributes(service, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    services[0] = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            /* specified service attributes don't include a name.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            Vector matchedServices = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            for (int i = 0; i< services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                if (matchesAttributes(services[i], serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    matchedServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            services = new PrintService[matchedServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            for (int i = 0; i< services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                services[i] = (PrintService)matchedServices.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * If service attributes are specified then there must be additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public PrintService[] getPrintServices(DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                           AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        PrintRequestAttributeSet requestSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        PrintServiceAttributeSet serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (attributes != null && !attributes.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            requestSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            serviceSet = new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            Attribute[] attrs = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    requestSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                } else if (attrs[i] instanceof PrintServiceAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    serviceSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        PrintService[] services = getPrintServices(serviceSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (services.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            ArrayList matchingServices = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            for (int i=0; i<services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    if (services[i].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        getUnsupportedAttributes(flavor, requestSet) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        matchingServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            services = new PrintService[matchingServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return (PrintService[])matchingServices.toArray(services);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            // We only need to compare 1 PrintService because all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            // UnixPrintServices are the same anyway.  We will not use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            // default PrintService because it might be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            PrintService service = services[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if ((flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                 service.isDocFlavorSupported(flavor)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                 service.getUnsupportedAttributes(flavor, requestSet) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * return empty array as don't support multi docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public MultiDocPrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        getMultiDocPrintServices(DocFlavor[] flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                 AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return new MultiDocPrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public synchronized PrintService getDefaultPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // clear defaultPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        IPPPrintService.debug_println("isRunning ? "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                      (CUPSPrinter.isCupsRunning()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            defaultPrinter = CUPSPrinter.getDefaultPrinter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        } else {
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   558
            if (isMac() || isSysV()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                defaultPrinter = getDefaultPrinterNameSysV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                defaultPrinter = getDefaultPrinterNameBSD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (defaultPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            for (int j=0; j<printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (defaultPrinter.equals(printServices[j].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    defaultPrintService = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (defaultPrintService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    PrintService defaultPS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        new IPPPrintService(defaultPrinter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                            new URL("http://"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                                    CUPSPrinter.getServer()+":"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                                    CUPSPrinter.getPort()+"/"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                                    defaultPrinter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    defaultPrintService = defaultPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                defaultPrintService = new UnixPrintService(defaultPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        getServicesInbackground(BackgroundLookupListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            listener.notifyServices(printServices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            if (lookupListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                lookupListeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                lookupListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                Thread lookupThread = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                lookupThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                lookupListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /* This method isn't used in most cases because we rely on code in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * javax.print.PrintServiceLookup. This is needed just for the cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * where those interfaces are by-passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private PrintService[] copyOf(PrintService[] inArr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (inArr == null || inArr.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return inArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            PrintService []outArr = new PrintService[inArr.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            System.arraycopy(inArr, 0, outArr, 0, inArr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return outArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        PrintService[] services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            BackgroundLookupListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            for (int i=0; i<lookupListeners.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                listener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    (BackgroundLookupListener)lookupListeners.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                listener.notifyServices(copyOf(services));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            lookupListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    private String getDefaultPrinterNameBSD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        String[] names = execCmd(lpcFirstCom[cmdIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if ((cmdIndex==BSD_LPD_NG) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            (names[0].startsWith("missingprinter"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return names[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    private PrintService getNamedPrinterNameBSD(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
      if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
      String command = "/usr/sbin/lpc status " + name + lpcNameCom[cmdIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
      String[] result = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
      if (result == null || !(result[0].equals(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
      return new UnixPrintService(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private String[] getAllPrinterNamesBSD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        String[] names = execCmd(lpcAllCom[cmdIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   679
    static String getDefaultPrinterNameSysV() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        String defaultPrinter = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        String command = "/usr/bin/lpstat -d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        String [] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            int index = names[0].indexOf(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (index == -1  || (names[0].length() <= index+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                String name = names[0].substring(index+1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                if (name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    private PrintService getNamedPrinterNameSysV(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        String command = "/usr/bin/lpstat -v " + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        String []result = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (result == null || result[0].indexOf("unknown printer") > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return new UnixPrintService(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    private String[] getAllPrinterNamesSysV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        String defaultPrinter = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        String [] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        ArrayList printerNames = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        for (int i=0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (!names[i].equals("_default") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                !names[i].equals(defaultPrinter) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                !names[i].equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                printerNames.add(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return (String[])printerNames.toArray(new String[printerNames.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    static String[] execCmd(final String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        ArrayList results = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            final String[] cmd = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (isSysV()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                cmd[0] = "/usr/bin/sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                cmd[1] = "-c";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                cmd[2] = "env LC_ALL=C " + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                cmd[0] = "/bin/sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                cmd[1] = "-c";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                cmd[2] = "LC_ALL=C " + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            results = (ArrayList)AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    public Object run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        Process proc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                        BufferedReader bufferedReader = null;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 12047
diff changeset
   749
                        File f = Files.createTempFile("prn","xc").toFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        cmd[2] = cmd[2]+">"+f.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        proc = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                            boolean done = false; // in case of interrupt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                    proc.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                            if (proc.exitValue() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                FileReader reader = new FileReader(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                                bufferedReader = new BufferedReader(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                ArrayList results = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                while ((line = bufferedReader.readLine())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                                       != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                    results.add(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                            f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                            // promptly close all streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                            if (bufferedReader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                bufferedReader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                            proc.getInputStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                            proc.getErrorStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                            proc.getOutputStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (results == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return (String[])results.toArray(new String[results.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    private class PrinterChangeListener extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            int refreshSecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                } catch (Exception se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    IPPPrintService.debug_println(debugPrefix+"Exception in refresh thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                if ((printServices != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    (printServices.length > minRefreshTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    // compute new refresh time 1 printer = 1 sec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    refreshSecs = printServices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    refreshSecs = minRefreshTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    sleep(refreshSecs * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
}