From 2f92e2d747310881a292c80425619d7467d97d90 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Mon, 4 Jul 2016 15:37:21 +0200 Subject: [PATCH 01/15] add cli possibility --- .gitignore | 1 + build.gradle | 6 +- .../ericsson/eiffel/remrem/generate/App.java | 36 +++--- .../eiffel/remrem/generate/cli/CLI.java | 103 ++++++++++++++++++ 4 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java diff --git a/.gitignore b/.gitignore index 84a43ca6..74103a69 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ out/ # Can be used for repackaging with semantic information libs/ +/bin/ diff --git a/build.gradle b/build.gradle index 6d62d1a5..4f5d4eac 100644 --- a/build.gradle +++ b/build.gradle @@ -55,7 +55,8 @@ sourceSets { install.dependsOn assemble apply plugin: 'spring-boot' - +apply plugin: 'java' +apply plugin: 'eclipse' group 'com.ericsson.eiffel.remrem' version '1.0-SNAPSHOT' @@ -88,6 +89,7 @@ repositories { } dependencies { + compile('com.jayway.restassured:rest-assured:2.3.2') compile("org.springframework.boot:spring-boot-starter-web:$sprintBootVersion") { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } @@ -115,6 +117,8 @@ dependencies { //compileOnly is supported in gradle 2.12 but it might be required // in cucumber folder compile "org.projectlombok:lombok:1.16.8" + //commons CLI + compile 'commons-cli:commons-cli:1.3.1' // Declare the dependency for your favourite test framework you want to use in your tests. // TestNG is also supported by the Gradle Test task. Just change the diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java index 248dd3e0..bd6fb083 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java @@ -7,6 +7,8 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; +import com.ericsson.eiffel.remrem.generate.cli.CLI; + import java.util.Arrays; @SpringBootApplication @@ -14,19 +16,25 @@ public class App extends SpringBootServletInitializer { public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(App.class, args); - - System.out.println("Let's inspect active profiles:"); - for (String envNames : ctx.getEnvironment().getActiveProfiles()) { - System.out.println(envNames); - } - - System.out.println("Let's inspect the beans provided by Spring Boot:"); - - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } + + CLI cli = new CLI(); + boolean startService = cli.parse(args); + + if (startService) { + ApplicationContext ctx = SpringApplication.run(App.class, args); + + System.out.println("Let's inspect active profiles:"); + for (String envNames : ctx.getEnvironment().getActiveProfiles()) { + System.out.println(envNames); + } + + System.out.println("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } + } } } diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java new file mode 100644 index 00000000..75e375a7 --- /dev/null +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -0,0 +1,103 @@ +package com.ericsson.eiffel.remrem.generate.cli; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.tomcat.util.http.fileupload.FileUtils; + +import com.ericsson.eiffel.remrem.semantics.SemanticsService; +import com.ericsson.eiffel.remrem.shared.MsgService; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +/** + * @author evasiba + * + */ +public class CLI { + private Options options=null; + + public CLI() { + options = createCLIOptions(); + } + + /** + * Creates the options needed by command line interface + * @return + */ + private static Options createCLIOptions() { + Options options = new Options(); + options.addOption("h", "help", false, "show help."); + options.addOption("f", "content_file", true, "message content file"); + options.addOption("t", "message_type", true, "message type, mandatory if -f is given"); +// options.addOption("r", "response_file", true, "file to store the response in"); + return options; + } + + /** + * Prints the help for this application and exits. + * @param options + */ + private static void help(Options options) { + // This prints out some help + HelpFormatter formater = new HelpFormatter(); + formater.printHelp("java -jar", options); + System.exit(0); + } + + /** + * Parse the given arguments + * @param args + * @return + */ + public boolean parse(String[] args) { + CommandLineParser parser = new DefaultParser(); + boolean startService = true; + try { + CommandLine commandLine = parser.parse(options, args); + Option[] existingOptions = commandLine.getOptions(); + if (existingOptions.length > 0) { + startService = false; + } + + if (commandLine.hasOption("h")) { + help(options); + } + + if (commandLine.hasOption("f") && commandLine.hasOption("t")) { + String filePath = commandLine.getOptionValue("f"); + String msgType = commandLine.getOptionValue("t"); + handleContentFile(msgType, filePath); + } + } catch (Exception e) { + help(options); + } + return startService; + } + + public void handleContentFile(String msgType, String filePath) { + JsonParser parser = new JsonParser(); + MsgService msgService = new SemanticsService(); + try { + byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); + String fileContent = new String(fileBytes); + JsonObject bodyJson = parser.parse(fileContent).getAsJsonObject(); + JsonElement returnJson = parser.parse(msgService.generateMsg(msgType, bodyJson)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} From c88db918320eecc63ea99b4656aa20e813c52fd8 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Tue, 5 Jul 2016 09:27:05 +0200 Subject: [PATCH 02/15] add file to write json response --- .../eiffel/remrem/generate/cli/CLI.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 75e375a7..376d501a 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Paths; @@ -18,6 +19,7 @@ import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; +import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -42,7 +44,7 @@ private static Options createCLIOptions() { options.addOption("h", "help", false, "show help."); options.addOption("f", "content_file", true, "message content file"); options.addOption("t", "message_type", true, "message type, mandatory if -f is given"); -// options.addOption("r", "response_file", true, "file to store the response in"); + options.addOption("r", "response_file", true, "file to store the response in, mandatory if -f is given"); return options; } @@ -76,10 +78,12 @@ public boolean parse(String[] args) { help(options); } - if (commandLine.hasOption("f") && commandLine.hasOption("t")) { + if (commandLine.hasOption("f") && commandLine.hasOption("t") && + commandLine.hasOption("r")) { String filePath = commandLine.getOptionValue("f"); + String responseFilePath = commandLine.getOptionValue("r"); String msgType = commandLine.getOptionValue("t"); - handleContentFile(msgType, filePath); + handleContentFile(msgType, filePath, responseFilePath); } } catch (Exception e) { help(options); @@ -87,7 +91,8 @@ public boolean parse(String[] args) { return startService; } - public void handleContentFile(String msgType, String filePath) { + public void handleContentFile(String msgType, String filePath, + String responseFilePath) { JsonParser parser = new JsonParser(); MsgService msgService = new SemanticsService(); try { @@ -95,6 +100,11 @@ public void handleContentFile(String msgType, String filePath) { String fileContent = new String(fileBytes); JsonObject bodyJson = parser.parse(fileContent).getAsJsonObject(); JsonElement returnJson = parser.parse(msgService.generateMsg(msgType, bodyJson)); + Gson gson = new Gson(); + String returnJsonStr = gson.toJson(returnJson); + try( PrintWriter out = new PrintWriter( responseFilePath ) ){ + out.println( returnJsonStr ); + } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); From 280013f13e3b0d314a34dd9b3dbc8cb933c02e08 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Wed, 6 Jul 2016 13:41:58 +0200 Subject: [PATCH 03/15] improve after review comments --- build.gradle | 2 +- .../ericsson/eiffel/remrem/generate/App.java | 50 ++--- .../eiffel/remrem/generate/cli/CLI.java | 171 +++++++++--------- 3 files changed, 119 insertions(+), 104 deletions(-) diff --git a/build.gradle b/build.gradle index 4f5d4eac..09ba7146 100644 --- a/build.gradle +++ b/build.gradle @@ -89,7 +89,7 @@ repositories { } dependencies { - compile('com.jayway.restassured:rest-assured:2.3.2') + compile('com.jayway.restassured:rest-assured:2.3.2') compile("org.springframework.boot:spring-boot-starter-web:$sprintBootVersion") { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java index bd6fb083..b634fb06 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java @@ -16,25 +16,33 @@ public class App extends SpringBootServletInitializer { public static void main(String[] args) { - - CLI cli = new CLI(); - boolean startService = cli.parse(args); - - if (startService) { - ApplicationContext ctx = SpringApplication.run(App.class, args); - - System.out.println("Let's inspect active profiles:"); - for (String envNames : ctx.getEnvironment().getActiveProfiles()) { - System.out.println(envNames); - } - - System.out.println("Let's inspect the beans provided by Spring Boot:"); - - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } - } + + // CLI class checks if arguments are passed to application + // and if so we do not start the service but act based on + // passed arguments. If no arguments are passed the server + // will be started + CLI cli = new CLI(); + boolean needsStartService = cli.parse(args); + + if (needsStartService) { + startService(args); + } } -} + + private static void startService(String[] args) { + ApplicationContext ctx = SpringApplication.run(App.class, args); + + System.out.println("Let's inspect active profiles:"); + for (String envNames : ctx.getEnvironment().getActiveProfiles()) { + System.out.println(envNames); + } + + System.out.println("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 376d501a..9a3a3b10 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -1,10 +1,6 @@ package com.ericsson.eiffel.remrem.generate.cli; -import java.io.BufferedReader; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Paths; @@ -15,7 +11,6 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.apache.tomcat.util.http.fileupload.FileUtils; import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; @@ -29,85 +24,97 @@ * */ public class CLI { - private Options options=null; + private Options options=null; - public CLI() { - options = createCLIOptions(); - } + public CLI() { + options = createCLIOptions(); + } - /** - * Creates the options needed by command line interface - * @return - */ - private static Options createCLIOptions() { - Options options = new Options(); - options.addOption("h", "help", false, "show help."); - options.addOption("f", "content_file", true, "message content file"); - options.addOption("t", "message_type", true, "message type, mandatory if -f is given"); - options.addOption("r", "response_file", true, "file to store the response in, mandatory if -f is given"); - return options; - } + /** + * Creates the options needed by command line interface + * @return the options this CLI can handle + */ + private static Options createCLIOptions() { + Options options = new Options(); + options.addOption("h", "help", false, "show help."); + options.addOption("f", "content_file", true, "message content file"); + options.addOption("t", "message_type", true, "message type, mandatory if -f is given"); + options.addOption("r", "response_file", true, "file to store the response in, optional"); + return options; + } - /** - * Prints the help for this application and exits. - * @param options - */ - private static void help(Options options) { - // This prints out some help - HelpFormatter formater = new HelpFormatter(); - formater.printHelp("java -jar", options); - System.exit(0); - } + /** + * Prints the help for this application and exits. + * @param options the options to print usage help for + */ + private static void help(Options options) { + // This prints out some help + HelpFormatter formater = new HelpFormatter(); + formater.printHelp("java -jar", options); + System.exit(0); + } - /** - * Parse the given arguments - * @param args - * @return - */ - public boolean parse(String[] args) { - CommandLineParser parser = new DefaultParser(); - boolean startService = true; - try { - CommandLine commandLine = parser.parse(options, args); - Option[] existingOptions = commandLine.getOptions(); - if (existingOptions.length > 0) { - startService = false; - } + /** + * Parse the given arguments and act on them + * @param args command line arguments + * @return if the service should start or not + */ + public boolean parse(String[] args) { + CommandLineParser parser = new DefaultParser(); + boolean startService = true; + try { + CommandLine commandLine = parser.parse(options, args); + Option[] existingOptions = commandLine.getOptions(); + if (existingOptions.length > 0) { + startService = false; + } - if (commandLine.hasOption("h")) { - help(options); - } - - if (commandLine.hasOption("f") && commandLine.hasOption("t") && - commandLine.hasOption("r")) { - String filePath = commandLine.getOptionValue("f"); - String responseFilePath = commandLine.getOptionValue("r"); - String msgType = commandLine.getOptionValue("t"); - handleContentFile(msgType, filePath, responseFilePath); - } - } catch (Exception e) { - help(options); - } - return startService; - } - - public void handleContentFile(String msgType, String filePath, - String responseFilePath) { - JsonParser parser = new JsonParser(); - MsgService msgService = new SemanticsService(); - try { - byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); - String fileContent = new String(fileBytes); - JsonObject bodyJson = parser.parse(fileContent).getAsJsonObject(); - JsonElement returnJson = parser.parse(msgService.generateMsg(msgType, bodyJson)); - Gson gson = new Gson(); - String returnJsonStr = gson.toJson(returnJson); - try( PrintWriter out = new PrintWriter( responseFilePath ) ){ - out.println( returnJsonStr ); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } -} + if (commandLine.hasOption("h")) { + help(options); + } + + if (commandLine.hasOption("f") && commandLine.hasOption("t")) { + String filePath = commandLine.getOptionValue("f"); + String responseFilePath = null; + if (commandLine.hasOption("r")) + responseFilePath = commandLine.getOptionValue("r"); + String msgType = commandLine.getOptionValue("t"); + handleContentFile(msgType, filePath, responseFilePath); + } + } catch (Exception e) { + help(options); + } + return startService; + } + + /** + * Handle message from file + * @param msgType the Eiffel message type + * @param filePath the file path where the message content resides + * @param responseFilePath the file path where to store the prepared message, stdout if null + */ + public void handleContentFile(String msgType, + String filePath, + String responseFilePath) { + JsonParser parser = new JsonParser(); + MsgService msgService = new SemanticsService(); + try { + byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); + String fileContent = new String(fileBytes); + JsonObject bodyJson = parser.parse(fileContent).getAsJsonObject(); + JsonElement returnJson = parser.parse(msgService.generateMsg(msgType, bodyJson)); + Gson gson = new Gson(); + String returnJsonStr = gson.toJson(returnJson); + if (responseFilePath != null) { + try( PrintWriter out = new PrintWriter( responseFilePath ) ){ + out.println( returnJsonStr ); + } + } else { + System.out.println( returnJsonStr ); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} \ No newline at end of file From 9ff5e07dde47e0c09247ca43d9cdfaa52cd6d577 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Wed, 6 Jul 2016 14:55:22 +0200 Subject: [PATCH 04/15] add possibility to take json message as argument --- .../eiffel/remrem/generate/cli/CLI.java | 63 ++++++++++++------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 9a3a3b10..8031ac56 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -14,8 +14,6 @@ import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; -import com.google.gson.Gson; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -38,7 +36,8 @@ private static Options createCLIOptions() { Options options = new Options(); options.addOption("h", "help", false, "show help."); options.addOption("f", "content_file", true, "message content file"); - options.addOption("t", "message_type", true, "message type, mandatory if -f is given"); + options.addOption("json", "json_content", true, "json content"); + options.addOption("t", "message_type", true, "message type, mandatory if -f or -json is given"); options.addOption("r", "response_file", true, "file to store the response in, optional"); return options; } @@ -69,17 +68,12 @@ public boolean parse(String[] args) { startService = false; } - if (commandLine.hasOption("h")) { - help(options); - } - if (commandLine.hasOption("f") && commandLine.hasOption("t")) { - String filePath = commandLine.getOptionValue("f"); - String responseFilePath = null; - if (commandLine.hasOption("r")) - responseFilePath = commandLine.getOptionValue("r"); - String msgType = commandLine.getOptionValue("t"); - handleContentFile(msgType, filePath, responseFilePath); + handleFileArgs(commandLine); + } else if (commandLine.hasOption("json") && commandLine.hasOption("t")) { + handleJsonArgs(commandLine); + }else { + help(options); } } catch (Exception e) { help(options); @@ -87,24 +81,47 @@ public boolean parse(String[] args) { return startService; } + private void handleFileArgs(CommandLine commandLine) { + String filePath = commandLine.getOptionValue("f"); + String jsonContent = readJsonContent(filePath); + handleJsonString(jsonContent, commandLine); + } + + private String readJsonContent(String filePath) { + try { + JsonParser parser = new JsonParser(); + byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); + return new String(fileBytes); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + private void handleJsonArgs(CommandLine commandLine) { + String jsonContent = commandLine.getOptionValue("json"); + handleJsonString(jsonContent, commandLine); + } + /** * Handle message from file * @param msgType the Eiffel message type * @param filePath the file path where the message content resides * @param responseFilePath the file path where to store the prepared message, stdout if null */ - public void handleContentFile(String msgType, - String filePath, - String responseFilePath) { - JsonParser parser = new JsonParser(); + private void handleJsonString(String jsonString, + CommandLine commandLine) { + MsgService msgService = new SemanticsService(); + String responseFilePath = null; + if (commandLine.hasOption("r")) + responseFilePath = commandLine.getOptionValue("r"); + String msgType = commandLine.getOptionValue("t"); try { - byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); - String fileContent = new String(fileBytes); - JsonObject bodyJson = parser.parse(fileContent).getAsJsonObject(); - JsonElement returnJson = parser.parse(msgService.generateMsg(msgType, bodyJson)); - Gson gson = new Gson(); - String returnJsonStr = gson.toJson(returnJson); + JsonParser parser = new JsonParser(); + JsonObject jsonContent = parser.parse(jsonString).getAsJsonObject(); + String returnJsonStr = msgService.generateMsg(msgType, jsonContent); if (responseFilePath != null) { try( PrintWriter out = new PrintWriter( responseFilePath ) ){ out.println( returnJsonStr ); From d6d95e1d98184c1fcdda51a375b5e9f46344b523 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Thu, 7 Jul 2016 10:01:35 +0200 Subject: [PATCH 05/15] update class and method java doc --- .../eiffel/remrem/generate/cli/CLI.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 8031ac56..cd5a9729 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -18,6 +18,10 @@ import com.google.gson.JsonParser; /** + * Class for interpreting the passed arguments from command line. + * Parse method returns true, meaning we need to start the service afterwards, if no argument + * is given. The same method returns false, meaning we do not start the service afterwards, if any + * argument is given. If an argument is given that it is not recognized we print help * @author evasiba * */ @@ -81,15 +85,23 @@ public boolean parse(String[] args) { return startService; } + /** + * Reads the content from the given file and sends it to message service + * @param commandLine + */ private void handleFileArgs(CommandLine commandLine) { String filePath = commandLine.getOptionValue("f"); - String jsonContent = readJsonContent(filePath); + String jsonContent = readFileContent(filePath); handleJsonString(jsonContent, commandLine); } - private String readJsonContent(String filePath) { + /** + * Read file content as String + * @param filePath + * @return + */ + private String readFileContent(String filePath) { try { - JsonParser parser = new JsonParser(); byte[] fileBytes = Files.readAllBytes(Paths.get(filePath)); return new String(fileBytes); } catch (IOException e) { @@ -99,13 +111,17 @@ private String readJsonContent(String filePath) { return null; } + /** + * Read passed json string from command line and sends it to message service + * @param commandLine + */ private void handleJsonArgs(CommandLine commandLine) { String jsonContent = commandLine.getOptionValue("json"); handleJsonString(jsonContent, commandLine); } /** - * Handle message from file + * Send the given json string to message service * @param msgType the Eiffel message type * @param filePath the file path where the message content resides * @param responseFilePath the file path where to store the prepared message, stdout if null From e20e1226381784ab8fa7f08051aa109a43f4c7d0 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Tue, 12 Jul 2016 10:05:59 +0200 Subject: [PATCH 06/15] add debug flag and various fixes --- build.gradle | 3 +- .../eiffel/remrem/generate/cli/CLI.java | 56 +++++++++++++++---- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index 09ba7146..36710d03 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ apply plugin: 'war' war { baseName = 'remrem-generate' - version = '0.5.0' + version = '0.5.1' } @@ -89,6 +89,7 @@ repositories { } dependencies { + compile files('/home/evasiba/git/eiffel3messaging/build/libs/remrem-eiffel3messaging-1.2.jar') compile('com.jayway.restassured:rest-assured:2.3.2') compile("org.springframework.boot:spring-boot-starter-web:$sprintBootVersion") { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index cd5a9729..b2e0656c 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -11,12 +11,20 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.impl.SLF4JLog; +import org.apache.commons.logging.impl.SLF4JLogFactory; +import org.slf4j.LoggerFactory; -import com.ericsson.eiffel.remrem.semantics.SemanticsService; +import com.ericsson.eiffel.remrem.message.services.Eiffel3Service; +//import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; + /** * Class for interpreting the passed arguments from command line. * Parse method returns true, meaning we need to start the service afterwards, if no argument @@ -28,8 +36,8 @@ public class CLI { private Options options=null; - public CLI() { - options = createCLIOptions(); + public CLI() { + options = createCLIOptions(); } /** @@ -43,6 +51,7 @@ private static Options createCLIOptions() { options.addOption("json", "json_content", true, "json content"); options.addOption("t", "message_type", true, "message type, mandatory if -f or -json is given"); options.addOption("r", "response_file", true, "file to store the response in, optional"); + options.addOption("d", "debug", false, "enable debug traces"); return options; } @@ -70,21 +79,43 @@ public boolean parse(String[] args) { Option[] existingOptions = commandLine.getOptions(); if (existingOptions.length > 0) { startService = false; - } - - if (commandLine.hasOption("f") && commandLine.hasOption("t")) { - handleFileArgs(commandLine); - } else if (commandLine.hasOption("json") && commandLine.hasOption("t")) { - handleJsonArgs(commandLine); - }else { - help(options); + handleOptions(commandLine); } } catch (Exception e) { + e.printStackTrace(); help(options); } return startService; } + /** + * @param commandLine + */ + private void handleLogging(CommandLine commandLine) { + if (!commandLine.hasOption("-d")) { + //Eiffel 3 messaging logs to stdout but since we also write + //to stdout we need to turn off logging unless specified by the user + System.setProperty("logging.level.root", "OFF"); + Logger log = (Logger) LoggerFactory.getLogger("ROOT"); + log.setLevel(Level.OFF); + } + } + + /** + * Delegates actions depending on the passed arguments + * @param commandLine command line arguments + */ + private void handleOptions(CommandLine commandLine) { + handleLogging(commandLine); + if (commandLine.hasOption("f") && commandLine.hasOption("t")) { + handleFileArgs(commandLine); + } else if (commandLine.hasOption("json") && commandLine.hasOption("t")) { + handleJsonArgs(commandLine); + }else { + help(options); + } + } + /** * Reads the content from the given file and sends it to message service * @param commandLine @@ -129,7 +160,8 @@ private void handleJsonArgs(CommandLine commandLine) { private void handleJsonString(String jsonString, CommandLine commandLine) { - MsgService msgService = new SemanticsService(); +// MsgService msgService = new SemanticsService(); + MsgService msgService = new Eiffel3Service(); String responseFilePath = null; if (commandLine.hasOption("r")) responseFilePath = commandLine.getOptionValue("r"); From bc305cb0296053e7b400c2dbeb44677be4e18f5f Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Tue, 9 Aug 2016 10:23:10 +0200 Subject: [PATCH 07/15] show help if -h flag given among other parameters --- build.gradle | 2 +- .../java/com/ericsson/eiffel/remrem/generate/cli/CLI.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 36710d03..95f7c830 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ apply plugin: 'war' war { baseName = 'remrem-generate' - version = '0.5.1' + version = '0.5.2' } diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index b2e0656c..dc250485 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -107,7 +107,10 @@ private void handleLogging(CommandLine commandLine) { */ private void handleOptions(CommandLine commandLine) { handleLogging(commandLine); - if (commandLine.hasOption("f") && commandLine.hasOption("t")) { + if (commandLine.hasOption("h")) { + System.out.println("You passed help flag."); + help(options); + } else if (commandLine.hasOption("f") && commandLine.hasOption("t")) { handleFileArgs(commandLine); } else if (commandLine.hasOption("json") && commandLine.hasOption("t")) { handleJsonArgs(commandLine); From b896cdc3c9f18a3c2277545b6e1e3d42851db281 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Tue, 9 Aug 2016 10:27:51 +0200 Subject: [PATCH 08/15] cleaning build.gradle --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 95f7c830..d58c5002 100644 --- a/build.gradle +++ b/build.gradle @@ -89,7 +89,6 @@ repositories { } dependencies { - compile files('/home/evasiba/git/eiffel3messaging/build/libs/remrem-eiffel3messaging-1.2.jar') compile('com.jayway.restassured:rest-assured:2.3.2') compile("org.springframework.boot:spring-boot-starter-web:$sprintBootVersion") { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' From e6d90c78266692d4a591392a8b35d1fc218b6a3d Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Tue, 23 Aug 2016 08:26:19 +0200 Subject: [PATCH 09/15] start spring without web --- build.gradle | 2 +- .../ericsson/eiffel/remrem/generate/App.java | 20 ++++++++------ .../eiffel/remrem/generate/cli/CLI.java | 27 ++++++++++++++----- .../controller/Eiffel3Controller.java | 3 ++- .../controller/EiffelSemanticsController.java | 2 +- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index d58c5002..c6111ff4 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ apply plugin: 'war' war { baseName = 'remrem-generate' - version = '0.5.2' + version = '0.5.3' } diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java index b634fb06..ab04d1a8 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java @@ -6,6 +6,8 @@ import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.web.WebAppConfiguration; import com.ericsson.eiffel.remrem.generate.cli.CLI; @@ -16,21 +18,23 @@ public class App extends SpringBootServletInitializer { public static void main(String[] args) { - + startService(args); // CLI class checks if arguments are passed to application // and if so we do not start the service but act based on // passed arguments. If no arguments are passed the server // will be started - CLI cli = new CLI(); - boolean needsStartService = cli.parse(args); - - if (needsStartService) { - startService(args); - } +// CLI cli = new CLI(); +// boolean needsStartService = cli.parse(args); +// +// if (needsStartService) { +// startService(args); +// } } private static void startService(String[] args) { - ApplicationContext ctx = SpringApplication.run(App.class, args); + SpringApplication application = new SpringApplication(App.class); + application.setWebEnvironment(false); + ApplicationContext ctx = application.run(args); System.out.println("Let's inspect active profiles:"); for (String envNames : ctx.getEnvironment().getActiveProfiles()) { diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index dc250485..43b3cd42 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -11,12 +11,14 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.impl.SLF4JLog; -import org.apache.commons.logging.impl.SLF4JLogFactory; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Component; +import org.springframework.boot.CommandLineRunner; -import com.ericsson.eiffel.remrem.message.services.Eiffel3Service; +//import com.ericsson.eiffel.remrem.message.services.Eiffel3Service; //import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; import com.google.gson.JsonObject; @@ -33,13 +35,26 @@ * @author evasiba * */ -public class CLI { +@Component +@ComponentScan(basePackages = "com.ericsson.eiffel.remrem") +public class CLI implements CommandLineRunner{ private Options options=null; + @Autowired + @Qualifier("eiffel3") + private MsgService msgService; public CLI() { options = createCLIOptions(); } + @Override + public void run(String... args) throws Exception { + // TODO Auto-generated method stub + boolean startService = parse(args); + if (!startService) + return; + } + /** * Creates the options needed by command line interface * @return the options this CLI can handle @@ -164,7 +179,7 @@ private void handleJsonString(String jsonString, CommandLine commandLine) { // MsgService msgService = new SemanticsService(); - MsgService msgService = new Eiffel3Service(); +// MsgService msgService = new Eiffel3Service(); String responseFilePath = null; if (commandLine.hasOption("r")) responseFilePath = commandLine.getOptionValue("r"); diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java index 6eb63c35..adaa1087 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java @@ -15,7 +15,8 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -@Profile("eiffel3") @RestController @RequestMapping("/eiffel3") public class Eiffel3Controller { +//@Profile("eiffel3") @RestController @RequestMapping("/eiffel3") +public class Eiffel3Controller { @Autowired @Qualifier("eiffel3") private MsgService msgService; diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java index dd37ddcb..00ee2790 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java @@ -16,7 +16,7 @@ -@RestController @RequestMapping("/eiffelsemantics") +//@RestController @RequestMapping("/eiffelsemantics") public class EiffelSemanticsController { @Autowired @Qualifier("eiffel-semantics") private MsgService msgService; From fde8a1ae011f5685a9ba56744430a65e235a705e Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Wed, 24 Aug 2016 11:47:38 +0200 Subject: [PATCH 10/15] remove @Configuration from swagger config --- build.gradle | 2 +- .../ericsson/eiffel/remrem/generate/App.java | 31 +++---------------- .../eiffel/remrem/generate/cli/CLI.java | 2 +- .../remrem/generate/config/SwaggerConfig.java | 1 - 4 files changed, 7 insertions(+), 29 deletions(-) diff --git a/build.gradle b/build.gradle index c6111ff4..77f04edb 100644 --- a/build.gradle +++ b/build.gradle @@ -112,7 +112,7 @@ dependencies { //Injectable Message Library and its Implementation compile 'com.github.Ericsson:eiffel-remrem-shared:0.1.4' - compile 'com.github.Ericsson:eiffel-remrem-semantics:0.1.2' + compile 'com.github.Ericsson:eiffel-remrem-semantics:0.1.0' //compileOnly is supported in gradle 2.12 but it might be required // in cucumber folder diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java index ab04d1a8..551091e7 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java @@ -1,13 +1,12 @@ package com.ericsson.eiffel.remrem.generate; +import org.apache.commons.cli.Options; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.web.WebAppConfiguration; import com.ericsson.eiffel.remrem.generate.cli.CLI; @@ -19,34 +18,14 @@ public class App extends SpringBootServletInitializer { public static void main(String[] args) { startService(args); - // CLI class checks if arguments are passed to application - // and if so we do not start the service but act based on - // passed arguments. If no arguments are passed the server - // will be started -// CLI cli = new CLI(); -// boolean needsStartService = cli.parse(args); -// -// if (needsStartService) { -// startService(args); -// } } private static void startService(String[] args) { SpringApplication application = new SpringApplication(App.class); - application.setWebEnvironment(false); + // We do not start web service if any arguments are passed + if (args.length > 0) + application.setWebEnvironment(false); ApplicationContext ctx = application.run(args); - - System.out.println("Let's inspect active profiles:"); - for (String envNames : ctx.getEnvironment().getActiveProfiles()) { - System.out.println(envNames); - } - - System.out.println("Let's inspect the beans provided by Spring Boot:"); - - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } } + } \ No newline at end of file diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 43b3cd42..78d7afd7 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -43,7 +43,7 @@ public class CLI implements CommandLineRunner{ @Qualifier("eiffel3") private MsgService msgService; - public CLI() { + public CLI() { options = createCLIOptions(); } diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/config/SwaggerConfig.java b/src/main/java/com/ericsson/eiffel/remrem/generate/config/SwaggerConfig.java index 9a7faebe..c832f7a7 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/config/SwaggerConfig.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/config/SwaggerConfig.java @@ -8,7 +8,6 @@ import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; -@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean From a55943071499d9a721521d2803272f33fcd58d87 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Thu, 25 Aug 2016 11:00:24 +0200 Subject: [PATCH 11/15] small CLI comment updates --- .../java/com/ericsson/eiffel/remrem/generate/cli/CLI.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 78d7afd7..a53fe02a 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -31,7 +31,9 @@ * Class for interpreting the passed arguments from command line. * Parse method returns true, meaning we need to start the service afterwards, if no argument * is given. The same method returns false, meaning we do not start the service afterwards, if any - * argument is given. If an argument is given that it is not recognized we print help + * argument is given. If an argument is given that it is not recognized we print help. + * Only Eiffel3 messaging is supported right now by default. + * Comment out @Qualifier("eiffel3") and activate @Qualifier("eiffel-semantics") if you want semantics * @author evasiba * */ @@ -41,6 +43,7 @@ public class CLI implements CommandLineRunner{ private Options options=null; @Autowired @Qualifier("eiffel3") +// @Qualifier("eiffel-semantics") private MsgService msgService; public CLI() { From 161bb9578486bc4b964d754bc49bcb3b41c4bb49 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Fri, 26 Aug 2016 08:44:54 +0200 Subject: [PATCH 12/15] activate controllers again --- .../java/com/ericsson/eiffel/remrem/generate/cli/CLI.java | 2 -- .../eiffel/remrem/generate/controller/Eiffel3Controller.java | 2 +- .../remrem/generate/controller/EiffelSemanticsController.java | 4 +--- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index a53fe02a..5f783f93 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -18,8 +18,6 @@ import org.springframework.stereotype.Component; import org.springframework.boot.CommandLineRunner; -//import com.ericsson.eiffel.remrem.message.services.Eiffel3Service; -//import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; import com.google.gson.JsonObject; import com.google.gson.JsonParser; diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java index adaa1087..891661e2 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/Eiffel3Controller.java @@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -//@Profile("eiffel3") @RestController @RequestMapping("/eiffel3") +@Profile("eiffel3") @RestController @RequestMapping("/eiffel3") public class Eiffel3Controller { @Autowired @Qualifier("eiffel3") private MsgService msgService; diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java index 00ee2790..498c0141 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/controller/EiffelSemanticsController.java @@ -14,9 +14,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; - - -//@RestController @RequestMapping("/eiffelsemantics") +@RestController @RequestMapping("/eiffelsemantics") public class EiffelSemanticsController { @Autowired @Qualifier("eiffel-semantics") private MsgService msgService; From da197a63c8dc78b787869eea0d165b69e7171a5b Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Wed, 31 Aug 2016 10:03:08 +0200 Subject: [PATCH 13/15] handle exit code --- .../ericsson/eiffel/remrem/generate/App.java | 3 ++ .../eiffel/remrem/generate/cli/CLI.java | 36 +++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java index 551091e7..6f4dc76e 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java @@ -2,6 +2,7 @@ import org.apache.commons.cli.Options; +import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.web.SpringBootServletInitializer; @@ -22,6 +23,8 @@ public static void main(String[] args) { private static void startService(String[] args) { SpringApplication application = new SpringApplication(App.class); + application.setBannerMode(Banner.Mode.OFF); + application.setLogStartupInfo(false); // We do not start web service if any arguments are passed if (args.length > 0) application.setWebEnvironment(false); diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 5f783f93..295a1431 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -18,6 +18,7 @@ import org.springframework.stereotype.Component; import org.springframework.boot.CommandLineRunner; +import com.ericsson.eiffel.remrem.semantics.SemanticsService; import com.ericsson.eiffel.remrem.shared.MsgService; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -40,9 +41,7 @@ public class CLI implements CommandLineRunner{ private Options options=null; @Autowired - @Qualifier("eiffel3") -// @Qualifier("eiffel-semantics") - private MsgService msgService; + private MsgService[] msgServices; public CLI() { options = createCLIOptions(); @@ -68,6 +67,7 @@ private static Options createCLIOptions() { options.addOption("t", "message_type", true, "message type, mandatory if -f or -json is given"); options.addOption("r", "response_file", true, "file to store the response in, optional"); options.addOption("d", "debug", false, "enable debug traces"); + options.addOption("mp", "messaging_protocol", true, "name of messaging protocol to be used, e.g. eiffel3, semantics"); return options; } @@ -79,7 +79,7 @@ private static void help(Options options) { // This prints out some help HelpFormatter formater = new HelpFormatter(); formater.printHelp("java -jar", options); - System.exit(0); + System.exit(1); } /** @@ -157,6 +157,7 @@ private String readFileContent(String filePath) { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); + System.exit(-2); } return null; } @@ -179,8 +180,6 @@ private void handleJsonArgs(CommandLine commandLine) { private void handleJsonString(String jsonString, CommandLine commandLine) { -// MsgService msgService = new SemanticsService(); -// MsgService msgService = new Eiffel3Service(); String responseFilePath = null; if (commandLine.hasOption("r")) responseFilePath = commandLine.getOptionValue("r"); @@ -188,6 +187,7 @@ private void handleJsonString(String jsonString, try { JsonParser parser = new JsonParser(); JsonObject jsonContent = parser.parse(jsonString).getAsJsonObject(); + MsgService msgService = getMessageService(commandLine); String returnJsonStr = msgService.generateMsg(msgType, jsonContent); if (responseFilePath != null) { try( PrintWriter out = new PrintWriter( responseFilePath ) ){ @@ -199,6 +199,30 @@ private void handleJsonString(String jsonString, } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); + System.exit(-1); } } + + private MsgService getMessageService(CommandLine commandLine) { + if (commandLine.hasOption("mp")) { + String protocol = commandLine.getOptionValue("mp"); + for (MsgService service : msgServices) { + boolean isEiffel3 = (protocol.equals("eiffel3")); + boolean isEiffel3Service = service.getClass().getName().endsWith("Eiffel3Service"); + if (isEiffel3 && isEiffel3Service) + return service; + + } + } else { + System.out.println( "No protocol has been specified. Semantics is used as default"); + for (MsgService service : msgServices) { + if (service instanceof SemanticsService) + return service; + } + } + + System.out.println( "No protocol service has been found registered."); + System.exit(-3); + return null; + } } \ No newline at end of file From e8801df0e5edc25533a7c91effa64d6cd1bc424e Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Fri, 2 Sep 2016 14:54:44 +0200 Subject: [PATCH 14/15] add more help message --- build.gradle | 2 +- .../java/com/ericsson/eiffel/remrem/generate/cli/CLI.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 77f04edb..843607d8 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ apply plugin: 'war' war { baseName = 'remrem-generate' - version = '0.5.3' + version = '0.5.4' } diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 295a1431..3ab3b138 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -131,6 +131,7 @@ private void handleOptions(CommandLine commandLine) { } else if (commandLine.hasOption("json") && commandLine.hasOption("t")) { handleJsonArgs(commandLine); }else { + System.out.println("Nothing to do with the options you passed."); help(options); } } @@ -189,6 +190,7 @@ private void handleJsonString(String jsonString, JsonObject jsonContent = parser.parse(jsonString).getAsJsonObject(); MsgService msgService = getMessageService(commandLine); String returnJsonStr = msgService.generateMsg(msgType, jsonContent); + returnJsonStr = "[" + returnJsonStr + "]"; if (responseFilePath != null) { try( PrintWriter out = new PrintWriter( responseFilePath ) ){ out.println( returnJsonStr ); @@ -196,7 +198,7 @@ private void handleJsonString(String jsonString, } else { System.out.println( returnJsonStr ); } - } catch (IOException e) { + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); From 2e67c20cadbb783a563ba8e87d683484b113bbe0 Mon Sep 17 00:00:00 2001 From: "Vali (Vasile Baluta)" Date: Fri, 9 Sep 2016 10:36:43 +0200 Subject: [PATCH 15/15] turn off Spring logging --- build.gradle | 6 +++- .../ericsson/eiffel/remrem/generate/App.java | 10 ++---- .../eiffel/remrem/generate/cli/CLI.java | 1 + .../config/SpringLoggingInitializer.java | 33 +++++++++++++++++++ src/main/resources/application.yml | 12 +++++++ 5 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/ericsson/eiffel/remrem/generate/config/SpringLoggingInitializer.java diff --git a/build.gradle b/build.gradle index 843607d8..2c377094 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ apply plugin: 'war' war { baseName = 'remrem-generate' - version = '0.5.4' + version = '0.5.5' } @@ -52,6 +52,10 @@ sourceSets { } } +processResources { + expand(project.properties) +} + install.dependsOn assemble apply plugin: 'spring-boot' diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java index 6f4dc76e..03878f5d 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/App.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/App.java @@ -1,7 +1,5 @@ package com.ericsson.eiffel.remrem.generate; - -import org.apache.commons.cli.Options; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -9,9 +7,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; -import com.ericsson.eiffel.remrem.generate.cli.CLI; - -import java.util.Arrays; +import com.ericsson.eiffel.remrem.generate.config.SpringLoggingInitializer; @SpringBootApplication @ComponentScan("com.ericsson.eiffel.remrem") @@ -21,8 +17,9 @@ public static void main(String[] args) { startService(args); } - private static void startService(String[] args) { + private static void startService(String[] args) { SpringApplication application = new SpringApplication(App.class); + application.addInitializers(new SpringLoggingInitializer()); application.setBannerMode(Banner.Mode.OFF); application.setLogStartupInfo(false); // We do not start web service if any arguments are passed @@ -30,5 +27,4 @@ private static void startService(String[] args) { application.setWebEnvironment(false); ApplicationContext ctx = application.run(args); } - } \ No newline at end of file diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java index 3ab3b138..b54bb7e0 100644 --- a/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/cli/CLI.java @@ -88,6 +88,7 @@ private static void help(Options options) { * @return if the service should start or not */ public boolean parse(String[] args) { + Logger log = (Logger) LoggerFactory.getLogger("ROOT"); CommandLineParser parser = new DefaultParser(); boolean startService = true; try { diff --git a/src/main/java/com/ericsson/eiffel/remrem/generate/config/SpringLoggingInitializer.java b/src/main/java/com/ericsson/eiffel/remrem/generate/config/SpringLoggingInitializer.java new file mode 100644 index 00000000..e04c2b2b --- /dev/null +++ b/src/main/java/com/ericsson/eiffel/remrem/generate/config/SpringLoggingInitializer.java @@ -0,0 +1,33 @@ +package com.ericsson.eiffel.remrem.generate.config; + +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter; +import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer; +import org.springframework.boot.context.config.ConfigFileApplicationListener; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import com.ericsson.eiffel.remrem.generate.App; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; + +public class SpringLoggingInitializer implements ApplicationContextInitializer { + + /* (non-Javadoc) + * @see org.springframework.context.ApplicationContextInitializer#initialize(org.springframework.context.ConfigurableApplicationContext) + * + * We need to turn off Spring logging since we want write the generated message to console. + */ + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { + Class[] loggers = {SpringApplication.class, App.class, ConfigFileApplicationListener.class, EndpointMBeanExporter.class, + AutoConfigurationReportLoggingInitializer.class}; + Logger log = (Logger) LoggerFactory.getLogger("ROOT"); + log.setLevel(Level.ERROR); + for (Class logger : loggers) { + log = (Logger) LoggerFactory.getLogger(logger); + log.setLevel(Level.ERROR); + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9983a4df..1f0e0d55 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,3 +2,15 @@ spring: http: converters: preferred-json-mapper: gson +debug: false +logging: + level: + root: ERROR + org: + springframework: + web: ERROR +org: + springframework: + boot: + logging: + LoggingSystem: none