Apache Lucene open-source search software
SOLR-4525: Fix more leaks with open files on properties load/store
Uwe Schindler6ba62cd0a240
diff --git a/solr/core/src/java/org/apache/solr/core/SolrProperties.java b/solr/core/src/java/org/apache/solr/core/SolrProperties.javaindex c71261760fd..42aad794e51 100644--- a/solr/core/src/java/org/apache/solr/core/SolrProperties.java+++ b/solr/core/src/java/org/apache/solr/core/SolrProperties.java@@ -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()) {diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrDiscoveryProperties.java b/solr/core/src/test/org/apache/solr/core/TestSolrDiscoveryProperties.javaindex 39c928fead9..4ab7619101d 100644--- a/solr/core/src/test/org/apache/solr/core/TestSolrDiscoveryProperties.java+++ b/solr/core/src/test/org/apache/solr/core/TestSolrDiscoveryProperties.java@@ -58,8 +58,11 @@ public class TestSolrDiscoveryProperties extends SolrTestCaseJ4 {props.put(parts[0], parts[1]);}FileOutputStream out = new FileOutputStream(solrProps.getAbsolutePath());- props.store(out, null);- out.close();+ try {+ props.store(out, null);+ } finally {+ out.close();+ }}private void addSolrXml() throws Exception {@@ -92,8 +95,11 @@ public class TestSolrDiscoveryProperties extends SolrTestCaseJ4 {assertTrue("Failed to mkdirs for " + parent.getAbsolutePath(), parent.mkdirs());FileOutputStream out = new FileOutputStream(propFile);- stockProps.store(out, null);- out.close();+ 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());