Fix criticals sonar issues

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-08-05 10:51:11 +02:00
parent 54ce3ad8e0
commit 88e5bdf8f4
6 changed files with 57 additions and 102 deletions

View File

@@ -14,7 +14,7 @@ import java.net.ServerSocket;
/**
*
*
* Look for a free port.
*/
public class FreePortFileWriter {
@@ -24,7 +24,9 @@ public class FreePortFileWriter {
/**
* @param from
* port range from (start point)
* @param to
* port range to (end point)
*/
public FreePortFileWriter(final int from, final int to, final String filePortPath) {
this.from = from;
@@ -51,23 +53,20 @@ public class FreePortFileWriter {
portFile.getParentFile().mkdirs();
if (portFile.exists()) {
return false;
} else {
boolean isFree = false;
final ServerSocket sock = new ServerSocket();
sock.setReuseAddress(true);
sock.bind(new InetSocketAddress(port));
if (portFile.createNewFile()) {
portFile.deleteOnExit();
isFree = true;
}
sock.close();
// is free:
return isFree;
// We rely on an exception thrown to determine availability or
// not availability.
}
} catch (final Exception e) {
// not free.
boolean isFree = false;
final ServerSocket sock = new ServerSocket();
sock.setReuseAddress(true);
sock.bind(new InetSocketAddress(port));
if (portFile.createNewFile()) {
portFile.deleteOnExit();
isFree = true;
}
sock.close();
return isFree;
// We rely on an exception thrown to determine availability or
// not availability and don't want to log the exception.
} catch (@SuppressWarnings({ "squid:S2221", "squid:S1166" }) final Exception e) {
return false;
}
}