@PreDestroy private void exit() { timer.cancel(); try { logger.debug("Closing IndexWriter for directory lockid " + directory.getLockID()); iwriter.commit(); iwriter.close(); iwriter = null; logger.debug("IndexWriter closed for directory lockid " + directory.getLockID()); searcherManager.close(); logger.debug("SearcherManager closed for directory lockid " + directory.getLockID()); directory.close(); directory = null; logger.info("Directory closed"); } catch (Exception e) { StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); logger.fatal(errors.toString()); } }
@PostConstruct private void init() { luceneDirectory = propertyHandler.getLuceneDirectory(); active = luceneDirectory != null; if (active) { luceneRefreshSeconds = propertyHandler.getLuceneRefreshSeconds() * 1000L; luceneCommitCount = propertyHandler.getLuceneCommitCount(); ei = EntityInfoHandler.getInstance(); try { directory = FSDirectory.open(new File(luceneDirectory)); logger.debug("Opened FSDirectory with lockid " + directory.getLockID()); Analyzer analyzer = new IcatAnalyzer(Version.LUCENE_43); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, analyzer); iwriter = new IndexWriter(directory, config); String[] files = directory.listAll(); if (files.length == 1 && files[0].equals("write.lock")) { logger.debug("Directory only has the write.lock file so commit and reopen"); iwriter.commit(); iwriter.close(); iwriter = new IndexWriter(directory, config); } searcherManager = new SearcherManager(directory, new SearcherFactory()); isearcher = searcherManager.acquire(); logger.debug("Got a new IndexSearcher " + isearcher); parser = new StandardQueryParser(); StandardQueryConfigHandler qpConf = (StandardQueryConfigHandler) parser.getQueryConfigHandler(); qpConf.set(ConfigurationKeys.ANALYZER, analyzer); qpConf.set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, true); } catch (Exception e) { StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); logger.fatal(errors.toString()); if (directory != null) { try { String lockId = directory.getLockID(); directory.clearLock(lockId); logger.warn("Cleared lock " + lockId); } catch (IOException e1) { // Ignore } } throw new IllegalStateException(errors.toString()); } timer = new Timer("Lucene"); timer.schedule( new TimerTask() { @Override public void run() { try { commit(); } catch (IcatException e) { logger.error(e.getMessage()); } catch (Throwable t) { logger.error(t.getMessage()); } } }, luceneRefreshSeconds, luceneRefreshSeconds); logger.debug("Created LuceneSingleton"); } }