/**
* Copyright 2006 by Know-Center, Graz, Austria
* PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
* joint initiative of the Federal Chancellery Austria and Graz University of
* Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
*
* $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_;
}
}