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.Arrays; import java.util.Enumeration; import java.util.List; import java.util.Map; 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 org.springframework.transaction.annotation.Transactional; import at.gv.egiz.components.configuration.api.Configuration; import at.gv.egiz.components.configuration.api.ConfigurationException; import at.gv.egovernment.moa.id.commons.db.dao.config.deprecated.MOAIDConfiguration; import at.gv.egovernment.moa.id.commons.db.dao.config.deprecated.OnlineApplication; import at.gv.egovernment.moa.id.commons.db.dao.config.deprecated.STORK; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.MiscUtil; 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.deprecated"); 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 */ public static Properties moaIdConfigToJsonProperties(MOAIDConfiguration config) { Properties result = new Properties(); if (config == null) { return null; } STORK storkConfig = null; try { storkConfig = config.getAuthComponentGeneral().getForeignIdentities().getSTORK(); } catch (Exception e) { Logger.debug("No general STORK configuration found."); } //convert all online applications List oaList = config.getOnlineApplication(); for (int i=0; i keyValueOA = ConfigurationMigrationUtils.convertHyberJaxBOnlineApplicationToKeyValue(oa, storkConfig); String serviceIdentifier = keyValueOA.get(MOAIDConfigurationConstants.PREFIX_SERVICES); if (MiscUtil.isEmpty(serviceIdentifier)) { Logger.info("Use default ServiceIdentifier."); serviceIdentifier = MOAIDConfigurationConstants.PREFIX_OA; } //write all OA key/value pairs to configuration for (String key : keyValueOA.keySet()) { if (MiscUtil.isNotEmpty(keyValueOA.get(key))) result.put(MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES + "." + serviceIdentifier + "." + String.valueOf(i) + "." + key, keyValueOA.get(key)); } //set correct metadata list identifier result.put(MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES + "." + serviceIdentifier + "." + String.valueOf(i) + "." + MOAIDConfigurationConstants.METADATA_LIST +".0", MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES + "." + serviceIdentifier); } Map keyValueGeneral = ConfigurationMigrationUtils.convertHyberJaxBMOAIDConfigToKeyValue(config); //write all OA key/value pairs to configuration for (String key : keyValueGeneral.keySet()) { if (MiscUtil.isNotEmpty(keyValueGeneral.get(key))) result.put(key, keyValueGeneral.get(key)); } 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"); String[] allKeys = dbConfiguration.getConfigurationIds(); for (String key : allKeys) { // extract database value String value = dbConfiguration.getStringValue(key); // add to properties result.setProperty(key, value); } // 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); } catch (ConfigurationException e) { System.out.println("Could not read from database."); 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 */ @Transactional public void readFromFileWriteToDB(FileInputStream inStream, String outputDBConfigFilePath) throws IOException { Properties inProperties = new Properties(); inProperties.load(inStream); System.getProperties().setProperty("moa.id.webconfig", "file:" + outputDBConfigFilePath); ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{ "configuration.beans.xml", "moaid.migration.beans.xml" }); Configuration dbConfiguration = (Configuration) context.getBean("moaidconfig"); List keys = null; try { keys = Arrays.asList(dbConfiguration.getConfigurationIds()); } catch (ConfigurationException e1) { System.out.println("Database can not be read."); System.exit(1); } 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) { try { dbConfiguration.deleteIds(key); } catch (ConfigurationException e) { System.out.println("Could NOT persist the configuration file's information in the database."); } } } Enumeration propertyNames = inProperties.propertyNames(); while (propertyNames.hasMoreElements()) { String key = (String) propertyNames.nextElement(); String json = inProperties.getProperty(key); // add to database try { dbConfiguration.setStringValue(key, json); } catch (ConfigurationException e) { 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 } }