From 06b3bcb003e4b0cde76ae5a06477d1dcec4ff200 Mon Sep 17 00:00:00 2001 From: akorobov Date: Thu, 15 Feb 2024 16:14:53 +0300 Subject: [PATCH] #78 - configuration migration to startup-parameters library --- README.md | 8 +++++ .../logback/ILogbackAdapterConfig.java | 15 ++++++++ .../adapter/logback/LogbackTCPAdapter.java | 15 +++++--- .../adapter/logback/LogbackUDPAdapter.java | 16 +++++---- .../service/impl/ILogBroadcastingConfig.java | 9 +++++ .../srvlog/service/impl/ISphinxConfig.java | 18 ++++++++++ .../impl/LogBroadcastingServiceImpl.java | 18 ++++++---- .../service/impl/SphinxIndexerService.java | 36 ++++--------------- .../src/main/resources/inMemoryLog.properties | 1 - .../main/resources/logbackAdapter.properties | 3 -- .../main/resources/sphinxclient.properties | 4 --- 11 files changed, 87 insertions(+), 56 deletions(-) create mode 100644 srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/ILogbackAdapterConfig.java create mode 100644 srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ILogBroadcastingConfig.java create mode 100644 srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ISphinxConfig.java delete mode 100644 srvlog-service/src/main/resources/inMemoryLog.properties delete mode 100644 srvlog-service/src/main/resources/logbackAdapter.properties delete mode 100644 srvlog-service/src/main/resources/sphinxclient.properties diff --git a/README.md b/README.md index aa1c414e..5dccee66 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,14 @@ export JSON_ADAPTER_BIND_ADDRESS=127.0.0.1 # json adapter bind address export JSON_ADAPTER_PORT=28080 # json adapter port export JSON_ADAPTER_PATH=/save-logs # json adapter path export JSON_ADAPTER_TOKEN=token # json adapter token +export SPHINX_HOST=localhost # sphinx host +export SPHINX_PORT=9312 # sphinx port +export SPHINX_CONNECT_TIMEOUT=30000 # sphinx connect timeout +export SPHINX_QUERY_INDEXES=index1,index2 # comma separated query indexes +export LOG_STORAGE_CAPACITY=1000 # log broadcasting service storage capacity (web terminal page) +export LOGBACK_PROGRAM=programName # logback program name +export LOGBACK_TCP_PORT=4713 # port for logback tcp adapter +export LOGBACK_UDP_PORT=4713 # port for logback udp adapter ``` Start server uber-jar: ```shell diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/ILogbackAdapterConfig.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/ILogbackAdapterConfig.java new file mode 100644 index 00000000..7a72d762 --- /dev/null +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/ILogbackAdapterConfig.java @@ -0,0 +1,15 @@ +package com.payneteasy.srvlog.adapter.logback; + +import com.payneteasy.startup.parameters.AStartupParameter; + +public interface ILogbackAdapterConfig { + + @AStartupParameter(name = "LOGBACK_PROGRAM", value = "paynet") + String getProgram(); + + @AStartupParameter(name = "LOGBACK_TCP_PORT", value = "4713") + int getTcpPort(); + + @AStartupParameter(name = "LOGBACK_UDP_PORT", value = "4713") + int getUdpPort(); +} diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackTCPAdapter.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackTCPAdapter.java index 0e3f2375..7f7aec97 100644 --- a/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackTCPAdapter.java +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackTCPAdapter.java @@ -6,19 +6,24 @@ import com.payneteasy.srvlog.adapter.utils.IRunnableFactory; import com.payneteasy.srvlog.data.LogData; import com.payneteasy.srvlog.service.ILogCollector; +import com.payneteasy.startup.parameters.StartupParametersFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.net.Socket; @Service public class LogbackTCPAdapter extends AbstractTCPLoggerAdapter { + + private static final ILogbackAdapterConfig adapterConfig = StartupParametersFactory + .getStartupParameters(ILogbackAdapterConfig.class); + @Autowired - public LogbackTCPAdapter( - ILogCollector logCollector, - @Value("${logbackProgram}") String program, - @Value("${logbackTCPPort}") int serverPort) { + public LogbackTCPAdapter(ILogCollector logCollector) { + super(logCollector, adapterConfig.getProgram(), adapterConfig.getTcpPort()); + } + + public LogbackTCPAdapter(ILogCollector logCollector, String program, int serverPort) { super(logCollector, program, serverPort); } diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackUDPAdapter.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackUDPAdapter.java index 61404a61..8cb46371 100644 --- a/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackUDPAdapter.java +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/adapter/logback/LogbackUDPAdapter.java @@ -5,8 +5,8 @@ import com.payneteasy.srvlog.adapter.udp.IDatagramProcessor; import com.payneteasy.srvlog.data.LogData; import com.payneteasy.srvlog.service.ILogCollector; +import com.payneteasy.startup.parameters.StartupParametersFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.ByteArrayInputStream; @@ -16,14 +16,18 @@ @Service public class LogbackUDPAdapter extends AbstractUDPLoggerAdapter { - @Autowired - public LogbackUDPAdapter( - ILogCollector logCollector, - @Value("${logbackProgram}") String program, - @Value("${logbackUDPPort}") int serverPort) { + private static final ILogbackAdapterConfig adapterConfig = StartupParametersFactory + .getStartupParameters(ILogbackAdapterConfig.class); + + public LogbackUDPAdapter(ILogCollector logCollector, String program, int serverPort) { super(serverPort, program, logCollector); } + @Autowired + public LogbackUDPAdapter(ILogCollector logCollector) { + super(adapterConfig.getUdpPort(), adapterConfig.getProgram(), logCollector); + } + @Override protected String getLoggerTypeName() { return "logback"; diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ILogBroadcastingConfig.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ILogBroadcastingConfig.java new file mode 100644 index 00000000..bd11ca52 --- /dev/null +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ILogBroadcastingConfig.java @@ -0,0 +1,9 @@ +package com.payneteasy.srvlog.service.impl; + +import com.payneteasy.startup.parameters.AStartupParameter; + +public interface ILogBroadcastingConfig { + + @AStartupParameter(name = "LOG_STORAGE_CAPACITY", value = "1000") + int getProgramLogStorageCapacity(); +} diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ISphinxConfig.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ISphinxConfig.java new file mode 100644 index 00000000..d7f9a2aa --- /dev/null +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/ISphinxConfig.java @@ -0,0 +1,18 @@ +package com.payneteasy.srvlog.service.impl; + +import com.payneteasy.startup.parameters.AStartupParameter; + +public interface ISphinxConfig { + + @AStartupParameter(name = "SPHINX_HOST", value = "localhost") + String getHost(); + + @AStartupParameter(name = "SPHINX_PORT", value = "9312") + int getPort(); + + @AStartupParameter(name = "SPHINX_CONNECT_TIMEOUT", value = "30000") + int getConnectTimeout(); + + @AStartupParameter(name = "SPHINX_QUERY_INDEXES", value = "srvlog_index_main,srvlog_index_delta") + String getQueryIndexes(); +} diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/LogBroadcastingServiceImpl.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/LogBroadcastingServiceImpl.java index 726d0c6c..b2876e9c 100644 --- a/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/LogBroadcastingServiceImpl.java +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/LogBroadcastingServiceImpl.java @@ -4,14 +4,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.payneteasy.srvlog.data.LogData; import com.payneteasy.srvlog.service.ILogBroadcastingService; +import com.payneteasy.startup.parameters.StartupParametersFactory; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jetty.websocket.api.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.eclipse.jetty.websocket.api.Session; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -37,14 +36,19 @@ public class LogBroadcastingServiceImpl implements ILogBroadcastingService { private final ConcurrentMap> logStorage = new ConcurrentHashMap<>(); + private static final ILogBroadcastingConfig logBroadcastingConfig = StartupParametersFactory + .getStartupParameters(ILogBroadcastingConfig.class); private static final int DEFAULT_PROGRAM_LOG_STORAGE_CAPACITY = 1000; private final int programLogStorageCapacity; - @Autowired - public LogBroadcastingServiceImpl(@Value("${programLogStorageCapacity}") int programLogStorageCapacity) { - this.programLogStorageCapacity = programLogStorageCapacity > 0 ? - programLogStorageCapacity : DEFAULT_PROGRAM_LOG_STORAGE_CAPACITY; + public LogBroadcastingServiceImpl() { + this.programLogStorageCapacity = logBroadcastingConfig.getProgramLogStorageCapacity() > 0 ? + logBroadcastingConfig.getProgramLogStorageCapacity() : DEFAULT_PROGRAM_LOG_STORAGE_CAPACITY; + } + + public LogBroadcastingServiceImpl(int programLogStorageCapacity) { + this.programLogStorageCapacity = programLogStorageCapacity; } @Override diff --git a/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/SphinxIndexerService.java b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/SphinxIndexerService.java index 75bc6102..731850bc 100644 --- a/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/SphinxIndexerService.java +++ b/srvlog-service/src/main/java/com/payneteasy/srvlog/service/impl/SphinxIndexerService.java @@ -3,6 +3,7 @@ import com.payneteasy.srvlog.data.LogLevel; import com.payneteasy.srvlog.service.IIndexerService; import com.payneteasy.srvlog.service.IndexerServiceException; +import com.payneteasy.startup.parameters.StartupParametersFactory; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,7 +11,6 @@ import org.sphx.api.SphinxException; import org.sphx.api.SphinxMatch; import org.sphx.api.SphinxResult; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -28,17 +28,8 @@ public class SphinxIndexerService implements IIndexerService{ private static final Logger LOG = LoggerFactory.getLogger(SphinxIndexerService.class); - @Value( "${sphinxHost}" ) - private String host; - - @Value( "${sphinxPort}" ) - private int port; - - @Value( "${sphinxConnectTimeout}" ) - private int connectTimeout; - - @Value( "${sphinxQueryIndexes}" ) - private String sphinxQueryIndexes; + private static final ISphinxConfig sphinxConfig = StartupParametersFactory + .getStartupParameters(ISphinxConfig.class); @Override public List search(Date from, Date to, List facilities, List severities, List hosts, String pattern, Integer offset, Integer limit) throws IndexerServiceException { @@ -83,8 +74,8 @@ private void setLimits(Integer offset, Integer limit, SphinxClient sphinxClient) } private SphinxClient createSphinxClient() { - SphinxClient sphinxClient = new SphinxClient(host, port); - sphinxClient.SetConnectTimeout(connectTimeout); + SphinxClient sphinxClient = new SphinxClient(sphinxConfig.getHost(), sphinxConfig.getPort()); + sphinxClient.SetConnectTimeout(sphinxConfig.getConnectTimeout()); return sphinxClient; } @@ -108,7 +99,7 @@ private boolean errorOccuredWhileQuerying(SphinxClient sphinxClient, SphinxResul private SphinxResult querySphinx(String pattern, SphinxClient sphinxClient) throws IndexerServiceException { SphinxResult result; try { - result = sphinxClient.Query(pattern, sphinxQueryIndexes); + result = sphinxClient.Query(pattern, sphinxConfig.getQueryIndexes()); } catch (SphinxException e) { LOG.error("While sending query to Sphinx Daemon", e); throw new IndexerServiceException("While sending query to Sphinx Daemon", e); @@ -263,19 +254,4 @@ private int[] toIntArray(List list) { return array; } - public void setHost(String host) { - this.host = host; - } - - public void setPort(int port) { - this.port = port; - } - - public int getPort() { - return port; - } - - public void setConnectTimeout(int connectTimeout) { - this.connectTimeout = connectTimeout; - } } diff --git a/srvlog-service/src/main/resources/inMemoryLog.properties b/srvlog-service/src/main/resources/inMemoryLog.properties deleted file mode 100644 index 745ddc9f..00000000 --- a/srvlog-service/src/main/resources/inMemoryLog.properties +++ /dev/null @@ -1 +0,0 @@ -programLogStorageCapacity=1000 \ No newline at end of file diff --git a/srvlog-service/src/main/resources/logbackAdapter.properties b/srvlog-service/src/main/resources/logbackAdapter.properties deleted file mode 100644 index cc783175..00000000 --- a/srvlog-service/src/main/resources/logbackAdapter.properties +++ /dev/null @@ -1,3 +0,0 @@ -logbackProgram=paynet -logbackTCPPort=4713 -logbackUDPPort=4713 \ No newline at end of file diff --git a/srvlog-service/src/main/resources/sphinxclient.properties b/srvlog-service/src/main/resources/sphinxclient.properties deleted file mode 100644 index 9084e920..00000000 --- a/srvlog-service/src/main/resources/sphinxclient.properties +++ /dev/null @@ -1,4 +0,0 @@ -sphinxPort=9312 -sphinxHost=localhost -sphinxConnectTimeout=30000 -sphinxQueryIndexes=srvlog_index_main,srvlog_index_delta \ No newline at end of file