Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-35494] - FindBugs - fix minor File Management issues in hudson.remoting.Launcher #88

Merged
merged 1 commit into from
Jun 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/main/java/hudson/remoting/Launcher.java
Original file line number Diff line number Diff line change
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try-with-resources?

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW - java 7 NIO is much better as it will tell you why it could not delete the file...

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());
}