diff options
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java')
-rw-r--r-- | id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java | 263 |
1 files changed, 52 insertions, 211 deletions
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 6110f41ea..fefcf5028 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 @@ -3,260 +3,101 @@ package com.datentechnik.moa.id.conf; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -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.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: * <ul> - * <li>transform a MoaID 2 XML configuration XML file to a MoaID 3 property file</li> + * <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 boolean isOverwriteData = false; - public static void main(String[] args) { MOAIDConfCLI cli = new MOAIDConfCLI(); MigrateConfigurationParams parsedParameters = cli.parse(args); // consider settings of force switch - isOverwriteData = parsedParameters.isOverwriteData(); - - 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()); - } - } - - } else if (parsedParameters.getInputDBConfig() != null) { - // read input from database - 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(); - readFromDBWriteToFile(inputDBConfigFilePath, outFile); - - } else if (parsedParameters.getOutputDBConfig() != null) { - // input from database and output to a database is desired - // readFromDBWriteToDB(inDBConfigFilePath, outDBConfigFilePath); - } - - } else { - System.exit(1); - } - } 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); - } - } - - /** - * 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 JAXBException { - - try (FileOutputStream outStream = new FileOutputStream(outFile);) { + boolean isOverwriteData = parsedParameters.isOverwriteData(); + ConfigurationUtil configUtil = new ConfigurationUtil(isOverwriteData); - // 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); + if (!parsedParameters.isInputDB() && (parsedParameters.getInputTarget() != null)) { + // read input from file + workWithInputFromFile(parsedParameters.getInputTarget(), parsedParameters, configUtil); - // serialize config to JSON properties - Properties result = moaIdConfigToJsonProperties(config); + } else if (parsedParameters.getInputDBConfig() != null) { + // read input from database + workWithImputFromDB(parsedParameters, configUtil); - // 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."); + } else { System.exit(1); } } /** - * Helper method to serialize a {@link MOAIDConfiguration} to Properties - * with JSON encoded values. + * Handle the case where input from a file is read. * - * @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 + * @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 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. - */ - 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); + 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()); } - - // write to output stream - result.store(outStream, null); - - System.out.println("Old XML configuration written to:"); - System.out.println(outFile.getAbsolutePath()); - + } 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 output file."); + System.out.println("Could not find the input file."); System.exit(1); } catch (IOException e) { - System.out.println("Could not write to the output file."); + System.out.println("Could not read from the input file."); System.exit(1); } } /** - * Read an input property file, deserialize it's values and write them to - * the given database. + * Handle the case where input is read from a 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 + * @param parsedParameters + * the command line parameters. + * @param configUtil + * the class for working with the configuration. */ - 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 - boolean result = dbConfiguration.set(key, value, isOverwriteData); - if (!result) { - System.out.println("Could NOT persist the configuration file's information in the database."); - System.out.println("The database already contains a configuration (see force switch if you want to override data.)"); - System.exit(1); - } - System.out.println("Data has been successfully written to the database."); + 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); } } - - private static void readFromDBWriteToDB(String inputDBConfigFilePath, String outputDBConfigFilePath) { - //TODO: implement - } }
\ No newline at end of file |