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