Skip to content

Commit

Permalink
slight rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Vzor- committed Sep 26, 2023
1 parent 04c5e80 commit 7bc4679
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
22 changes: 17 additions & 5 deletions src/qz/printer/PrintServiceMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -48,7 +61,7 @@ public static NativePrinterMap getNativePrinterList() {
}

public static NativePrinter getDefaultPrinter() {
PrintService defaultService = CachedPrintService.lookupDefaultPrintService();
PrintService defaultService = lookupDefaultPrintService();

if(defaultService == null) {
return null;
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 3 additions & 5 deletions src/qz/printer/info/CachedPrintService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package qz.printer.info;

import qz.common.CachedObject;
import qz.utils.SystemUtilities;

import javax.print.*;
import javax.print.attribute.Attribute;
Expand All @@ -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<PrintService> cachedDefault = new CachedObject<>(CachedPrintService::innerLookupDefaultPrintService, lifespan);
private static final CachedObject<PrintService[]> cachedPrintServices = new CachedObject<>(CachedPrintService::innerLookupPrintServices, lifespan);
private final CachedObject<String> cachedName;
private final CachedObject<PrintServiceAttributeSet> cachedAttributeSet;
private final HashMap<Class<?>, CachedObject> cachedAttributes = new HashMap<>();
private final HashMap<Class<?>, CachedObject<?>> cachedAttributes = new HashMap<>();

public static PrintService lookupDefaultPrintService() {
return cachedDefault.get();
Expand Down Expand Up @@ -76,13 +75,12 @@ public PrintServiceAttributeSet getAttributes() {

@Override
public <T extends PrintServiceAttribute> T getAttribute(Class<T> category) {
if (lifespan <= 0) return innerPrintService.getAttribute(category);
if (!cachedAttributes.containsKey(category)) {
Supplier<T> supplier = () -> innerPrintService.getAttribute(category);
CachedObject<T> cachedObject = new CachedObject<>(supplier, lifespan);
cachedAttributes.put(category, cachedObject);
}
return (T)cachedAttributes.get(category).get();
return category.cast(cachedAttributes.get(category).get());
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/qz/printer/info/NativePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/qz/printer/info/NativePrinterMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public ArrayList<PrintService> 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;
}
Expand Down

0 comments on commit 7bc4679

Please sign in to comment.