From 15c60cf9939536c352778c6c411608397e57c516 Mon Sep 17 00:00:00 2001 From: Gerwin Gsenger Date: Thu, 22 Jan 2015 16:49:49 +0100 Subject: add CLI migration (XML to property file, property file to db, db to property file) --- .../id/commons/db/MOAIDConfigurationConstants.java | 15 +- .../moa/id/conf/MigrateConfiguration.java | 251 ++++++++++++++++----- .../moa/id/conf/persistence/ConfigurationImpl.java | 68 +----- .../moa/id/conf/persistence/JsonMapper.java | 73 ++++++ .../src/main/resources/configuration.beans.xml | 2 - .../src/main/resources/persistence_template.xml | 11 +- 6 files changed, 289 insertions(+), 131 deletions(-) create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/JsonMapper.java (limited to 'id') diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOAIDConfigurationConstants.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOAIDConfigurationConstants.java index dc7c92a1d..30897bc1d 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOAIDConfigurationConstants.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOAIDConfigurationConstants.java @@ -28,8 +28,17 @@ public final class MOAIDConfigurationConstants { * @return the keys as {@code String[]} */ public static final String[] getMOAIDConfigurationKeys() { - String[] keys = new String[] { AUTH_COMPONENT_GENERAL_KEY, CHAINING_MODES_KEY, TRUSTED_CERTIFICATES_KEY, DEFAULT_BKUS_KEY, SLREQUEST_TEMPLATES_KEY, - TIMESTAMP_ITEM_KEY, PVP2REFRESH_ITEM_KEY }; - return keys; + return new String[] { AUTH_COMPONENT_GENERAL_KEY, CHAINING_MODES_KEY, TRUSTED_CERTIFICATES_KEY, + DEFAULT_BKUS_KEY, SLREQUEST_TEMPLATES_KEY, TIMESTAMP_ITEM_KEY, PVP2REFRESH_ITEM_KEY }; + } + + /** + * Returns all (database-) keys that {@link MOAIDConfiguration} contains. + * @return the keys as {@code String[]} + */ + public static final String[] getAllMOAIDConfigurationKeys() { + return new String[] { ONLINE_APPLICATIONS_KEY, AUTH_COMPONENT_GENERAL_KEY, CHAINING_MODES_KEY, + TRUSTED_CERTIFICATES_KEY, DEFAULT_BKUS_KEY, SLREQUEST_TEMPLATES_KEY, TIMESTAMP_ITEM_KEY, + PVP2REFRESH_ITEM_KEY }; } } diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java index 6197bbcae..a313107ad 100644 --- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java @@ -5,78 +5,107 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Properties; -import javax.xml.parsers.ParserConfigurationException; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; -import org.xml.sax.SAXException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import at.gv.egovernment.moa.id.commons.db.MOAIDConfigurationConstants; +import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration; import com.datentechnik.moa.id.conf.cli.MOAIDConfCLI; import com.datentechnik.moa.id.conf.cli.MigrateConfigurationParams; - +import com.datentechnik.moa.id.conf.persistence.Configuration; +import com.datentechnik.moa.id.conf.persistence.JsonMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +/** + * CLI tool which is able to perform the following tasks: + * + */ public class MigrateConfiguration { -// public static final String XML_APPLICATION_CONTEXT = ""; - public static void main(String[] args) { MOAIDConfCLI cli = new MOAIDConfCLI(); MigrateConfigurationParams parsedParameters = cli.parse(args); - - if (!parsedParameters.isInputDB() && (parsedParameters.getInputTarget() != null)) { - - //read from input file - File inFile = new File(parsedParameters.getInputTarget()); - try (FileInputStream inStream = new FileInputStream(inFile);) { + try { + if (!parsedParameters.isInputDB() && (parsedParameters.getInputTarget() != null)) { + // read input from file + File inFile = new File(parsedParameters.getInputTarget()); + 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()); + readFromFileWriteToFile(inStream, outFile); + + } else if (parsedParameters.getOutputDBConfig() != null) { + // input from file and output to a database is desired + readFromFileWriteToDB(inStream, parsedParameters.getOutputDBConfig()); + } + } 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); + } + } else if (parsedParameters.getInputDBConfig() != null) { + // read input from database if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { - // input from file and output to a file is desired - // parseToOutputFile(inStream, outFile); + // input from database and output to a file is desired + File outFile = new File(parsedParameters.getOutputFile()); + String inputDBConfigFilePath = parsedParameters.getInputDBConfig(); + readFromDBWriteToFile(inputDBConfigFilePath, outFile); } else if (parsedParameters.getOutputDBConfig() != null) { - // input from file and output to a database is desired - // readFromFileWriteToDB(inStream,parsedParameters.getOutputDBConfig()); + // input from database and output to a database is desired + // readFromDBWriteToDB(inDBConfigFilePath, outDBConfigFilePath); } - } 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 to the input file."); - System.exit(1); - } - } else if(parsedParameters.getInputDBConfig() != null){ - // read from input database - if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { - // input from database and output to a file is desired - // readFromDBWriteToFile(dbConfigFilePath, outFile); - - } else if (parsedParameters.getOutputDBConfig() != null) { - // input from database and output to a database is desired - // readFromDBWriteToDB(inDBConfigFilePath, outDBConfigFilePath); + } else { + System.exit(1); } - - } - else { + } catch (JAXBException e) { + System.out.println("MOA-ID XML configuration can not be loaded from given file."); System.exit(1); } } /** - * Helper method to parse the input stream an write a output properties - * file. - * + * Read an input XML file, transfer it to properties and write the properties to a file. + * * @param inStream * the input stream to read from. * @param outFile * the output file to write to. + * @throws JAXBException */ - private static void readFromFileWriteToFile(FileInputStream inStream, File outFile) throws ParserConfigurationException, - SAXException { + private static void readFromFileWriteToFile(FileInputStream inStream, File outFile) throws JAXBException { try (FileOutputStream outStream = new FileOutputStream(outFile);) { - - //TODO: implement - + + // 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); @@ -85,26 +114,134 @@ public class MigrateConfiguration { System.exit(1); } } - - private static void readFromDBWriteToFile(String dbConfigFilePath, File outFile){ - //TODO: implement + + /** + * 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 static Properties moaIdConfigToJsonProperties(MOAIDConfiguration config) throws JsonProcessingException { + + Properties result = new Properties(); + boolean prettyPrint = true; + com.datentechnik.moa.id.conf.persistence.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; } /** - * Helper method to parse the input stream an write the properties to a - * database. - * - * @param inStream - * the input stream to read from. - * @param dbConfigFilePath - * the path to the database configuration file. + * 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. */ - private static void readFromFileWriteToDB(FileInputStream inStream, String dbConfigFilePath) { - //TODO: implement + private static 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; + com.datentechnik.moa.id.conf.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); + + } 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); + } } - - private static void readFromDBWriteToDB(String inDBConfigFilePath, String outDBConfigFilePath){ - //TODO implement + + /** + * 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 + */ + private static 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; + com.datentechnik.moa.id.conf.persistence.JsonMapper mapper = new JsonMapper(prettyPrint); + + for (String key : MOAIDConfigurationConstants.getAllMOAIDConfigurationKeys()) { + + // extract database value + String json = inProperties.getProperty(key); + + // deserialize value to object + Object value = mapper.deserialize(json, null); + + // add to database + dbConfiguration.set(key, value); + } } + private static void readFromDBWriteToDB(String inputDBConfigFilePath, String outputDBConfigFilePath) { + //TODO: implement + } } \ No newline at end of file diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java index 6d05af791..5cab9a5ff 100644 --- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java @@ -11,14 +11,9 @@ import org.springframework.stereotype.Component; import com.datentechnik.moa.id.conf.persistence.dal.ConfigProperty; import com.datentechnik.moa.id.conf.persistence.dal.ConfigPropertyDao; -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.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; @@ -128,66 +123,5 @@ public class ConfigurationImpl implements Configuration { return new ArrayList(); } } - - - /** - * Helper class to handle the JSON (de-)serialization. - * - */ - private 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 Object deserialize(String value, Class 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/com/datentechnik/moa/id/conf/persistence/JsonMapper.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/JsonMapper.java new file mode 100644 index 000000000..8e5d2e7c4 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/JsonMapper.java @@ -0,0 +1,73 @@ +package com.datentechnik.moa.id.conf.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 Object deserialize(String value, Class 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/resources/configuration.beans.xml b/id/server/moa-id-commons/src/main/resources/configuration.beans.xml index 87cccc7b2..444b01095 100644 --- a/id/server/moa-id-commons/src/main/resources/configuration.beans.xml +++ b/id/server/moa-id-commons/src/main/resources/configuration.beans.xml @@ -21,8 +21,6 @@ - - diff --git a/id/server/moa-id-commons/src/main/resources/persistence_template.xml b/id/server/moa-id-commons/src/main/resources/persistence_template.xml index 25092ff58..f5bbe8555 100644 --- a/id/server/moa-id-commons/src/main/resources/persistence_template.xml +++ b/id/server/moa-id-commons/src/main/resources/persistence_template.xml @@ -1,7 +1,14 @@ - + + + + org.hibernate.ejb.HibernatePersistence + com.datentechnik.moa.id.conf.persistence.dal.ConfigProperty + + + - + -- cgit v1.2.3