Skip to content

Apache Lucene open-source search software

LUCENE-4255: Remove more useless try..catch on IOException after ResourceLoader update

Uwe Schindler58a9b85ae088
diff --git a/solr/contrib/velocity/src/java/org/apache/solr/response/SolrVelocityResourceLoader.java b/solr/contrib/velocity/src/java/org/apache/solr/response/SolrVelocityResourceLoader.java
index 9596bbb5ce4..5042a152884 100644
--- a/solr/contrib/velocity/src/java/org/apache/solr/response/SolrVelocityResourceLoader.java
+++ b/solr/contrib/velocity/src/java/org/apache/solr/response/SolrVelocityResourceLoader.java
@@ -22,6 +22,7 @@ import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.solr.core.SolrResourceLoader;
+import java.io.IOException;
import java.io.InputStream;
// TODO: the name of this class seems ridiculous
@@ -39,7 +40,11 @@ public class SolrVelocityResourceLoader extends ResourceLoader {
@Override
public InputStream getResourceStream(String template_name) throws ResourceNotFoundException {
- return loader.openResource(template_name);
+ try {
+ return loader.openResource(template_name);
+ } catch (IOException ioe) {
+ throw new ResourceNotFoundException(ioe);
+ }
}
@Override
diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java b/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java
index 42a9a8cebee..9089540b1c6 100644
--- a/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java
+++ b/solr/core/src/java/org/apache/solr/cloud/ZkSolrResourceLoader.java
@@ -18,6 +18,7 @@ package org.apache.solr.cloud;
*/
import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
@@ -69,7 +70,7 @@ public class ZkSolrResourceLoader extends SolrResourceLoader {
* @return the stream for the named resource
*/
@Override
- public InputStream openResource(String resource) {
+ public InputStream openResource(String resource) throws IOException {
InputStream is = null;
String file = collectionZkPath + "/" + resource;
try {
@@ -78,16 +79,16 @@ public class ZkSolrResourceLoader extends SolrResourceLoader {
return new ByteArrayInputStream(bytes);
}
} catch (Exception e) {
- throw new RuntimeException("Error opening " + file, e);
+ throw new IOException("Error opening " + file, e);
}
try {
// delegate to the class loader (looking into $INSTANCE_DIR/lib jars)
is = classLoader.getResourceAsStream(resource);
} catch (Exception e) {
- throw new RuntimeException("Error opening " + resource, e);
+ throw new IOException("Error opening " + resource, e);
}
if (is == null) {
- throw new RuntimeException("Can't find resource '" + resource
+ throw new IOException("Can't find resource '" + resource
+ "' in classpath or '" + collectionZkPath + "', cwd="
+ System.getProperty("user.dir"));
}
diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
index ea6a4c891bc..fdd283234d0 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java
@@ -252,7 +252,7 @@ public class SolrResourceLoader implements ResourceLoader
* Override this method to customize loading schema resources.
*@return the stream for the named schema
*/
- public InputStream openSchema(String name) {
+ public InputStream openSchema(String name) throws IOException {
return openResource(name);
}
@@ -260,7 +260,7 @@ public class SolrResourceLoader implements ResourceLoader
* Override this method to customize loading config resources.
*@return the stream for the named configuration
*/
- public InputStream openConfig(String name) {
+ public InputStream openConfig(String name) throws IOException {
return openResource(name);
}
@@ -272,7 +272,7 @@ public class SolrResourceLoader implements ResourceLoader
* Override this method to customize loading resources.
*@return the stream for the named resource
*/
- public InputStream openResource(String resource) {
+ public InputStream openResource(String resource) throws IOException {
InputStream is=null;
try {
File f0 = new File(resource);
diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index 43916ef91d9..417a8a70162 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -105,12 +105,12 @@ public final class IndexSchema {
name = DEFAULT_SCHEMA_FILE;
this.resourceName = name;
loader = solrConfig.getResourceLoader();
- if (is == null) {
- is = new InputSource(loader.openSchema(name));
- is.setSystemId(SystemIdResolver.createSystemIdFromResourceName(name));
- }
- readSchema(is);
try {
+ if (is == null) {
+ is = new InputSource(loader.openSchema(name));
+ is.setSystemId(SystemIdResolver.createSystemIdFromResourceName(name));
+ }
+ readSchema(is);
loader.inform( loader );
} catch (IOException e) {
throw new RuntimeException(e);
diff --git a/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java
index 574f5088adc..b1485d35c4c 100644
--- a/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java
+++ b/solr/core/src/java/org/apache/solr/spelling/ConjunctionSolrSpellChecker.java
@@ -106,7 +106,7 @@ public class ConjunctionSolrSpellChecker extends SolrSpellChecker {
}
@Override
- public void build(SolrCore core, SolrIndexSearcher searcher) {
+ public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
for (SolrSpellChecker c : checkers) {
c.build(core, searcher);
}
diff --git a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java
index 830968ae3e8..22850e4a8c4 100644
--- a/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java
+++ b/solr/core/src/java/org/apache/solr/spelling/DirectSolrSpellChecker.java
@@ -168,11 +168,10 @@ public class DirectSolrSpellChecker extends SolrSpellChecker {
}
@Override
- public void reload(SolrCore core, SolrIndexSearcher searcher)
- throws IOException {}
+ public void reload(SolrCore core, SolrIndexSearcher searcher) throws IOException {}
@Override
- public void build(SolrCore core, SolrIndexSearcher searcher) {}
+ public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {}
@Override
public SpellingResult getSuggestions(SpellingOptions options)
diff --git a/solr/core/src/java/org/apache/solr/spelling/FileBasedSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/FileBasedSpellChecker.java
index 11436f7e6c5..6654d1cd86a 100644
--- a/solr/core/src/java/org/apache/solr/spelling/FileBasedSpellChecker.java
+++ b/solr/core/src/java/org/apache/solr/spelling/FileBasedSpellChecker.java
@@ -59,17 +59,13 @@ public class FileBasedSpellChecker extends AbstractLuceneSpellChecker {
}
@Override
- public void build(SolrCore core, SolrIndexSearcher searcher) {
- try {
- loadExternalFileDictionary(core);
- spellChecker.clearIndex();
- // TODO: you should be able to specify the IWC params?
- // TODO: if we enable this, codec gets angry since field won't exist in the schema
- // config.setCodec(core.getCodec());
- spellChecker.indexDictionary(dictionary, new IndexWriterConfig(core.getSolrConfig().luceneMatchVersion, null), false);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+ public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
+ loadExternalFileDictionary(core);
+ spellChecker.clearIndex();
+ // TODO: you should be able to specify the IWC params?
+ // TODO: if we enable this, codec gets angry since field won't exist in the schema
+ // config.setCodec(core.getCodec());
+ spellChecker.indexDictionary(dictionary, new IndexWriterConfig(core.getSolrConfig().luceneMatchVersion, null), false);
}
/**
diff --git a/solr/core/src/java/org/apache/solr/spelling/IndexBasedSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/IndexBasedSpellChecker.java
index 4956db1b566..a33cf489ae0 100644
--- a/solr/core/src/java/org/apache/solr/spelling/IndexBasedSpellChecker.java
+++ b/solr/core/src/java/org/apache/solr/spelling/IndexBasedSpellChecker.java
@@ -73,31 +73,27 @@ public class IndexBasedSpellChecker extends AbstractLuceneSpellChecker {
}
@Override
- public void build(SolrCore core, SolrIndexSearcher searcher) {
+ public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
IndexReader reader = null;
- try {
- if (sourceLocation == null) {
- // Load from Solr's index
- reader = searcher.getIndexReader();
- } else {
- // Load from Lucene index at given sourceLocation
- reader = this.reader;
- }
-
- // Create the dictionary
- dictionary = new HighFrequencyDictionary(reader, field,
- threshold);
- // TODO: maybe whether or not to clear the index should be configurable?
- // an incremental update is faster (just adds new terms), but if you 'expunged'
- // old terms I think they might hang around.
- spellChecker.clearIndex();
- // TODO: you should be able to specify the IWC params?
- // TODO: if we enable this, codec gets angry since field won't exist in the schema
- // config.setCodec(core.getCodec());
- spellChecker.indexDictionary(dictionary, new IndexWriterConfig(core.getSolrConfig().luceneMatchVersion, null), false);
- } catch (IOException e) {
- throw new RuntimeException(e);
+ if (sourceLocation == null) {
+ // Load from Solr's index
+ reader = searcher.getIndexReader();
+ } else {
+ // Load from Lucene index at given sourceLocation
+ reader = this.reader;
}
+
+ // Create the dictionary
+ dictionary = new HighFrequencyDictionary(reader, field,
+ threshold);
+ // TODO: maybe whether or not to clear the index should be configurable?
+ // an incremental update is faster (just adds new terms), but if you 'expunged'
+ // old terms I think they might hang around.
+ spellChecker.clearIndex();
+ // TODO: you should be able to specify the IWC params?
+ // TODO: if we enable this, codec gets angry since field won't exist in the schema
+ // config.setCodec(core.getCodec());
+ spellChecker.indexDictionary(dictionary, new IndexWriterConfig(core.getSolrConfig().luceneMatchVersion, null), false);
}
@Override
diff --git a/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java b/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java
index fe15e0ec339..b369dedcf61 100644
--- a/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java
+++ b/solr/core/src/java/org/apache/solr/spelling/SolrSpellChecker.java
@@ -167,7 +167,7 @@ public abstract class SolrSpellChecker {
/**
* (re)Builds the spelling index. May be a NOOP if the implementation doesn't require building, or can't be rebuilt.
*/
- public abstract void build(SolrCore core, SolrIndexSearcher searcher);
+ public abstract void build(SolrCore core, SolrIndexSearcher searcher) throws IOException;
/**
* Get the value of {@link SpellingParams#SPELLCHECK_ACCURACY} if supported.
diff --git a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java
index 18a8f97c367..b1220a90922 100644
--- a/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java
+++ b/solr/core/src/java/org/apache/solr/spelling/suggest/Suggester.java
@@ -35,6 +35,7 @@ import org.apache.lucene.search.suggest.FileDictionary;
import org.apache.lucene.search.suggest.Lookup;
import org.apache.lucene.search.suggest.Lookup.LookupResult;
import org.apache.lucene.util.CharsRef;
+import org.apache.lucene.util.IOUtils;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
@@ -121,7 +122,7 @@ public class Suggester extends SolrSpellChecker {
}
@Override
- public void build(SolrCore core, SolrIndexSearcher searcher) {
+ public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
LOG.info("build()");
if (sourceLocation == null) {
reader = searcher.getIndexReader();
@@ -129,30 +130,26 @@ public class Suggester extends SolrSpellChecker {
} else {
try {
dictionary = new FileDictionary(new InputStreamReader(
- core.getResourceLoader().openResource(sourceLocation), "UTF-8"));
+ core.getResourceLoader().openResource(sourceLocation), IOUtils.CHARSET_UTF_8));
} catch (UnsupportedEncodingException e) {
// should not happen
LOG.error("should not happen", e);
}
}
- try {
- lookup.build(dictionary);
- if (storeDir != null) {
- File target = new File(storeDir, factory.storeFileName());
- if(!lookup.store(new FileOutputStream(target))) {
- if (sourceLocation == null) {
- assert reader != null && field != null;
- LOG.error("Store Lookup build from index on field: " + field + " failed reader has: " + reader.maxDoc() + " docs");
- } else {
- LOG.error("Store Lookup build from sourceloaction: " + sourceLocation + " failed");
- }
+
+ lookup.build(dictionary);
+ if (storeDir != null) {
+ File target = new File(storeDir, factory.storeFileName());
+ if(!lookup.store(new FileOutputStream(target))) {
+ if (sourceLocation == null) {
+ assert reader != null && field != null;
+ LOG.error("Store Lookup build from index on field: " + field + " failed reader has: " + reader.maxDoc() + " docs");
} else {
- LOG.info("Stored suggest data to: " + target.getAbsolutePath());
+ LOG.error("Store Lookup build from sourceloaction: " + sourceLocation + " failed");
}
+ } else {
+ LOG.info("Stored suggest data to: " + target.getAbsolutePath());
}
-
- } catch (Exception e) {
- LOG.error("Error while building or storing Suggester data", e);
}
}
@@ -161,8 +158,13 @@ public class Suggester extends SolrSpellChecker {
LOG.info("reload()");
if (dictionary == null && storeDir != null) {
// this may be a firstSearcher event, try loading it
- if (lookup.load(new FileInputStream(new File(storeDir, factory.storeFileName())))) {
- return; // loaded ok
+ FileInputStream is = new FileInputStream(new File(storeDir, factory.storeFileName()));
+ try {
+ if (lookup.load(is)) {
+ return; // loaded ok
+ }
+ } finally {
+ IOUtils.closeWhileHandlingException(is);
}
LOG.debug("load failed, need to build Lookup again");
}
diff --git a/solr/core/src/java/org/apache/solr/update/processor/StatelessScriptUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/StatelessScriptUpdateProcessorFactory.java
index 80061c67276..dc32f03313d 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/StatelessScriptUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/StatelessScriptUpdateProcessorFactory.java
@@ -305,16 +305,22 @@ public class StatelessScriptUpdateProcessorFactory extends UpdateRequestProcesso
}
scriptEngines.add(new EngineInfo((Invocable)engine, scriptFile));
- Reader scriptSrc = scriptFile.openReader(resourceLoader);
-
try {
- engine.eval(scriptSrc);
- } catch (ScriptException e) {
+ Reader scriptSrc = scriptFile.openReader(resourceLoader);
+
+ try {
+ engine.eval(scriptSrc);
+ } catch (ScriptException e) {
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+ "Unable to evaluate script: " +
+ scriptFile.getFileName(), e);
+ } finally {
+ IOUtils.closeQuietly(scriptSrc);
+ }
+ } catch (IOException ioe) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
- "Unable to evaluate script: " +
- scriptFile.getFileName(), e);
- } finally {
- IOUtils.closeQuietly(scriptSrc);
+ "Unable to evaluate script: " +
+ scriptFile.getFileName(), ioe);
}
}
return scriptEngines;
@@ -485,7 +491,7 @@ public class StatelessScriptUpdateProcessorFactory extends UpdateRequestProcesso
return extension;
}
- public Reader openReader(SolrResourceLoader resourceLoader) {
+ public Reader openReader(SolrResourceLoader resourceLoader) throws IOException {
InputStream input = resourceLoader.openResource(fileName);
return org.apache.lucene.util.IOUtils.getDecodingReader
(input, org.apache.lucene.util.IOUtils.CHARSET_UTF_8);
diff --git a/solr/core/src/test/org/apache/solr/handler/component/DummyCustomParamSpellChecker.java b/solr/core/src/test/org/apache/solr/handler/component/DummyCustomParamSpellChecker.java
index b640e3422f2..a4ef1c59a52 100644
--- a/solr/core/src/test/org/apache/solr/handler/component/DummyCustomParamSpellChecker.java
+++ b/solr/core/src/test/org/apache/solr/handler/component/DummyCustomParamSpellChecker.java
@@ -40,7 +40,7 @@ public class DummyCustomParamSpellChecker extends SolrSpellChecker {
}
@Override
- public void build(SolrCore core, SolrIndexSearcher searcher) {
+ public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
}