Fix criticals sonar issues
Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
@@ -14,7 +14,7 @@ import java.net.ServerSocket;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
* Look for a free port.
|
||||||
*/
|
*/
|
||||||
public class FreePortFileWriter {
|
public class FreePortFileWriter {
|
||||||
|
|
||||||
@@ -24,7 +24,9 @@ public class FreePortFileWriter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param from
|
* @param from
|
||||||
|
* port range from (start point)
|
||||||
* @param to
|
* @param to
|
||||||
|
* port range to (end point)
|
||||||
*/
|
*/
|
||||||
public FreePortFileWriter(final int from, final int to, final String filePortPath) {
|
public FreePortFileWriter(final int from, final int to, final String filePortPath) {
|
||||||
this.from = from;
|
this.from = from;
|
||||||
@@ -51,25 +53,21 @@ public class FreePortFileWriter {
|
|||||||
portFile.getParentFile().mkdirs();
|
portFile.getParentFile().mkdirs();
|
||||||
if (portFile.exists()) {
|
if (portFile.exists()) {
|
||||||
return false;
|
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) {
|
boolean isFree = false;
|
||||||
// not free.
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user