From 6798e01f492773305ec89030628d684156b84b80 Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Thu, 5 Apr 2018 19:19:22 +0200 Subject: [PATCH] Closes #224: scan for indexing handler --- .../sismics/docs/core/model/context/AppContext.java | 13 +++++++++++-- .../docs/core/util/indexing/IndexingHandler.java | 7 +++++++ .../core/util/indexing/LuceneIndexingHandler.java | 8 ++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java b/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java index fcd42a1c..c340ad62 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java +++ b/docs-core/src/main/java/com/sismics/docs/core/model/context/AppContext.java @@ -1,5 +1,6 @@ package com.sismics.docs.core.model.context; +import com.google.common.collect.Lists; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; import com.sismics.docs.core.constant.Constants; @@ -11,7 +12,7 @@ import com.sismics.docs.core.service.FileService; import com.sismics.docs.core.service.InboxService; import com.sismics.docs.core.util.PdfUtil; import com.sismics.docs.core.util.indexing.IndexingHandler; -import com.sismics.docs.core.util.indexing.LuceneIndexingHandler; +import com.sismics.util.ClasspathScanner; import com.sismics.util.EnvironmentUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,8 +77,16 @@ public class AppContext { resetEventBus(); // Start indexing handler - indexingHandler = new LuceneIndexingHandler(); try { + List> indexingHandlerList = Lists.newArrayList( + new ClasspathScanner().findClasses(IndexingHandler.class, "com.sismics.docs.core.util.indexing")); + for (Class handlerClass : indexingHandlerList) { + IndexingHandler handler = handlerClass.newInstance(); + if (handler.accept()) { + indexingHandler = handler; + break; + } + } indexingHandler.startUp(); } catch (Exception e) { log.error("Error starting the indexing handler, rebuilding the index: " + e.getMessage()); diff --git a/docs-core/src/main/java/com/sismics/docs/core/util/indexing/IndexingHandler.java b/docs-core/src/main/java/com/sismics/docs/core/util/indexing/IndexingHandler.java index 563676f7..459caa6c 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/util/indexing/IndexingHandler.java +++ b/docs-core/src/main/java/com/sismics/docs/core/util/indexing/IndexingHandler.java @@ -16,6 +16,13 @@ import java.util.List; * @author bgamard */ public interface IndexingHandler { + /** + * Return true if this indexing handler can start. + * + * @return True if able to start + */ + boolean accept(); + /** * Start the indexing handler. * diff --git a/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java b/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java index a9285318..c753fb62 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java +++ b/docs-core/src/main/java/com/sismics/docs/core/util/indexing/LuceneIndexingHandler.java @@ -16,6 +16,7 @@ import com.sismics.docs.core.util.jpa.PaginatedList; import com.sismics.docs.core.util.jpa.PaginatedLists; import com.sismics.docs.core.util.jpa.QueryParam; import com.sismics.docs.core.util.jpa.SortCriteria; +import com.sismics.util.ClasspathScanner; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; @@ -44,6 +45,7 @@ import java.util.*; * * @author bgamard */ +@ClasspathScanner.Priority(100) public class LuceneIndexingHandler implements IndexingHandler { /** * Logger. @@ -60,6 +62,12 @@ public class LuceneIndexingHandler implements IndexingHandler { */ private DirectoryReader directoryReader; + @Override + public boolean accept() { + // Embedded Lucene can always start + return true; + } + @Override public void startUp() throws Exception { ConfigDao configDao = new ConfigDao();