diff options
author | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-08-08 13:32:30 +0200 |
---|---|---|
committer | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-08-08 13:32:30 +0200 |
commit | 3679f37f129b16daadb490d6987fdd038b096bfc (patch) | |
tree | 960193629b0899d90297d5f918f337024fad1db5 /pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas | |
parent | 84a9465b14154f3e56a6b772d21e70f1c60bdf47 (diff) | |
download | pdf-over-3679f37f129b16daadb490d6987fdd038b096bfc.tar.gz pdf-over-3679f37f129b16daadb490d6987fdd038b096bfc.tar.bz2 pdf-over-3679f37f129b16daadb490d6987fdd038b096bfc.zip |
cached asynchronous pdf-as placeholders
Diffstat (limited to 'pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas')
2 files changed, 71 insertions, 1 deletions
diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java index 4356f1e0..78dda185 100644 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java @@ -83,7 +83,7 @@ public class PdfAs4SignatureParameter { /** The signature profile in use */ public Profile signatureProfile = Profile.getDefaultProfile(); - public Image getPlaceholder() { + Image getPlaceholder() { String sigProfile = getPdfAsSignatureProfileId(); String sigEmblem = (this.emblem == null ? null : this.emblem.getCachedFileName()); diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java new file mode 100644 index 00000000..b944bf24 --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java @@ -0,0 +1,70 @@ +package at.asit.pdfover.signer.pdfas; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.TreeMap; +import java.util.function.Consumer; + +import at.asit.pdfover.commons.utils.ImageUtil; + +/** + * caches placeholders for signature parameters (placeholder generation is pretty slow) + */ +public final class PdfAs4SignaturePlaceholder implements Runnable { + private static TreeMap<PdfAs4SignatureParameter, PdfAs4SignaturePlaceholder> cache = new TreeMap<>( + Comparator + .comparing(PdfAs4SignatureParameter::getPdfAsSignatureProfileId) + .thenComparing((p) -> { return (p.emblem != null) ? p.emblem.getOriginalFileHash() : ""; }) + .thenComparing((p) -> { return p.signatureNote; }, Comparator.nullsFirst(String::compareTo)) + ); + + /** + * request a placeholder for the specified parameter asynchronously + * @param callback the callback to be invoked on completion (may also be invoked before this function returns!) + */ + public static void For(PdfAs4SignatureParameter param, Consumer<PdfAs4SignaturePlaceholder> callback) { + synchronized(cache) { + cache.computeIfAbsent(param, (p) -> new PdfAs4SignaturePlaceholder(p)).AddCallback(callback); + } + } + + private final PdfAs4SignatureParameter param; + private PdfAs4SignaturePlaceholder(PdfAs4SignatureParameter param) { + this.param = param; + new Thread(this).start(); + } + + private java.awt.image.BufferedImage awtImageData; + /** AWT image data for the placeholder */ + public java.awt.image.BufferedImage getAWTImage() { return this.awtImageData; } + private org.eclipse.swt.graphics.ImageData swtImageData; + /** SWT image data for the placeholder */ + public org.eclipse.swt.graphics.ImageData getSWTImage() { return this.swtImageData; } + + private ArrayList<Consumer<PdfAs4SignaturePlaceholder>> callbacks = new ArrayList<>(); + private void AddCallback(Consumer<PdfAs4SignaturePlaceholder> c) { + synchronized (this) { + if (this.callbacks != null) + { + this.callbacks.add(c); + return; + } /* else... */ + } + /* ... else, not synchronized */ + c.accept(this); + } + + + @Override + public void run() { + this.awtImageData = (java.awt.image.BufferedImage) this.param.getPlaceholder(); + this.swtImageData = ImageUtil.convertToSWT(this.awtImageData); + ArrayList<Consumer<PdfAs4SignaturePlaceholder>> _callbacks; + synchronized (this) { + _callbacks = this.callbacks; + this.callbacks = null; + } + _callbacks.forEach((c) -> c.accept(this)); + } + +} |