| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.data.lucene; |
| 20 | |
|
| 21 | |
import java.io.IOException; |
| 22 | |
import java.util.logging.Level; |
| 23 | |
import java.util.logging.Logger; |
| 24 | |
import org.apache.lucene.analysis.Analyzer; |
| 25 | |
import org.apache.lucene.document.Document; |
| 26 | |
import org.apache.lucene.index.CorruptIndexException; |
| 27 | |
import org.apache.lucene.index.DirectoryReader; |
| 28 | |
import org.apache.lucene.index.IndexReader; |
| 29 | |
import org.apache.lucene.index.IndexWriter; |
| 30 | |
import org.apache.lucene.index.IndexWriterConfig; |
| 31 | |
import org.apache.lucene.queryparser.classic.ParseException; |
| 32 | |
import org.apache.lucene.queryparser.classic.QueryParser; |
| 33 | |
import org.apache.lucene.search.IndexSearcher; |
| 34 | |
import org.apache.lucene.search.Query; |
| 35 | |
import org.apache.lucene.search.TopDocs; |
| 36 | |
import org.apache.lucene.store.Directory; |
| 37 | |
import org.apache.lucene.store.LockObtainFailedException; |
| 38 | |
import org.apache.lucene.util.Version; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 30 | public abstract class AbstractIndex { |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
private Directory directory; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private IndexWriter indexWriter; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
private IndexReader indexReader; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private IndexSearcher indexSearcher; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private Analyzer indexingAnalyzer; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
private Analyzer searchingAnalyzer; |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private QueryParser queryParser; |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | 30 | private boolean indexOpen = false; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public void open() throws IOException { |
| 87 | 27 | directory = this.getDirectory(); |
| 88 | 27 | indexingAnalyzer = this.getIndexingAnalyzer(); |
| 89 | 27 | searchingAnalyzer = this.getSearchingAnalyzer(); |
| 90 | 27 | indexOpen = true; |
| 91 | 27 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public void commit() { |
| 97 | 0 | if (indexWriter != null) { |
| 98 | |
try { |
| 99 | 0 | indexWriter.commit(); |
| 100 | 0 | } catch (CorruptIndexException ex) { |
| 101 | 0 | final String msg = "Unable to update database, there is a corrupt index."; |
| 102 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.SEVERE, msg); |
| 103 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.FINE, null, ex); |
| 104 | 0 | } catch (IOException ex) { |
| 105 | 0 | final String msg = "Unable to update database due to an IO error."; |
| 106 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.SEVERE, msg); |
| 107 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.FINE, null, ex); |
| 108 | 0 | } |
| 109 | |
} |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public void close() { |
| 116 | 27 | if (indexWriter != null) { |
| 117 | 0 | commit(); |
| 118 | |
try { |
| 119 | 0 | indexWriter.close(true); |
| 120 | 0 | } catch (CorruptIndexException ex) { |
| 121 | 0 | final String msg = "Unable to update database, there is a corrupt index."; |
| 122 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.SEVERE, msg); |
| 123 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.FINE, null, ex); |
| 124 | 0 | } catch (IOException ex) { |
| 125 | 0 | final String msg = "Unable to update database due to an IO error."; |
| 126 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.SEVERE, msg); |
| 127 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.FINE, null, ex); |
| 128 | |
} finally { |
| 129 | 0 | indexWriter = null; |
| 130 | 0 | } |
| 131 | |
} |
| 132 | 27 | if (indexSearcher != null) { |
| 133 | 21 | indexSearcher = null; |
| 134 | |
} |
| 135 | |
|
| 136 | 27 | if (indexingAnalyzer != null) { |
| 137 | 27 | indexingAnalyzer.close(); |
| 138 | 27 | indexingAnalyzer = null; |
| 139 | |
} |
| 140 | |
|
| 141 | 27 | if (searchingAnalyzer != null) { |
| 142 | 27 | searchingAnalyzer.close(); |
| 143 | 27 | searchingAnalyzer = null; |
| 144 | |
} |
| 145 | |
|
| 146 | |
try { |
| 147 | 27 | directory.close(); |
| 148 | 0 | } catch (IOException ex) { |
| 149 | 0 | final String msg = "Unable to update database due to an IO error."; |
| 150 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.SEVERE, msg); |
| 151 | 0 | Logger.getLogger(AbstractIndex.class.getName()).log(Level.FINE, null, ex); |
| 152 | |
} finally { |
| 153 | 27 | directory = null; |
| 154 | 27 | } |
| 155 | 27 | indexOpen = false; |
| 156 | 27 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public boolean isOpen() { |
| 164 | 37 | return indexOpen; |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public void openIndexWriter() throws CorruptIndexException, IOException { |
| 174 | 0 | if (!isOpen()) { |
| 175 | 0 | open(); |
| 176 | |
} |
| 177 | 0 | final IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, indexingAnalyzer); |
| 178 | 0 | indexWriter = new IndexWriter(directory, conf); |
| 179 | 0 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public IndexWriter getIndexWriter() throws CorruptIndexException, LockObtainFailedException, IOException { |
| 191 | 0 | if (indexWriter == null) { |
| 192 | 0 | openIndexWriter(); |
| 193 | |
} |
| 194 | 0 | return indexWriter; |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public void openIndexReader() throws CorruptIndexException, IOException { |
| 204 | 21 | if (!isOpen()) { |
| 205 | 0 | open(); |
| 206 | |
} |
| 207 | |
|
| 208 | 21 | indexReader = DirectoryReader.open(directory); |
| 209 | 21 | } |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
protected IndexSearcher getIndexSearcher() throws CorruptIndexException, IOException { |
| 219 | 2820 | if (indexReader == null) { |
| 220 | 21 | openIndexReader(); |
| 221 | |
} |
| 222 | 2820 | if (indexSearcher == null) { |
| 223 | 21 | indexSearcher = new IndexSearcher(indexReader); |
| 224 | |
} |
| 225 | 2820 | return indexSearcher; |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
public Analyzer getIndexingAnalyzer() { |
| 234 | 27 | if (indexingAnalyzer == null) { |
| 235 | 27 | indexingAnalyzer = createIndexingAnalyzer(); |
| 236 | |
} |
| 237 | 27 | return indexingAnalyzer; |
| 238 | |
} |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
protected Analyzer getSearchingAnalyzer() { |
| 246 | 48 | if (searchingAnalyzer == null) { |
| 247 | 27 | searchingAnalyzer = createSearchingAnalyzer(); |
| 248 | |
} |
| 249 | 48 | return searchingAnalyzer; |
| 250 | |
} |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
protected QueryParser getQueryParser() { |
| 258 | 111 | if (queryParser == null) { |
| 259 | 21 | queryParser = createQueryParser(); |
| 260 | |
} |
| 261 | 111 | return queryParser; |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
public TopDocs search(String searchString, int maxQueryResults) throws ParseException, IOException { |
| 275 | 111 | final QueryParser parser = getQueryParser(); |
| 276 | 111 | final Query query = parser.parse(searchString); |
| 277 | 111 | resetSearchingAnalyzer(); |
| 278 | 111 | final IndexSearcher is = getIndexSearcher(); |
| 279 | 111 | return is.search(query, maxQueryResults); |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
public TopDocs search(Query query, int maxQueryResults) throws CorruptIndexException, IOException { |
| 292 | 0 | final IndexSearcher is = getIndexSearcher(); |
| 293 | 0 | return is.search(query, maxQueryResults); |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public Document getDocument(int documentId) throws IOException { |
| 304 | 2709 | final IndexSearcher is = getIndexSearcher(); |
| 305 | 2709 | return is.doc(documentId); |
| 306 | |
} |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
public abstract Directory getDirectory() throws IOException; |
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
public abstract Analyzer createIndexingAnalyzer(); |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
public abstract Analyzer createSearchingAnalyzer(); |
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
public abstract QueryParser createQueryParser(); |
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
protected abstract void resetSearchingAnalyzer(); |
| 341 | |
} |