SOLR-4525: Fix more leaks with open files on properties load/store

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1452143 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2013-03-03 22:17:01 +00:00
parent 716f10c0c1
commit 6ba62cd0a2
2 changed files with 39 additions and 9 deletions

View File

@ -38,6 +38,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -332,10 +333,14 @@ public class SolrProperties implements ConfigSolr {
}
}
OutputStream os = null;
try {
outProps.store(new FileOutputStream(plus.getFilePath()), null);
os = new FileOutputStream(plus.getFilePath());
outProps.store(os, null);
} catch (IOException e) {
log.error("Failed to persist core {}, filepath {}", coreName, plus.getFilePath());
} finally {
IOUtils.closeQuietly(os);
}
}
@ -381,10 +386,14 @@ public class SolrProperties implements ConfigSolr {
propsOut.put(prop, toTest);
}
}
OutputStream os = null;
try {
propsOut.store(new FileOutputStream(props), null);
os = new FileOutputStream(props);
propsOut.store(os, null);
} catch (IOException e) {
log.error("Failed to persist file " + props.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(os);
}
}
@ -439,7 +448,12 @@ public class SolrProperties implements ConfigSolr {
if (propFile.exists()) { // Stop looking after processing this file!
log.info("Discovered properties file {}, adding to cores", propFile.getAbsolutePath());
Properties propsOrig = new Properties();
propsOrig.load(new FileInputStream(propFile));
InputStream is = new FileInputStream(propFile);
try {
propsOrig.load(is);
} finally {
IOUtils.closeQuietly(is);
}
Properties props = new Properties();
for (String prop : propsOrig.stringPropertyNames()) {

View File

@ -58,9 +58,12 @@ public class TestSolrDiscoveryProperties extends SolrTestCaseJ4 {
props.put(parts[0], parts[1]);
}
FileOutputStream out = new FileOutputStream(solrProps.getAbsolutePath());
try {
props.store(out, null);
} finally {
out.close();
}
}
private void addSolrXml() throws Exception {
File tmpFile = new File(solrHomeDirectory, SolrProperties.SOLR_XML_FILE);
@ -92,8 +95,11 @@ public class TestSolrDiscoveryProperties extends SolrTestCaseJ4 {
assertTrue("Failed to mkdirs for " + parent.getAbsolutePath(), parent.mkdirs());
FileOutputStream out = new FileOutputStream(propFile);
try {
stockProps.store(out, null);
} finally {
out.close();
}
addConfFiles(new File(parent, "conf"));
}
@ -304,7 +310,12 @@ public class TestSolrDiscoveryProperties extends SolrTestCaseJ4 {
Properties curr = cc.getContainerProperties();
Properties persisted = new Properties();
persisted.load(new FileInputStream(new File(solrHomeDirectory, SolrProperties.SOLR_PROPERTIES_FILE)));
FileInputStream in = new FileInputStream(new File(solrHomeDirectory, SolrProperties.SOLR_PROPERTIES_FILE));
try {
persisted.load(in);
} finally {
in.close();
}
assertEquals("Persisted and original should be the same size", orig.size(), persisted.size());
@ -332,7 +343,12 @@ public class TestSolrDiscoveryProperties extends SolrTestCaseJ4 {
// Read the persisted file.
Properties props = new Properties();
File propParent = new File(solrHomeDirectory, orig.getProperty(CoreDescriptor.CORE_NAME));
props.load(new FileInputStream(new File(propParent, SolrProperties.CORE_PROP_FILE)));
FileInputStream in = new FileInputStream(new File(propParent, SolrProperties.CORE_PROP_FILE));
try {
props.load(in);
} finally {
in.close();
}
Set<String> propSet = props.stringPropertyNames();
assertEquals("Persisted properties should NOT contain extra properties", propSet.size(), orig.size());