From a95ebceaf0f77a25c26f866dff60faa6b388526c Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 20 Aug 2010 10:59:37 +0000 Subject: Internal error passed as RuntimeException instead of being ignored. PdfAsFactory: Switch implemented that allows to skip provider registration. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@589 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- src/main/java/at/gv/egiz/pdfas/PdfAsFactory.java | 53 +++++++++- .../at/gv/egiz/pdfas/impl/api/PdfAsObject.java | 38 ++++++- .../wag/egov/egiz/cfg/SettingsReader.java | 113 ++++++++++++++++----- .../wag/egov/egiz/tools/CodingHelper.java | 3 +- 4 files changed, 173 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/main/java/at/gv/egiz/pdfas/PdfAsFactory.java b/src/main/java/at/gv/egiz/pdfas/PdfAsFactory.java index 6ec16f1..ceaae85 100644 --- a/src/main/java/at/gv/egiz/pdfas/PdfAsFactory.java +++ b/src/main/java/at/gv/egiz/pdfas/PdfAsFactory.java @@ -9,6 +9,7 @@ import at.gv.egiz.pdfas.api.PdfAs; import at.gv.egiz.pdfas.api.exceptions.PdfAsException; import at.gv.egiz.pdfas.impl.api.PdfAsObject; import at.knowcenter.wag.egov.egiz.PdfAS; +import at.knowcenter.wag.egov.egiz.cfg.SettingsReader; /** * Main factory for creating a PDF-AS API Instance (PdfAs Interface). @@ -24,7 +25,8 @@ public class PdfAsFactory * * @param workDirectory * The work directory. If null the configuration is assumed to be located - * within the user's home directory. + * within the user's home directory. Note: IAIK JCE and IAIK ECC security provders are + * automatically registered. * * @return Returns an instance of the PDF-AS API. * @throws IllegalArgumentException @@ -36,12 +38,35 @@ public class PdfAsFactory */ public static PdfAs createPdfAs(File workDirectory) throws PdfAsException { - return new PdfAsObject(workDirectory); + return createPdfAs(workDirectory, SettingsReader.REGISTER_IAIK_PROVIDERS_ON_DEFAULT); + } + + /** + * Creates a PDF-AS API instance for the given work directory. + * + * @param workDirectory + * The work directory. If null the configuration is assumed to be located + * within the user's home directory. + * + * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; + * false: providers will NOT be automatically registered, providers + * needed have to be registered by the API user + * @return Returns an instance of the PDF-AS API. + * @throws IllegalArgumentException + * Thrown, if the workDirectory doesn't exist. + * @throws PdfAsException + * Thrown, if the work directory does not meet its requirements, or + * if the config file is invalid. + * @see PdfAS#USERHOME_CONFIG_FOLDER + */ + public static PdfAs createPdfAs(File workDirectory, boolean registerProvider) throws PdfAsException + { + return new PdfAsObject(workDirectory, registerProvider); } /** * Creates a PDF-AS API instance assuming that the configuration is located within the user's - * home directory. + * home directory. Note: IAIK JCE and IAIK ECC security providers are automatically registered. * * @return Returns an instance of the PDF-AS API. * @throws IllegalArgumentException @@ -53,7 +78,27 @@ public class PdfAsFactory */ public static PdfAs createPdfAs() throws PdfAsException { - return createPdfAs(null); + return createPdfAs(null); + } + + /** + * Creates a PDF-AS API instance assuming that the configuration is located within the user's + * home directory. + * + * @return Returns an instance of the PDF-AS API. + * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; + * false: providers will NOT be automatically registered, providers + * needed have to be registered by the API user + * @throws IllegalArgumentException + * Thrown, if the work directory doesn't exist within the user's home directory. + * @throws PdfAsException + * Thrown, if the work directory does not meet its requirements, or + * if the config file is invalid. + * @see PdfAS#USERHOME_CONFIG_FOLDER + */ + public static PdfAs createPdfAs(boolean registerProvider) throws PdfAsException + { + return createPdfAs(null, registerProvider); } } diff --git a/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java b/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java index 2ccc1b7..e94acfd 100644 --- a/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java +++ b/src/main/java/at/gv/egiz/pdfas/impl/api/PdfAsObject.java @@ -81,6 +81,7 @@ public class PdfAsObject implements PdfAs /** * This constructor is for internal use only - use * {@link at.gv.egiz.pdfas.PdfAsFactory} instead. + * Note: IAIK JCE and IAIK ECC security providers are automatically registered. * * @param workDirectory * The work directory. @@ -89,14 +90,32 @@ public class PdfAsObject implements PdfAs */ public PdfAsObject(File workDirectory) throws PdfAsException { - String path = workDirectory != null ? workDirectory.getPath() : null; + this(workDirectory, SettingsReader.REGISTER_IAIK_PROVIDERS_ON_DEFAULT); + } + + /** + * This constructor is for internal use only - use + * {@link at.gv.egiz.pdfas.PdfAsFactory} instead. + * + * @param workDirectory + * The work directory. + * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; + * false: providers will NOT be automatically registered, providers + * needed have to be registered by the API user + * @throws PdfAsException + * Thrown, if the configuration cannot be processed. + */ + public PdfAsObject(File workDirectory, boolean registerProvider) throws PdfAsException + { + String path = workDirectory != null ? workDirectory.getPath() : null; SettingsReader.initialize(path, path); - reloadConfig(); + reloadConfig(registerProvider); } /** * This constructor is for internal use only - use * {@link at.gv.egiz.pdfas.PdfAsFactory} instead. + * Note: IAIK JCE and IAIK ECC security providers are automatically registered. * * @throws PdfAsException * Thrown, if the configuration cannot be processed. @@ -112,7 +131,20 @@ public class PdfAsObject implements PdfAs public void reloadConfig() throws PdfAsException { ConfigUtils.initializeLogger(); - SettingsReader.createInstance(); + SettingsReader.createInstance(); + SignatureTypes.createInstance(); + } + + /** + * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; + * false: providers will NOT be automatically registered, providers + * needed have to be registered by the API user + * @see at.gv.egiz.pdfas.api.PdfAs#reloadConfig() + */ + private void reloadConfig(boolean registerProvider) throws PdfAsException + { + ConfigUtils.initializeLogger(); + SettingsReader.createInstance(registerProvider); SignatureTypes.createInstance(); } diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java index c85513c..f4c28c7 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/cfg/SettingsReader.java @@ -28,12 +28,12 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.security.Security; import java.util.ArrayList; import java.util.Enumeration; import java.util.Properties; import java.util.Vector; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.text.StrSubstitutor; import org.apache.commons.logging.Log; @@ -147,6 +147,8 @@ public class SettingsReader implements Serializable * internal help file */ private static final String HELP_TEXT_PROP_RESOURCE = "/config/help_text.properties"; + + public static final boolean REGISTER_IAIK_PROVIDERS_ON_DEFAULT = true; // /** // * The web application path @@ -319,7 +321,8 @@ public class SettingsReader implements Serializable * This method returns an synchronized instance of this class. The settings * file is read only once using this class. This method returns the instance * holding the definitions of the default settings file. Default file: - * {@link SettingsReader#CONFIG_FILE_DEFAULT_NAME}: "settings.txt" + * {@link SettingsReader#CONFIG_FILE_DEFAULT_NAME}: "settings.txt". + * Note: IAIK JCE and IAIK ECC security providers are automatically registered. * * @return an instance of the SettingsReader * @throws SettingsException @@ -335,14 +338,32 @@ public class SettingsReader implements Serializable * *

* Subsequent calls to getInstance will return the new settings. + * Note: IAIK JCE and IAIK ECC security providers are automatically registered. *

* * @throws SettingsException f.e. */ - public synchronized static void createInstance () throws SettingsException + public synchronized static void createInstance() throws SettingsException + { + instance_ = null; + getInstance(); + } + + /** + * Reloads the Settings file. + * + *

+ * Subsequent calls to getInstance will return the new settings. + *

+ * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; + * false: providers will NOT be automatically registered, providers + * needed have to be registered by the API user + * @throws SettingsException f.e. + */ + public synchronized static void createInstance(boolean registerProvider) throws SettingsException { instance_ = null; - getInstance(); + getInstance(null, registerProvider); } /** @@ -361,33 +382,75 @@ public class SettingsReader implements Serializable * * @param settingsFile * the settings file that should be load. + * @param registerProvider true: automatically registers IAIK JCE and ECC Provider; + * false: providers will NOT be automatically registered, providers + * needed have to be registered by the API user * @return an instance of the SettingsReader * @throws SettingsException * if the settings file could not be read */ - private synchronized static SettingsReader getInstance(String settingsFile) throws SettingsException + private synchronized static SettingsReader getInstance(String settingsFile, boolean registerProvider) throws SettingsException { - if (instance_ == null) - { - int length = Utils.max(new int[] { RESOURCES_PATH.length(), TMP_PATH.length(), CONFIG_PATH.length(), CERT_PATH.length() }); - - logger_.info(StringUtils.repeat("*", length + 25)); - logger_.info(" resources path = \"" + RESOURCES_PATH + "\""); - logger_.info(" configuration path = \"" + CONFIG_PATH + "\""); - logger_.info(" certstore path = \"" + CERT_PATH + "\""); - logger_.info(" temporary path = \"" + TMP_PATH + "\""); - logger_.debug(" file.encoding = \"" + System.getProperty("file.encoding") + "\""); - logger_.info(StringUtils.repeat("*", length + 25)); - - IAIK.addAsProvider(); - ECCProvider.addAsProvider(); - // Does not conform with PKIX, but is used by belgium citizen card + if (instance_ == null) + { + int length = Utils.max(new int[] { RESOURCES_PATH.length(), TMP_PATH.length(), CONFIG_PATH.length(), CERT_PATH.length() }); + + logger_.info(StringUtils.repeat("*", length + 25)); + logger_.info(" resources path = \"" + RESOURCES_PATH + "\""); + logger_.info(" configuration path = \"" + CONFIG_PATH + "\""); + logger_.info(" certstore path = \"" + CERT_PATH + "\""); + logger_.info(" temporary path = \"" + TMP_PATH + "\""); + logger_.debug(" file.encoding = \"" + System.getProperty("file.encoding") + "\""); + logger_.info(StringUtils.repeat("*", length + 25)); + + if (registerProvider) { + IAIK.addAsProvider(); + ECCProvider.addAsProvider(); + } else { + if (Security.getProvider("IAIK") == null) { + logger_.debug("Default IAIK JCE provider not registered."); + } else { + logger_.debug("IAIK JCE provider already registered."); + } + if (Security.getProvider("IAIK_ECC") == null) { + logger_.debug("Default IAIK ECC provider not registered."); + } else { + logger_.debug("IAIK ECC provider already registered."); + } + } + // Does not conform with PKIX, but is used by belgium citizen card // log.info("Registering RDN \"SERIALNUMBER\" as " + ObjectID.serialNumber + "."); - RFC2253NameParser.register("SERIALNUMBER", ObjectID.serialNumber); - - instance_ = new SettingsReader(settingsFile); - } - return instance_; + RFC2253NameParser.register("SERIALNUMBER", ObjectID.serialNumber); + + instance_ = new SettingsReader(settingsFile); + } + return instance_; + } + + /** + * This method returns an synchronized instance of this class. The settings + * file is read only once using this class. This method returns the instance + * holding the definitions of the settingsFile. If the input param + * settingsFile == null the default settings file will be load. + * Default file: {@link SettingsReader#CONFIG_FILE_DEFAULT_NAME}: + * "settings.txt". + * Note: IAIK JCE and IAIK ECC security providers are automatically registered. + * + * If an instance of this class exist, the input param is ignored! The + * SettingsReader is singleton and therefore the first + * {@link SettingsReader#getInstance()}defines the settings file that has to + * be loaded. This means changes between a application lifecyle can not be + * done! + * + * @param settingsFile + * the settings file that should be load. + * @return an instance of the SettingsReader + * @throws SettingsException + * if the settings file could not be read + */ + private static SettingsReader getInstance(String settingsFile) throws SettingsException + { + return getInstance(settingsFile, REGISTER_IAIK_PROVIDERS_ON_DEFAULT); } /** diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/tools/CodingHelper.java b/src/main/java/at/knowcenter/wag/egov/egiz/tools/CodingHelper.java index bed1cb3..af406b6 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/tools/CodingHelper.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/tools/CodingHelper.java @@ -217,8 +217,7 @@ public class CodingHelper } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - return null; + throw new RuntimeException(e); } } -- cgit v1.2.3