diff --git a/src/qz/printer/PrintServiceMatcher.java b/src/qz/printer/PrintServiceMatcher.java index 1667f72bf..4375f072b 100644 --- a/src/qz/printer/PrintServiceMatcher.java +++ b/src/qz/printer/PrintServiceMatcher.java @@ -21,6 +21,7 @@ import qz.utils.SystemUtilities; import javax.print.PrintService; +import javax.print.PrintServiceLookup; import javax.print.attribute.ResolutionSyntax; import javax.print.attribute.standard.Media; import javax.print.attribute.standard.MediaTray; @@ -30,15 +31,27 @@ public class PrintServiceMatcher { private static final Logger log = LogManager.getLogger(PrintServiceMatcher.class); + //todo: include jdk version test for caching when JDK-7001133 is resolved + private static final boolean useCache = SystemUtilities.isMac(); // PrintService is slow, use a cache instead per JDK-7001133 public static NativePrinterMap getNativePrinterList(boolean silent, boolean withAttributes) { NativePrinterMap printers = NativePrinterMap.getInstance(); - printers.putAll(CachedPrintService.lookupPrintServices()); + printers.putAll(lookupPrintServices()); if (withAttributes) { printers.values().forEach(NativePrinter::getDriverAttributes); } if (!silent) { log.debug("Found {} printers", printers.size()); } return printers; } + private static PrintService[] lookupPrintServices() { + if (useCache) return CachedPrintService.lookupPrintServices(); + return PrintServiceLookup.lookupPrintServices(null, null); + } + + private static PrintService lookupDefaultPrintService() { + if (useCache) return CachedPrintService.lookupDefaultPrintService(); + return PrintServiceLookup.lookupDefaultPrintService(); + } + public static NativePrinterMap getNativePrinterList(boolean silent) { return getNativePrinterList(silent, false); } @@ -48,7 +61,7 @@ public static NativePrinterMap getNativePrinterList() { } public static NativePrinter getDefaultPrinter() { - PrintService defaultService = CachedPrintService.lookupDefaultPrintService(); + PrintService defaultService = lookupDefaultPrintService(); if(defaultService == null) { return null; @@ -152,15 +165,14 @@ public static NativePrinter matchPrinter(String printerSearch, boolean silent) { return use; } - public static NativePrinter - matchPrinter(String printerSearch) { + public static NativePrinter matchPrinter(String printerSearch) { return matchPrinter(printerSearch, false); } public static JSONArray getPrintersJSON(boolean includeDetails) throws JSONException { JSONArray list = new JSONArray(); - PrintService defaultService = CachedPrintService.lookupDefaultPrintService(); + PrintService defaultService = lookupDefaultPrintService(); boolean mediaTrayCrawled = false; diff --git a/src/qz/printer/info/CachedPrintService.java b/src/qz/printer/info/CachedPrintService.java index b9cc719b2..445435710 100644 --- a/src/qz/printer/info/CachedPrintService.java +++ b/src/qz/printer/info/CachedPrintService.java @@ -1,7 +1,6 @@ package qz.printer.info; import qz.common.CachedObject; -import qz.utils.SystemUtilities; import javax.print.*; import javax.print.attribute.Attribute; @@ -16,12 +15,12 @@ public class CachedPrintService implements PrintService { public PrintService innerPrintService; // PrintService.getName() is slow, use a cache instead per JDK-7001133 // TODO: Remove this comment when upstream bug report is filed - private static final long lifespan = SystemUtilities.isMac() ? CachedObject.DEFAULT_LIFESPAN : 0; + private static final long lifespan = CachedObject.DEFAULT_LIFESPAN; private static final CachedObject cachedDefault = new CachedObject<>(CachedPrintService::innerLookupDefaultPrintService, lifespan); private static final CachedObject cachedPrintServices = new CachedObject<>(CachedPrintService::innerLookupPrintServices, lifespan); private final CachedObject cachedName; private final CachedObject cachedAttributeSet; - private final HashMap, CachedObject> cachedAttributes = new HashMap<>(); + private final HashMap, CachedObject> cachedAttributes = new HashMap<>(); public static PrintService lookupDefaultPrintService() { return cachedDefault.get(); @@ -76,13 +75,12 @@ public PrintServiceAttributeSet getAttributes() { @Override public T getAttribute(Class category) { - if (lifespan <= 0) return innerPrintService.getAttribute(category); if (!cachedAttributes.containsKey(category)) { Supplier supplier = () -> innerPrintService.getAttribute(category); CachedObject cachedObject = new CachedObject<>(supplier, lifespan); cachedAttributes.put(category, cachedObject); } - return (T)cachedAttributes.get(category).get(); + return category.cast(cachedAttributes.get(category).get()); } @Override diff --git a/src/qz/printer/info/NativePrinter.java b/src/qz/printer/info/NativePrinter.java index ab1966a1e..e1d03f356 100644 --- a/src/qz/printer/info/NativePrinter.java +++ b/src/qz/printer/info/NativePrinter.java @@ -58,9 +58,9 @@ public boolean isNull() { @Override public boolean equals(Object o) { // PrintService.equals(...) is very slow in CUPS; use the pointer instead per JDK-7001133 - if (SystemUtilities.isUnix() && value instanceof PrintService ps) { + if (SystemUtilities.isUnix() && value instanceof PrintService) { //todo this needs to be more than a name check. maybe use attribute set - return ps.getName().equals(getName()); + return ((PrintService)value).getName().equals(getName()); } if (value != null) { return value.equals(o); diff --git a/src/qz/printer/info/NativePrinterMap.java b/src/qz/printer/info/NativePrinterMap.java index 6c4089320..15fb0db59 100644 --- a/src/qz/printer/info/NativePrinterMap.java +++ b/src/qz/printer/info/NativePrinterMap.java @@ -65,7 +65,6 @@ public ArrayList findMissing(PrintService[] services) { public boolean contains(PrintService service) { for (NativePrinter printer : values()) { - //todo, this is a bad if, just return if (printer.getPrintService().equals(service)) { return true; }