jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 542 eb75700cdf75
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        String[] printers; /* excludes the default printer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        getDefaultPrintService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            printers = CUPSPrinter.getAllPrinters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (isSysV()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                printers = getAllPrinterNamesSysV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } else { //BSD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                printers = getAllPrinterNamesBSD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (printers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (defaultPrintService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                printServices = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                printServices[0] = defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                printServices = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ArrayList printerList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int defaultIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (int p=0; p<printers.length; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (printers[p] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if ((defaultPrintService != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                && printers[p].equals(defaultPrintService.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                printerList.add(defaultPrintService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                defaultIndex = printerList.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (printServices == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                                  "total# of printers = "+printers.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            URL serviceURL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                new URL("http://"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                        CUPSPrinter.getServer()+":"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                        CUPSPrinter.getPort()+"/"+printers[p]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            printerList.add(new IPPPrintService( printers[p],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                                                 serviceURL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                                          " getAllPrinters Exception "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                          e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        printerList.add(new UnixPrintService(printers[p]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    for (j=0; j<printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        if ((printServices[j] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            (printers[p].equals(printServices[j].getName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                            printerList.add(printServices[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            printServices[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    if (j == printServices.length) {      // not found?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                URL serviceURL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                    new URL("http://"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                        CUPSPrinter.getServer()+":"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                        CUPSPrinter.getPort()+"/"+printers[p]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                printerList.add(new IPPPrintService( printers[p],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                                                 serviceURL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                IPPPrintService.debug_println(debugPrefix+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                                              " getAllPrinters Exception "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                                              e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                            printerList.add(new UnixPrintService(printers[p]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        // Look for deleted services and invalidate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            for (int j=0; j < printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if ((printServices[j] instanceof UnixPrintService) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    (!printServices[j].equals(defaultPrintService))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    ((UnixPrintService)printServices[j]).invalidateService();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        //if defaultService is not found in printerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (defaultIndex == -1 && defaultPrintService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            //add default to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            printerList.add(defaultPrintService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            defaultIndex = printerList.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        printServices = (PrintService[])printerList.toArray(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                      new PrintService[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // swap default with the first in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (defaultIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            PrintService saveService = printServices[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            printServices[0] = printServices[defaultIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            printServices[defaultIndex] = saveService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private boolean matchesAttributes(PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                      PrintServiceAttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        Attribute [] attrs =  attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        Attribute serviceAttr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            serviceAttr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
      /* This checks for validity of the printer name before passing as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
       * parameter to a shell command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
      private boolean checkPrinterName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        for (int i=0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
          c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
          if (Character.isLetterOrDigit(c) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
              c == '-' || c == '_' || c == '.' || c == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /* On a network with many (hundreds) of network printers, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * can save several seconds if you know all you want is a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * printer, to ask for that printer rather than retrieving all printers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private PrintService getServiceByName(PrinterName nameAttr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        String name = nameAttr.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        PrintService printer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (name == null || name.equals("") || !checkPrinterName(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (isSysV()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            printer = getNamedPrinterNameSysV(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            printer = getNamedPrinterNameBSD(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private PrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        getPrintServices(PrintServiceAttributeSet serviceSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (serviceSet == null || serviceSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        /* Typically expect that if a service attribute is specified that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * its a printer name and there ought to be only one match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * Directly retrieve that service and confirm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * that it meets the other requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         * If printer name isn't mentioned then go a slow path checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * all printers if they meet the reqiremements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        PrintService[] services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        PrintService defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (name != null && (defService = getDefaultPrintService()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            /* To avoid execing a unix command  see if the client is asking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
             * for the default printer by name, since we already have that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
             * initialised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            PrinterName defName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                (PrinterName)defService.getAttribute(PrinterName.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (defName != null && name.equals(defName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (matchesAttributes(defService, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    services[0] = defService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                /* Its not the default service */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                PrintService service = getServiceByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (service != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    matchesAttributes(service, serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    services = new PrintService[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    services[0] = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            /* specified service attributes don't include a name.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            Vector matchedServices = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            for (int i = 0; i< services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (matchesAttributes(services[i], serviceSet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    matchedServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            services = new PrintService[matchedServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            for (int i = 0; i< services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                services[i] = (PrintService)matchedServices.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * If service attributes are specified then there must be additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * filtering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public PrintService[] getPrintServices(DocFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                           AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        PrintRequestAttributeSet requestSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        PrintServiceAttributeSet serviceSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (attributes != null && !attributes.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            requestSet = new HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            serviceSet = new HashPrintServiceAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Attribute[] attrs = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            for (int i=0; i<attrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (attrs[i] instanceof PrintRequestAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    requestSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                } else if (attrs[i] instanceof PrintServiceAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    serviceSet.add(attrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        PrintService[] services = getPrintServices(serviceSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (services.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            ArrayList matchingServices = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            for (int i=0; i<services.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    if (services[i].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        getUnsupportedAttributes(flavor, requestSet) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        matchingServices.add(services[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            services = new PrintService[matchingServices.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return (PrintService[])matchingServices.toArray(services);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // We only need to compare 1 PrintService because all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            // UnixPrintServices are the same anyway.  We will not use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            // default PrintService because it might be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            PrintService service = services[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if ((flavor == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                 service.isDocFlavorSupported(flavor)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                 service.getUnsupportedAttributes(flavor, requestSet) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                return services;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                return new PrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * return empty array as don't support multi docs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public MultiDocPrintService[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        getMultiDocPrintServices(DocFlavor[] flavors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                 AttributeSet attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return new MultiDocPrintService[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public synchronized PrintService getDefaultPrintService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
          security.checkPrintJobAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // clear defaultPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        IPPPrintService.debug_println("isRunning ? "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                      (CUPSPrinter.isCupsRunning()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            defaultPrinter = CUPSPrinter.getDefaultPrinter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (isSysV()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                defaultPrinter = getDefaultPrinterNameSysV();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                defaultPrinter = getDefaultPrinterNameBSD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (defaultPrinter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        defaultPrintService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            for (int j=0; j<printServices.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (defaultPrinter.equals(printServices[j].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    defaultPrintService = printServices[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (defaultPrintService == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (CUPSPrinter.isCupsRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    PrintService defaultPS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        new IPPPrintService(defaultPrinter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                            new URL("http://"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                                    CUPSPrinter.getServer()+":"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                                    CUPSPrinter.getPort()+"/"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                                    defaultPrinter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    defaultPrintService = defaultPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                defaultPrintService = new UnixPrintService(defaultPrinter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return defaultPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        getServicesInbackground(BackgroundLookupListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (printServices != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            listener.notifyServices(printServices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (lookupListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                lookupListeners = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                lookupListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                Thread lookupThread = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                lookupThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                lookupListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /* This method isn't used in most cases because we rely on code in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * javax.print.PrintServiceLookup. This is needed just for the cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * where those interfaces are by-passed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    private PrintService[] copyOf(PrintService[] inArr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (inArr == null || inArr.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return inArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            PrintService []outArr = new PrintService[inArr.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            System.arraycopy(inArr, 0, outArr, 0, inArr.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return outArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        PrintService[] services = getPrintServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            BackgroundLookupListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            for (int i=0; i<lookupListeners.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                listener =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    (BackgroundLookupListener)lookupListeners.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                listener.notifyServices(copyOf(services));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            lookupListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private String getDefaultPrinterNameBSD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        String[] names = execCmd(lpcFirstCom[cmdIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if ((cmdIndex==BSD_LPD_NG) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            (names[0].startsWith("missingprinter"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return names[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    private PrintService getNamedPrinterNameBSD(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
      String command = "/usr/sbin/lpc status " + name + lpcNameCom[cmdIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
      String[] result = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
      if (result == null || !(result[0].equals(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
      return new UnixPrintService(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    private String[] getAllPrinterNamesBSD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (cmdIndex == UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            cmdIndex = getBSDCommandIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        String[] names = execCmd(lpcAllCom[cmdIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    private String getDefaultPrinterNameSysV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        String defaultPrinter = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        String command = "/usr/bin/lpstat -d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        String [] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (names == null || names.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            return defaultPrinter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            int index = names[0].indexOf(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (index == -1  || (names[0].length() <= index+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                String name = names[0].substring(index+1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                if (name.length() == 0) {
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
                    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private PrintService getNamedPrinterNameSysV(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        String command = "/usr/bin/lpstat -v " + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        String []result = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (result == null || result[0].indexOf("unknown printer") > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return new UnixPrintService(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    private String[] getAllPrinterNamesSysV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        String defaultPrinter = "lp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        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
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        String [] names = execCmd(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        ArrayList printerNames = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        for (int i=0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if (!names[i].equals("_default") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                !names[i].equals(defaultPrinter) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                !names[i].equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                printerNames.add(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        return (String[])printerNames.toArray(new String[printerNames.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    static String[] execCmd(final String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        ArrayList results = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            final String[] cmd = new String[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if (isSysV()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                cmd[0] = "/usr/bin/sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                cmd[1] = "-c";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                cmd[2] = "env LC_ALL=C " + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                cmd[0] = "/bin/sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                cmd[1] = "-c";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                cmd[2] = "LC_ALL=C " + command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            results = (ArrayList)AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    public Object run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        Process proc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        BufferedReader bufferedReader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        File f = File.createTempFile("prn","xc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        cmd[2] = cmd[2]+">"+f.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        proc = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                            boolean done = false; // in case of interrupt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                    proc.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                            if (proc.exitValue() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                                FileReader reader = new FileReader(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                bufferedReader = new BufferedReader(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                                ArrayList results = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                                while ((line = bufferedReader.readLine())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                                       != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                    results.add(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                            f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                            // promptly close all streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            if (bufferedReader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                bufferedReader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                            proc.getInputStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                            proc.getErrorStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                            proc.getOutputStream().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (results == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return (String[])results.toArray(new String[results.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    private class PrinterChangeListener extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int refreshSecs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    refreshServices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                } catch (Exception se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    IPPPrintService.debug_println(debugPrefix+"Exception in refresh thread.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                if ((printServices != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    (printServices.length > minRefreshTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                    // compute new refresh time 1 printer = 1 sec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    refreshSecs = printServices.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    refreshSecs = minRefreshTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    sleep(refreshSecs * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
}