Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-35494] - FindBugs - fix minor File Management issues in huds…
…on.remoting.Launcher (#88)

- [x] Tcp port temporary file is not being closed properly when running in the runAsTcpServer() mode
- [x] tcpPortFile deletion issues are not being logged
- [x] Resource input stream is not being closed correctly
  • Loading branch information
oleg-nenashev committed Jun 9, 2016
1 parent e5e972e commit 4610a90
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/main/java/hudson/remoting/Launcher.java
Expand Up @@ -51,6 +51,7 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileWriter;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -379,17 +380,23 @@ private void runAsTcpServer() throws IOException, InterruptedException {

// write a port file to report the port number
FileWriter w = new FileWriter(tcpPortFile);
w.write(String.valueOf(ss.getLocalPort()));
w.close();

try {
w.write(String.valueOf(ss.getLocalPort()));
} finally {
w.close();
}

// accept just one connection and that's it.
// when we are done, remove the port file to avoid stale port file
Socket s;
try {
s = ss.accept();
ss.close();
} finally {
tcpPortFile.delete();
boolean deleted = tcpPortFile.delete();
if (!deleted) {
LOGGER.log(Level.WARNING, "Cannot delete the temporary TCP port file {0}", tcpPortFile);
}
}

runOnSocket(s);
Expand Down Expand Up @@ -566,18 +573,38 @@ public static boolean isWindows() {

private static String computeVersion() {
Properties props = new Properties();
InputStream is = Launcher.class.getResourceAsStream(JENKINS_VERSION_PROP_FILE);
if (is == null) {
LOGGER.log(Level.FINE, "Cannot locate the {0} resource file. Hudson/Jenkins version is unknown",
JENKINS_VERSION_PROP_FILE);
return UNKNOWN_JENKINS_VERSION_STR;
}

try {
InputStream is = Launcher.class.getResourceAsStream("hudson-version.properties");
if(is!=null)
props.load(is);
props.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeWithLogOnly(is, JENKINS_VERSION_PROP_FILE);
}
return props.getProperty("version", UNKNOWN_JENKINS_VERSION_STR);
}

private static void closeWithLogOnly(Closeable stream, String name) {
try {
stream.close();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Cannot close the resource file " + name, ex);
}
return props.getProperty("version", "?");
}

/**
* Version number of Hudson this slave.jar is from.
*/
public static final String VERSION = computeVersion();

private static final String JENKINS_VERSION_PROP_FILE = "hudson-version.properties";
private static final String UNKNOWN_JENKINS_VERSION_STR = "?";

private static final Logger LOGGER = Logger.getLogger(Launcher.class.getName());
}

0 comments on commit 4610a90

Please sign in to comment.