提交 386ec0d1 编写于 作者: J Jerome Lacoste 提交者: Kohsuke Kawaguchi

Ensure that the Readers are closed properly

上级 b58044ad
......@@ -27,14 +27,13 @@ import hudson.Plugin.DummyImpl;
import hudson.PluginWrapper.Dependency;
import hudson.model.Hudson;
import hudson.util.IOException2;
import hudson.util.IOUtils;
import hudson.util.MaskingClassLoader;
import hudson.util.VersionNumber;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
......@@ -86,13 +85,7 @@ public class ClassicPluginStrategy implements PluginStrategy {
boolean isLinked = archive.getName().endsWith(".hpl");
if (isLinked) {
// resolve the .hpl file to the location of the manifest file
final String firstLine;
BufferedReader reader = new BufferedReader(new FileReader(archive));
try {
firstLine = reader.readLine();
} finally {
reader.close();
}
final String firstLine = IOUtils.readFirstLine(archive);
if (firstLine.startsWith("Manifest-Version:")) {
// this is the manifest already
} else {
......@@ -179,7 +172,7 @@ public class ClassicPluginStrategy implements PluginStrategy {
return new PluginWrapper(pluginManager, archive, manifest, baseResourceURL,
createClassLoader(paths, dependencyLoader, atts), disableFile, dependencies, optionalDependencies);
}
@Deprecated
protected ClassLoader createClassLoader(List<File> paths, ClassLoader parent) throws IOException {
return createClassLoader( paths, parent, null );
......
......@@ -32,6 +32,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
......@@ -123,9 +124,7 @@ public class Main {
HttpURLConnection con = open(new URL(home +
"crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'"));
if (auth != null) con.setRequestProperty("Authorization", auth);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = in.readLine();
in.close();
String line = readFirstLine(con.getInputStream());
String[] components = line.split(":");
if (components.length == 2) {
crumbField = components[0];
......@@ -197,6 +196,15 @@ public class Main {
}
}
private static String readFirstLine(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
return reader.readLine();
} finally {
reader.close();
}
}
/**
* Connects to the given HTTP URL and configure time out, to avoid infinite hang.
*/
......
......@@ -200,11 +200,14 @@ public class Util {
StringBuilder str = new StringBuilder((int)logfile.length());
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(logfile),charset));
char[] buf = new char[1024];
int len;
while((len=r.read(buf,0,buf.length))>0)
str.append(buf,0,len);
r.close();
try {
char[] buf = new char[1024];
int len;
while((len=r.read(buf,0,buf.length))>0)
str.append(buf,0,len);
} finally {
r.close();
}
return str.toString();
}
......
......@@ -274,13 +274,16 @@ public class Queue extends ResourceController implements Saveable {
File queueFile = getQueueFile();
if (queueFile.exists()) {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(queueFile)));
String line;
while ((line = in.readLine()) != null) {
AbstractProject j = Jenkins.getInstance().getItemByFullName(line, AbstractProject.class);
if (j != null)
j.scheduleBuild();
try {
String line;
while ((line = in.readLine()) != null) {
AbstractProject j = Jenkins.getInstance().getItemByFullName(line, AbstractProject.class);
if (j != null)
j.scheduleBuild();
}
} finally {
in.close();
}
in.close();
// discard the queue file now that we are done
queueFile.delete();
} else {
......
......@@ -124,5 +124,21 @@ public class IOUtils extends org.apache.commons.io.IOUtils {
return PosixAPI.get().stat(f.getPath()).mode();
}
/**
* Read the first line of a file
* @param file
* @return the first line of a file
* @throws IOException
* @since 1.422
*/
public static String readFirstLine(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
return reader.readLine();
} finally {
reader.close();
}
}
private static final byte[] SKIP_BUFFER = new byte[8192];
}
......@@ -51,23 +51,27 @@ public class Service {
while (e.hasMoreElements()) {
URL url = e.nextElement();
BufferedReader configFile = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
String line;
while ((line = configFile.readLine()) != null) {
line = line.trim();
if (line.startsWith("#") || line.length()==0) continue;
try {
String line;
while ((line = configFile.readLine()) != null) {
line = line.trim();
if (line.startsWith("#") || line.length()==0) continue;
try {
Class<?> t = classLoader.loadClass(line);
if (!type.isAssignableFrom(t)) continue; // invalid type
try {
Class<?> t = classLoader.loadClass(line);
if (!type.isAssignableFrom(t)) continue; // invalid type
result.add(type.cast(t.newInstance()));
} catch (ClassNotFoundException x) {
LOGGER.log(WARNING,"Failed to load "+line,x);
} catch (InstantiationException x) {
LOGGER.log(WARNING,"Failed to load "+line,x);
} catch (IllegalAccessException x) {
LOGGER.log(WARNING,"Failed to load "+line,x);
result.add(type.cast(t.newInstance()));
} catch (ClassNotFoundException x) {
LOGGER.log(WARNING,"Failed to load "+line,x);
} catch (InstantiationException x) {
LOGGER.log(WARNING,"Failed to load "+line,x);
} catch (IllegalAccessException x) {
LOGGER.log(WARNING,"Failed to load "+line,x);
}
}
} finally {
configFile.close();
}
}
......
......@@ -88,9 +88,13 @@ public class TestPluginManager extends PluginManager {
URL index = getClass().getResource("/test-dependencies/index");
if (index!=null) {// if built with maven-hpi-plugin < 1.52 this file won't exist.
BufferedReader r = new BufferedReader(new InputStreamReader(index.openStream(),"UTF-8"));
String line;
while ((line=r.readLine())!=null) {
copyBundledPlugin(new URL(index, line + ".hpi"), line + ".hpi");
try {
String line;
while ((line=r.readLine())!=null) {
copyBundledPlugin(new URL(index, line + ".hpi"), line + ".hpi");
}
} finally {
r.close();
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册