You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2022/06/21 17:04:34 UTC

[tika] branch main updated: TIKA-3797 -- ServiceLoader's loadServiceProviders() should return unique classes

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 3b7d7f32d TIKA-3797 -- ServiceLoader's loadServiceProviders() should return unique classes
3b7d7f32d is described below

commit 3b7d7f32dcdc3a7444134dc08bc804a7846701c1
Author: tallison <ta...@apache.org>
AuthorDate: Tue Jun 21 13:04:20 2022 -0400

    TIKA-3797 -- ServiceLoader's loadServiceProviders() should return unique classes
---
 CHANGES.txt                                             |  3 +++
 .../main/java/org/apache/tika/config/ServiceLoader.java | 17 +++++++++++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 8a4f1ce4d..76fa225c0 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,8 @@
 Release 2.4.2 - ???
 
+   * ServiceLoader's loadServiceProviders() now guarantees
+     unique classes (TIKA-3797).
+
    * Fix bug that prevented setting of includeHeadersAndFooters
      for xls, xlsx, doc and docx via tika-config (TIKA-3796).
 
diff --git a/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java b/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
index 61b19be74..acc53ca88 100644
--- a/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
+++ b/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
@@ -28,8 +28,10 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 import org.apache.tika.exception.TikaConfigException;
@@ -241,13 +243,24 @@ public class ServiceLoader {
     /**
      * Returns all the available service providers of the given type.
      *
+     * As of versions after 2.4.1, this removes duplicate classes
+     *
      * @param iface service provider interface
      * @return available service providers
      */
     public <T> List<T> loadServiceProviders(Class<T> iface) {
+        List<T> tmp = new ArrayList<>();
+        tmp.addAll(loadDynamicServiceProviders(iface));
+        tmp.addAll(loadStaticServiceProviders(iface));
+
         List<T> providers = new ArrayList<>();
-        providers.addAll(loadDynamicServiceProviders(iface));
-        providers.addAll(loadStaticServiceProviders(iface));
+        Set<String> seen = new HashSet<>();
+        for (T provider : tmp) {
+            if (! seen.contains(provider.getClass().getCanonicalName())) {
+                providers.add(provider);
+                seen.add(provider.getClass().getCanonicalName());
+            }
+        }
         return providers;
     }