Skip to content

Commit e0163c3

Browse files
committed
- using explicit CLI parsing
- extended ServiceConfig to support unlimited defaults - parametrized dependency versions in pom
1 parent b8e038c commit e0163c3

File tree

8 files changed

+150
-21
lines changed

8 files changed

+150
-21
lines changed

sampling-message-client/src/main/java/de/dhbw/ravensburg/verteiltesysteme/client/CommandLineClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ private void deleteSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.
276276
* @param errorMessage Message to be displayed on the console during the program shutdown
277277
*/
278278
private void exitWithError(String errorMessage) {
279-
log.debug("ParsingError: ", errorMessage);
280279
log.error(errorMessage);
281280
helpFormatter.printHelp("sampling-message-client", options);
282281
System.exit(1);

sampling-message-server/pom.xml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
<io.grpc.version>1.12.0</io.grpc.version>
2121
<org.apache.logging.log4j.version>2.11.0</org.apache.logging.log4j.version>
2222
<org.projectlombok.version>1.16.20</org.projectlombok.version>
23-
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
23+
<commons-cli.version>1.4</commons-cli.version>
24+
<org.testng.version>6.14.3</org.testng.version>
2425
</properties>
2526

2627
<build>
@@ -120,17 +121,14 @@
120121
<dependency>
121122
<groupId>commons-cli</groupId>
122123
<artifactId>commons-cli</artifactId>
123-
<version>1.4</version>
124+
<version>${commons-cli.version}</version>
124125
</dependency>
125-
126-
127126
<dependency>
128127
<groupId>org.testng</groupId>
129128
<artifactId>testng</artifactId>
130-
<version>6.14.3</version>
129+
<version>${org.testng.version}</version>
131130
<scope>test</scope>
132131
</dependency>
133-
134132
<dependency>
135133
<groupId>de.dhbw.ravensburg.verteiltesysteme</groupId>
136134
<artifactId>sampling-message-proto</artifactId>

sampling-message-server/src/it/java/de/dhbw/ravensburg/verteiltesysteme/server/SamplingMessageServerIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class SamplingMessageServerIT {
1818

1919
final Integer testingPort = 8888;
2020
ServiceEndpoint serviceEndpoint;
21-
final Integer maxSamplingMessageNameLength = 20;
22-
final Integer maxSamplingMessageContentLength = 20;
23-
final Integer maxSamplingMessageCount = 20;
24-
ServiceConfig serviceConfig = new ServiceConfig(maxSamplingMessageNameLength, maxSamplingMessageContentLength, maxSamplingMessageCount, testingPort);
21+
final int maxSamplingMessageNameLength = 20;
22+
final int maxSamplingMessageContentLength = 20;
23+
final int maxSamplingMessageCount = 20;
24+
ServiceConfig serviceConfig = new ServiceConfig((long) maxSamplingMessageNameLength, (long) maxSamplingMessageContentLength, (long) maxSamplingMessageCount, testingPort);
2525
SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub;
2626

2727
private static SamplingMessageGrpcService.CreateSamplingMessageRequest createSamplingMessageRequest(String name, long duration) {

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/Main.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package de.dhbw.ravensburg.verteiltesysteme.server;
22

33
import de.dhbw.ravensburg.verteiltesysteme.server.service.ServiceConfig;
4+
import de.dhbw.ravensburg.verteiltesysteme.server.util.ServerCommandLineParser;
45
import lombok.extern.slf4j.Slf4j;
6+
import org.apache.commons.cli.HelpFormatter;
7+
import org.apache.commons.cli.ParseException;
58

69
/**
710
* Class providing the application entry point.
@@ -16,13 +19,24 @@ public class Main {
1619
* @param args CLI arguments
1720
*/
1821
public static void main(String[] args) {
19-
20-
final ServiceConfig serviceConfig = new ServiceConfig(255, 32, 32, 8080);
22+
final ServiceConfig serviceConfig;
23+
try {
24+
serviceConfig = ServerCommandLineParser.fromCliArgs(args);
25+
} catch (ParseException e) {
26+
e.printStackTrace();
27+
log.debug("ParsingError: ", e.getMessage());
28+
log.error(e.getMessage());
29+
new HelpFormatter().printHelp("sampling-message-client", ServerCommandLineParser.defaultOptions());
30+
System.exit(1);
31+
//dead code to satisfy for debugger
32+
return;
33+
}
34+
//final ServiceConfig serviceConfig = new ServiceConfig(255, 32, 32, 8080);
2135

2236
final ServiceEndpoint serviceEndpoint = new ServiceEndpoint(serviceConfig);
2337

2438
/*
25-
Graceful shutdown on shutdown hook
39+
Register graceful shutdown hook
2640
*/
2741
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
2842
log.info("Shutting Hook received.");

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/ServiceEndpoint.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ServiceEndpoint {
3333
public ServiceEndpoint(final ServiceConfig serviceConfig) {
3434
log.info("Preparing Service Endpoint");
3535
this.serviceConfig = serviceConfig;
36+
log.info(String.format("Using following config: %s", serviceConfig.toString()));
3637

3738
final DatabaseAccessObject databaseAccessObject = new DatabaseAccessObjectImpl(new FakePersistence<>());
3839
final InputValidator inputValidator = new InputValidator(this.serviceConfig);

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/service/InputValidator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ public InputValidator(final ServiceConfig serviceConfig) {
1818
}
1919

2020
public boolean isInvalidMessageName(final String messageName) {
21+
if (this.serviceConfig.isUnlimitedMessageNameSize())
22+
return true;
23+
2124
return (messageName == null || messageName.length() > this.serviceConfig.getMaximumSamplingMessageNameSize()); // messageName.isEmpty());
2225
}
2326

2427
public boolean isInvalidMessageContent(final String messageContent) {
28+
if (this.serviceConfig.isUnlimitedMessageContentSize())
29+
return true;
30+
2531
return (messageContent == null || messageContent.length() > this.serviceConfig.getMaximumSamplingMessageContentSize()); // messageContent.isEmpty();
2632
}
2733

2834
public boolean isMessageCountExceeded(final Long totalMessageCount) {
35+
if (this.serviceConfig.isUnlimitedMessageCount())
36+
return true;
37+
2938
return totalMessageCount >= serviceConfig.getMaximumSamplingMessageContentSize();
3039
}
3140

@@ -34,7 +43,7 @@ public boolean isValid(@NonNull final Instant creationTime, @NonNull final Durat
3443
return Instant.now().minus(lifetime).isBefore(creationTime);
3544
}
3645

37-
public int getCurrentSamplingMaximumMessageCount() {
46+
public long getCurrentSamplingMaximumMessageCount() {
3847
return this.serviceConfig.getMaximumSamplingMessageCount();
3948
}
4049
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/service/ServiceConfig.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,60 @@
1414
@AllArgsConstructor
1515
public class ServiceConfig {
1616

17-
public static final Integer DEFAULT_MAXIMUM_SAMPLING_MESSAGE_NAME_SIZE = 32;
18-
public static final Integer DEFAULT_MAXIMUM_SAMPLING_MESSAGE_CONTENT_SIZE = 256;
19-
public static final Integer DEFAULT_MAXIMUM_SAMPLING_MESSAGE_COUNT = 32;
17+
public static final Long DEFAULT_MAXIMUM_SAMPLING_MESSAGE_NAME_SIZE = -1L;
18+
public static final Long DEFAULT_MAXIMUM_SAMPLING_MESSAGE_CONTENT_SIZE = -1L;
19+
public static final Long DEFAULT_MAXIMUM_SAMPLING_MESSAGE_COUNT = -1L;
2020
public static final Integer DEFAULT_SERVICE_ENDPOINT_PORT = 8088;
21-
private final Integer maximumSamplingMessageNameSize;
22-
private final Integer maximumSamplingMessageContentSize;
23-
private final Integer maximumSamplingMessageCount;
21+
private final Long maximumSamplingMessageNameSize;
22+
private final Long maximumSamplingMessageContentSize;
23+
private final Long maximumSamplingMessageCount;
2424
private final Integer serviceEndpointListeningPort;
2525

26+
/**
27+
* Default constructor with unlimited size limits and default port
28+
*/
2629
public ServiceConfig() {
2730
this.maximumSamplingMessageNameSize = DEFAULT_MAXIMUM_SAMPLING_MESSAGE_NAME_SIZE;
2831
this.maximumSamplingMessageContentSize = DEFAULT_MAXIMUM_SAMPLING_MESSAGE_CONTENT_SIZE;
2932
this.maximumSamplingMessageCount = DEFAULT_MAXIMUM_SAMPLING_MESSAGE_COUNT;
3033

3134
this.serviceEndpointListeningPort = DEFAULT_SERVICE_ENDPOINT_PORT;
3235
}
36+
37+
/**
38+
* Check if unlimited sampling message name length was configured
39+
*
40+
* @return true if unlimited
41+
*/
42+
public boolean isUnlimitedMessageNameSize() {
43+
return this.maximumSamplingMessageNameSize < 0;
44+
}
45+
46+
/**
47+
* Check if unlimited sampling message content length was configured
48+
*
49+
* @return true if unlimited
50+
*/
51+
public boolean isUnlimitedMessageContentSize() {
52+
return this.maximumSamplingMessageContentSize < 0;
53+
}
54+
55+
/**
56+
* Check if unlimited sampling message count was configured
57+
*
58+
* @return true if unlimited
59+
*/
60+
public boolean isUnlimitedMessageCount() {
61+
return this.maximumSamplingMessageCount < 0;
62+
}
63+
64+
65+
/**
66+
* Check if default was was configured
67+
*
68+
* @return true if default port
69+
*/
70+
public boolean isDefaultPort() {
71+
return this.serviceEndpointListeningPort.equals(DEFAULT_SERVICE_ENDPOINT_PORT);
72+
}
3373
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package de.dhbw.ravensburg.verteiltesysteme.server.util;
2+
3+
import de.dhbw.ravensburg.verteiltesysteme.server.service.ServiceConfig;
4+
import org.apache.commons.cli.*;
5+
6+
/**
7+
* Utility class for CLI related stuff
8+
*/
9+
public class ServerCommandLineParser {
10+
11+
/**
12+
* Provides default Command Line Options
13+
*
14+
* @return Options object containing the defaults
15+
*/
16+
public static Options defaultOptions() {
17+
final Options options = new Options();
18+
19+
options.addOption(Option.builder("p")
20+
.longOpt("port")
21+
.hasArg(true)
22+
.required(false)
23+
.desc("local TCP Server Port - default is 8080")
24+
.build());
25+
26+
options.addOption(Option.builder("cl")
27+
.longOpt("content-length")
28+
.hasArg(true)
29+
.required(false)
30+
.desc("maximum length of a message's content - default is unlimited")
31+
.build());
32+
33+
options.addOption(Option.builder("nl")
34+
.longOpt("name-length")
35+
.hasArg(true)
36+
.required(false)
37+
.desc("maximum length of a message's name - default is unlimited")
38+
.build());
39+
40+
options.addOption(Option.builder("mc")
41+
.longOpt("message-count")
42+
.hasArg(true)
43+
.required(false)
44+
.desc("maximum number of messages the server can hold - default is unlimited")
45+
.build());
46+
47+
return options;
48+
}
49+
50+
/**
51+
* Produce a {@link ServiceConfig} from a raw command line args String[]
52+
*
53+
* @param args Command lines as provided in main method
54+
* @return appropriate service configuration object
55+
* @throws ParseException if parsing
56+
*/
57+
public static ServiceConfig fromCliArgs(final String[] args) throws ParseException {
58+
final CommandLineParser serverCommandLineParser = new DefaultParser();
59+
final CommandLine commandLine = serverCommandLineParser.parse(defaultOptions(), args);
60+
61+
final Long maxNameLength = commandLine.hasOption("name-length") ? Long.valueOf(commandLine.getOptionValue("name-length")) : ServiceConfig.DEFAULT_MAXIMUM_SAMPLING_MESSAGE_NAME_SIZE;
62+
final Long maxContentLength = commandLine.hasOption("content-length") ? Long.valueOf(commandLine.getOptionValue("content-length")) : ServiceConfig.DEFAULT_MAXIMUM_SAMPLING_MESSAGE_CONTENT_SIZE;
63+
final Long maxMessageCount = commandLine.hasOption("content-length") ? Long.valueOf(commandLine.getOptionValue("content-length")) : ServiceConfig.DEFAULT_MAXIMUM_SAMPLING_MESSAGE_COUNT;
64+
final Integer serverPort = commandLine.hasOption("content-length") ? Integer.valueOf(commandLine.getOptionValue("content-length")) : ServiceConfig.DEFAULT_SERVICE_ENDPOINT_PORT;
65+
66+
return new ServiceConfig(maxNameLength, maxContentLength, maxMessageCount, serverPort);
67+
}
68+
}

0 commit comments

Comments
 (0)