< prev index next >

src/java.desktop/unix/classes/sun/print/PrintServiceLookupProvider.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 440         return true;
 441       }
 442 
 443     /*
 444      * Gets the printer name compatible with the list of printers returned by
 445      * the system when we query default or all the available printers.
 446      */
 447     private String getPrinterDestName(PrintService ps) {
 448         if (isMac()) {
 449             return ((IPPPrintService)ps).getDest();
 450         }
 451         return ps.getName();
 452     }
 453 
 454     /* On a network with many (hundreds) of network printers, it
 455      * can save several seconds if you know all you want is a particular
 456      * printer, to ask for that printer rather than retrieving all printers.
 457      */
 458     private PrintService getServiceByName(PrinterName nameAttr) {
 459         String name = nameAttr.getValue();
 460         if (name == null || name.equals("") || !checkPrinterName(name)) {
 461             return null;
 462         }
 463         /* check if all printers are already available */
 464         if (printServices != null) {
 465             for (PrintService printService : printServices) {
 466                 PrinterName printerName = printService.getAttribute(PrinterName.class);
 467                 if (printerName.getValue().equals(name)) {
 468                     return printService;
 469                 }
 470             }
 471         }
 472         /* take CUPS into account first */
 473         if (CUPSPrinter.isCupsRunning()) {
 474             try {
 475                 return new IPPPrintService(name,
 476                                            new URL("http://"+
 477                                                    CUPSPrinter.getServer()+":"+
 478                                                    CUPSPrinter.getPort()+"/"+
 479                                                    name));
 480             } catch (Exception e) {


 806 
 807         String command = "/usr/bin/lpstat -v " + name;
 808         String []result = execCmd(command);
 809 
 810         if (result == null || result[0].indexOf("unknown printer") > 0) {
 811             return null;
 812         } else {
 813             return new UnixPrintService(name);
 814         }
 815     }
 816 
 817     private String[] getAllPrinterNamesSysV() {
 818         String defaultPrinter = "lp";
 819         String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort";
 820 
 821         String [] names = execCmd(command);
 822         ArrayList<String> printerNames = new ArrayList<>();
 823         for (int i=0; i < names.length; i++) {
 824             if (!names[i].equals("_default") &&
 825                 !names[i].equals(defaultPrinter) &&
 826                 !names[i].equals("")) {
 827                 printerNames.add(names[i]);
 828             }
 829         }
 830         return printerNames.toArray(new String[printerNames.size()]);
 831     }
 832 
 833     private String getDefaultPrinterNameAIX() {
 834         String[] names = execCmd(lpNameComAix[aix_lpstat_d]);
 835         // Remove headers and bogus entries added by remote printers.
 836         names = UnixPrintService.filterPrinterNamesAIX(names);
 837         if (names == null || names.length != 1) {
 838             // No default printer found
 839             return null;
 840         } else {
 841             return names[0];
 842         }
 843     }
 844 
 845     private PrintService getNamedPrinterNameAIX(String name) {
 846         // On AIX there should be no blank after '-v'.


   1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 440         return true;
 441       }
 442 
 443     /*
 444      * Gets the printer name compatible with the list of printers returned by
 445      * the system when we query default or all the available printers.
 446      */
 447     private String getPrinterDestName(PrintService ps) {
 448         if (isMac()) {
 449             return ((IPPPrintService)ps).getDest();
 450         }
 451         return ps.getName();
 452     }
 453 
 454     /* On a network with many (hundreds) of network printers, it
 455      * can save several seconds if you know all you want is a particular
 456      * printer, to ask for that printer rather than retrieving all printers.
 457      */
 458     private PrintService getServiceByName(PrinterName nameAttr) {
 459         String name = nameAttr.getValue();
 460         if (name == null || name.isEmpty() || !checkPrinterName(name)) {
 461             return null;
 462         }
 463         /* check if all printers are already available */
 464         if (printServices != null) {
 465             for (PrintService printService : printServices) {
 466                 PrinterName printerName = printService.getAttribute(PrinterName.class);
 467                 if (printerName.getValue().equals(name)) {
 468                     return printService;
 469                 }
 470             }
 471         }
 472         /* take CUPS into account first */
 473         if (CUPSPrinter.isCupsRunning()) {
 474             try {
 475                 return new IPPPrintService(name,
 476                                            new URL("http://"+
 477                                                    CUPSPrinter.getServer()+":"+
 478                                                    CUPSPrinter.getPort()+"/"+
 479                                                    name));
 480             } catch (Exception e) {


 806 
 807         String command = "/usr/bin/lpstat -v " + name;
 808         String []result = execCmd(command);
 809 
 810         if (result == null || result[0].indexOf("unknown printer") > 0) {
 811             return null;
 812         } else {
 813             return new UnixPrintService(name);
 814         }
 815     }
 816 
 817     private String[] getAllPrinterNamesSysV() {
 818         String defaultPrinter = "lp";
 819         String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':' | /usr/bin/sort";
 820 
 821         String [] names = execCmd(command);
 822         ArrayList<String> printerNames = new ArrayList<>();
 823         for (int i=0; i < names.length; i++) {
 824             if (!names[i].equals("_default") &&
 825                 !names[i].equals(defaultPrinter) &&
 826                 !names[i].isEmpty()) {
 827                 printerNames.add(names[i]);
 828             }
 829         }
 830         return printerNames.toArray(new String[printerNames.size()]);
 831     }
 832 
 833     private String getDefaultPrinterNameAIX() {
 834         String[] names = execCmd(lpNameComAix[aix_lpstat_d]);
 835         // Remove headers and bogus entries added by remote printers.
 836         names = UnixPrintService.filterPrinterNamesAIX(names);
 837         if (names == null || names.length != 1) {
 838             // No default printer found
 839             return null;
 840         } else {
 841             return names[0];
 842         }
 843     }
 844 
 845     private PrintService getNamedPrinterNameAIX(String name) {
 846         // On AIX there should be no blank after '-v'.


< prev index next >