diff options
author | Martin Bonato <mbonato@datentechnik-innovation.com> | 2015-04-09 13:24:55 +0200 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:09:55 +0200 |
commit | 0fb4c31f049d71e917dfbfdab96553a807195d0c (patch) | |
tree | edeb1c502de674320928feb71bcd83c3056d135e /id/server/moa-id-commons/src/main/java/at/gv | |
parent | 6d1f99a50744de690afccc12568539c23d839f93 (diff) | |
download | moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.tar.gz moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.tar.bz2 moa-id-spss-0fb4c31f049d71e917dfbfdab96553a807195d0c.zip |
Rename java packages
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv')
12 files changed, 1058 insertions, 4 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java new file mode 100644 index 000000000..d8fde7eee --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/ConfigurationUtil.java @@ -0,0 +1,227 @@ +package at.gv.egovernment.moa.id.commons.config; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import at.gv.egovernment.moa.id.commons.config.persistence.Configuration; +import at.gv.egovernment.moa.id.commons.config.persistence.JsonMapper; +import at.gv.egovernment.moa.id.commons.db.MOAIDConfigurationConstants; +import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration; + +import com.fasterxml.jackson.core.JsonProcessingException; + +public class ConfigurationUtil { + + final boolean isOverwriteData; + + public ConfigurationUtil(boolean isOverwriteData){ + this.isOverwriteData = isOverwriteData; + } + + /** + * Read an input MOAID 2 XML file, transfer it to properties and write the + * properties to a MOAID 3 property file. + * + * @param inStream + * the input stream to read from. + * @param outFile + * the output file to write to. + * @throws JAXBException + */ + public void readFromXMLFileConvertToPropertyFile(FileInputStream inStream, File outFile) throws JAXBException { + + try (FileOutputStream outStream = new FileOutputStream(outFile);) { + + // get config from xml file + JAXBContext jc = JAXBContext.newInstance("at.gv.egovernment.moa.id.commons.db.dao.config"); + Unmarshaller m = jc.createUnmarshaller(); + MOAIDConfiguration config = (MOAIDConfiguration) m.unmarshal(inStream); + + // serialize config to JSON properties + Properties result = moaIdConfigToJsonProperties(config); + + // write to output stream + result.store(outStream, null); + + } catch (FileNotFoundException e) { + System.out.println("Could not find the output file."); + System.exit(1); + } catch (IOException e) { + System.out.println("Could not write to the output file."); + System.exit(1); + } + } + + /** + * Helper method to serialize a {@link MOAIDConfiguration} to Properties + * with JSON encoded values. + * + * @param config + * the MOAIDConfiguration to serialize + * @return {@link Properties} containing the database key and the serialized + * values + * @throws JsonProcessingException + * is thrown if problem occurred while serializing one of the + * database values + */ + private Properties moaIdConfigToJsonProperties(MOAIDConfiguration config) throws JsonProcessingException { + + Properties result = new Properties(); + boolean prettyPrint = true; + JsonMapper mapper = new JsonMapper(prettyPrint); + + // serialize config to JSON + String oaJson = mapper.serialize(config.getOnlineApplication()); + String authCompGeneralJson = mapper.serialize(config.getAuthComponentGeneral()); + String chainingModeJson = mapper.serialize(config.getChainingModes()); + String defaultBKUJson = mapper.serialize(config.getDefaultBKUs()); + String genericConfigJson = mapper.serialize(config.getGenericConfiguration()); + String pvp2RefreshJson = mapper.serialize(config.getPvp2RefreshItem()); + String slRequestTemplatesJson = mapper.serialize(config.getSLRequestTemplates()); + String timestampJson = mapper.serialize(config.getTimestampItem()); + String trustedCaCertJson = mapper.serialize(config.getTrustedCACertificates()); + + // add to properties + result.put(MOAIDConfigurationConstants.ONLINE_APPLICATIONS_KEY, oaJson); + result.put(MOAIDConfigurationConstants.AUTH_COMPONENT_GENERAL_KEY, authCompGeneralJson); + result.put(MOAIDConfigurationConstants.CHAINING_MODES_KEY, chainingModeJson); + result.put(MOAIDConfigurationConstants.DEFAULT_BKUS_KEY, defaultBKUJson); + result.put(MOAIDConfigurationConstants.GENERIC_CONFIGURATION_KEY, genericConfigJson); + result.put(MOAIDConfigurationConstants.PVP2REFRESH_ITEM_KEY, pvp2RefreshJson); + result.put(MOAIDConfigurationConstants.SLREQUEST_TEMPLATES_KEY, slRequestTemplatesJson); + result.put(MOAIDConfigurationConstants.TIMESTAMP_ITEM_KEY, timestampJson); + result.put(MOAIDConfigurationConstants.TRUSTED_CERTIFICATES_KEY, trustedCaCertJson); + + return result; + } + + /** + * Exports a key-value database to a property file, where keys are the same + * as in the database, and the values are serialized JSON objects. + * + * @param inputDBConfigFilePath + * the path to the database properties, for the db the data is + * read from. + * @param outFile + * the destination file for the exported data. + */ + public void readFromDBWriteToFile(String inputDBConfigFilePath, File outFile) { + + try (FileOutputStream outStream = new FileOutputStream(outFile);) { + + Properties result = new Properties(); + + System.getProperties().setProperty("location", "file:" + inputDBConfigFilePath); + ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); + Configuration dbConfiguration = (Configuration) context.getBean("config"); + boolean prettyPrint = true; + at.gv.egovernment.moa.id.commons.config.persistence.JsonMapper mapper = new JsonMapper(prettyPrint); + + for (String key : MOAIDConfigurationConstants.getAllMOAIDConfigurationKeys()) { + + // extract database value + Object value = dbConfiguration.get(key); + + // serialize value to JSON + String json = mapper.serialize(value); + + // add to properties + result.setProperty(key, json); + } + + // write to output stream + result.store(outStream, null); + + System.out.println("Property configuration written to:"); + System.out.println(outFile.getAbsolutePath()); + + } catch (FileNotFoundException e) { + System.out.println("Could not find the output file."); + System.exit(1); + } catch (IOException e) { + System.out.println("Could not write to the output file."); + System.exit(1); + } + } + + /** + * Read an input property file, deserialize it's values and write them to + * the given database. + * + * @param inStream + * the FileInputStream to read from. + * @param outputDBConfigFilePath + * the path to the database properties, for the db which is + * written. + * @throws IOException + * is thrown in case the properties could not be loaded from the + * stream + */ + public void readFromFileWriteToDB(FileInputStream inStream, String outputDBConfigFilePath) throws IOException { + + Properties inProperties = new Properties(); + inProperties.load(inStream); + + System.getProperties().setProperty("location", "file:" + outputDBConfigFilePath); + ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); + Configuration dbConfiguration = (Configuration) context.getBean("config"); + boolean prettyPrint = true; + JsonMapper mapper = new JsonMapper(prettyPrint); + + List<String> keys = dbConfiguration.getAllKeys(); + + if (keys == null) { + System.out.println("Database can not be read."); + System.exit(1); + } + + if (!keys.isEmpty() && !isOverwriteData) { + System.out.println("The database already contains configuration data."); + System.out.println("Use force switch if you want to override data)"); + System.exit(1); + } + + if (isOverwriteData) { + // remove existing entries + for (String key : keys) { + dbConfiguration.set(key, null); + } + } + + Enumeration<?> propertyNames = inProperties.propertyNames(); + + while (propertyNames.hasMoreElements()) { + String key = (String) propertyNames.nextElement(); + // extract database value + String json = inProperties.getProperty(key); + + // deserialize value to object + Object value = mapper.deserialize(json, null); + + // add to database + boolean result = dbConfiguration.set(key, value); + if (!result) { + System.out.println("Could NOT persist the configuration file's information in the database."); + } + } + System.out.println("Data has been successfully written to the database."); + } + + private static void readFromDBWriteToDB(String inputDBConfigFilePath, String outputDBConfigFilePath) { + //TODO: implement + } + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/MigrateConfiguration.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/MigrateConfiguration.java new file mode 100644 index 000000000..4e8c7dffd --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/MigrateConfiguration.java @@ -0,0 +1,103 @@ +package at.gv.egovernment.moa.id.commons.config; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.xml.bind.JAXBException; + +import at.gv.egovernment.moa.id.commons.config.cli.MOAIDConfCLI; +import at.gv.egovernment.moa.id.commons.config.cli.MigrateConfigurationParams; + +/** + * CLI tool which is able to perform the following tasks: + * <ul> + * <li>transform a MoaID 2 XML configuration XML file to a MoaID 3 property file + * </li> + * <li>read a property file and transfer it's content to a database</li> + * <li>write the content of a database to a property file</li> + * </ul> + */ +public class MigrateConfiguration { + + public static void main(String[] args) { + + MOAIDConfCLI cli = new MOAIDConfCLI(); + MigrateConfigurationParams parsedParameters = cli.parse(args); + + // consider settings of force switch + boolean isOverwriteData = parsedParameters.isOverwriteData(); + ConfigurationUtil configUtil = new ConfigurationUtil(isOverwriteData); + + if (!parsedParameters.isInputDB() && (parsedParameters.getInputTarget() != null)) { + // read input from file + workWithInputFromFile(parsedParameters.getInputTarget(), parsedParameters, configUtil); + + } else if (parsedParameters.getInputDBConfig() != null) { + // read input from database + workWithImputFromDB(parsedParameters, configUtil); + + } else { + System.exit(1); + } + } + + /** + * Handle the case where input from a file is read. + * + * @param inputFileUrl + * the url of the input file. + * @param parsedParameters + * the command line parameters. + * @param configUtil + * the class for working with the configuration. + */ + private static void workWithInputFromFile(String inputFileUrl, MigrateConfigurationParams parsedParameters, + ConfigurationUtil configUtil) { + File inFile = new File(inputFileUrl); + try (FileInputStream inStream = new FileInputStream(inFile);) { + + if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { + // input from file and output to a file is desired + File outFile = new File(parsedParameters.getOutputFile()); + configUtil.readFromXMLFileConvertToPropertyFile(inStream, outFile); + + } else if (parsedParameters.getOutputDBConfig() != null) { + // input from file and output to a database is desired + configUtil.readFromFileWriteToDB(inStream, parsedParameters.getOutputDBConfig()); + } + } catch (JAXBException e) { + System.out.println("MOA-ID XML configuration can not be loaded from given file."); + System.exit(1); + } catch (FileNotFoundException e) { + System.out.println("Could not find the input file."); + System.exit(1); + } catch (IOException e) { + System.out.println("Could not read from the input file."); + System.exit(1); + } + } + + /** + * Handle the case where input is read from a database. + * + * @param parsedParameters + * the command line parameters. + * @param configUtil + * the class for working with the configuration. + */ + private static void workWithImputFromDB(MigrateConfigurationParams parsedParameters, ConfigurationUtil configUtil) { + if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { + // input from database and output to a file is desired + File outFile = new File(parsedParameters.getOutputFile()); + String inputDBConfigFilePath = parsedParameters.getInputDBConfig(); + configUtil.readFromDBWriteToFile(inputDBConfigFilePath, outFile); + + } else if (parsedParameters.getOutputDBConfig() != null) { + // input from database and output to a database is desired + // configUtil.readFromDBWriteToDB(inDBConfigFilePath, + // outDBConfigFilePath); + } + } +}
\ No newline at end of file diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/CLIConstants.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/CLIConstants.java new file mode 100644 index 000000000..c652645fc --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/CLIConstants.java @@ -0,0 +1,37 @@ +package at.gv.egovernment.moa.id.commons.config.cli; + +/** + * Constants for the CLI. + * @author Christian Wagner + * + */ +public class CLIConstants { + private CLIConstants() { + } + + public static final String CMD_LINE_SYNTAX = "java -jar migrateMOAIDconfiguration.jar"; + + public static final String HELP_HEADER = "Convert a given MOAID 2.x config-file."; + public static final String HELP_FOOTER = ""; + // default width of a printed row + public static final int HELP_ROW_WIDTH = 80; + + public static final int HELP_SPACE_BEFORE_OPT = 2; + public static final int HELP_SPACE_BEFORE_DESC = 4; + + public static final String CLI_PARAM_IN = "in"; + public static final String CLI_PARAM_IN_LONG = "input-file"; + public static final String CLI_PARAM_OUT = "out"; + public static final String CLI_PARAM_OUT_LONG = "output-file"; + public static final String CLI_PARAM_INDB = "indb"; + public static final String CLI_PARAM_INDB_LONG = "input-dbconf"; + public static final String CLI_PARAM_OUTDB = "outdb"; + public static final String CLI_PARAM_OUTDB_LONG = "output-dbconf"; + + public static final String CLI_PARAM_HELP = "h"; + public static final String CLI_PARAM_HELP_LONG = "help"; + + public static final String CLI_PARAM_FORCE = "f"; + public static final String CLI_PARAM_FORCE_LONG = "force"; + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/MOAIDConfCLI.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/MOAIDConfCLI.java new file mode 100644 index 000000000..f2753c3d0 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/MOAIDConfCLI.java @@ -0,0 +1,127 @@ +package at.gv.egovernment.moa.id.commons.config.cli; + +import java.io.OutputStream; +import java.io.PrintWriter; + +import org.apache.commons.cli.BasicParser; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The command-line interface for MOAID configuration migration + * @author Christian Wagner + * + */ +public class MOAIDConfCLI { + + // the default output to write usage information and help text to + private static final OutputStream OUTPUT_STREAM = System.out; + + private Logger log = LoggerFactory.getLogger(getClass()); + + /** + * Parses the given command-line arguments using a {@link BasicParser} with small modifications. + * @param commandLineArgs the command-line arguments. + */ + public MigrateConfigurationParams parse(String[] commandLineArgs) { + + CommandLineParser parser = new BasicParser(); + CommandLine cmd = null; + MigrateConfigurationParams result = null; + try { + + if (null == commandLineArgs || commandLineArgs.length == 0) { + printUsage(OUTPUT_STREAM, true); + System.exit(0); + } + + cmd = parser.parse(createOptions(), commandLineArgs, true); + + if( null != cmd && cmd.hasOption(CLIConstants.CLI_PARAM_HELP)){ + printUsage(OUTPUT_STREAM, true); + System.exit(0); + } + + result = new MigrateConfigurationParams(cmd); + + } catch (ParseException e) { + log.warn("Encountered exception while parsing: {}", e.getMessage()); + System.err.println(e.getMessage()); + printUsage(OUTPUT_STREAM, false); + System.exit(1); + } + return result; + } + + /** + * Prints information about the usage to the given output. + * @param out the {@link OutputStream} to write to + * @param printOptions determines whether the available options are printed + */ + private void printUsage(OutputStream out, boolean printOptions) { + + PrintWriter pOut = new PrintWriter(out); + + HelpFormatter formatter = new HelpFormatter(); + pOut.println(); + pOut.println("usage: " + CLIConstants.CMD_LINE_SYNTAX + " -" + CLIConstants.CLI_PARAM_FORCE + " -" + + CLIConstants.CLI_PARAM_IN + " <inputfile> | -" + CLIConstants.CLI_PARAM_INDB + " <dbconfig> -" + + CLIConstants.CLI_PARAM_OUT + " <outputfile> | -" + CLIConstants.CLI_PARAM_OUTDB + " <dbconfig> [-" + + CLIConstants.CLI_PARAM_HELP + "]"); + pOut.println(); + pOut.println(CLIConstants.HELP_HEADER); + if(printOptions){ + pOut.println(); + formatter.printOptions(pOut, CLIConstants.HELP_ROW_WIDTH, createOptions(), CLIConstants.HELP_SPACE_BEFORE_OPT, CLIConstants.HELP_SPACE_BEFORE_DESC); + } + pOut.flush(); + + } + + /** + * Create all {@linkplain Option options} that should be available in the CLI. + * @return The {@linkplain Options options} + */ + private Options createOptions() { + + Options options = new Options(); + + OptionGroup inGroup = new OptionGroup(); + Option optionInput = new Option(CLIConstants.CLI_PARAM_IN, CLIConstants.CLI_PARAM_IN_LONG, true, "MOAID config-file to convert"); + optionInput.setArgName("inputfile"); + Option optionDBInput = new Option(CLIConstants.CLI_PARAM_INDB, CLIConstants.CLI_PARAM_INDB_LONG, true, "config for database to read from"); + optionDBInput.setArgName("dbconfig"); + + inGroup.addOption(optionDBInput); + inGroup.addOption(optionInput); + optionInput.setRequired(false); + + OptionGroup outGroup = new OptionGroup(); + Option optionOutput = new Option(CLIConstants.CLI_PARAM_OUT, CLIConstants.CLI_PARAM_OUT_LONG, true, "target file to write to"); + optionOutput.setArgName("outputfile"); + Option optionDBOutput = new Option(CLIConstants.CLI_PARAM_OUTDB, CLIConstants.CLI_PARAM_OUTDB_LONG, true, "config for database to write to"); + optionDBOutput.setArgName("dbconfig"); + + outGroup.addOption(optionDBOutput); + outGroup.addOption(optionOutput); + outGroup.setRequired(false); + + options.addOptionGroup(inGroup); + options.addOptionGroup(outGroup); + + Option optForce = new Option(CLIConstants.CLI_PARAM_FORCE, CLIConstants.CLI_PARAM_FORCE_LONG, false, "overwrite existing data with imported data"); + options.addOption(optForce); + + Option optHelp = new Option(CLIConstants.CLI_PARAM_HELP, CLIConstants.CLI_PARAM_HELP_LONG, false, "prints this message"); + options.addOption(optHelp); + return options; + } + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/MigrateConfigurationParams.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/MigrateConfigurationParams.java new file mode 100644 index 000000000..86bde1310 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/cli/MigrateConfigurationParams.java @@ -0,0 +1,106 @@ +package at.gv.egovernment.moa.id.commons.config.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.MissingOptionException; + +/** + * The result set for the parsed command line arguments + * @author Christian Wagner + * + */ +public class MigrateConfigurationParams { + + private String inputFile = null; + private String outputFile = null; + private String inputDbConfigFile = null; + private String outputDbConfigFile = null; + + private boolean overwriteData = false; + + /** + * Get the path to the input source which is MOAID 2.x config file in XML-format. + * @return the path to the input source or {@code null} if not set. + */ + public String getInputTarget() { + return this.inputFile; + } + + /** + * Get the path to the output file to write to. + * @return the path to the output file or {@code null} if not set. + */ + public String getOutputFile() { + return outputFile; + } + + /** + * Get the path to the configuration file for the input database. + * @return the path to the config file or {@code null} if not set. + */ + public String getInputDBConfig() { + return inputDbConfigFile; + } + + /** + * Get the path to the configuration file for the output database. + * @return the path to the config file or {@code null} if not set. + */ + public String getOutputDBConfig() { + return outputDbConfigFile; + } + + /** + * Returns whether the desired input is a config file for a database. + * @return <code>true</code> if the stored path points at a database config file; <code>false</code> otherwise. + */ + public boolean isInputDB() { + return inputDbConfigFile != null; + } + + /** + * Returns whether the desired output is a config file for a database. + * @return <code>true</code> if the stored path points at a database config file; <code>false</code> otherwise. + */ + public boolean isOutputDB() { + return outputDbConfigFile != null; + } + + /** + * Returns whether existing data should be overwritten by the imported data or not. + * @return <code>true</code> if the existing data should be overwritten; <code>false</code> otherwise. + */ + public boolean isOverwriteData() { + return overwriteData; + } + + /** + * + * @param cmdLine + * @throws MissingOptionException + */ + public MigrateConfigurationParams(CommandLine cmdLine) throws MissingOptionException { + inputFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_IN); + inputDbConfigFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_INDB); + outputFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_OUT); + outputDbConfigFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_OUTDB); + overwriteData = cmdLine.hasOption(CLIConstants.CLI_PARAM_FORCE); + + if (null == inputFile && null == inputDbConfigFile) { + throw new MissingOptionException("One of [-" + CLIConstants.CLI_PARAM_IN + ", -" + CLIConstants.CLI_PARAM_INDB + "] required."); + } + + if (null == outputFile && null == outputDbConfigFile) { + throw new MissingOptionException("One of [-" + CLIConstants.CLI_PARAM_OUT + ", -" + CLIConstants.CLI_PARAM_OUTDB + "] required."); + } + + if (null != inputFile && null != inputDbConfigFile) { + throw new MissingOptionException("Only one of [-" + CLIConstants.CLI_PARAM_IN + ", -" + CLIConstants.CLI_PARAM_INDB + "] allowed."); + } + + if (null != outputFile && null != outputDbConfigFile) { + throw new MissingOptionException("Only one of [-" + CLIConstants.CLI_PARAM_OUT + ", -" + CLIConstants.CLI_PARAM_OUTDB + "] allowed."); + } + + } + +}
\ No newline at end of file diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/Configuration.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/Configuration.java new file mode 100644 index 000000000..f357fc570 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/Configuration.java @@ -0,0 +1,60 @@ +package at.gv.egovernment.moa.id.commons.config.persistence; + +import java.util.List; + +/** + * An interface for a key-value configuration. + */ +public interface Configuration { + + /** + * Gets all keys in the database. NOTE: may return an empty list or {@code null}. + * @return a List containing all keys in the database or {@code null}. + */ + List<String> getAllKeys(); + + /** + * Get the value associated with the given key as {@link Object}. + * @param key the key + * @return the object associated with the given key or {@code null} if the key does not exist or does not have a value. + */ + Object get(String key); + + /** + * Get the object of type {@code T} associated with the given key. + * + * @param key the key + * @param clazz the type of the requested object + * @return the object associated with the given key or {@code null} if the key does not exist or does not have a value. + */ + <T> T get(String key, Class<T> clazz); + + /** + * Store an object associated with a key. If the given object is set to {@code null} then the entry associated with the key is deleted. + * + * @param key the key under which the value is stored, respectively key determining the entry to be deleted. + * @param value the object to store. if value is set to {@code null} then the entry associated with key {@code key} is deleted. + * @return {@code true} if the operation was carried out successfully, {@code false} otherwise. + */ + boolean set(String key, Object value); + + /** + * Get the object of type {@code T} associated with the given key from the database. If the key does not exist or does not have a value, the given default + * value is returned. + * + * @param key the key + * @param clazz the type of the requested object + * @param defaultValue the default value to return + * @return the object associated with the given key or {@code defaultValue} if the key does not exist or does not have a value. + */ + <T> T get(String key, Class<T> clazz, Object defaultValue); + + /** + * Get a list of objects associated with the given key. The list may be empty or contain only a single object. + * @param key the key + * @param clazz the type of the requested object + * @return a list containing objects of type {@code T} or an empty list if no objects are associated with the key. + */ + <T> List<T> getList(String key, Class<T> clazz); + +}
\ No newline at end of file diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/ConfigurationImpl.java new file mode 100644 index 000000000..c90b60440 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/ConfigurationImpl.java @@ -0,0 +1,161 @@ +package at.gv.egovernment.moa.id.commons.config.persistence; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.EntityExistsException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Required; +import org.springframework.stereotype.Component; + +import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigProperty; +import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigPropertyDao; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.CollectionType; +import com.fasterxml.jackson.databind.type.TypeFactory; + +/** + * The implementation of a key-value configuration implementing the {@link Configuration} interface. + * It employs the {@link ConfigPropertyDao} to persist configuration data. + */ +@Component +public class ConfigurationImpl implements Configuration { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + ConfigPropertyDao configPropertyDao; + private JsonMapper mapper = new JsonMapper(); + + /** + * Sets the {@link ConfigPropertyDao}. + * @param configPropertyDao the ConfigPropertyDao + */ + @Required + public void setConfigPropertyDao(ConfigPropertyDao configPropertyDao) { + this.configPropertyDao = configPropertyDao; + } + + @Override + public List<String> getAllKeys(){ + try { + return this.configPropertyDao.getAllKeys(); + } catch (Exception e) { + log.debug("Error while retrieving a list of all keys in the database."); + return null; + } + } + + @Override + public Object get(String key) { + // return null if key does not exist + try { + ConfigProperty property = configPropertyDao.getProperty(key); + if (property != null && property.getValue() != null) { + return mapper.deserialize(property.getValue(), null); + } else { + return null; + } + } catch (IllegalArgumentException e) { + log.debug("Error while searching for key '{}' in the database.", key); + return null; + } catch (Exception e) { + log.debug("Error while deserializing value of key '{}' to object.", key); + return null; + } + } + + @Override + public <T> T get(String key, Class<T> clazz) { + // return null if key does not exist + try { + ConfigProperty property = configPropertyDao.getProperty(key); + if (property != null && property.getValue() != null) { + return clazz.cast(mapper.deserialize(property.getValue(), clazz)); + } else { + return null; + } + } catch (IllegalArgumentException e) { + log.debug("Error while searching for key '{}' in the database.", key); + return null; + } catch (Exception e) { + log.debug("Error while deserializing value of key '{}' to object of type {}.", key, clazz.getClass()); + return null; + } + } + + @Override + public boolean set(String key, Object value) { + + try { + if (value == null) { + configPropertyDao.delete(key); + return true; + } else { + + ConfigProperty keyValue = new ConfigProperty(); + keyValue.setKey(key); + + keyValue.setValue(mapper.serialize(value)); + configPropertyDao.saveProperty(keyValue); + return true; + } + } catch (JsonProcessingException e) { + log.debug("Error while serializing object for key '{}'.", key); + return false; + } catch (EntityExistsException e) { + log.debug("Property '{}' already exists!", key); + return false; + } catch (Exception e) { + log.debug("Error while setting value for key '{}' in the database.", key); + return false; + } + } + + @Override + public <T> T get(String key, Class<T> clazz, Object defaultValue) { + + T value = get(key, clazz); + if (value != null) { + return value; + } else { + return clazz.cast(defaultValue); + } + } + + @SuppressWarnings("unchecked") + @Override + public <T> List<T> getList(String key, Class<T> clazz) { + + CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz); + try { + if ((configPropertyDao.getProperty(key) == null) + || (configPropertyDao.getProperty(key).getValue() == null)) { + return new ArrayList<T>(); + } + String json = configPropertyDao.getProperty(key).getValue(); + ObjectMapper mapper = new ObjectMapper(); + + return (List<T>) mapper.readValue(json, listType); + } catch (JsonMappingException e) { + ArrayList<T> tmp = new ArrayList<T>(); + T value = get(key, clazz); + if (value != null) { + tmp.add(value); + } + return tmp; + } catch (IOException e) { + log.debug("Error while deserializing value for key '{}' to List<{}>.", key, clazz.getClass()); + return new ArrayList<T>(); + } catch (Exception e){ + log.debug("Error while searching key '{}' in the database.", key); + return new ArrayList<T>(); + } + } + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/JsonMapper.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/JsonMapper.java new file mode 100644 index 000000000..6138d571b --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/JsonMapper.java @@ -0,0 +1,73 @@ +package at.gv.egovernment.moa.id.commons.config.persistence; + +import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.type.TypeFactory; + +/** + * Helper class to handle the JSON (de-)serialization. + * + */ +public class JsonMapper { + + private ObjectMapper mapper = new ObjectMapper(); + + /** + * The default constructor where the default pretty printer is disabled. + */ + public JsonMapper() { + this(false); + } + + /** + * The constructor. + * @param prettyPrint enables or disables the default pretty printer + */ + public JsonMapper(boolean prettyPrint) { + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); + mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + if (prettyPrint) { + mapper.enable(SerializationFeature.INDENT_OUTPUT); + } + } + + /** + * Serialize an object to a JSON string. + * @param value the object to serialize + * @return a JSON string + * @throws JsonProcessingException thrown when an error occurs during serialization + */ + public String serialize(Object value) throws JsonProcessingException { + return mapper.writeValueAsString(value); + } + + /** + * Deserialize a JSON string. + * + * @param value the JSON string to deserialize + * @param clazz optional parameter that determines the type of the returned object. If not set, an {@link Object} is returned. + * @return the deserialized JSON string as an object of type {@code clazz} or {@link Object} + * @throws JsonParseException if the JSON string contains invalid content. + * @throws JsonMappingException if the input JSON structure does not match structure expected for result type + * @throws IOException if an I/O problem occurs (e.g. unexpected end-of-input) + */ + public <T> Object deserialize(String value, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException{ + + ObjectMapper mapper = new ObjectMapper(); + if (clazz != null) { + JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); + return mapper.readValue(value, javaType); + } else { + return mapper.readValue(value, Object.class); + } + } +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java index 0dd232773..28363a1eb 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBRead.java @@ -7,6 +7,7 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import at.gv.egovernment.moa.id.commons.config.persistence.Configuration; import at.gv.egovernment.moa.id.commons.db.dao.config.AuthComponentGeneral; import at.gv.egovernment.moa.id.commons.db.dao.config.ChainingModes; import at.gv.egovernment.moa.id.commons.db.dao.config.DefaultBKUs; @@ -17,8 +18,6 @@ import at.gv.egovernment.moa.id.commons.db.dao.config.SLRequestTemplates; import at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase; import at.gv.egovernment.moa.logging.Logger; -import com.datentechnik.moa.id.conf.persistence.Configuration; - /** * * diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBWrite.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBWrite.java index e1b51ee9b..de4a1789e 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBWrite.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/NewConfigurationDBWrite.java @@ -6,6 +6,7 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import at.gv.egovernment.moa.id.commons.config.persistence.Configuration; import at.gv.egovernment.moa.id.commons.db.dao.config.AuthComponentGeneral; import at.gv.egovernment.moa.id.commons.db.dao.config.ChainingModes; import at.gv.egovernment.moa.id.commons.db.dao.config.DefaultBKUs; @@ -15,8 +16,6 @@ import at.gv.egovernment.moa.id.commons.db.dao.config.OnlineApplication; import at.gv.egovernment.moa.id.commons.db.dao.config.SLRequestTemplates; import at.gv.egovernment.moa.id.commons.db.dao.config.UserDatabase; -import com.datentechnik.moa.id.conf.persistence.Configuration; - /** * This class is used for writing to the key-value database. */ diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDao.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDao.java new file mode 100644 index 000000000..db35ba1df --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDao.java @@ -0,0 +1,58 @@ +package at.gv.egovernment.moa.id.commons.db.dao.config; + +import java.util.List; +import java.util.Set; + +/** + * DAO interface providing means for accessing MOAID configuration properties. + * + */ +public interface ConfigPropertyDao { + + /** + * Gets all keys in the database. + * @return a List containing all keys in the database. + */ + List<String> getAllKeys(); + + /** + * Returns the {@link ConfigProperty} associated with {@code key} or {@code null} if the entry does not exist. + * + * @param key The configuration key. + * @return The configuration property value or {@code null}. + */ + ConfigProperty getProperty(String key); + + /** + * Persists a given {@link ConfigProperty}. + * @param property The property to be persisted. + */ + void saveProperty(ConfigProperty property); + + /** + * Returns a {@link List} containing all stored {@linkplain ConfigProperty ConfigProperties}. + * @return The list with the properties. + */ + List<ConfigProperty> getProperties(); + + /** + * Returns the value for the configuration property associated with {@code key} or {@code null} if the entry does not exist or its value is {@code null}. + * + * @param key The configuration key. + * @return The configuration property value or {@code null}. + */ + String getPropertyValue(String key); + + /** + * Persists a {@link List} of {@linkplain ConfigProperty ConfigProperties}. + * @param properties The list containing all the properties to be persisted. + */ + void saveProperties(Set<ConfigProperty> properties); + + /** + * Deletes the object associated with the given key. + * @param key the key + */ + void delete(String key); + +} diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java new file mode 100644 index 000000000..6a76c1d17 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java @@ -0,0 +1,104 @@ +package at.gv.egovernment.moa.id.commons.db.dao.config; + +import java.util.List; +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.transaction.annotation.Transactional; + +/** + * Database backed implementation of the DAO interface + * + */ +@Transactional("transactionManager") +public class ConfigPropertyDaoImpl implements ConfigPropertyDao { + + private Logger log = LoggerFactory.getLogger(getClass()); + + @PersistenceContext(unitName = "moaidconf") + private EntityManager em; + + @Override + public List<String> getAllKeys() { + if (null == em) { + log.error("No EntityManager set!"); + return null; + } + TypedQuery<String> query = em.createQuery("select key from ConfigProperty", String.class); + List<String> result = query.getResultList(); + return result; + } + + @Override + public void saveProperty(ConfigProperty property) { + if (null == em) { + log.error("No EntityManager set!"); + return; + } + + log.debug("Storing '{}'.", property.toString()); + em.persist(property); + } + + @Override + public ConfigProperty getProperty(String key) { + log.debug("Looking for configuration property for key '{}'.", key); + ConfigProperty result = em.find(ConfigProperty.class, key); + if (result != null) { + log.debug("Found configuration property {}.", result); + } else { + log.debug("Unable to find configuration property for key '{}'.", key); + } + return result; + } + + @Override + public String getPropertyValue(String key) { + ConfigProperty property = getProperty(key); + if (property == null) { + return null; + } + return property.getValue(); + } + + @Override + public List<ConfigProperty> getProperties() { + + if (null == em) { + log.error("No EntityManager set!"); + return null; + } + + log.debug("Retrieving all properties from database."); + TypedQuery<ConfigProperty> query = em.createQuery("select mc from ConfigProperty mc", ConfigProperty.class); + try { + List<ConfigProperty> propertiesList = query.getResultList(); + return propertiesList; + } catch (NoResultException e) { + log.debug("No property found in database."); + return null; + } + } + + @Override + public void saveProperties(Set<ConfigProperty> properties) { + log.debug("Storing {} properties to database.", properties.size()); + for (ConfigProperty cp : properties) { + saveProperty(cp); + } + em.flush(); + } + + @Override + public void delete(String key) { + log.debug("Deleting entry with key '{}'.", key); + em.remove(em.find(ConfigProperty.class, key)); + } + +} |