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