aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorschristou882012-07-18 02:45:22 (EDT)
committerschristou882012-07-18 02:45:22 (EDT)
commitc4159e1ea623b6da52af7b1dc272dea8408eadb7 (patch)
tree81102047938bce97c8a28fb9ad0bdafd393d5fc6
parent496cc402e9ac02f7d4ebb883ffe70f2859d3e942 (diff)
downloadorg.eclipse.hudson.core-c4159e1ea623b6da52af7b1dc272dea8408eadb7.zip
org.eclipse.hudson.core-c4159e1ea623b6da52af7b1dc272dea8408eadb7.tar.gz
org.eclipse.hudson.core-c4159e1ea623b6da52af7b1dc272dea8408eadb7.tar.bz2
Closed resources using Apache IOUtils.refs/changes/31/6831/1
-rw-r--r--hudson-core/src/main/java/hudson/ClassicPluginStrategy.java8
-rw-r--r--hudson-core/src/main/java/hudson/FilePath.java19
-rw-r--r--hudson-core/src/main/java/hudson/Launcher.java20
-rw-r--r--hudson-core/src/main/java/hudson/console/AnnotatedLargeText.java5
-rw-r--r--hudson-core/src/main/java/hudson/console/ConsoleNote.java10
-rw-r--r--hudson-core/src/main/java/hudson/model/ExternalRun.java60
-rw-r--r--hudson-core/src/main/java/hudson/util/TextFile.java1
-rw-r--r--hudson-core/src/test/java/hudson/tasks/junit/SuiteResultTest.java6
-rw-r--r--hudson-core/src/test/java/hudson/util/LineEndNormalizingWriterTest.java3
9 files changed, 92 insertions, 40 deletions
diff --git a/hudson-core/src/main/java/hudson/ClassicPluginStrategy.java b/hudson-core/src/main/java/hudson/ClassicPluginStrategy.java
index 8c92326..aa8eb99 100644
--- a/hudson-core/src/main/java/hudson/ClassicPluginStrategy.java
+++ b/hudson-core/src/main/java/hudson/ClassicPluginStrategy.java
@@ -44,6 +44,7 @@ import java.util.jar.Attributes;
import java.util.logging.Level;
import java.util.logging.Logger;
+import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.AntClassLoader;
@@ -77,8 +78,8 @@ public class ClassicPluginStrategy implements PluginStrategy {
boolean isLinked = archive.getName().endsWith(".hpl");
if (isLinked) {
// resolve the .hpl file to the location of the manifest file
- String firstLine = new BufferedReader(new FileReader(archive))
- .readLine();
+ BufferedReader br = new BufferedReader(new FileReader(archive));
+ String firstLine = br.readLine();
if (firstLine.startsWith("Manifest-Version:")) {
// this is the manifest already
} else {
@@ -92,7 +93,8 @@ public class ClassicPluginStrategy implements PluginStrategy {
} catch (IOException e) {
throw new IOException2("Failed to load " + archive, e);
} finally {
- in.close();
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(br);
}
} else {
if (archive.isDirectory()) {// already expanded
diff --git a/hudson-core/src/main/java/hudson/FilePath.java b/hudson-core/src/main/java/hudson/FilePath.java
index 030e56a..692785a 100644
--- a/hudson-core/src/main/java/hudson/FilePath.java
+++ b/hudson-core/src/main/java/hudson/FilePath.java
@@ -397,14 +397,19 @@ public final class FilePath implements Serializable {
* to check for a situation where nothing is archived.
*/
public int archive(final ArchiverFactory factory, OutputStream os, final DirScanner scanner) throws IOException, InterruptedException {
- final OutputStream out = (channel != null) ? new RemoteOutputStream(os) : os;
+ if (channel != null) {
+ os = new RemoteOutputStream(os);
+ }
+ final OutputStream out = os;
+
return act(new FileCallable<Integer>() {
public Integer invoke(File f, VirtualChannel channel) throws IOException {
Archiver a = factory.create(out);
try {
scanner.scan(f, a);
} finally {
- a.close();
+ IOUtils.closeQuietly(out);
+ IOUtils.closeQuietly(a);
}
return a.countEntries();
}
@@ -1327,11 +1332,17 @@ public final class FilePath implements Serializable {
public Void invoke(File f, VirtualChannel channel) throws IOException {
f.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(f);
- Writer w = encoding != null ? new OutputStreamWriter(fos, encoding) : new OutputStreamWriter(fos);
+ Writer w;
+ if (encoding != null) {
+ w = new OutputStreamWriter(fos, encoding);
+ } else {
+ w = new OutputStreamWriter(fos);
+ }
+
try {
w.write(content);
} finally {
- w.close();
+ IOUtils.closeQuietly(w);
}
return null;
}
diff --git a/hudson-core/src/main/java/hudson/Launcher.java b/hudson-core/src/main/java/hudson/Launcher.java
index 2dde9bf..c3e1ae3 100644
--- a/hudson-core/src/main/java/hudson/Launcher.java
+++ b/hudson-core/src/main/java/hudson/Launcher.java
@@ -727,9 +727,23 @@ public abstract class Launcher {
}
public Proc launch(ProcStarter ps) throws IOException {
- final OutputStream out = ps.stdout == null ? null : new RemoteOutputStream(new CloseProofOutputStream(ps.stdout));
- final OutputStream err = ps.stderr == null ? null : new RemoteOutputStream(new CloseProofOutputStream(ps.stderr));
- final InputStream in = ps.stdin == null ? null : new RemoteInputStream(ps.stdin);
+ OutputStream out = null;
+ OutputStream err = null;
+ InputStream in = null;
+
+ if (ps.stdout != null) {
+ out = new RemoteOutputStream(new CloseProofOutputStream(ps.stdout));
+ }
+
+ if (ps.stderr != null) {
+ err = new RemoteOutputStream(new CloseProofOutputStream(ps.stderr));
+ }
+
+ if (ps.stdin != null) {
+ in = new RemoteInputStream(ps.stdin);
+ }
+
+
final String workDir = ps.pwd == null ? null : ps.pwd.getRemote();
return new RemoteProc(getChannel().callAsync(new RemoteLaunchCallable(ps.commands, ps.masks, ps.envs, in, out, err, workDir, listener)));
diff --git a/hudson-core/src/main/java/hudson/console/AnnotatedLargeText.java b/hudson-core/src/main/java/hudson/console/AnnotatedLargeText.java
index f891bd4..a8fee67 100644
--- a/hudson-core/src/main/java/hudson/console/AnnotatedLargeText.java
+++ b/hudson-core/src/main/java/hudson/console/AnnotatedLargeText.java
@@ -106,13 +106,14 @@ public class AnnotatedLargeText<T> extends LargeText {
}
private ConsoleAnnotator createAnnotator(StaplerRequest req) throws IOException {
+ ObjectInputStream ois = null;
try {
String base64 = req!=null ? req.getHeader("X-ConsoleAnnotator") : null;
if (base64!=null) {
Cipher sym = Secret.getCipher("AES");
sym.init(Cipher.DECRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
- ObjectInputStream ois = new ObjectInputStreamEx(new GZIPInputStream(
+ ois = new ObjectInputStreamEx(new GZIPInputStream(
new CipherInputStream(new ByteArrayInputStream(Base64.decodeBase64(base64)),sym)),
Hudson.getInstance().pluginManager.uberClassLoader);
long timestamp = ois.readLong();
@@ -124,6 +125,8 @@ public class AnnotatedLargeText<T> extends LargeText {
throw new IOException2(e);
} catch (ClassNotFoundException e) {
throw new IOException2(e);
+ } finally {
+ IOUtils.closeQuietly(ois);
}
// start from scratch
return ConsoleAnnotator.initial(context==null ? null : context.getClass());
diff --git a/hudson-core/src/main/java/hudson/console/ConsoleNote.java b/hudson-core/src/main/java/hudson/console/ConsoleNote.java
index ca027be..56b31f8 100644
--- a/hudson-core/src/main/java/hudson/console/ConsoleNote.java
+++ b/hudson-core/src/main/java/hudson/console/ConsoleNote.java
@@ -205,13 +205,16 @@ public abstract class ConsoleNote<T> implements Serializable, Describable<Consol
* @return null if the encoded form is malformed.
*/
public static ConsoleNote readFrom(DataInputStream in) throws IOException, ClassNotFoundException {
+ ObjectInputStream ois = null;
+ DataInputStream decoded = null;
+
try {
byte[] preamble = new byte[PREAMBLE.length];
in.readFully(preamble);
if (!Arrays.equals(preamble,PREAMBLE))
return null; // not a valid preamble
- DataInputStream decoded = new DataInputStream(new UnbufferedBase64InputStream(in));
+ decoded = new DataInputStream(new UnbufferedBase64InputStream(in));
int sz = decoded.readInt();
//Size should be greater than Zero. See http://issues.hudson-ci.org/browse/HUDSON-6558
if (sz < 0) {
@@ -225,13 +228,16 @@ public abstract class ConsoleNote<T> implements Serializable, Describable<Consol
if (!Arrays.equals(postamble,POSTAMBLE))
return null; // not a valid postamble
- ObjectInputStream ois = new ObjectInputStreamEx(
+ ois = new ObjectInputStreamEx(
new GZIPInputStream(new ByteArrayInputStream(buf)), Hudson.getInstance().pluginManager.uberClassLoader);
return (ConsoleNote) ois.readObject();
} catch (Error e) {
// for example, bogus 'sz' can result in OutOfMemoryError.
// package that up as IOException so that the caller won't fatally die.
throw new IOException2(e);
+ } finally {
+ IOUtils.closeQuietly(ois);
+ IOUtils.closeQuietly(decoded);
}
}
diff --git a/hudson-core/src/main/java/hudson/model/ExternalRun.java b/hudson-core/src/main/java/hudson/model/ExternalRun.java
index 975a889..68d650e 100644
--- a/hudson-core/src/main/java/hudson/model/ExternalRun.java
+++ b/hudson-core/src/main/java/hudson/model/ExternalRun.java
@@ -23,6 +23,9 @@ import hudson.util.DualOutputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+
+import org.apache.commons.io.IOUtils;
+
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
@@ -101,33 +104,38 @@ public class ExternalRun extends Run<ExternalJob,ExternalRun> {
}
public Result run(BuildListener listener) throws Exception {
- PrintStream logger = new PrintStream(new DecodingStream(listener.getLogger()));
-
- XMLInputFactory xif = XMLInputFactory.newInstance();
- XMLStreamReader p = xif.createXMLStreamReader(in);
-
- p.nextTag(); // get to the <run>
- p.nextTag(); // get to the <log>
-
- charset=p.getAttributeValue(null,"content-encoding");
- while(p.next()!= END_ELEMENT) {
- int type = p.getEventType();
- if(type== CHARACTERS || type== CDATA)
- logger.print(p.getText());
+ PrintStream logger = null;
+ try {
+ logger = new PrintStream(new DecodingStream(listener.getLogger()));
+
+ XMLInputFactory xif = XMLInputFactory.newInstance();
+ XMLStreamReader p = xif.createXMLStreamReader(in);
+
+ p.nextTag(); // get to the <run>
+ p.nextTag(); // get to the <log>
+
+ charset=p.getAttributeValue(null,"content-encoding");
+ while(p.next()!= END_ELEMENT) {
+ int type = p.getEventType();
+ if(type== CHARACTERS || type== CDATA)
+ logger.print(p.getText());
+ }
+ p.nextTag(); // get to <result>
+
+
+
+ Result r = Integer.parseInt(elementText(p))==0?Result.SUCCESS:Result.FAILURE;
+
+ p.nextTag(); // get to <duration> (optional)
+ if(p.getEventType()== START_ELEMENT
+ && p.getLocalName().equals("duration")) {
+ duration[0] = Long.parseLong(elementText(p));
+ }
+
+ return r;
+ } finally {
+ IOUtils.closeQuietly(logger);
}
- p.nextTag(); // get to <result>
-
-
-
- Result r = Integer.parseInt(elementText(p))==0?Result.SUCCESS:Result.FAILURE;
-
- p.nextTag(); // get to <duration> (optional)
- if(p.getEventType()== START_ELEMENT
- && p.getLocalName().equals("duration")) {
- duration[0] = Long.parseLong(elementText(p));
- }
-
- return r;
}
public void post(BuildListener listener) {
diff --git a/hudson-core/src/main/java/hudson/util/TextFile.java b/hudson-core/src/main/java/hudson/util/TextFile.java
index fa35ad4..744a692 100644
--- a/hudson-core/src/main/java/hudson/util/TextFile.java
+++ b/hudson-core/src/main/java/hudson/util/TextFile.java
@@ -76,6 +76,7 @@ public class TextFile {
w.commit();
} finally {
w.abort();
+ IOUtils.closeQuietly(w);
}
}
diff --git a/hudson-core/src/test/java/hudson/tasks/junit/SuiteResultTest.java b/hudson-core/src/test/java/hudson/tasks/junit/SuiteResultTest.java
index a27dc7d..2002804 100644
--- a/hudson-core/src/test/java/hudson/tasks/junit/SuiteResultTest.java
+++ b/hudson-core/src/test/java/hudson/tasks/junit/SuiteResultTest.java
@@ -27,6 +27,8 @@ import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Writer;
+import org.apache.commons.io.IOUtils;
+
/**
* Test cases for parsing JUnit report XML files.
* As there are no XML schema for JUnit xml files, Hudson needs to handle
@@ -144,8 +146,9 @@ public class SuiteResultTest extends TestCase {
File data = File.createTempFile("testSuiteStdioTrimming", ".xml");
try {
Writer w = new FileWriter(data);
+ PrintWriter pw = null;
try {
- PrintWriter pw = new PrintWriter(w);
+ pw = new PrintWriter(w);
pw.println("<testsuites name='x'>");
pw.println("<testsuite failures='0' errors='0' tests='1' name='x'>");
pw.println("<testcase name='x' classname='x'/>");
@@ -162,6 +165,7 @@ public class SuiteResultTest extends TestCase {
pw.flush();
} finally {
w.close();
+ IOUtils.closeQuietly(pw);
}
SuiteResult sr = parseOne(data);
assertEquals(sr.getStderr(), 1028, sr.getStderr().length());
diff --git a/hudson-core/src/test/java/hudson/util/LineEndNormalizingWriterTest.java b/hudson-core/src/test/java/hudson/util/LineEndNormalizingWriterTest.java
index e59b783..0fc619b 100644
--- a/hudson-core/src/test/java/hudson/util/LineEndNormalizingWriterTest.java
+++ b/hudson-core/src/test/java/hudson/util/LineEndNormalizingWriterTest.java
@@ -34,6 +34,7 @@ public class LineEndNormalizingWriterTest extends TestCase {
w.write("\n");
assertEquals(sw.toString(),"abc\r\ndef\r\n");
+ IOUtils.closeQuietly(w);
}
public void test2() throws IOException {
@@ -44,6 +45,7 @@ public class LineEndNormalizingWriterTest extends TestCase {
w.write("\n");
assertEquals(sw.toString(),"abc\r\ndef\r\n\r\n");
+ IOUtils.closeQuietly(w);
}
public void test3() throws IOException {
@@ -53,5 +55,6 @@ public class LineEndNormalizingWriterTest extends TestCase {
w.write("\r\n\n");
assertEquals(sw.toString(),"\r\n\r\n");
+ IOUtils.closeQuietly(w);
}
}