src/share/classes/sun/print/PSPrinterJob.java

Print this page
rev 4570 : [NEW BUG]: Printer spoolers ignore result from spool process
Summary: Add checking, exception for spool process failure
Contributed-by: ngmr


  51 import java.awt.print.Paper;
  52 import java.awt.print.Printable;
  53 import java.awt.print.PrinterException;
  54 import java.awt.print.PrinterIOException;
  55 import java.awt.print.PrinterJob;
  56 
  57 import javax.print.DocFlavor;
  58 import javax.print.PrintService;
  59 import javax.print.StreamPrintService;
  60 import javax.print.attribute.HashPrintRequestAttributeSet;
  61 import javax.print.attribute.PrintRequestAttributeSet;
  62 import javax.print.attribute.standard.Chromaticity;
  63 import javax.print.attribute.standard.Copies;
  64 import javax.print.attribute.standard.Destination;
  65 import javax.print.attribute.standard.DialogTypeSelection;
  66 import javax.print.attribute.standard.JobName;
  67 import javax.print.attribute.standard.Sides;
  68 
  69 import java.io.BufferedInputStream;
  70 import java.io.BufferedOutputStream;

  71 import java.io.CharConversionException;
  72 import java.io.File;
  73 import java.io.InputStream;

  74 import java.io.IOException;
  75 import java.io.FileInputStream;
  76 import java.io.FileOutputStream;
  77 import java.io.OutputStream;
  78 import java.io.PrintStream;


  79 
  80 import java.util.ArrayList;
  81 import java.util.Enumeration;
  82 import java.util.Locale;
  83 import java.util.Properties;
  84 
  85 import sun.awt.CharsetString;
  86 import sun.awt.FontConfiguration;
  87 import sun.awt.FontDescriptor;
  88 import sun.awt.PlatformFont;
  89 import sun.awt.SunToolkit;
  90 import sun.font.FontManagerFactory;
  91 import sun.font.FontUtilities;
  92 
  93 import java.nio.charset.*;
  94 import java.nio.CharBuffer;
  95 import java.nio.ByteBuffer;
  96 
  97 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
  98 import java.lang.reflect.Method;


 656                      * removed when the VM exits.
 657                      */
 658                     spoolFile = File.createTempFile("javaprint", ".ps", null);
 659                     spoolFile.deleteOnExit();
 660 
 661                 result = new FileOutputStream(spoolFile);
 662                 return result;
 663             } catch (IOException ex) {
 664                 // If there is an IOError we subvert it to a PrinterException.
 665                 pex = new PrinterIOException(ex);
 666             }
 667             return null;
 668         }
 669     }
 670 
 671     // Inner class to run "privileged" to invoke the system print command
 672 
 673     private class PrinterSpooler implements java.security.PrivilegedAction {
 674         PrinterException pex;
 675 























 676         public Object run() {
 677             try {
 678                 /**
 679                  * Spool to the printer.
 680                  */
 681                 if (spoolFile == null || !spoolFile.exists()) {
 682                    pex = new PrinterException("No spool file");
 683                    return null;
 684                 }




 685                 String fileName = spoolFile.getAbsolutePath();
 686                 String execCmd[] = printExecCmd(mDestination, mOptions,
 687                                mNoJobSheet, getJobNameInt(),
 688                                                 1, fileName);
 689 
 690                 Process process = Runtime.getRuntime().exec(execCmd);
 691                 process.waitFor();
 692                 spoolFile.delete();
 693 


 694             } catch (IOException ex) {
 695                 pex = new PrinterIOException(ex);
 696             } catch (InterruptedException ie) {
 697                 pex = new PrinterException(ie.toString());


 698             }
 699             return null;
 700         }
 701     }
 702 
 703 
 704     /**
 705      * Invoked if the application cancelled the printjob.
 706      */
 707     protected void abortDoc() {
 708         if (mPSStream != null && mDestType != RasterPrinterJob.STREAM) {
 709             mPSStream.close();
 710         }
 711         java.security.AccessController.doPrivileged(
 712             new java.security.PrivilegedAction() {
 713 
 714             public Object run() {
 715                if (spoolFile != null && spoolFile.exists()) {
 716                    spoolFile.delete();
 717                }




  51 import java.awt.print.Paper;
  52 import java.awt.print.Printable;
  53 import java.awt.print.PrinterException;
  54 import java.awt.print.PrinterIOException;
  55 import java.awt.print.PrinterJob;
  56 
  57 import javax.print.DocFlavor;
  58 import javax.print.PrintService;
  59 import javax.print.StreamPrintService;
  60 import javax.print.attribute.HashPrintRequestAttributeSet;
  61 import javax.print.attribute.PrintRequestAttributeSet;
  62 import javax.print.attribute.standard.Chromaticity;
  63 import javax.print.attribute.standard.Copies;
  64 import javax.print.attribute.standard.Destination;
  65 import javax.print.attribute.standard.DialogTypeSelection;
  66 import javax.print.attribute.standard.JobName;
  67 import javax.print.attribute.standard.Sides;
  68 
  69 import java.io.BufferedInputStream;
  70 import java.io.BufferedOutputStream;
  71 import java.io.BufferedReader;
  72 import java.io.CharConversionException;
  73 import java.io.File;
  74 import java.io.InputStream;
  75 import java.io.InputStreamReader;
  76 import java.io.IOException;
  77 import java.io.FileInputStream;
  78 import java.io.FileOutputStream;
  79 import java.io.OutputStream;
  80 import java.io.PrintStream;
  81 import java.io.PrintWriter;
  82 import java.io.StringWriter;
  83 
  84 import java.util.ArrayList;
  85 import java.util.Enumeration;
  86 import java.util.Locale;
  87 import java.util.Properties;
  88 
  89 import sun.awt.CharsetString;
  90 import sun.awt.FontConfiguration;
  91 import sun.awt.FontDescriptor;
  92 import sun.awt.PlatformFont;
  93 import sun.awt.SunToolkit;
  94 import sun.font.FontManagerFactory;
  95 import sun.font.FontUtilities;
  96 
  97 import java.nio.charset.*;
  98 import java.nio.CharBuffer;
  99 import java.nio.ByteBuffer;
 100 
 101 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
 102 import java.lang.reflect.Method;


 660                      * removed when the VM exits.
 661                      */
 662                     spoolFile = File.createTempFile("javaprint", ".ps", null);
 663                     spoolFile.deleteOnExit();
 664 
 665                 result = new FileOutputStream(spoolFile);
 666                 return result;
 667             } catch (IOException ex) {
 668                 // If there is an IOError we subvert it to a PrinterException.
 669                 pex = new PrinterIOException(ex);
 670             }
 671             return null;
 672         }
 673     }
 674 
 675     // Inner class to run "privileged" to invoke the system print command
 676 
 677     private class PrinterSpooler implements java.security.PrivilegedAction {
 678         PrinterException pex;
 679 
 680         private void handleProcessFailure(final Process failedProcess,
 681                 final String[] execCmd, final int result) throws IOException {
 682             try (StringWriter sw = new StringWriter();
 683                     PrintWriter pw = new PrintWriter(sw)) {
 684                 pw.append("error=").append(Integer.toString(result));
 685                 pw.append(" running:");
 686                 for (String arg: execCmd) {
 687                     pw.append(" '").append(arg).append("'");
 688                 }
 689                 try (InputStream is = failedProcess.getErrorStream();
 690                         InputStreamReader isr = new InputStreamReader(is);
 691                         BufferedReader br = new BufferedReader(isr)) {
 692                     while (br.ready()) {
 693                         pw.println();
 694                         pw.append("\t\t").append(br.readLine());
 695                     }
 696                 } finally {
 697                     pw.flush();
 698                     throw new IOException(sw.toString());
 699                 }
 700             }
 701         }
 702 
 703         public Object run() {




 704             if (spoolFile == null || !spoolFile.exists()) {
 705                pex = new PrinterException("No spool file");
 706                return null;
 707             }
 708             try {
 709                 /**
 710                  * Spool to the printer.
 711                  */
 712                 String fileName = spoolFile.getAbsolutePath();
 713                 String execCmd[] = printExecCmd(mDestination, mOptions,
 714                                mNoJobSheet, getJobNameInt(),
 715                                                 1, fileName);
 716 
 717                 Process process = Runtime.getRuntime().exec(execCmd);
 718                 process.waitFor();
 719                 final int result = process.exitValue();
 720                 if (0 != result) {
 721                     handleProcessFailure(process, execCmd, result);
 722                 }
 723             } catch (IOException ex) {
 724                 pex = new PrinterIOException(ex);
 725             } catch (InterruptedException ie) {
 726                 pex = new PrinterException(ie.toString());
 727             } finally {
 728                 spoolFile.delete();
 729             }
 730             return null;
 731         }
 732     }
 733 
 734 
 735     /**
 736      * Invoked if the application cancelled the printjob.
 737      */
 738     protected void abortDoc() {
 739         if (mPSStream != null && mDestType != RasterPrinterJob.STREAM) {
 740             mPSStream.close();
 741         }
 742         java.security.AccessController.doPrivileged(
 743             new java.security.PrivilegedAction() {
 744 
 745             public Object run() {
 746                if (spoolFile != null && spoolFile.exists()) {
 747                    spoolFile.delete();
 748                }