Skip to content

Commit

Permalink
Changed REMReM to read required properties from CLI options for CLI a…
Browse files Browse the repository at this point in the history
…nd from JAVA_OPTS, tomcat/conf/config.properties for service (#79)

* Changed REMReM to read required properties from CLI options for CLI and from JAVA_OPTS, tomcat/conf/config.properties for service

* removed unnecessary logs

* changed file name

* Added build.gradle and CHANGELOG.md files

* Extracted to a method in RabbitMqProperties

* changed logic of the method getValuesFromSystemProperties

* minor fixes

* Fixed testcase
  • Loading branch information
Umadevi-Kapu authored Sep 20, 2017
1 parent c3f533f commit a9c4401
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 91 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.4.2
- Changed REMReM publish to read required properties from CLI options for CLI and from JAVA_OPTS, tomcat/conf/config.properties for service.

## 0.4.1
- Implemented functionality to get properties from java opts for publish-service

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ subprojects {
apply plugin: 'java'

//Latest version for publish
version = "0.4.1"
version = "0.4.2"

//Declare where to find the dependencies of project here
repositories {
Expand Down Expand Up @@ -109,7 +109,7 @@ subprojects {
compile 'org.apache.commons:commons-lang3:3.5'

//Injectable Message Library and its Implementation
compile 'com.github.Ericsson:eiffel-remrem-shared:0.3.3'
compile 'com.github.Ericsson:eiffel-remrem-shared:0.3.4'
compile 'com.github.Ericsson:eiffel-remrem-protocol-interface:0.0.1'
//For publishing eiffel2.0 events
compile("com.github.Ericsson:eiffel-remrem-semantics:0.2.6"){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

import com.ericsson.eiffel.remrem.protocol.MsgService;
import com.ericsson.eiffel.remrem.publish.config.PropertiesConfig;
Expand Down Expand Up @@ -58,8 +56,6 @@
*
*/
@SpringBootApplication
@PropertySources({ @PropertySource("classpath:config.properties"),
@PropertySource(value = "file:${catalina.home}/conf/config.properties", ignoreResourceNotFound = true) })
@ComponentScan(basePackages = "com.ericsson.eiffel.remrem")
public class CLI implements CommandLineRunner{

Expand Down Expand Up @@ -136,10 +132,8 @@ public void handleContent(String content) {
try {
String msgProtocol = CliOptions.getCommandLine().getOptionValue("mp");
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices);
if(msgService != null && msgProtocol != null && !msgProtocol.equals("eiffelsemantics")) {
rmqHelper.otherProtocolInit(msgProtocol);
}
if (msgService != null) {
if(msgService != null) {
rmqHelper.rabbitMqPropertiesInit(msgService.getServiceName());
SendResult results = messageService.send(content, msgService,CliOptions.getCommandLine().getOptionValue("ud"));
JsonArray jarray=new JsonArray();
for (PublishResultItem result : results.getEvents()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public static void createCLIOptions() {
options = new Options();
options.addOption(createHelpOption());
options.addOption("d", "debug", false, "enable debug traces");
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 amq.direct");
options.addOption("mb", "message_bus", true, "host of message bus to use");
options.addOption("en", "exchange_name", true, "exchange name");
options.addOption("np", "non_persistent", false, "remove persistence from message sending");
options.addOption("port", "port", true, "port to connect to message bus, default is 5672");
options.addOption("tls", "tls", true, "tls version, specify a valid tls version: '1', '1.1, '1.2' or 'default'. It is required for RabbitMq secured port.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,36 @@ public class RabbitMqPropertiesConfig {
*/
public Map<String, RabbitMqProperties> getRabbitMqProperties() {
Map<String, Object> map = new HashMap<String, Object>();
String catalina_home = System.getProperty("catalina.home").replace('\\', '/');
for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
PropertySource propertySource = (PropertySource) it.next();
if (propertySource instanceof MapPropertySource) {
if(propertySource.getName().equalsIgnoreCase("class path resource [config.properties]")) {
if(propertySource.getName().contains("[file:"+catalina_home+"/conf/config.properties]")) {
map.putAll(((MapPropertySource) propertySource).getSource());
}
}
}
for (Entry<String, Object> entry : map.entrySet())
{
String key = entry.getKey();
if(key.contains("rabbitmq") && rabbitMqPropertiesMap.get(key.split("\\.")[0]) == null){
rabbitMqPropertiesMap.put(key.split("\\.")[0], new RabbitMqProperties());
}
if(key.contains("rabbitmq")) {
if (key.contains("rabbitmq")) {
String protocol = key.split("\\.")[0];
if(key.contains("rabbitmq.host")) {
if (rabbitMqPropertiesMap.get(protocol) == null) {
rabbitMqPropertiesMap.put(protocol, new RabbitMqProperties());
}
if (key.contains("rabbitmq.host")) {
rabbitMqPropertiesMap.get(protocol).setHost(entry.getValue().toString());
} else if(key.contains("rabbitmq.port")) {
} else if (key.contains("rabbitmq.port")) {
rabbitMqPropertiesMap.get(protocol).setPort(Integer.getInteger(entry.getValue().toString()));
} else if(key.contains("rabbitmq.username")) {
} else if (key.contains("rabbitmq.username")) {
rabbitMqPropertiesMap.get(protocol).setUsername(entry.getValue().toString());
} else if(key.contains("rabbitmq.password")) {
} else if (key.contains("rabbitmq.password")) {
rabbitMqPropertiesMap.get(protocol).setPassword(entry.getValue().toString());
} else if(key.contains("rabbitmq.tls")) {
} else if (key.contains("rabbitmq.tls")) {
rabbitMqPropertiesMap.get(protocol).setTlsVer(entry.getValue().toString());
} else if(key.contains("rabbitmq.exchangeName")) {
} else if (key.contains("rabbitmq.exchangeName")) {
rabbitMqPropertiesMap.get(protocol).setExchangeName(entry.getValue().toString());
} else if(key.contains("rabbitmq.domainId")) {
} else if (key.contains("rabbitmq.domainId")) {
rabbitMqPropertiesMap.get(protocol).setDomainId(entry.getValue().toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -35,14 +35,12 @@

@Component("rmqHelper") public class RMQHelper {

@Inject
RMQBeanConnectionFactory factory;
private static final String FALSE = "false";

@Autowired
RabbitMqPropertiesConfig rabbitMqPropertiesConfig;

Map<String, RabbitMqProperties> rabbitMqPropertiesMap;
Map<String, RabbitMqProperties> rabbitMqPropertiesMap = new HashMap<String, RabbitMqProperties>();

Logger log = (Logger) LoggerFactory.getLogger(RMQHelper.class);

Expand All @@ -54,26 +52,37 @@ public void setRabbitMqPropertiesMap(Map<String, RabbitMqProperties> rabbitMqPro
this.rabbitMqPropertiesMap = rabbitMqPropertiesMap;
}

@PostConstruct public void init() {
handleLogging();
log.info("RMQHelper init ...");
rabbitMqPropertiesMap = rabbitMqPropertiesConfig.getRabbitMqProperties();
for(String protocol : rabbitMqPropertiesMap.keySet()) {
rabbitMqPropertiesMap.get(protocol).setFactory(factory);
rabbitMqPropertiesMap.get(protocol).setProtocol(protocol);
rabbitMqPropertiesMap.get(protocol).init();
@PostConstruct
public void init() {
if (!Boolean.getBoolean(PropertiesConfig.CLI_MODE)) {
log.info("RMQHelper init ...");
rabbitMqPropertiesMap = rabbitMqPropertiesConfig.getRabbitMqProperties();
for (String protocol : rabbitMqPropertiesMap.keySet()) {
protocolInit(protocol);
}
}
}

public void otherProtocolInit(String protocol) {
/***
* This method is used to set protocol specific RabbitMQ properties
* @param protocol name
*/
public void rabbitMqPropertiesInit(String protocol) {
if(!rabbitMqPropertiesMap.containsKey(protocol)) {
rabbitMqPropertiesMap.put(protocol, new RabbitMqProperties());
rabbitMqPropertiesMap.get(protocol).setFactory(factory);
rabbitMqPropertiesMap.get(protocol).setProtocol(protocol);
rabbitMqPropertiesMap.get(protocol).init();
protocolInit(protocol);
}
}

/***
* This method is used to set the values of protocol and initialize the RabbitMq properties
* @param protocol name
*/
private void protocolInit(String protocol) {
rabbitMqPropertiesMap.get(protocol).setProtocol(protocol);
rabbitMqPropertiesMap.get(protocol).init();
}

public void send(String routingKey, String msg, MsgService msgService) throws IOException {
String protocol = msgService.getServiceName();
if(rabbitMqPropertiesMap.get(protocol) != null) {
Expand All @@ -97,6 +106,7 @@ public void cleanUp() throws IOException {
}
}

@PostConstruct
private void handleLogging() {
String debug = System.getProperty(PropertiesConfig.DEBUG);
log.setLevel(Level.ALL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class RabbitMqProperties {

private RMQBeanConnectionFactory factory;
private RMQBeanConnectionFactory factory = new RMQBeanConnectionFactory();
private static final int CHANNEL_COUNT = 100;
private static final Random random = new Random();
private boolean usePersitance = true;
Expand Down Expand Up @@ -192,60 +192,48 @@ private void initCli() {
}

private void initService() {
String passedHost = System.getProperty(protocol+".rabbitmq.host");
if (passedHost != null) {
host = passedHost;
if (host == null) {
host = getValuesFromSystemProperties(protocol + ".rabbitmq.host");
}

Integer passedPort = Integer.getInteger(System.getProperty(protocol+".rabbitmq.port"));
if (passedPort != null) {
port = passedPort;
if (port == null) {
port = Integer.getInteger(getValuesFromSystemProperties(protocol + ".rabbitmq.port"));
}

String passedDomain = System.getProperty(protocol+".rabbitmq.domainId");
if (passedDomain != null) {
domainId = passedDomain;
if (domainId == null) {
domainId = getValuesFromSystemProperties(protocol + ".rabbitmq.domainId");
}

String passedTlsVer = System.getProperty(protocol+".rabbitmq.tls");
if (passedTlsVer != null) {
tlsVer = passedTlsVer;
if (tlsVer == null) {
tlsVer = getValuesFromSystemProperties(protocol + ".rabbitmq.tls");
}

String passedExchange = System.getProperty(protocol+".rabbitmq.exchangeName");
if (passedExchange != null) {
exchangeName = passedExchange;
}
}

private void setValues() {
String passedHost = System.getProperty(PropertiesConfig.MESSAGE_BUS_HOST);
if (passedHost != null) {
host = passedHost;
}

Integer passedPort = Integer.getInteger(PropertiesConfig.MESSAGE_BUS_PORT);
if (passedPort != null) {
port = passedPort;
if (exchangeName == null) {
exchangeName = getValuesFromSystemProperties(protocol + ".rabbitmq.exchangeName");
}

String passedDomain = System.getProperty(PropertiesConfig.DOMAIN_ID);
if (passedDomain != null) {
domainId = passedDomain;
if (username == null) {
username = getValuesFromSystemProperties(protocol + ".rabbitmq.username");
}

String passedTlsVer = System.getProperty(PropertiesConfig.TLS);
if (passedTlsVer != null) {
tlsVer = passedTlsVer;
if (password == null) {
password = getValuesFromSystemProperties(protocol + ".rabbitmq.password");
}
}

String passedExchange = System.getProperty(PropertiesConfig.EXCHANGE_NAME);
if (passedExchange != null) {
exchangeName = passedExchange;
}
private void setValues() {
host = getValuesFromSystemProperties(PropertiesConfig.MESSAGE_BUS_HOST);
port = Integer.getInteger(PropertiesConfig.MESSAGE_BUS_PORT);
domainId = getValuesFromSystemProperties(PropertiesConfig.DOMAIN_ID);
tlsVer = getValuesFromSystemProperties(PropertiesConfig.TLS);
exchangeName = getValuesFromSystemProperties(PropertiesConfig.EXCHANGE_NAME);
usePersitance = Boolean.getBoolean(PropertiesConfig.USE_PERSISTENCE);
}

private String getValuesFromSystemProperties(String propertyName) {
return System.getProperty(propertyName);
}

/****
* This method is used to check mandatory RabbitMQ properties.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#This file is for reference only. Will not pick any values from this file.

#server.port=8080
debug: false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ public class RMQHelperUnitTest {
Mockito.doNothing().when(factory).useSslProtocol();
Mockito.when(factory.newConnection()).thenReturn(mockConnection);
Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
Mockito.when(rmqHelper.rabbitMqPropertiesConfig.getRabbitMqProperties()).thenReturn(rabbitMqPropertiesMap);
initProperties();
rabbitMqPropertiesMap.put(protocol, rabbitMqProperties);
rmqHelper.setRabbitMqPropertiesMap(rabbitMqPropertiesMap);
rmqHelper.init();
rmqHelper.rabbitMqPropertiesInit(protocol);
rmqHelper.rabbitMqPropertiesMap.get(protocol).setFactory(factory);
}

@After public void tearDown() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

import com.ericsson.eiffel.remrem.publish.config.SpringLoggingInitializer;

@SpringBootApplication
@ComponentScan("com.ericsson.eiffel.remrem")
@PropertySources({ @PropertySource("classpath:config.properties"),
@PropertySource(value = "file:${catalina.home}/conf/config.properties", ignoreResourceNotFound = true) })
@PropertySource(value = "file:${catalina.home}/conf/config.properties", ignoreResourceNotFound = true)
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public ResponseEntity send(@RequestParam(value = "mp", required = false) String

log.debug("mp: " + msgProtocol);
log.debug("body: " + body);
if(msgService != null && msgProtocol != null && !msgProtocol.equals("eiffelsemantics")) {
rmqHelper.otherProtocolInit(msgProtocol);
if(msgService != null && msgProtocol != null) {
rmqHelper.rabbitMqPropertiesInit(msgProtocol);
}
SendResult result = messageService.send(body, msgService, userDomain);
return new ResponseEntity(result, messageService.getHttpStatus());
Expand Down
Loading

0 comments on commit a9c4401

Please sign in to comment.