Skip to content

Commit

Permalink
Merge pull request #26 from vasba/master
Browse files Browse the repository at this point in the history
add cli functionality to publish component
  • Loading branch information
vasile-baluta authored Sep 9, 2016
2 parents edf2e62 + 9adb669 commit 1e28a96
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ producer.iml



/bin/
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ plugins {

war {
baseName = 'remrem-publish'
version = '0.1.0'
version = '0.1.5'
}

apply plugin: 'spring-boot'
apply plugin: 'java'
apply plugin: 'eclipse'

jacocoTestReport {
reports {
Expand Down Expand Up @@ -89,6 +91,8 @@ task integrationTest(type: Test) {

// In this section you declare the dependencies for your production and test code
dependencies {
compile('com.jayway.restassured:rest-assured:2.3.2')
compile('org.yaml:snakeyaml:1.8')
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.18'
compile("org.springframework.boot:spring-boot-starter-web:$sprintBootVersion") {
Expand All @@ -99,6 +103,9 @@ dependencies {
compile 'com.google.code.gson:gson:2.6.2'
compile 'org.projectlombok:lombok:1.16.8'

//commons CLI
compile 'commons-cli:commons-cli:1.3.1'

//ServletException requires compile time servlet dependency but it causes problems
//when deployed if exist on war run time.. hence provided but also compileOnly
compileOnly("org.springframework.boot:spring-boot-starter-tomcat")
Expand Down
2 changes: 1 addition & 1 deletion src/integration-test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rabbitmq:
host: 127.0.0.1
host: 150.132.35.5
exchange:
name: eiffel.test
spring:
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/ericsson/eiffel/remrem/publish/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import com.ericsson.eiffel.remrem.publish.cli.CLI;

import java.util.Arrays;

@SpringBootApplication @Slf4j public class App extends SpringBootServletInitializer {
@SpringBootApplication
@Slf4j
public class App extends SpringBootServletInitializer {
public static void main(String[] 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) {
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);

log.info("Let's inspect the beans provided by Spring Boot:");
Expand All @@ -20,4 +38,4 @@ public static void main(String[] args) {
log.info(beanName);
}
}
}
}
179 changes: 179 additions & 0 deletions src/main/java/com/ericsson/eiffel/remrem/publish/cli/CLI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package com.ericsson.eiffel.remrem.publish.cli;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

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 com.ericsson.eiffel.remrem.publish.config.PropertiesConfig;
import com.ericsson.eiffel.remrem.publish.helper.RMQHelper;
import com.ericsson.eiffel.remrem.publish.service.MessageService;
import com.ericsson.eiffel.remrem.publish.service.MessageServiceRMQImpl;
import com.ericsson.eiffel.remrem.publish.service.SendResult;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
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
*
* This class also uses System Properties to pass some arguments to underlying service. It is important to
* choose properties names that are difficult to be matched by the system
*
* @author evasiba
*
*/
public class CLI {
private Options options=null;

public CLI() {
options = createCLIOptions();
}

/**
* 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, "event content file, mandatory");
options.addOption("mb", "message_bus", true, "host of message bus to use, default is 127.0.0.1");
options.addOption("en", "exchange_name", true, "exchange name, default is eiffel.poc");
options.addOption("rk", "routing_key", true, "routing key, mandatory");
options.addOption("np", "non_persistent", false, "remove persistence from message sending");

return options;
}

/**
* 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(1);
}

/**
* 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;
handleOptions(commandLine);
}
} catch (Exception e) {
e.printStackTrace();
help(options);
}
return startService;
}

/**
* Delegates actions depending on the passed arguments
* @param commandLine command line arguments
*/
private void handleOptions(CommandLine commandLine) {
handleMessageBusOptions(commandLine);
if (commandLine.hasOption("h")) {
System.out.println("You passed help flag.");
clearSystemProperties();
help(options);
} else if (commandLine.hasOption("f") && commandLine.hasOption("rk")) {
String filePath = commandLine.getOptionValue("f");
String routingKey = commandLine.getOptionValue("rk");
handleContentFile(filePath, routingKey);
} else {
System.out.println("Missing arguments, please review your arguments" +
" and check if any mandatory argument is missing");
clearSystemProperties();
help(options);
}
}

/**
* Sets the system properties with values passed for
* message bus host and exchange name
* @param commandLine command line arguments
*/
private void handleMessageBusOptions(CommandLine commandLine){
if (commandLine.hasOption("mb")) {
String messageBusHost = commandLine.getOptionValue("mb");
String key = PropertiesConfig.MESSAGE_BUSS_HOST;
System.setProperty(key, messageBusHost);
}

if (commandLine.hasOption("en")) {
String exchangeName = commandLine.getOptionValue("en");
String key = PropertiesConfig.EXCHANGE_NAME;
System.setProperty(key, exchangeName);
}

String usePersistance = "true";
if (commandLine.hasOption("np")) {
usePersistance = "false";
}
String key = PropertiesConfig.USE_PERSISTENCE;
System.setProperty(key, usePersistance);
}

/**
* Handle event from file
* @param filePath the path of the file where the messages reside
*/
public void handleContentFile(String filePath, String routingKey) {
JsonParser parser = new JsonParser();
try {
byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
String fileContent = new String(fileBytes);
JsonArray bodyJson = parser.parse(fileContent).getAsJsonArray();
List<String> msgs = new ArrayList<>();
for (JsonElement obj : bodyJson) {
msgs.add(obj.toString());
}
MessageService msgService = new MessageServiceRMQImpl();
List<SendResult> results = msgService.send(routingKey, msgs);
msgService.cleanUp();
clearSystemProperties();
for(SendResult result : results)
System.out.println(result.getMsg());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
}

/**
* Remove the system properties add by this application
*/
private void clearSystemProperties() {
String key = PropertiesConfig.MESSAGE_BUSS_HOST;
System.clearProperty(key);
key = PropertiesConfig.EXCHANGE_NAME;
System.clearProperty(key);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ericsson.eiffel.remrem.publish.config;

public class PropertiesConfig {
public static final String MESSAGE_BUSS_HOST = "com.ericsson.eiffel.remrem.publish.messagebus.host";
public static final String EXCHANGE_NAME = "com.ericsson.eiffel.remrem.publish.exchange.name";
public static final String USE_PERSISTENCE = "com.ericsson.eiffel.remrem.publish.use.persistence";
}
Loading

0 comments on commit 1e28a96

Please sign in to comment.