From 54cb4518bd64dba0c1f373f262afb7a988f35e1a Mon Sep 17 00:00:00 2001 From: Gerwin Gsenger Date: Wed, 21 Jan 2015 15:35:02 +0100 Subject: move 'cli' package from project 'moa-id-DTI', add additional input parameter --- .../moa/id/conf/MigrateConfiguration.java | 110 ++++++++++++++++++ .../datentechnik/moa/id/conf/cli/CLIConstants.java | 35 ++++++ .../datentechnik/moa/id/conf/cli/MOAIDConfCLI.java | 123 +++++++++++++++++++++ .../id/conf/cli/MigrateConfigurationParams.java | 95 ++++++++++++++++ 4 files changed, 363 insertions(+) create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java (limited to 'id/server/moa-id-commons') 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 new file mode 100644 index 000000000..6197bbcae --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java @@ -0,0 +1,110 @@ +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 javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + +import com.datentechnik.moa.id.conf.cli.MOAIDConfCLI; +import com.datentechnik.moa.id.conf.cli.MigrateConfigurationParams; + +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);) { + + if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { + // input from file and output to a file is desired + // parseToOutputFile(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 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); + } + } + + /** + * Helper method to parse the input stream an write a output properties + * file. + * + * @param inStream + * the input stream to read from. + * @param outFile + * the output file to write to. + */ + private static void readFromFileWriteToFile(FileInputStream inStream, File outFile) throws ParserConfigurationException, + SAXException { + + try (FileOutputStream outStream = new FileOutputStream(outFile);) { + + //TODO: implement + + } 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 readFromDBWriteToFile(String dbConfigFilePath, File outFile){ + //TODO: implement + } + + /** + * 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. + */ + private static void readFromFileWriteToDB(FileInputStream inStream, String dbConfigFilePath) { + //TODO: implement + } + + private static void readFromDBWriteToDB(String inDBConfigFilePath, String outDBConfigFilePath){ + //TODO implement + } + +} \ No newline at end of file diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java new file mode 100644 index 000000000..b5e0f242f --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java @@ -0,0 +1,35 @@ +package com.datentechnik.moa.id.conf.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"; + + +} diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java new file mode 100644 index 000000000..78b9450ce --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java @@ -0,0 +1,123 @@ +package com.datentechnik.moa.id.conf.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_IN + " | -" + CLIConstants.CLI_PARAM_INDB + " -" + CLIConstants.CLI_PARAM_OUT + + " | -" + CLIConstants.CLI_PARAM_OUTDB + " [-" + 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 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/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java new file mode 100644 index 000000000..0a13d952b --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java @@ -0,0 +1,95 @@ +package com.datentechnik.moa.id.conf.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; + + /** + * 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 true if the stored path points at a database config file; false otherwise. + */ + public boolean isInputDB() { + return inputDbConfigFile != null; + } + + /** + * Returns whether the desired output is a config file for a database. + * @return true if the stored path points at a database config file; false otherwise. + */ + public boolean isOutputDB() { + return outputDbConfigFile != null; + } + + /** + * + * @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); + + 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 -- cgit v1.2.3