From c54f9b114479a1f8305ff3fb26b766acf74e79cd Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 11 Mar 2017 11:11:02 -0500 Subject: [PATCH] fixed throws in finally and converted to try with resources --- .../data/cpe/CpeMemoryIndex.java | 66 +++++++------------ 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/cpe/CpeMemoryIndex.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/cpe/CpeMemoryIndex.java index 5776f7e55..fd2437d2b 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/cpe/CpeMemoryIndex.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/cpe/CpeMemoryIndex.java @@ -191,57 +191,35 @@ public final class CpeMemoryIndex { * @throws IndexException thrown if there is an issue creating the index */ private void buildIndex(CveDB cve) throws IndexException { - Analyzer analyzer = null; - IndexWriter indexWriter = null; - 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... - // See "Re-use Document and Field instances" from - // http://wiki.apache.org/lucene-java/ImproveIndexingSpeed - final Document doc = new Document(); - final Field v = new TextField(Fields.VENDOR, Fields.VENDOR, Field.Store.YES); - final Field p = new TextField(Fields.PRODUCT, Fields.PRODUCT, Field.Store.YES); - doc.add(v); - doc.add(p); + try (Analyzer analyzer = createSearchingAnalyzer(); + IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(LuceneUtils.CURRENT_VERSION, analyzer))) { + // Tip: reuse the Document and Fields for performance... + // See "Re-use Document and Field instances" from + // http://wiki.apache.org/lucene-java/ImproveIndexingSpeed + final Document doc = new Document(); + final Field v = new TextField(Fields.VENDOR, Fields.VENDOR, Field.Store.YES); + final Field p = new TextField(Fields.PRODUCT, Fields.PRODUCT, Field.Store.YES); + doc.add(v); + doc.add(p); - final Set> data = cve.getVendorProductList(); - for (Pair pair : data) { - //todo figure out why there are null products - if (pair.getLeft() != null && pair.getRight() != null) { - v.setStringValue(pair.getLeft()); - p.setStringValue(pair.getRight()); - indexWriter.addDocument(doc); - resetFieldAnalyzer(); - } + final Set> data = cve.getVendorProductList(); + for (Pair pair : data) { + if (pair.getLeft() != null && pair.getRight() != null) { + v.setStringValue(pair.getLeft()); + p.setStringValue(pair.getRight()); + indexWriter.addDocument(doc); + resetFieldAnalyzer(); } - } catch (DatabaseException ex) { - LOGGER.debug("", ex); - throw new IndexException("Error reading CPE data", ex); } + indexWriter.commit(); + indexWriter.close(true); + } catch (DatabaseException ex) { + LOGGER.debug("", ex); + throw new IndexException("Error reading CPE data", ex); } 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); - } 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(); - } - } } }