fixed throws in finally and converted to try with resources

This commit is contained in:
Jeremy Long
2017-03-11 11:11:02 -05:00
parent 318f3e14dd
commit c54f9b1144

View File

@@ -191,13 +191,8 @@ public final class CpeMemoryIndex {
* @throws IndexException thrown if there is an issue creating the index * @throws IndexException thrown if there is an issue creating the index
*/ */
private void buildIndex(CveDB cve) throws IndexException { private void buildIndex(CveDB cve) throws IndexException {
Analyzer analyzer = null; try (Analyzer analyzer = createSearchingAnalyzer();
IndexWriter indexWriter = null; IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(LuceneUtils.CURRENT_VERSION, analyzer))) {
try {
analyzer = createSearchingAnalyzer();
final IndexWriterConfig conf = new IndexWriterConfig(LuceneUtils.CURRENT_VERSION, analyzer);
indexWriter = new IndexWriter(index, conf);
try {
// Tip: reuse the Document and Fields for performance... // Tip: reuse the Document and Fields for performance...
// See "Re-use Document and Field instances" from // See "Re-use Document and Field instances" from
// http://wiki.apache.org/lucene-java/ImproveIndexingSpeed // http://wiki.apache.org/lucene-java/ImproveIndexingSpeed
@@ -209,7 +204,6 @@ public final class CpeMemoryIndex {
final Set<Pair<String, String>> data = cve.getVendorProductList(); final Set<Pair<String, String>> data = cve.getVendorProductList();
for (Pair<String, String> pair : data) { for (Pair<String, String> pair : data) {
//todo figure out why there are null products
if (pair.getLeft() != null && pair.getRight() != null) { if (pair.getLeft() != null && pair.getRight() != null) {
v.setStringValue(pair.getLeft()); v.setStringValue(pair.getLeft());
p.setStringValue(pair.getRight()); p.setStringValue(pair.getRight());
@@ -217,31 +211,15 @@ public final class CpeMemoryIndex {
resetFieldAnalyzer(); resetFieldAnalyzer();
} }
} }
indexWriter.commit();
indexWriter.close(true);
} catch (DatabaseException ex) { } catch (DatabaseException ex) {
LOGGER.debug("", ex); LOGGER.debug("", ex);
throw new IndexException("Error reading CPE data", ex); throw new IndexException("Error reading CPE data", ex);
}
} catch (CorruptIndexException ex) { } catch (CorruptIndexException ex) {
throw new IndexException("Unable to close an in-memory index", ex); throw new IndexException("Unable to close an in-memory index", ex);
} catch (IOException ex) { } catch (IOException ex) {
throw new IndexException("Unable to close an in-memory index", ex); throw new IndexException("Unable to close an in-memory index", ex);
} finally {
if (indexWriter != null) {
try {
try {
indexWriter.commit();
} finally {
indexWriter.close(true);
}
} catch (CorruptIndexException ex) {
throw new IndexException("Unable to close an in-memory index", ex);
} catch (IOException ex) {
throw new IndexException("Unable to close an in-memory index", ex);
}
if (analyzer != null) {
analyzer.close();
}
}
} }
} }