예제 #1
0
  /** {@inheritDoc} */
  public void initialize(WikiEngine engine, Properties props)
      throws NoRequiredPropertyException, IOException {
    m_engine = engine;

    m_luceneDirectory = engine.getWorkDir() + File.separator + LUCENE_DIR;

    int initialDelay =
        TextUtil.getIntegerProperty(props, PROP_LUCENE_INITIALDELAY, LuceneUpdater.INITIAL_DELAY);
    int indexDelay =
        TextUtil.getIntegerProperty(props, PROP_LUCENE_INDEXDELAY, LuceneUpdater.INDEX_DELAY);

    m_analyzerClass = TextUtil.getStringProperty(props, PROP_LUCENE_ANALYZER, m_analyzerClass);
    // FIXME: Just to be simple for now, we will do full reindex
    // only if no files are in lucene directory.

    File dir = new File(m_luceneDirectory);

    log.info("Lucene enabled, cache will be in: " + dir.getAbsolutePath());

    try {
      if (!dir.exists()) {
        dir.mkdirs();
      }

      if (!dir.exists() || !dir.canWrite() || !dir.canRead()) {
        log.error("Cannot write to Lucene directory, disabling Lucene: " + dir.getAbsolutePath());
        throw new IOException("Invalid Lucene directory.");
      }

      String[] filelist = dir.list();

      if (filelist == null) {
        throw new IOException(
            "Invalid Lucene directory: cannot produce listing: " + dir.getAbsolutePath());
      }
    } catch (IOException e) {
      log.error("Problem while creating Lucene index - not using Lucene.", e);
    }

    // Start the Lucene update thread, which waits first
    // for a little while before starting to go through
    // the Lucene "pages that need updating".
    LuceneUpdater updater = new LuceneUpdater(m_engine, this, initialDelay, indexDelay);
    updater.start();
  }
예제 #2
0
  /**
   * Performs a full Lucene reindex, if necessary.
   *
   * @throws IOException If there's a problem during indexing
   */
  protected void doFullLuceneReindex() throws IOException {
    File dir = new File(m_luceneDirectory);

    String[] filelist = dir.list();

    if (filelist == null) {
      throw new IOException(
          "Invalid Lucene directory: cannot produce listing: " + dir.getAbsolutePath());
    }

    try {
      if (filelist.length == 0) {
        //
        //  No files? Reindex!
        //
        Date start = new Date();
        IndexWriter writer = null;

        log.info("Starting Lucene reindexing, this can take a couple minutes...");

        Directory luceneDir = new SimpleFSDirectory(dir, null);

        try {
          writer = getIndexWriter(luceneDir);
          Collection allPages = m_engine.getPageManager().getAllPages();

          for (Iterator iterator = allPages.iterator(); iterator.hasNext(); ) {
            WikiPage page = (WikiPage) iterator.next();

            try {
              String text =
                  m_engine
                      .getPageManager()
                      .getPageText(page.getName(), WikiProvider.LATEST_VERSION);
              luceneIndexPage(page, text, writer);
            } catch (IOException e) {
              log.warn("Unable to index page " + page.getName() + ", continuing to next ", e);
            }
          }

          Collection allAttachments = m_engine.getAttachmentManager().getAllAttachments();
          for (Iterator iterator = allAttachments.iterator(); iterator.hasNext(); ) {
            Attachment att = (Attachment) iterator.next();

            try {
              String text = getAttachmentContent(att.getName(), WikiProvider.LATEST_VERSION);
              luceneIndexPage(att, text, writer);
            } catch (IOException e) {
              log.warn("Unable to index attachment " + att.getName() + ", continuing to next", e);
            }
          }

        } finally {
          close(writer);
        }

        Date end = new Date();
        log.info(
            "Full Lucene index finished in "
                + (end.getTime() - start.getTime())
                + " milliseconds.");
      } else {
        log.info("Files found in Lucene directory, not reindexing.");
      }
    } catch (NoClassDefFoundError e) {
      log.info("Lucene libraries do not exist - not using Lucene.");
    } catch (IOException e) {
      log.error("Problem while creating Lucene index - not using Lucene.", e);
    } catch (ProviderException e) {
      log.error("Problem reading pages while creating Lucene index (JSPWiki won't start.)", e);
      throw new IllegalArgumentException("unable to create Lucene index");
    } catch (Exception e) {
      log.error("Unable to start lucene", e);
    }
  }