Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Apr 27, 2024
1 parent 6943235 commit fb9f723
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
10 changes: 0 additions & 10 deletions src/qz/printer/PrintOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ public boolean isSetService() {
public PrintService getPrintService() {
return printer.getPrintService().value();
}

public void refreshPrintService() {
String name = printer.getName();
NativePrinterMap.getInstance().remove(name);
printer = PrintServiceMatcher.matchPrinter(name);
if (printer == null) {
throw new IllegalArgumentException("Cannot find printer with name \"" + name + "\"");
}
}

public NativePrinter getNativePrinter() {
return printer;
}
Expand Down
14 changes: 14 additions & 0 deletions src/qz/printer/PrintServiceMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.print.PrintServiceLookup;
import javax.print.attribute.ResolutionSyntax;
import javax.print.attribute.standard.*;
import java.awt.print.PrinterException;
import java.util.*;

public class PrintServiceMatcher {
Expand Down Expand Up @@ -167,6 +168,19 @@ public static NativePrinter matchPrinter(String printerSearch) {
return matchPrinter(printerSearch, false);
}

/**
* Attempts to recover from a PrintService becoming unavailable
* See: https://github.com/qzind/tray/issues/1259
*/
public static void refreshPrintService(NativePrinter nativePrinter) throws IllegalArgumentException {
String name = nativePrinter.getName();
NativePrinterMap.getInstance().remove(name);

if (matchPrinter(name) == null) {
throw new IllegalArgumentException("Cannot find printer with name \"" + name + "\"");
}
}

public static JSONArray getPrintersJSON(boolean includeDetails) throws JSONException {
JSONArray list = new JSONArray();

Expand Down
24 changes: 13 additions & 11 deletions src/qz/utils/PrintingUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,26 +202,18 @@ public static void processPrintRequest(Session session, String UID, JSONObject p
PrintProcessor processor = PrintingUtilities.getPrintProcessor(format);
log.debug("Using {} to print", processor.getClass().getName());

PrintOutput output = null;
try {
PrintOutput output = new PrintOutput(params.optJSONObject("printer"));
output = new PrintOutput(params.optJSONObject("printer"));
PrintOptions options = new PrintOptions(params.optJSONObject("options"), output, format);

if(type != Type.RAW && !output.isSetService()) {
throw new Exception(String.format("%s cannot print to a raw %s", type, output.isSetFile() ? "file" : "host"));
}

processor.parseData(params.getJSONArray("data"), options);
processor.print(output, options);

try {
processor.print(output, options);
} catch (PrinterException e) {
// e.getMessage() can be null, do not reverse this comparison
if (!PRINT_SERVICE_MESSAGE.equals(e.getMessage())) throw e;
// https://github.com/qzind/tray/issues/1259
log.info("Print Failed, retrying with a new PrintService");
output.refreshPrintService();
processor.print(output, options);
}
log.info("Printing complete");

PrintSocketClient.sendResult(session, UID, null);
Expand All @@ -231,6 +223,16 @@ public static void processPrintRequest(Session session, String UID, JSONObject p
PrintSocketClient.sendError(session, UID, "Printing cancelled");
}
catch(Exception e) {
// Catch edge-case with lost PrintService reference
// See: https://github.com/qzind/tray/issues/1259
if(e instanceof PrinterException) {
if(PRINT_SERVICE_MESSAGE.equals(e.getMessage())) {
log.warn("Suppressed exception: \"{}\". Trying to recover...", PRINT_SERVICE_MESSAGE);
PrintServiceMatcher.refreshPrintService(output.getNativePrinter());
processPrintRequest(session, UID, params); // recurse
return;
}
}
log.error("Failed to print", e);
PrintSocketClient.sendError(session, UID, e);
}
Expand Down

0 comments on commit fb9f723

Please sign in to comment.