Handle the cases where the Java agent could find a port file
that is either empty, or contains text that cannot be parsed
to a number.
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
* @return port value if found else 0.
*/
private static int getPortFromFile(String path) throws IOException {
* @return port value if found else 0.
*/
private static int getPortFromFile(String path) throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(path), PORT_FILE_ENCODING));
String line = br.readLine();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(path), PORT_FILE_ENCODING));
String line = br.readLine();
- port = Integer.parseInt(line, 10);
+ if (line == null) {
+ /* File exists but is empty. */
+ return 0;
+ }
+
+ int port = Integer.parseInt(line, 10);
if (port < 0 || port > 65535) {
/* Invalid value. Ignore. */
port = 0;
}
if (port < 0 || port > 65535) {
/* Invalid value. Ignore. */
port = 0;
}
+ return port;
+
+ } catch (NumberFormatException e) {
+ /* File contained something that was not a number. */
+ return 0;
} catch (FileNotFoundException e) {
/* No port available. */
} catch (FileNotFoundException e) {
/* No port available. */
} finally {
if (br != null) {
br.close();
}
}
} finally {
if (br != null) {
br.close();
}
}
}
private void registerToSessiond() throws IOException {
}
private void registerToSessiond() throws IOException {