Skip to content

Commit 0e13615

Browse files
committed
add codestyle checks
1 parent afa1dc6 commit 0e13615

9 files changed

Lines changed: 74 additions & 58 deletions

File tree

.codestyle/pmd.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ limitations under the License.
7474
<exclude name="MissingBreakInSwitch" />
7575
</rule>
7676
<rule ref="category/java/multithreading.xml">
77+
<exclude name="DoNotUseThreads" />
7778
</rule>
7879
<rule ref="category/java/performance.xml">
7980
<exclude name="AvoidUsingShortType" />

client/src/main/java/io/appulse/epmd/java/client/CommandKill.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.appulse.epmd.java.core.model.request.Kill;
2424
import io.appulse.epmd.java.core.model.response.KillResult;
2525
import io.appulse.epmd.java.core.model.response.Response;
26+
2627
import lombok.Builder;
2728
import lombok.extern.slf4j.Slf4j;
2829
import lombok.val;
@@ -61,6 +62,7 @@ public Boolean get () {
6162
val response = Response.parse(responseBytes, KillResult.class);
6263
return response == OK;
6364
} catch (Exception ex) {
65+
log.error("Error during killing the EPMD server", ex);
6466
return false;
6567
}
6668
}

server/src/main/java/io/appulse/epmd/java/server/Epmd.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,32 @@
2828
@NoArgsConstructor
2929
@AllArgsConstructor
3030
@Command(
31-
name = "epmd",
32-
description = "Erlang Port Mapper Daemon",
33-
mixinStandardHelpOptions = true,
34-
version = "epmd 2.0.0",
35-
subcommands = {
36-
SubcommandNames.class,
37-
SubcommandServer.class,
38-
SubcommandStop.class,
39-
SubcommandKill.class
40-
}
31+
name = "epmd",
32+
description = "Erlang Port Mapper Daemon",
33+
mixinStandardHelpOptions = true,
34+
version = "epmd 2.0.0",
35+
subcommands = {
36+
SubcommandNames.class,
37+
SubcommandServer.class,
38+
SubcommandStop.class,
39+
SubcommandKill.class
40+
}
4141
)
4242
class Epmd implements Runnable {
4343

4444
@Option(
45-
names = { "-p", "--port" },
46-
paramLabel = "PORT",
47-
description =
48-
"Let epmd listen to another port than default ${DEFAULT-VALUE}. " +
49-
"This can also be set using environment variable ERL_EPMD_PORT"
45+
names = { "-p", "--port" },
46+
paramLabel = "PORT",
47+
description =
48+
"Let epmd listen to another port than default ${DEFAULT-VALUE}. " +
49+
"This can also be set using environment variable ERL_EPMD_PORT"
5050
)
5151
@Builder.Default
5252
int port = 4369;
5353

5454
@Option(
55-
names = { "-d", "--debug" },
56-
description = "Enables debugging logs"
55+
names = { "-d", "--debug" },
56+
description = "Enables debugging logs"
5757
)
5858
boolean debug;
5959

server/src/main/java/io/appulse/epmd/java/server/Main.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616

1717
package io.appulse.epmd.java.server;
1818

19-
import static java.util.Optional.ofNullable;
2019
import static java.util.Arrays.asList;
20+
import static java.util.Optional.ofNullable;
2121

2222
import java.util.ArrayList;
23-
import java.util.List;
2423

2524
import lombok.val;
2625
import picocli.CommandLine;

server/src/main/java/io/appulse/epmd/java/server/SubcommandKill.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import io.appulse.epmd.java.client.EpmdClient;
2626

27-
import lombok.val;
2827
import lombok.extern.slf4j.Slf4j;
28+
import lombok.val;
2929
import picocli.CommandLine.Command;
3030
import picocli.CommandLine.ParentCommand;
3131

server/src/main/java/io/appulse/epmd/java/server/SubcommandNames.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.appulse.epmd.java.server;
218

3-
import static java.util.stream.Collectors.joining;
419
import static java.util.Locale.ENGLISH;
520
import static java.util.concurrent.TimeUnit.SECONDS;
21+
import static java.util.stream.Collectors.joining;
622

723
import java.util.concurrent.CompletionException;
824
import java.util.concurrent.ExecutionException;
925
import java.util.concurrent.TimeoutException;
1026

1127
import io.appulse.epmd.java.client.EpmdClient;
1228

13-
import lombok.val;
1429
import lombok.extern.slf4j.Slf4j;
30+
import lombok.val;
1531
import picocli.CommandLine.Command;
1632
import picocli.CommandLine.ParentCommand;
1733

server/src/main/java/io/appulse/epmd/java/server/SubcommandServer.java

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import static ch.qos.logback.classic.Level.DEBUG;
2020
import static java.util.Arrays.asList;
21-
import static java.util.stream.Collectors.toList;
22-
import static java.util.concurrent.TimeUnit.MILLISECONDS;
23-
import static java.util.concurrent.TimeUnit.SECONDS;
24-
import static java.util.Optional.of;
2521
import static java.util.Optional.empty;
22+
import static java.util.Optional.of;
2623
import static java.util.Optional.ofNullable;
24+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
25+
import static java.util.concurrent.TimeUnit.SECONDS;
26+
import static java.util.stream.Collectors.toSet;
2727
import static org.slf4j.Logger.ROOT_LOGGER_NAME;
2828

2929
import java.io.Closeable;
@@ -54,29 +54,30 @@
5454
import io.appulse.epmd.java.core.model.request.Request;
5555
import io.appulse.epmd.java.core.model.request.Stop;
5656
import io.appulse.epmd.java.core.model.response.EpmdDump;
57+
import io.appulse.epmd.java.core.model.response.EpmdDump.NodeDump;
5758
import io.appulse.epmd.java.core.model.response.EpmdInfo;
59+
import io.appulse.epmd.java.core.model.response.EpmdInfo.NodeDescription;
5860
import io.appulse.epmd.java.core.model.response.KillResult;
5961
import io.appulse.epmd.java.core.model.response.NodeInfo;
6062
import io.appulse.epmd.java.core.model.response.RegistrationResult;
6163
import io.appulse.epmd.java.core.model.response.Response;
6264
import io.appulse.epmd.java.core.model.response.StopResult;
63-
import io.appulse.epmd.java.core.model.response.EpmdDump.NodeDump;
64-
import io.appulse.epmd.java.core.model.response.EpmdInfo.NodeDescription;
6565
import io.appulse.utils.BytesUtils;
6666
import io.appulse.utils.SocketUtils;
6767
import io.appulse.utils.threads.AppulseExecutors;
6868
import io.appulse.utils.threads.AppulseThreadFactory;
6969

7070
import ch.qos.logback.classic.Logger;
71+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
7172
import lombok.Builder;
72-
import lombok.extern.slf4j.Slf4j;
7373
import lombok.NoArgsConstructor;
7474
import lombok.NonNull;
7575
import lombok.RequiredArgsConstructor;
7676
import lombok.Singular;
7777
import lombok.SneakyThrows;
78-
import lombok.val;
7978
import lombok.Value;
79+
import lombok.extern.slf4j.Slf4j;
80+
import lombok.val;
8081
import org.slf4j.LoggerFactory;
8182
import picocli.CommandLine.Command;
8283
import picocli.CommandLine.Option;
@@ -96,7 +97,7 @@ class SubcommandServer implements Runnable {
9697
ANY_ADDRESS = InetAddress.getByName("0.0.0.0");
9798
LOOPBACK_ADDRESS = InetAddress.getLoopbackAddress();
9899
} catch (UnknownHostException ex) {
99-
throw new RuntimeException(ex);
100+
throw new IllegalStateException(ex);
100101
}
101102
}
102103

@@ -128,27 +129,27 @@ class SubcommandServer implements Runnable {
128129
}
129130

130131
private void setupEnvironmentVariables () {
131-
ips.add(LOOPBACK_ADDRESS);
132-
if (ips.size() == 1) {
132+
if (new SubcommandServer().ips.equals(ips)) {
133133
ips = ofNullable(System.getProperty("ERL_EPMD_ADDRESS"))
134-
.map(it -> it.split(",\\s*"))
134+
.map(it -> it.split(","))
135135
.map(it -> Stream.of(it)
136+
.map(String::trim)
137+
.filter(ip -> !ip.isEmpty())
136138
.map(ip -> {
137139
try {
138140
return InetAddress.getByName(ip);
139141
} catch (UnknownHostException ex) {
140-
return null;
142+
throw new IllegalArgumentException("Invalid host " + ip, ex);
141143
}
142144
})
143-
.collect(toList()))
144-
.map(HashSet::new)
145-
.map(it -> (Set<InetAddress>) it)
145+
.collect(toSet()))
146146
.map(it -> {
147147
it.add(LOOPBACK_ADDRESS);
148148
return it;
149149
})
150150
.orElse(ips);
151151
}
152+
ips.add(LOOPBACK_ADDRESS);
152153

153154
if (!unsafe) {
154155
val string = ofNullable(System.getProperty("ERL_EPMD_RELAXED_COMMAND_CHECK"))
@@ -160,6 +161,7 @@ private void setupEnvironmentVariables () {
160161

161162
@Override
162163
@SneakyThrows
164+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
163165
public void run () {
164166
setupEnvironmentVariables();
165167

@@ -205,7 +207,7 @@ public void run () {
205207
log.debug("{} - a new incoming connection", remoteSocketAddress);
206208

207209
val handler = new ServerHandler(clientSocket);
208-
executor.submit(handler);
210+
executor.execute(handler);
209211
}
210212
} finally {
211213
executor.shutdown();
@@ -250,7 +252,7 @@ public void run () {
250252
return it;
251253
})
252254
.ifPresent(RequestProcessor::process);
253-
} catch (Throwable ex) {
255+
} catch (Exception ex) {
254256
if (options.debug) {
255257
log.error("handling {} connection error - '{}'", ex.getMessage(), ex);
256258
} else {
@@ -259,20 +261,20 @@ public void run () {
259261
}
260262
}
261263

262-
private Optional<RequestProcessor<?>> findProcessor (Request request, Socket socket) {
264+
private Optional<RequestProcessor<?>> findProcessor (Request request, Socket clientSocket) {
263265
switch (request.getTag()) {
264266
case ALIVE2_REQUEST:
265-
return of(new RegistrationRequestProcessor(request, socket));
267+
return of(new RegistrationRequestProcessor(request, clientSocket));
266268
case DUMP_REQUEST:
267-
return of(new DumpRequestProcessor(request, socket));
269+
return of(new DumpRequestProcessor(request, clientSocket));
268270
case KILL_REQUEST:
269-
return of(new KillRequestProcessor(request, socket));
271+
return of(new KillRequestProcessor(request, clientSocket));
270272
case PORT_PLEASE2_REQUEST:
271-
return of(new GetNodeInfoRequestProcessor(request, socket));
273+
return of(new GetNodeInfoRequestProcessor(request, clientSocket));
272274
case NAMES_REQUEST:
273-
return of(new GetEpmdInfoRequestProcessor(request, socket));
275+
return of(new GetEpmdInfoRequestProcessor(request, clientSocket));
274276
case STOP_REQUEST:
275-
return of(new StopRequestProcessor(request, socket));
277+
return of(new StopRequestProcessor(request, clientSocket));
276278
default:
277279
log.warn("unsupported request's tag - {}", request.getTag());
278280
return empty();
@@ -395,13 +397,7 @@ protected Response respond () {
395397
if (!unsafe) {
396398
return KillResult.NOK;
397399
}
398-
nodes.values().forEach(it -> {
399-
try {
400-
it.getSocket().close();
401-
} catch (IOException ex) {
402-
// noop
403-
}
404-
});
400+
nodes.values().forEach(Node::close);
405401
return KillResult.OK;
406402
}
407403

@@ -513,6 +509,7 @@ private static class Node implements Closeable {
513509
Socket socket;
514510

515511
@Override
512+
@SuppressWarnings("PMD.EmptyCatchBlock")
516513
public void close () {
517514
try {
518515
socket.close();
@@ -522,12 +519,13 @@ public void close () {
522519
}
523520

524521
@SneakyThrows
522+
@SuppressFBWarnings("REC_CATCH_EXCEPTION")
525523
boolean isAlive () {
526524
val nodeSocketAddress = new InetSocketAddress(socket.getInetAddress(), port);
527525
try (val nodeSocket = new Socket()) {
528526
nodeSocket.connect(nodeSocketAddress, 2_000);
529527
nodeSocket.close();
530-
} catch (Throwable ex) {
528+
} catch (Exception ex) {
531529
return false;
532530
}
533531
return true;

server/src/main/java/io/appulse/epmd/java/server/SubcommandStop.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import io.appulse.epmd.java.client.EpmdClient;
2626

27-
import lombok.val;
2827
import lombok.extern.slf4j.Slf4j;
28+
import lombok.val;
2929
import picocli.CommandLine.Command;
3030
import picocli.CommandLine.Parameters;
3131
import picocli.CommandLine.ParentCommand;

server/src/test/java/io/appulse/epmd/java/server/SubcommandServerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
import io.appulse.epmd.java.client.EpmdClient;
3434
import io.appulse.epmd.java.client.exception.EpmdRegistrationException;
3535
import io.appulse.epmd.java.core.model.request.Registration;
36-
import io.appulse.epmd.java.core.model.response.NodeInfo;
3736
import io.appulse.epmd.java.core.model.response.EpmdDump.NodeDump;
37+
import io.appulse.epmd.java.core.model.response.NodeInfo;
3838
import io.appulse.utils.SocketUtils;
3939
import io.appulse.utils.threads.AppulseExecutors;
4040

41-
import lombok.val;
4241
import lombok.extern.slf4j.Slf4j;
42+
import lombok.val;
4343

4444
import org.assertj.core.api.SoftAssertions;
4545
import org.junit.jupiter.api.AfterEach;

0 commit comments

Comments
 (0)