Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8262731: [macOS] Exception from "Printable.print" is swallowed during…
… "PrinterJob.print"

Reviewed-by: prr
  • Loading branch information
Anton Litvinov committed Jun 14, 2021
1 parent c420735 commit c0b4407
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java
Expand Up @@ -33,6 +33,7 @@
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.atomic.AtomicReference;

import javax.print.*;
import javax.print.attribute.PrintRequestAttributeSet;
Expand Down Expand Up @@ -60,6 +61,7 @@ public final class CPrinterJob extends RasterPrinterJob {
private static String sShouldNotReachHere = "Should not reach here.";

private volatile SecondaryLoop printingLoop;
private AtomicReference<Throwable> printErrorRef = new AtomicReference<>();

private boolean noDefaultPrinter = false;

Expand Down Expand Up @@ -323,6 +325,7 @@ public void print(PrintRequestAttributeSet attributes) throws PrinterException {
performingPrinting = true;
userCancelled = false;
}
printErrorRef.set(null);

//Add support for PageRange
PageRanges pr = (attributes == null) ? null
Expand Down Expand Up @@ -381,6 +384,15 @@ public SecondaryLoop run() {
if (printingLoop != null) {
printingLoop.exit();
}

Throwable printError = printErrorRef.getAndSet(null);
if (printError != null) {
if (printError instanceof PrinterException) {
throw (PrinterException) printError;
}
throw (PrinterException)
new PrinterException().initCause(printError);
}
}

// Normalize the collated, # copies, numPages, first/last pages. Need to
Expand Down Expand Up @@ -786,22 +798,36 @@ private Object[] getPageformatPrintablePeekgraphics(final int pageIndex) {
private Rectangle2D printAndGetPageFormatArea(final Printable printable, final Graphics graphics, final PageFormat pageFormat, final int pageIndex) {
final Rectangle2D[] ret = new Rectangle2D[1];

Runnable r = new Runnable() { public void run() { synchronized(ret) {
try {
int pageResult = printable.print(graphics, pageFormat, pageIndex);
if (pageResult != Printable.NO_SUCH_PAGE) {
ret[0] = getPageFormatArea(pageFormat);
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (ret) {
try {
int pageResult = printable.print(
graphics, pageFormat, pageIndex);
if (pageResult != Printable.NO_SUCH_PAGE) {
ret[0] = getPageFormatArea(pageFormat);
}
} catch (Throwable t) {
printErrorRef.compareAndSet(null, t);
}
}
} catch (Exception e) {} // Original code bailed on any exception
}}};
}
};

if (onEventThread) {
try { EventQueue.invokeAndWait(r); } catch (Exception e) { e.printStackTrace(); }
try {
EventQueue.invokeAndWait(r);
} catch (Throwable t) {
printErrorRef.compareAndSet(null, t);
}
} else {
r.run();
}

synchronized(ret) { return ret[0]; }
synchronized (ret) {
return ret[0];
}
}

// upcall from native
Expand Down
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2021, 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.
*/

/* @test
@bug 8262731
@key headful printer
@summary Verify that "PrinterJob.print" throws the expected exception,
if "Printable.print" throws an exception.
@run main ExceptionFromPrintableIsIgnoredTest MAIN PE
@run main ExceptionFromPrintableIsIgnoredTest MAIN RE
@run main ExceptionFromPrintableIsIgnoredTest EDT PE
@run main ExceptionFromPrintableIsIgnoredTest EDT RE
*/

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;

public class ExceptionFromPrintableIsIgnoredTest {
private enum TestThreadType {MAIN, EDT}
private enum TestExceptionType {PE, RE}

private volatile Throwable printError;

public static void main(String[] args) {
if (args.length < 2) {
throw new RuntimeException("Two arguments are expected:"
+ " test thread type and test exception type.");
}

new ExceptionFromPrintableIsIgnoredTest(
TestThreadType.valueOf(args[0]),
TestExceptionType.valueOf(args[1]));
}

public ExceptionFromPrintableIsIgnoredTest(
final TestThreadType threadType,
final TestExceptionType exceptionType) {
System.out.println(String.format(
"Test started. threadType='%s', exceptionType='%s'",
threadType, exceptionType));

String osName = System.getProperty("os.name");
boolean isOSX = osName.toLowerCase().startsWith("mac");
if ((exceptionType == TestExceptionType.RE) && !isOSX) {
System.out.println(
"Currently this test scenario can be verified only on macOS.");
return;
}

printError = null;

if (threadType == TestThreadType.MAIN) {
runTest(exceptionType);
} else if (threadType == TestThreadType.EDT) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
runTest(exceptionType);
}
});
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

if (printError == null) {
throw new RuntimeException("No exception was thrown.");
} else if (!(printError instanceof PrinterException)) {
throw new RuntimeException("Unexpected exception was thrown.");
}
System.out.println("Test passed.");
}

private void runTest(final TestExceptionType exceptionType) {
PrinterJob job = PrinterJob.getPrinterJob();
if (job.getPrintService() == null) {
System.out.println("No printers are available.");
return;
}

job.setPrintable(new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException {
if (pageIndex > 1) {
return NO_SUCH_PAGE;
}
if (exceptionType == TestExceptionType.PE) {
throw new PrinterException(
"Exception from 'Printable.print'.");
} else if (exceptionType == TestExceptionType.RE) {
throw new RuntimeException(
"Exception from 'Printable.print'.");
}
return PAGE_EXISTS;
}
});

try {
job.print();
} catch (Throwable t) {
printError = t;

System.out.println("'PrinterJob.print' threw the exception:");
t.printStackTrace(System.out);
}
}
}

5 comments on commit c0b4407

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RealCLanger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u

@openjdk
Copy link

@openjdk openjdk bot commented on c0b4407 Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-c0b4407d in my personal fork of openjdk/jdk17u. To create a pull request with this backport targeting openjdk/jdk17u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit c0b4407d from the openjdk/jdk repository.

The commit being backported was authored by Anton Litvinov on 14 Jun 2021 and was reviewed by Phil Race.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u:

$ git fetch https://github.com/openjdk-bots/jdk17u RealCLanger-backport-c0b4407d:RealCLanger-backport-c0b4407d
$ git checkout RealCLanger-backport-c0b4407d
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u RealCLanger-backport-c0b4407d

@TheRealMDoerr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on c0b4407 Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheRealMDoerr the backport was successfully created on the branch TheRealMDoerr-backport-c0b4407d in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

this pull request contains a backport of commit c0b4407d from the openjdk/jdk repository.

The commit being backported was authored by Anton Litvinov on 14 Jun 2021 and was reviewed by Phil Race.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev TheRealMDoerr-backport-c0b4407d:TheRealMDoerr-backport-c0b4407d
$ git checkout TheRealMDoerr-backport-c0b4407d
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev TheRealMDoerr-backport-c0b4407d

Please sign in to comment.