From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../wag/egov/egiz/pdf/PDFSignatureCreation.java | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java new file mode 100644 index 0000000..453103b --- /dev/null +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/PDFSignatureCreation.java @@ -0,0 +1,170 @@ +/* + * + * Copyright (c) 2006 by Know-Center, Graz, Austria + * + * + * This software is the confidential and proprietary information of Know-Center, + * Graz, Austria. You shall not disclose such Confidential Information and shall + * use it only in accordance with the terms of the license agreement you entered + * into with Know-Center. + * + * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE + * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, + * OR NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES + * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING + * THIS SOFTWARE OR ITS DERIVATIVES. + * + * $Id: PDFSignatureCreation.java,v 1.6 2006/10/31 08:09:33 wprinz Exp $ + */ +package at.knowcenter.wag.egov.egiz.pdf; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; +import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; +import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException; +import at.knowcenter.wag.egov.egiz.exceptions.SettingsException; +import at.knowcenter.wag.egov.egiz.sig.SignatureObject; + +/** + * This class provides wrapper methods to get an access to abstract PDF documents (PDFSignator). + * There exists many open source libraries and commercial libraries that can implement the abstract + * interface.
+ * This class is to load the corresponding implementation of an abstract PDFSignator class. Therefor + * it seams to be a factory. The factory settings are read from the configuration file calling the + * SettingsReader. + * + * @author wlackner + * @see at.knowcenter.wag.egov.egiz.cfg.SettingsReader + */ +public class PDFSignatureCreation { + /** + * The abstract signature object + */ + private SignatureObject sigObject_ = null; + /** + * The abstract pdf siganture object + */ + private PDFSignatureObject pdfSigObject_ = null; + /** + * The SettingsReader instance + */ + private SettingsReader settings_ = null; + /** + * The factory class prefix + */ + private final static String CLASS_PREFIX = ".PDFSignatureObject"; + /** + * The factory class prefix of the default library + */ + protected final static String DEFAULT_LIBRARY = "IText"; + /** + * The settings key defined in the settings file + * + * @see SettingsReader + */ + protected final static String SETTINGS_KEY = "pdf.signature.library"; + /** + * The logger definition. + */ + private static final Logger logger_ = ConfigLogger.getLogger(PDFSignatureCreation.class); + + /** + * Load the configuration settings. Load the corresponding class implementation for the abstract + * PDFSignature class. Init with a signature object. + * + * @param sigObject the native signature object + * @throws PDFDocumentException ErrorCode:101 + */ + public PDFSignatureCreation(SignatureObject sigObject) throws PDFDocumentException { + try { + loadSettings(); + } catch (PDFDocumentException e) { + e.setErrorCode(101); + throw e; + } + sigObject_ = sigObject; + } + + /** + * Load the factory implementation. This method trys to load the configured PDF library. + * + * @throws PDFDocumentException + */ + private PDFSignatureObject createPDFSignatureObject() throws PDFDocumentException { + PDFSignatureObject pdf_sig_object = null; + String class_name = this.getClass().getPackage().getName() + getClassName(); + Class pdf_sig_obj_class = null; + try { + pdf_sig_obj_class = Class.forName(class_name); + } catch (ClassNotFoundException e) { + if (logger_.isEnabledFor(Level.FATAL)) { + logger_.fatal("Class not found:" + class_name); + } + throw new PDFDocumentException(203, "Can not load pdf signator library", e); + } + try { + pdf_sig_object = (PDFSignatureObject) pdf_sig_obj_class.newInstance(); + } catch (InstantiationException e) { + if (logger_.isEnabledFor(Level.FATAL)) { + logger_.fatal("Can not instantiate:" + class_name); + } + throw new PDFDocumentException(203, "Can not load pdf signator library", e); + } catch (IllegalAccessException e) { + if (logger_.isEnabledFor(Level.FATAL)) { + logger_.fatal("Can not access:" + class_name); + } + throw new PDFDocumentException(203, "Can not load pdf signator library", e); + } + return pdf_sig_object; + } + + /** + * load the class settings + * + * @throws PDFDocumentException + * @see SettingsReader + */ + private void loadSettings() throws PDFDocumentException { + if (settings_ == null) { + try { + settings_ = SettingsReader.getInstance(); + } catch (SettingsException e) { + String log_message = "Can not load pdf signature settings. Cause:\n" + e.getMessage(); + logger_.error(log_message); + throw new PDFDocumentException(101, log_message, e); + } + } + } + + /** + * Read the class postfix from the configuration file + * + * @return the full qualified class name + */ + private String getClassName() { + String extract_class = settings_.getSetting(SETTINGS_KEY, DEFAULT_LIBRARY); + return CLASS_PREFIX + extract_class; + } + + /** + * Creates a new pdf signature object using the configured pdf library. + * + * @return a new pdf signature object + * @throws PDFDocumentException ErrorCode:203 + */ + public PDFSignatureObject getPDFSignatureObject() throws PDFDocumentException { + if (pdfSigObject_ == null) { + try { + pdfSigObject_ = createPDFSignatureObject(); + } catch (PDFDocumentException e) { + e.setErrorCode(203); + throw e; + } + pdfSigObject_.setSignatorObject(sigObject_); + } + return pdfSigObject_; + } +} \ No newline at end of file -- cgit v1.2.3