From b88f9c59491c19f6a47aa4cfac48d43b1da5d8c6 Mon Sep 17 00:00:00 2001 From: pdanner Date: Mon, 29 Nov 2010 14:18:23 +0000 Subject: added DynamicSignatureProfile support git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@618 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../api/commons/DynamicSignatureLifetimeEnum.java | 49 ++++++ .../pdfas/api/commons/DynamicSignatureProfile.java | 68 ++++++++ .../api/commons/DynamicSignatureProfileImpl.java | 191 +++++++++++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureLifetimeEnum.java create mode 100644 src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfile.java create mode 100644 src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java diff --git a/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureLifetimeEnum.java b/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureLifetimeEnum.java new file mode 100644 index 0000000..e71c9f9 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureLifetimeEnum.java @@ -0,0 +1,49 @@ +package at.gv.egiz.pdfas.api.commons; + +import java.io.Serializable; + +/** + * Pseudo enum defining lifetime models for {@link DynamicSignatureProfile}s. + * + * @author exthex + * + */ +public final class DynamicSignatureLifetimeEnum implements Serializable { + private static final long serialVersionUID = 1L; + + private int value; + + /** + * Automatic lifetime bound to one sign process + */ + public static final DynamicSignatureLifetimeEnum AUTO = new DynamicSignatureLifetimeEnum(1); + + /** + * Manual lifetime making YOU responsible for calling {@link DynamicSignatureProfile#dispose()}. + */ + public static final DynamicSignatureLifetimeEnum MANUAL = new DynamicSignatureLifetimeEnum(2); + + private DynamicSignatureLifetimeEnum(int val) { + this.value = val; + } + + public int hashCode() { + return value; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DynamicSignatureLifetimeEnum other = (DynamicSignatureLifetimeEnum) obj; + if (value != other.value) + return false; + return true; + } + + + +} diff --git a/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfile.java b/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfile.java new file mode 100644 index 0000000..fbcfaf9 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfile.java @@ -0,0 +1,68 @@ +package at.gv.egiz.pdfas.api.commons; + +import at.gv.egiz.pdfas.api.PdfAs; +import at.gv.egiz.pdfas.api.sign.SignParameters; + +/** + * A dynamic signature profile. It is used to define a signature profile like the ones from pdf-as/config.properties at runtime. + * After creation via {@link PdfAs} you can set properties via {@link #setPropertyRaw(String, String)} + * or {@link #setFieldValue(String, String)}.
+ * You have to call {@link #apply()} to use the profile. The identifying name (e.g. for {@link SignParameters#setSignatureProfileId(String)} + * can be obtained via {@link #getName()}.
+ * Depending on the {@link DynamicSignatureLifetimeEnum} the profile can be alive and usable till you {@link #dispose()} it manually. + * + * @author exthex + * + */ +public interface DynamicSignatureProfile { + + /** + * Get the name of the dynamic signature profile. Equals the SignatureProfileId + * @return + */ + public abstract String getName(); + + /** + * Set a field value for the profile. Use {@link #setPropertyRaw(String, String)} for setting any property.
+ * For example to set sig_obj.MEIN_DYN_SIGNATURBLOCK.value.SIG_META just use SIG_META as fieldName. + * @param fieldName the name of the field + * @param value the value to set + */ + public abstract void setFieldValue(String fieldName, String value); + + /** + * Get a field value from the profile. See {@link #setFieldValue(String, String)} + * @param fieldName + * @return + */ + public abstract String getFieldValue(String fieldName); + + /** + * Set any property for the signature profile. + * Uses the same keys as the property file without the "prefix" for the profile. + * For example to set sig_obj.MEIN_DYN_SIGNATURBLOCK.key.SIG_META use key.SIG_META + * @param key property key + * @param val property value + */ + public void setPropertyRaw(String key, String val); + + /** + * Get any property from the signature profile. See {@link #setPropertyRaw(String, String)} for details. + * @param key + * @return + */ + public String getPropertyRaw(String key); + + /** + * Apply the signature profile. Call this after all properties are set and you want to use the profile. It is then added + * to the globally available signature profiles. Depending on the lifetime model {@link DynamicSignatureLifetimeEnum} you + * have to {@link #dispose()} it manually when not needed anymore. + */ + public abstract void apply(); + + /** + * Disposes the signature profile from the global store. Call this for {@link DynamicSignatureLifetimeEnum#MANUAL} only. + */ + public abstract void dispose(); + +} \ No newline at end of file diff --git a/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java b/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java new file mode 100644 index 0000000..c89b4fe --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/api/commons/DynamicSignatureProfileImpl.java @@ -0,0 +1,191 @@ +package at.gv.egiz.pdfas.api.commons; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import at.gv.egiz.pdfas.api.PdfAs; +import at.knowcenter.wag.egov.egiz.cfg.PropertyTree; +import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; +import at.knowcenter.wag.egov.egiz.sig.SignatureTypes; + +// TODO exception types? +/** + * Implementation class of the {@link DynamicSignatureProfile}. Don't use this class directly. Use {@link PdfAs} to create and the + * {@link DynamicSignatureProfile} interface for manipulation. + * @author exthex + * + */ +public class DynamicSignatureProfileImpl implements DynamicSignatureProfile { + private String name; + private Properties newProps = new Properties(); + private int dynamicTypeCounter = 0; + private static Map profiles = new HashMap(); + private static ThreadLocal localProfiles = new ThreadLocal(); + private DynamicSignatureLifetimeEnum lifeMode; + + + private DynamicSignatureProfileImpl(DynamicSignatureLifetimeEnum mode, String name) { + if (name != null) { + this.name = name; + } else { + this.name = createDynamicTypeName(); + } + this.lifeMode = mode; + } + + public static DynamicSignatureProfileImpl createFromParent(String myUniqueName, String parentProfile, DynamicSignatureLifetimeEnum mode) { + DynamicSignatureProfileImpl res = new DynamicSignatureProfileImpl(mode, myUniqueName); + res.initFromParent(parentProfile); + return res; + } + + private void store() { + if (lifeMode.equals(DynamicSignatureLifetimeEnum.MANUAL)) { + profiles.put(this.getName(), this); + } else if (lifeMode.equals(DynamicSignatureLifetimeEnum.AUTO)) { + localProfiles.set(this); + } + } + + private void remove() { + if (lifeMode.equals(DynamicSignatureLifetimeEnum.MANUAL)) { + profiles.remove(this); + } else if (lifeMode.equals(DynamicSignatureLifetimeEnum.AUTO)) { + localProfiles.remove(); + } + } + + public static void disposeLocalProfile() { + DynamicSignatureProfileImpl profile = (DynamicSignatureProfileImpl) localProfiles.get(); + if (profile != null) { + profile.dispose(); + } + } + + public static DynamicSignatureProfileImpl createEmptyProfile(String myUniqueName, DynamicSignatureLifetimeEnum mode) { + return new DynamicSignatureProfileImpl(mode, myUniqueName); + } + + public static DynamicSignatureProfileImpl loadProfile(String name) { + return (DynamicSignatureProfileImpl) profiles.get(name); + } + + private synchronized String createDynamicTypeName() { + return "dynprofile__#" + this.dynamicTypeCounter++; + } + + /* (non-Javadoc) + * @see at.gv.egiz.pdfas.api.commons.DynamicSignatureProfile#getName() + */ + public String getName() { + return name; + } + + /* (non-Javadoc) + * @see at.gv.egiz.pdfas.api.commons.DynamicSignatureProfile#setName(String) + */ + public void setName(String uniqueName) { + this.name = uniqueName; + } + + public void setPropertyRaw(String key, String val) { + this.newProps.setProperty(localPropName(key), val); + } + + public String getPropertyRaw(String key) { + return this.newProps.getProperty(localPropName(key)); + } + + private void assertPropExists(String key) { + if (!this.newProps.containsKey(localPropName(key))) { + throw new RuntimeException("property '" + key + "'not existing, cannot add one"); + } + } + + private String localPropName(String key) { + return "sig_obj." + this.name + "." + key; + } + + /* (non-Javadoc) + * @see at.gv.egiz.pdfas.api.commons.DynamicSignatureProfile#setFieldValue(java.lang.String, java.lang.String) + */ + public void setFieldValue(String fieldName, String value) { + if (SignatureTypes.isRequredSigTypeKey(fieldName)) { + throw new RuntimeException("cannot set value for pre defined signature field names"); + } + + String key = "value." +fieldName; + assertPropExists(key); + setPropertyRaw(key, value); + } + + /* (non-Javadoc) + * @see at.gv.egiz.pdfas.api.commons.DynamicSignatureProfile#getFieldValue(java.lang.String) + */ + public String getFieldValue(String fieldName) { + return getPropertyRaw("value."+fieldName); + } + + private void initFromParent(String parentProfile) { + try { + SettingsReader cfg = null; + + cfg = SettingsReader.getInstance(); + + Properties props = cfg.getProperties(); + for (Enumeration e = props.keys(); e.hasMoreElements();) { + String oldKey = (String) e.nextElement(); + if (oldKey.startsWith("sig_obj." + parentProfile + ".")) { + String newKey = oldKey.replace(parentProfile, name); + String val = props.getProperty(oldKey); + this.newProps.put(newKey, val); + } + } + this.newProps.put("sig_obj.types." + name, "on"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /* (non-Javadoc) + * @see at.gv.egiz.pdfas.api.commons.DynamicSignatureProfile#register() + */ + public synchronized void apply() { + try { + SettingsReader settings = SettingsReader.getInstance(); + + settings.getProperties().putAll(this.newProps); + + for (Enumeration e = newProps.keys(); e.hasMoreElements();) { + String key = (String) e.nextElement(); + settings.getPTree().setKeyValue(key, newProps.getProperty(key)); + } + + SignatureTypes.getInstance().addSignatureType(this.name); + store(); + } catch (Exception e) { + throw new RuntimeException("schas", e); + } + } + + /* (non-Javadoc) + * @see at.gv.egiz.pdfas.api.commons.DynamicSignatureProfile#dispose() + */ + public synchronized void dispose() { + try { + SettingsReader.getInstance().getProperties().keySet().removeAll(newProps.keySet()); + + PropertyTree root = SettingsReader.getInstance().getPTree(); + root.getSubTree("sig_obj").removeEntry(this.name); + + SignatureTypes.getInstance().removeSignatureType(this.name); + remove(); + System.err.println("DISPOSED ddd"); + } catch (Exception e) { + throw new RuntimeException("schas", e); + } + } + +} -- cgit v1.2.3