From da64c825de6ad061f5c734630faf0d6e9606dc12 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Thu, 23 Jun 2016 10:06:40 +0200 Subject: Added Profile configuration validation --- .../at/gv/egiz/pdfas/lib/api/PdfAsFactory.java | 25 ++++- .../impl/configuration/ConfigValidatorLoader.java | 108 +++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigValidatorLoader.java (limited to 'pdf-as-lib/src/main/java/at/gv/egiz') diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java index cd0a4699..72d182ed 100644 --- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/api/PdfAsFactory.java @@ -37,9 +37,10 @@ import java.io.OutputStream; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; -import java.lang.reflect.Field; import java.security.Provider; import java.security.Security; +import java.util.Map; +import java.util.Map.Entry; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -49,12 +50,16 @@ import javax.crypto.Cipher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsException; +import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsValidationException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; +import at.gv.egiz.pdfas.lib.configuration.ConfigurationValidator; import at.gv.egiz.pdfas.lib.impl.PdfAsImpl; import at.gv.egiz.pdfas.lib.impl.SignParameterImpl; import at.gv.egiz.pdfas.lib.impl.VerifyParameterImpl; +import at.gv.egiz.pdfas.lib.impl.configuration.ConfigValidatorLoader; public class PdfAsFactory implements IConfigurationConstants { @@ -359,4 +364,22 @@ public class PdfAsFactory implements IConfigurationConstants { Package pack = PdfAsFactory.class.getPackage(); return pack.getImplementationVersion(); } + + /** + * Execute all loaded Configuration Validators + * + * @throws PdfAsSettingsValidationException + */ + public static void validateConfiguration(ISettings configuration) throws PdfAsSettingsValidationException{ + Map availableValidators = ConfigValidatorLoader.getAvailableValidators(); + if(availableValidators.isEmpty()){ + logger.info("No configuration validators available"); + } + for(Entry validatorEntry : availableValidators.entrySet()){ + logger.info("Running configuration validator: "+validatorEntry.getKey()); + validatorEntry.getValue().validate(configuration); + } + logger.info("All configuration validators succeded."); + + } } diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigValidatorLoader.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigValidatorLoader.java new file mode 100644 index 00000000..0e1e2580 --- /dev/null +++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/configuration/ConfigValidatorLoader.java @@ -0,0 +1,108 @@ +package at.gv.egiz.pdfas.lib.impl.configuration; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.exceptions.ErrorConstants; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.lib.api.Configuration; +import at.gv.egiz.pdfas.lib.backend.PDFASBackend; +import at.gv.egiz.pdfas.lib.configuration.ConfigurationValidator; + +public class ConfigValidatorLoader implements ErrorConstants{ + /** The Constant logger. */ + private static final Logger logger = LoggerFactory + .getLogger(ConfigValidatorLoader.class); + + /** The configuration verifier loader. */ + private static ServiceLoader configurationValidator = ServiceLoader.load(ConfigurationValidator.class); + + /** The available verification backends. */ + private static Map availableValidators = new HashMap(); + + /** The Constant VALIDATOR_CONFIG. */ + public static final String VALIDATOR_CONFIG = "runtime.config.validator"; + + /** The default validator. */ + private static ConfigurationValidator defaultValidator = null; + + static { + logger.debug("building Configuration Validator"); + + Iterator backendIterator = configurationValidator.iterator(); + + while(backendIterator.hasNext()) { + ConfigurationValidator backend = backendIterator.next(); + logger.debug("Loading " + backend.getName() + " [" + backend.getClass().getName() + "]"); + availableValidators.put(backend.getName(), backend); + logger.debug("PDF-Backend added " + backend.getName()); + if(backend.usedAsDefault()) { + defaultValidator = backend; + logger.debug("PDF-Backend added as default " + backend.getName()); + } + } + + logger.debug("Configuration Validator Backends constructed"); + + if(defaultValidator != null) { + logger.debug("Default backend is " + defaultValidator.getName()); + } + } + + /** + * Gets the Configuration valdiator. + * + * @param configuration the configuration + * @return the validator backend + * @throws PDFASError the PDFAS error + */ + public static ConfigurationValidator getConfigurationValidator(Configuration configuration) throws PDFASError { + String backendName = configuration.getValue(VALIDATOR_CONFIG); + return getConfigurationValidator(backendName); + } + + /** + * Gets the Validator by name. + * + * @param name the name + * @return the validator backend + * @throws PDFASError the PDFAS error + */ + public static ConfigurationValidator getConfigurationValidator(String name) throws PDFASError { + if(name != null) { + if(availableValidators.containsKey(name)) { + return availableValidators.get(name); + } + throw new PDFASError(ERROR_NO_CONF_VALIDATION_BACKEND); + } + + if(defaultValidator != null) { + return defaultValidator; + } + + throw new PDFASError(ERROR_NO_CONF_VALIDATION_BACKEND); + } + + /** + * Gets the Configuration Validator. + * + * @return the validator backend + * @throws PDFASError the PDFAS error + */ + public static ConfigurationValidator getConfigurationValidator() throws PDFASError { + if(defaultValidator != null) { + return defaultValidator; + } + + throw new PDFASError(ERROR_NO_CONF_VALIDATION_BACKEND); + } + + public static Map getAvailableValidators(){ + return availableValidators; + } +} -- cgit v1.2.3