jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java
author kvn
Tue, 28 Jan 2014 14:57:41 -0800
changeset 22607 ba232b417248
parent 22584 eed64ee05369
parent 22603 816588059f9d
child 23287 c0f8cdafef56
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21780
diff changeset
     2
 * Copyright (c) 2000, 2014, 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;
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
    51
import javax.print.attribute.standard.PrinterURI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.io.FileReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.net.URL;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 12047
diff changeset
    55
import java.nio.file.Files;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Remind: This class uses solaris commands. We also need a linux
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class UnixPrintServiceLookup extends PrintServiceLookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    implements BackgroundServiceLookup, Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Remind: the current implementation is static, as its assumed
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19357
diff changeset
    65
     * its preferable to minimize creation of PrintService instances.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Later we should add logic to add/remove services on the fly which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * will take a hit of needing to regather the list of services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private PrintService defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private PrintService[] printServices; /* includes the default printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Vector lookupListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static String debugPrefix = "UnixPrintServiceLookup>> ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static boolean pollServices = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int DEFAULT_MINREFRESH = 120;  // 2 minutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static int minRefreshTime = DEFAULT_MINREFRESH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static String osname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    81
    // List of commands used to deal with the printer queues on AIX
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    82
    String[] lpNameComAix = {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    83
      "/usr/bin/lsallq",
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    84
      "/usr/bin/lpstat -W -p|/usr/bin/expand|/usr/bin/cut -f1 -d' '",
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    85
      "/usr/bin/lpstat -W -d|/usr/bin/expand|/usr/bin/cut -f1 -d' '",
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    86
      "/usr/bin/lpstat -W -v"
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    87
    };
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    88
    private static final int aix_lsallq = 0;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    89
    private static final int aix_lpstat_p = 1;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    90
    private static final int aix_lpstat_d = 2;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    91
    private static final int aix_lpstat_v = 3;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    92
    private static int aix_defaultPrinterEnumeration = aix_lsallq;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        /* The system property "sun.java2d.print.polling"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * can be used to force the printing code to poll or not poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * for PrintServices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        String pollStr = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            new sun.security.action.GetPropertyAction("sun.java2d.print.polling"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (pollStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (pollStr.equalsIgnoreCase("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                pollServices = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else if (pollStr.equalsIgnoreCase("false")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                pollServices = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        /* The system property "sun.java2d.print.minRefreshTime"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * can be used to specify minimum refresh time (in seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * for polling PrintServices.  The default is 120.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String refreshTimeStr = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            new sun.security.action.GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "sun.java2d.print.minRefreshTime"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (refreshTimeStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                minRefreshTime = (new Integer(refreshTimeStr)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (minRefreshTime < DEFAULT_MINREFRESH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                minRefreshTime = DEFAULT_MINREFRESH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        osname = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            new sun.security.action.GetPropertyAction("os.name"));
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   130
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   131
        /* The system property "sun.java2d.print.aix.lpstat"
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   132
         * can be used to force the usage of 'lpstat -p' to enumerate all
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   133
         * printer queues. By default we use 'lsallq', because 'lpstat -p' can
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   134
         * take lots of time if thousands of printers are attached to a server.
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   135
         */
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   136
        if (isAIX()) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   137
            String aixPrinterEnumerator = java.security.AccessController.doPrivileged(
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   138
                new sun.security.action.GetPropertyAction("sun.java2d.print.aix.lpstat"));
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   139
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   140
            if (aixPrinterEnumerator != null) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   141
                if (aixPrinterEnumerator.equalsIgnoreCase("lpstat")) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   142
                    aix_defaultPrinterEnumeration = aix_lpstat_p;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   143
                } else if (aixPrinterEnumerator.equalsIgnoreCase("lsallq")) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   144
                    aix_defaultPrinterEnumeration = aix_lsallq;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   145
                }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   146
            }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   147
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   150
    static boolean isMac() {
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   151
        return osname.startsWith("Mac");
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   152
    }
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   153
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static boolean isSysV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return osname.equals("SunOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
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
   158
    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
   159
        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
   160
    }
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
   161
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static boolean isBSD() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   163
        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
   164
                osname.contains("OS X"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   167
    static boolean isAIX() {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   168
        return osname.equals("AIX");
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   169
    }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static final int UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static final int BSD_LPD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static final int BSD_LPD_NG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static int cmdIndex = UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    String[] lpcFirstCom = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        "/usr/sbin/lpc status | grep : | sed -ne '1,1 s/://p'",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        "/usr/sbin/lpc status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    String[] lpcAllCom = {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   183
        "/usr/sbin/lpc status all | grep : | sed -e 's/://'",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   184
        "/usr/sbin/lpc status all | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}' | sort"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    String[] lpcNameCom = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        "| grep : | sed -ne 's/://p'",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        "| grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static int getBSDCommandIndex() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10779
diff changeset
   194
        String command  = "/usr/sbin/lpc status all";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        String[] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if ((names == null) || (names.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return BSD_LPD_NG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i=0; i<names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (names[i].indexOf('@') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                return BSD_LPD_NG;
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
        return BSD_LPD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public UnixPrintServiceLookup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // start the printer listener thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (pollServices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            PrinterChangeListener thr = new PrinterChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            IPPPrintService.debug_println(debugPrefix+"polling turned on");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /* Want the PrintService which is default print service to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * equality of reference with the equivalent in list of print services
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * This isn't required by the API and there's a risk doing this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * lead people to assume its guaranteed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public synchronized PrintService[] getPrintServices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (printServices == null || !pollServices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21780
diff changeset
   238
            return printServices.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   242
    private int addPrintServiceToList(ArrayList printerList, PrintService ps) {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   243
        int index = printerList.indexOf(ps);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   244
        // Check if PrintService with same name is already in the list.
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   245
        if (CUPSPrinter.isCupsRunning() && index != -1) {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   246
            // Bug in Linux: Duplicate entry of a remote printer
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   247
            // and treats it as local printer but it is returning wrong
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   248
            // information when queried using IPP. Workaround is to remove it.
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   249
            // Even CUPS ignores these entries as shown in lpstat or using
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   250
            // their web configuration.
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21780
diff changeset
   251
            PrinterURI uri = ps.getAttribute(PrinterURI.class);
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   252
            if (uri.getURI().getHost().equals("localhost")) {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   253
                IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, ignoring the new local printer: "+ps);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   254
                return index;  // Do not add this.
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   255
            }
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   256
            PrintService oldPS = (PrintService)(printerList.get(index));
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21780
diff changeset
   257
            uri = oldPS.getAttribute(PrinterURI.class);
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   258
            if (uri.getURI().getHost().equals("localhost")) {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   259
                IPPPrintService.debug_println(debugPrefix+"duplicate PrintService, removing existing local printer: "+oldPS);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   260
                printerList.remove(oldPS);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   261
            } else {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   262
                return index;
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   263
            }
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   264
        }
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   265
        printerList.add(ps);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   266
        return (printerList.size() - 1);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   267
    }
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   268
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // refreshes "printServices"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public synchronized void refreshServices() {
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   272
        /* excludes the default printer */
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   273
        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
   274
        String[] printerURIs = null; //array of printer URIs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        getDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (CUPSPrinter.isCupsRunning()) {
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   278
            printerURIs = CUPSPrinter.getAllPrinters();
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   279
            if ((printerURIs != null) && (printerURIs.length > 0)) {
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   280
                printers = new String[printerURIs.length];
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   281
                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
   282
                    int lastIndex = printerURIs[i].lastIndexOf("/");
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   283
                    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
   284
                }
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   285
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   287
            if (isMac() || isSysV()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                printers = getAllPrinterNamesSysV();
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   289
            } else if (isAIX()) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   290
                printers = getAllPrinterNamesAIX();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } else { //BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                printers = getAllPrinterNamesBSD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (printers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (defaultPrintService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                printServices = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                printServices[0] = defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                printServices = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        ArrayList printerList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int defaultIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        for (int p=0; p<printers.length; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (printers[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if ((defaultPrintService != null)
19357
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   313
                && printers[p].equals(getPrinterDestName(defaultPrintService))) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   314
                defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                  "total# of printers = "+printers.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        try {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   322
                            addPrintServiceToList(printerList,
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   323
                                                  new IPPPrintService(printers[p],
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   324
                                                                   printerURIs[p],
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   325
                                                                   true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                                          " getAllPrinters Exception "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                          e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        printerList.add(new UnixPrintService(printers[p]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    for (j=0; j<printServices.length; j++) {
19357
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   338
                        if (printServices[j] != null) {
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   339
                            if (printers[p].equals(getPrinterDestName(printServices[j]))) {
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   340
                                printerList.add(printServices[j]);
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   341
                                printServices[j] = null;
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   342
                                break;
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   343
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    if (j == printServices.length) {      // not found?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            try {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   350
                                addPrintServiceToList(printerList,
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   351
                                             new IPPPrintService(printers[p],
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   352
                                                                 printerURIs[p],
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   353
                                                                 true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                                              " getAllPrinters Exception "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                                              e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            printerList.add(new UnixPrintService(printers[p]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // Look for deleted services and invalidate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            for (int j=0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                if ((printServices[j] instanceof UnixPrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    (!printServices[j].equals(defaultPrintService))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    ((UnixPrintService)printServices[j]).invalidateService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        //if defaultService is not found in printerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (defaultIndex == -1 && defaultPrintService != null) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   380
            defaultIndex = addPrintServiceToList(printerList, defaultPrintService);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        printServices = (PrintService[])printerList.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                      new PrintService[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // swap default with the first in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (defaultIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            PrintService saveService = printServices[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            printServices[0] = printServices[defaultIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            printServices[defaultIndex] = saveService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private boolean matchesAttributes(PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                      PrintServiceAttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        Attribute [] attrs =  attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        Attribute serviceAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            serviceAttr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
      /* This checks for validity of the printer name before passing as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
       * parameter to a shell command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
      private boolean checkPrinterName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        for (int i=0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
          c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
          if (Character.isLetterOrDigit(c) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
              c == '-' || c == '_' || c == '.' || c == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
19357
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   427
    /*
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   428
     * Gets the printer name compatible with the list of printers returned by
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   429
     * the system when we query default or all the available printers.
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   430
     */
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   431
    private String getPrinterDestName(PrintService ps) {
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   432
        if (isMac()) {
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   433
            return ((IPPPrintService)ps).getDest();
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   434
        }
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   435
        return ps.getName();
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   436
    }
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   437
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /* On a network with many (hundreds) of network printers, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * can save several seconds if you know all you want is a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * printer, to ask for that printer rather than retrieving all printers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    private PrintService getServiceByName(PrinterName nameAttr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        String name = nameAttr.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (name == null || name.equals("") || !checkPrinterName(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
19357
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   447
        /* check if all printers are already available */
18115
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   448
        if (printServices != null) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   449
            for (PrintService printService : printServices) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21780
diff changeset
   450
                PrinterName printerName = printService.getAttribute(PrinterName.class);
19357
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   451
                if (printerName.getValue().equals(name)) {
18115
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   452
                    return printService;
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   453
                }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   454
            }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   455
        }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   456
        /* take CUPS into account first */
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   457
        if (CUPSPrinter.isCupsRunning()) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   458
            try {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   459
                return new IPPPrintService(name,
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   460
                                           new URL("http://"+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   461
                                                   CUPSPrinter.getServer()+":"+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   462
                                                   CUPSPrinter.getPort()+"/"+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   463
                                                   name));
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   464
            } catch (Exception e) {
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   465
                IPPPrintService.debug_println(debugPrefix+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   466
                                              " getServiceByName Exception "+
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   467
                                              e);
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   468
            }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   469
        }
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   470
        /* fallback if nothing not having a printer at this point */
dbd9791b69d2 8013810: PrintServiceLookup.lookupPrintServices() does not return consistent result
jgodinez
parents: 13048
diff changeset
   471
        PrintService printer = null;
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   472
        if (isMac() || isSysV()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            printer = getNamedPrinterNameSysV(name);
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   474
        } else if (isAIX()) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   475
            printer = getNamedPrinterNameAIX(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            printer = getNamedPrinterNameBSD(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private PrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        getPrintServices(PrintServiceAttributeSet serviceSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (serviceSet == null || serviceSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /* Typically expect that if a service attribute is specified that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * its a printer name and there ought to be only one match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * Directly retrieve that service and confirm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * that it meets the other requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * If printer name isn't mentioned then go a slow path checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * all printers if they meet the reqiremements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        PrintService[] services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        PrintService defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (name != null && (defService = getDefaultPrintService()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            /* To avoid execing a unix command  see if the client is asking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             * for the default printer by name, since we already have that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
             * initialised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21780
diff changeset
   505
            PrinterName defName = defService.getAttribute(PrinterName.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (defName != null && name.equals(defName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                if (matchesAttributes(defService, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    services[0] = defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                /* Its not the default service */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                PrintService service = getServiceByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                if (service != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    matchesAttributes(service, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    services[0] = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            /* specified service attributes don't include a name.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            Vector matchedServices = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            for (int i = 0; i< services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (matchesAttributes(services[i], serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    matchedServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            services = new PrintService[matchedServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            for (int i = 0; i< services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                services[i] = (PrintService)matchedServices.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return services;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * If service attributes are specified then there must be additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public PrintService[] getPrintServices(DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                           AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        PrintRequestAttributeSet requestSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        PrintServiceAttributeSet serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (attributes != null && !attributes.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            requestSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            serviceSet = new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            Attribute[] attrs = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    requestSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                } else if (attrs[i] instanceof PrintServiceAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    serviceSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        PrintService[] services = getPrintServices(serviceSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (services.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            ArrayList matchingServices = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            for (int i=0; i<services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    if (services[i].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        getUnsupportedAttributes(flavor, requestSet) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        matchingServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            services = new PrintService[matchingServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return (PrintService[])matchingServices.toArray(services);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // We only need to compare 1 PrintService because all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            // UnixPrintServices are the same anyway.  We will not use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // default PrintService because it might be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            PrintService service = services[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if ((flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                 service.isDocFlavorSupported(flavor)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                 service.getUnsupportedAttributes(flavor, requestSet) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * return empty array as don't support multi docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public MultiDocPrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        getMultiDocPrintServices(DocFlavor[] flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                 AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return new MultiDocPrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public synchronized PrintService getDefaultPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // clear defaultPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        defaultPrintService = null;
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   629
        String psuri = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        IPPPrintService.debug_println("isRunning ? "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                      (CUPSPrinter.isCupsRunning()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (CUPSPrinter.isCupsRunning()) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   634
            String[] printerInfo = CUPSPrinter.getDefaultPrinter();
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   635
            defaultPrinter = printerInfo[0];
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   636
            psuri = printerInfo[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        } else {
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   638
            if (isMac() || isSysV()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                defaultPrinter = getDefaultPrinterNameSysV();
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   640
            } else if (isAIX()) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   641
                defaultPrinter = getDefaultPrinterNameAIX();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                defaultPrinter = getDefaultPrinterNameBSD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (defaultPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            for (int j=0; j<printServices.length; j++) {
19357
7780d9ec205c 8022241: [macosx] [PIT] lookupPrintServices() returns one too long array
jgodinez
parents: 18752
diff changeset
   652
                if (defaultPrinter.equals(getPrinterDestName(printServices[j]))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    defaultPrintService = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (defaultPrintService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                try {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   661
                    PrintService defaultPS;
21780
aa006e1a6eb9 8028584: sun.net.www.protocol.file.FileURLConnection cannot be cast to java.net.HttpURLConnection
jgodinez
parents: 21283
diff changeset
   662
                    if ((psuri != null) && !psuri.startsWith("file")) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   663
                        defaultPS = new IPPPrintService(defaultPrinter,
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   664
                                                        psuri, true);
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   665
                    } else {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   666
                        defaultPS = new IPPPrintService(defaultPrinter,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                            new URL("http://"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                                                    CUPSPrinter.getServer()+":"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                                                    CUPSPrinter.getPort()+"/"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                                                    defaultPrinter));
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 19357
diff changeset
   671
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    defaultPrintService = defaultPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                defaultPrintService = new UnixPrintService(defaultPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        getServicesInbackground(BackgroundLookupListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            listener.notifyServices(printServices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (lookupListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                lookupListeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                lookupListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                Thread lookupThread = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                lookupThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                lookupListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
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
    /* This method isn't used in most cases because we rely on code in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * javax.print.PrintServiceLookup. This is needed just for the cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * where those interfaces are by-passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    private PrintService[] copyOf(PrintService[] inArr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (inArr == null || inArr.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return inArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            PrintService []outArr = new PrintService[inArr.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            System.arraycopy(inArr, 0, outArr, 0, inArr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return outArr;
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
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        PrintService[] services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            BackgroundLookupListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            for (int i=0; i<lookupListeners.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                listener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    (BackgroundLookupListener)lookupListeners.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                listener.notifyServices(copyOf(services));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            lookupListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    private String getDefaultPrinterNameBSD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        String[] names = execCmd(lpcFirstCom[cmdIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if ((cmdIndex==BSD_LPD_NG) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            (names[0].startsWith("missingprinter"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return names[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    private PrintService getNamedPrinterNameBSD(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
      if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
      String command = "/usr/sbin/lpc status " + name + lpcNameCom[cmdIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
      String[] result = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
      if (result == null || !(result[0].equals(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
      return new UnixPrintService(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private String[] getAllPrinterNamesBSD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        String[] names = execCmd(lpcAllCom[cmdIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12538
diff changeset
   766
    static String getDefaultPrinterNameSysV() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        String defaultPrinter = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        String command = "/usr/bin/lpstat -d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        String [] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            return defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            int index = names[0].indexOf(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            if (index == -1  || (names[0].length() <= index+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                String name = names[0].substring(index+1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                if (name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    private PrintService getNamedPrinterNameSysV(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        String command = "/usr/bin/lpstat -v " + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        String []result = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (result == null || result[0].indexOf("unknown printer") > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            return new UnixPrintService(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    private String[] getAllPrinterNamesSysV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        String defaultPrinter = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        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
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        String [] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        ArrayList printerNames = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        for (int i=0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            if (!names[i].equals("_default") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                !names[i].equals(defaultPrinter) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                !names[i].equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                printerNames.add(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return (String[])printerNames.toArray(new String[printerNames.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   816
    private String getDefaultPrinterNameAIX() {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   817
        String[] names = execCmd(lpNameComAix[aix_lpstat_d]);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   818
        // Remove headers and bogus entries added by remote printers.
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   819
        names = UnixPrintService.filterPrinterNamesAIX(names);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   820
        if (names == null || names.length != 1) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   821
            // No default printer found
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   822
            return null;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   823
        } else {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   824
            return names[0];
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   825
        }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   826
    }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   827
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   828
    private PrintService getNamedPrinterNameAIX(String name) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   829
        // On AIX there should be no blank after '-v'.
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   830
        String[] result = execCmd(lpNameComAix[aix_lpstat_v] + name);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   831
        // Remove headers and bogus entries added by remote printers.
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   832
        result = UnixPrintService.filterPrinterNamesAIX(result);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   833
        if (result == null || result.length != 1) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   834
            return null;
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   835
        } else {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   836
            return new UnixPrintService(name);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   837
        }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   838
    }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   839
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   840
    private String[] getAllPrinterNamesAIX() {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   841
        // Determine all printers of the system.
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   842
        String [] names = execCmd(lpNameComAix[aix_defaultPrinterEnumeration]);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   843
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   844
        // Remove headers and bogus entries added by remote printers.
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   845
        names = UnixPrintService.filterPrinterNamesAIX(names);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   846
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   847
        ArrayList<String> printerNames = new ArrayList<String>();
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   848
        for ( int i=0; i < names.length; i++) {
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   849
            printerNames.add(names[i]);
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   850
        }
22607
kvn
parents: 22584 22603
diff changeset
   851
        return printerNames.toArray(new String[printerNames.size()]);
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   852
    }
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   853
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    static String[] execCmd(final String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        ArrayList results = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            final String[] cmd = new String[3];
22603
816588059f9d 8031134: PPC64: implement printing on AIX
simonis
parents: 21780
diff changeset
   858
            if (isSysV() || isAIX()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                cmd[0] = "/usr/bin/sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                cmd[1] = "-c";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                cmd[2] = "env LC_ALL=C " + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                cmd[0] = "/bin/sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                cmd[1] = "-c";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                cmd[2] = "LC_ALL=C " + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            results = (ArrayList)AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    public Object run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                        Process proc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        BufferedReader bufferedReader = null;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 12047
diff changeset
   874
                        File f = Files.createTempFile("prn","xc").toFile();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        cmd[2] = cmd[2]+">"+f.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        proc = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                            boolean done = false; // in case of interrupt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                    proc.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                            if (proc.exitValue() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                                FileReader reader = new FileReader(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                bufferedReader = new BufferedReader(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                ArrayList results = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                while ((line = bufferedReader.readLine())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                       != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                                    results.add(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                                return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                            f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                            // promptly close all streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                            if (bufferedReader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                bufferedReader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                            proc.getInputStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                            proc.getErrorStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                            proc.getOutputStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (results == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return (String[])results.toArray(new String[results.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    private class PrinterChangeListener extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            int refreshSecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                } catch (Exception se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    IPPPrintService.debug_println(debugPrefix+"Exception in refresh thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                if ((printServices != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    (printServices.length > minRefreshTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    // compute new refresh time 1 printer = 1 sec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    refreshSecs = printServices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    refreshSecs = minRefreshTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    sleep(refreshSecs * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
}