8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
authorprr
Fri, 07 Feb 2014 13:03:09 -0800
changeset 23287 c0f8cdafef56
parent 23286 b0eb66adf69d
child 23288 b7183846db97
8032693: javax.print.PrintService does not find any CUPS-Printers on Linux Reviewed-by: jgodinez, serb
jdk/src/solaris/classes/sun/print/CUPSPrinter.java
jdk/src/solaris/classes/sun/print/IPPPrintService.java
jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java
jdk/test/javax/print/PrintServiceLookup/CountPrintServices.java
--- a/jdk/src/solaris/classes/sun/print/CUPSPrinter.java	Fri Feb 07 17:32:46 2014 +0400
+++ b/jdk/src/solaris/classes/sun/print/CUPSPrinter.java	Fri Feb 07 13:03:09 2014 -0800
@@ -252,6 +252,7 @@
                             try {
                                 return urlConnection.getOutputStream();
                             } catch (Exception e) {
+                               IPPPrintService.debug_println(debugPrefix+e);
                             }
                             return null;
                         }
@@ -282,6 +283,9 @@
 
                     if (responseMap != null && responseMap.length > 0) {
                         defaultMap = responseMap[0];
+                    } else {
+                       IPPPrintService.debug_println(debugPrefix+
+                           " empty response map for GET_DEFAULT.");
                     }
 
                     if (defaultMap == null) {
@@ -310,7 +314,10 @@
 
                     if (attribClass != null) {
                         printerInfo[0] = attribClass.getStringValue();
-                        attribClass = (AttributeClass)defaultMap.get("device-uri");
+                        attribClass = (AttributeClass)
+                            defaultMap.get("printer-uri-supported");
+                        IPPPrintService.debug_println(debugPrefix+
+                          "printer-uri-supported="+attribClass);
                         if (attribClass != null) {
                             printerInfo[1] = attribClass.getStringValue();
                         } else {
--- a/jdk/src/solaris/classes/sun/print/IPPPrintService.java	Fri Feb 07 17:32:46 2014 +0400
+++ b/jdk/src/solaris/classes/sun/print/IPPPrintService.java	Fri Feb 07 13:03:09 2014 -0800
@@ -1904,9 +1904,8 @@
                                   new HashMap[respList.size()]);
             } else {
                 debug_println(debugPrefix+
-                              "readIPPResponse client error, IPP status code-"
-                                   +Integer.toHexString(response[2])+" & "
-                                   +Integer.toHexString(response[3]));
+                          "readIPPResponse client error, IPP status code: 0x"+
+                          toHex(response[2]) + toHex(response[3]));
                 return null;
             }
 
@@ -1919,6 +1918,10 @@
         }
     }
 
+    private static String toHex(byte v) {
+        String s = Integer.toHexString(v&0xff);
+        return (s.length() == 2) ? s :  "0"+s;
+    }
 
     public String toString() {
         return "IPP Printer : " + getName();
--- a/jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java	Fri Feb 07 17:32:46 2014 +0400
+++ b/jdk/src/solaris/classes/sun/print/UnixPrintServiceLookup.java	Fri Feb 07 13:03:09 2014 -0800
@@ -273,9 +273,25 @@
         String[] printers = null; // array of printer names
         String[] printerURIs = null; //array of printer URIs
 
-        getDefaultPrintService();
+        try {
+            getDefaultPrintService();
+        } catch (Throwable t) {
+            IPPPrintService.debug_println(debugPrefix+
+              "Exception getting default printer : " + t);
+        }
         if (CUPSPrinter.isCupsRunning()) {
-            printerURIs = CUPSPrinter.getAllPrinters();
+            try {
+                printerURIs = CUPSPrinter.getAllPrinters();
+                IPPPrintService.debug_println("CUPS URIs = " + printerURIs);
+                if (printerURIs != null) {
+                    for (int p = 0; p < printerURIs.length; p++) {
+                       IPPPrintService.debug_println("URI="+printerURIs[p]);
+                    }
+                }
+            } catch (Throwable t) {
+            IPPPrintService.debug_println(debugPrefix+
+              "Exception getting all CUPS printers : " + t);
+            }
             if ((printerURIs != null) && (printerURIs.length > 0)) {
                 printers = new String[printerURIs.length];
                 for (int i=0; i<printerURIs.length; i++) {
@@ -632,8 +648,10 @@
                                       (CUPSPrinter.isCupsRunning()));
         if (CUPSPrinter.isCupsRunning()) {
             String[] printerInfo = CUPSPrinter.getDefaultPrinter();
-            defaultPrinter = printerInfo[0];
-            psuri = printerInfo[1];
+            if (printerInfo != null && printerInfo.length >= 2) {
+                defaultPrinter = printerInfo[0];
+                psuri = printerInfo[1];
+            }
         } else {
             if (isMac() || isSysV()) {
                 defaultPrinter = getDefaultPrinterNameSysV();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/print/PrintServiceLookup/CountPrintServices.java	Fri Feb 07 13:03:09 2014 -0800
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import javax.print.PrintService;
+import javax.print.PrintServiceLookup;
+import javax.print.attribute.AttributeSet;
+import javax.print.attribute.HashAttributeSet;
+import javax.print.attribute.standard.PrinterName;
+
+/*
+ * @test
+ * @bug 8032693
+ * @summary Test that lpstat and JDK agree whether there are printers.
+ */
+public class CountPrintServices {
+
+  public static void main(String[] args) throws Exception {
+    String os = System.getProperty("os.name").toLowerCase();
+    System.out.println("OS is " + os);
+    if (!os.equals("linux")) {
+        System.out.println("Linux specific test. No need to continue");
+        return;
+    }
+    PrintService services[] =
+        PrintServiceLookup.lookupPrintServices(null, null);
+    if (services.length > 0) {
+       System.out.println("Services found. No need to test further.");
+       return;
+    }
+    String[] lpcmd = { "lpstat", "-a" };
+    Process proc = Runtime.getRuntime().exec(lpcmd);
+    proc.waitFor();
+    InputStreamReader ir = new InputStreamReader(proc.getInputStream());
+    BufferedReader br = new BufferedReader(ir);
+    int count = 0;
+    String printer;
+    while ((printer = br.readLine()) != null) {
+       System.out.println("lpstat:: " + printer);
+       count++;
+    }
+    if (count > 0) {
+        throw new RuntimeException("Services exist, but not found by JDK.");
+    }
+ }
+}
+