From a9c440100b6c6266d6f57fab831d0eaa599eebaf Mon Sep 17 00:00:00 2001 From: xumakap Date: Wed, 20 Sep 2017 16:51:56 +0530 Subject: [PATCH] Changed REMReM to read required properties from CLI options for CLI and 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 --- CHANGELOG.md | 3 + build.gradle | 4 +- .../eiffel/remrem/publish/cli/CLI.java | 10 +-- .../eiffel/remrem/publish/cli/CliOptions.java | 4 +- .../config/RabbitMqPropertiesConfig.java | 25 ++++---- .../remrem/publish/helper/RMQHelper.java | 42 +++++++----- .../publish/helper/RabbitMqProperties.java | 64 ++++++++----------- ....properties => config.template.properties} | 2 + .../publish/helper/RMQHelperUnitTest.java | 6 +- .../ericsson/eiffel/remrem/publish/App.java | 4 +- .../controller/ProducerController.java | 4 +- .../MessageServiceRMQImplUnitTest.java | 21 ++++-- 12 files changed, 98 insertions(+), 91 deletions(-) rename publish-common/src/main/resources/{config.properties => config.template.properties} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9495eabc..a324d6c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/build.gradle b/build.gradle index b77044ec..93efa1db 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { @@ -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"){ diff --git a/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CLI.java b/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CLI.java index e244da76..e649d544 100644 --- a/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CLI.java +++ b/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CLI.java @@ -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; @@ -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{ @@ -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()) { diff --git a/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CliOptions.java b/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CliOptions.java index 1920f3f1..40d061dd 100644 --- a/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CliOptions.java +++ b/publish-cli/src/main/java/com/ericsson/eiffel/remrem/publish/cli/CliOptions.java @@ -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."); diff --git a/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/config/RabbitMqPropertiesConfig.java b/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/config/RabbitMqPropertiesConfig.java index d126fabf..7477a767 100644 --- a/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/config/RabbitMqPropertiesConfig.java +++ b/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/config/RabbitMqPropertiesConfig.java @@ -28,10 +28,11 @@ public class RabbitMqPropertiesConfig { */ public Map getRabbitMqProperties() { Map map = new HashMap(); + 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()); } } @@ -39,24 +40,24 @@ public Map getRabbitMqProperties() { for (Entry 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()); } } diff --git a/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelper.java b/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelper.java index e5dd893c..11cb492a 100644 --- a/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelper.java +++ b/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelper.java @@ -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; @@ -35,14 +35,12 @@ @Component("rmqHelper") public class RMQHelper { - @Inject - RMQBeanConnectionFactory factory; private static final String FALSE = "false"; @Autowired RabbitMqPropertiesConfig rabbitMqPropertiesConfig; - Map rabbitMqPropertiesMap; + Map rabbitMqPropertiesMap = new HashMap(); Logger log = (Logger) LoggerFactory.getLogger(RMQHelper.class); @@ -54,26 +52,37 @@ public void setRabbitMqPropertiesMap(Map 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) { @@ -97,6 +106,7 @@ public void cleanUp() throws IOException { } } + @PostConstruct private void handleLogging() { String debug = System.getProperty(PropertiesConfig.DEBUG); log.setLevel(Level.ALL); diff --git a/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RabbitMqProperties.java b/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RabbitMqProperties.java index 68d015dd..de36701b 100644 --- a/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RabbitMqProperties.java +++ b/publish-common/src/main/java/com/ericsson/eiffel/remrem/publish/helper/RabbitMqProperties.java @@ -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; @@ -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. */ diff --git a/publish-common/src/main/resources/config.properties b/publish-common/src/main/resources/config.template.properties similarity index 93% rename from publish-common/src/main/resources/config.properties rename to publish-common/src/main/resources/config.template.properties index 2ee5b247..ac4330a3 100644 --- a/publish-common/src/main/resources/config.properties +++ b/publish-common/src/main/resources/config.template.properties @@ -1,3 +1,5 @@ +#This file is for reference only. Will not pick any values from this file. + #server.port=8080 debug: false diff --git a/publish-common/src/test/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelperUnitTest.java b/publish-common/src/test/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelperUnitTest.java index 2625a75a..01d79786 100644 --- a/publish-common/src/test/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelperUnitTest.java +++ b/publish-common/src/test/java/com/ericsson/eiffel/remrem/publish/helper/RMQHelperUnitTest.java @@ -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 { diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/App.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/App.java index c1ffe59f..2aed6118 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/App.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/App.java @@ -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); diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index c1e7a2e2..3f6ac883 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -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()); diff --git a/publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/MessageServiceRMQImplUnitTest.java b/publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/MessageServiceRMQImplUnitTest.java index 0bfdc165..9e442a75 100644 --- a/publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/MessageServiceRMQImplUnitTest.java +++ b/publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/MessageServiceRMQImplUnitTest.java @@ -15,7 +15,6 @@ package com.ericsson.eiffel.remrem.publish.service; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -25,9 +24,10 @@ import java.util.HashMap; import java.util.Map; +import javax.annotation.PostConstruct; + import org.apache.commons.io.FileUtils; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -39,6 +39,7 @@ import com.ericsson.eiffel.remrem.protocol.MsgService; import com.ericsson.eiffel.remrem.publish.helper.PublishUtils; import com.ericsson.eiffel.remrem.publish.helper.RMQHelper; +import com.ericsson.eiffel.remrem.publish.helper.RabbitMqProperties; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; @@ -56,7 +57,19 @@ public class MessageServiceRMQImplUnitTest { @Autowired @Qualifier("rmqHelper") RMQHelper rmqHelper; - private String protocol = "eiffelsemantics"; + private static final String protocol = "eiffelsemantics"; + private static final String host= "127.0.0.1"; + private static final String exchangeName= "amq.direct"; + private static final String domainId= "eiffelxxx"; + + @PostConstruct public void setUp() throws Exception { + rmqHelper.getRabbitMqPropertiesMap().put(protocol, new RabbitMqProperties()); + rmqHelper.getRabbitMqPropertiesMap().get(protocol).setProtocol(protocol); + rmqHelper.getRabbitMqPropertiesMap().get(protocol).setHost(host); + rmqHelper.getRabbitMqPropertiesMap().get(protocol).setExchangeName(exchangeName); + rmqHelper.getRabbitMqPropertiesMap().get(protocol).setDomainId(domainId); + rmqHelper.getRabbitMqPropertiesMap().get(protocol).init(); + } @Test public void sendNormal() throws Exception { Map map = new HashMap(); @@ -101,7 +114,7 @@ public class MessageServiceRMQImplUnitTest { } assertEquals(Expected, jarray.toString()); } - @Test public void testMultipleSuccessfulEvents() throws Exception { + @Test public void testMultipleSuccessfulEvents() throws Exception { String body = FileUtils.readFileToString(new File("src/integration-test/resources/MultipleValidEvents.json")); String Expected="[{\"id\":\"9cdd0f68-df85-44b0-88bd-fc4163ac90a1\",\"status_code\":200,\"result\":\"SUCCESS\",\"message\":\"Event sent successfully\"},{\"id\":\"9cdd0f68-df85-44b0-88bd-fc4163ac90a2\",\"status_code\":200,\"result\":\"SUCCESS\",\"message\":\"Event sent successfully\"},{\"id\":\"9cdd0f68-df85-44b0-88bd-fc4163ac90a3\",\"status_code\":200,\"result\":\"SUCCESS\",\"message\":\"Event sent successfully\"}]"; JsonArray jarray = new JsonArray();