You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2015/12/02 01:33:41 UTC

svn commit: r1717560 - in /tika/branches/2.x/tika-core/src: main/java/org/apache/tika/config/ test/java/org/apache/tika/config/

Author: nick
Date: Wed Dec  2 00:33:41 2015
New Revision: 1717560

URL: http://svn.apache.org/viewvc?rev=1717560&view=rev
Log:
TIKA-1805 Notify via LoadErrorHandler if DefaultParser or DefaultDetector could not find any implementations of their service classes

Modified:
    tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java
    tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
    tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
    tika/branches/2.x/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java

Modified: tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java
URL: http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java?rev=1717560&r1=1717559&r2=1717560&view=diff
==============================================================================
--- tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java (original)
+++ tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/LoadErrorHandler.java Wed Dec  2 00:33:41 2015
@@ -39,6 +39,16 @@ public interface LoadErrorHandler {
      * @param throwable the encountered problem
      */
     void handleLoadError(String classname, Throwable throwable);
+    
+    /**
+     * Handles the case of no occurrences of the specified service interface
+     * being found. The implementation can log or otherwise process
+     * the given error information. If the method returns normally, then
+     * the service loader simply returns an empty list to the caller.
+     *
+     * @param interfacename name of the service interface with no occurrences
+     */
+    void handleNoOccurrences(String interfacename);
 
     /**
      * Strategy that simply ignores all problems.
@@ -46,6 +56,8 @@ public interface LoadErrorHandler {
     LoadErrorHandler IGNORE = new LoadErrorHandler() {
         public void handleLoadError(String classname, Throwable throwable) {
         }
+        public void handleNoOccurrences(String interfacename) {
+        }
         @Override
         public String toString() {
             return "IGNORE";
@@ -61,6 +73,10 @@ public interface LoadErrorHandler {
             Logger.getLogger(classname).log(
                     Level.WARNING, "Unable to load " + classname, throwable);
         }
+        public void handleNoOccurrences(String interfacename) {
+            Logger.getLogger(interfacename).log(
+                    Level.WARNING, "No occurrences found of " + interfacename);
+        }
         @Override
         public String toString() {
             return "WARN";
@@ -76,6 +92,9 @@ public interface LoadErrorHandler {
         public void handleLoadError(String classname, Throwable throwable) {
             throw new RuntimeException("Unable to load " + classname, throwable);
         }
+        public void handleNoOccurrences(String interfacename) {
+            throw new RuntimeException("No occurrences found of " + interfacename);
+        }
         @Override
         public String toString() {
             return "THROW";

Modified: tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java
URL: http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java?rev=1717560&r1=1717559&r2=1717560&view=diff
==============================================================================
--- tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java (original)
+++ tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/ServiceLoader.java Wed Dec  2 00:33:41 2015
@@ -334,6 +334,9 @@ public class ServiceLoader {
                 }
             }
         }
+        if (providers.isEmpty()) {
+            handler.handleNoOccurrences(iface.getName());
+        }
         return providers;
     }
 

Modified: tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java
URL: http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java?rev=1717560&r1=1717559&r2=1717560&view=diff
==============================================================================
--- tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java (original)
+++ tika/branches/2.x/tika-core/src/main/java/org/apache/tika/config/TikaConfig.java Wed Dec  2 00:33:41 2015
@@ -162,8 +162,8 @@ public class TikaConfig {
         ExecutorServiceXmlLoader executorLoader = new ExecutorServiceXmlLoader();
         
         this.mimeTypes = typesFromDomElement(element);
-        this.detector = detectorLoader.loadOverall(element, mimeTypes, loader);
         this.parser = parserLoader.loadOverall(element, mimeTypes, loader);
+        this.detector = detectorLoader.loadOverall(element, mimeTypes, loader);
         this.translator = translatorLoader.loadOverall(element, mimeTypes, loader);
         this.executorService = executorLoader.loadOverall(element, mimeTypes, loader);
         this.serviceLoader = loader;

Modified: tika/branches/2.x/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
URL: http://svn.apache.org/viewvc/tika/branches/2.x/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java?rev=1717560&r1=1717559&r2=1717560&view=diff
==============================================================================
--- tika/branches/2.x/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java (original)
+++ tika/branches/2.x/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java Wed Dec  2 00:33:41 2015
@@ -36,6 +36,7 @@ import org.apache.tika.parser.EmptyParse
 import org.apache.tika.parser.ErrorParser;
 import org.apache.tika.parser.Parser;
 import org.apache.tika.parser.ParserDecorator;
+import org.junit.Before;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -51,6 +52,20 @@ import static org.junit.Assert.fail;
  *  testing using real parsers and detectors.
  */
 public class TikaConfigTest extends AbstractTikaConfigTest {
+    private ServiceLoader ignoreLoader;
+    private ServiceLoader warnLoader;
+    private ServiceLoader throwLoader;
+    
+    @Before
+    public void setup() {
+        ignoreLoader = new ServiceLoader(getClass().getClassLoader(), 
+                                         LoadErrorHandler.IGNORE);
+        warnLoader = new ServiceLoader(getClass().getClassLoader(), 
+                                       LoadErrorHandler.WARN);
+        throwLoader = new ServiceLoader(getClass().getClassLoader(), 
+                                        LoadErrorHandler.THROW);
+    }
+    
     /**
      * Make sure that a configuration file can't reference the
      * {@link AutoDetectParser} class a <parser> configuration element.
@@ -72,12 +87,6 @@ public class TikaConfigTest extends Abst
      */
     @Test
     public void testUnknownParser() throws Exception {
-        ServiceLoader ignoreLoader = new ServiceLoader(
-                getClass().getClassLoader(), LoadErrorHandler.IGNORE);
-        ServiceLoader warnLoader = new ServiceLoader(
-                getClass().getClassLoader(), LoadErrorHandler.WARN);
-        ServiceLoader throwLoader = new ServiceLoader(
-                getClass().getClassLoader(), LoadErrorHandler.THROW);
         Path configPath = Paths.get(new URI(getConfigPath("TIKA-1700-unknown-parser.xml")));
         
         TikaConfig ignore = new TikaConfig(configPath, ignoreLoader);
@@ -95,6 +104,24 @@ public class TikaConfigTest extends Abst
             fail("Shouldn't get here, invalid parser class");
         } catch (TikaException expected) {}
     }
+    
+    /**
+     * If there are no (eg) Parsers, then the service loader should
+     *  alert that via the Load Error Handler. This mimics what
+     *  DefaultParser / DefaultDetector will do
+     */
+    @Test
+    public void testNoInstancesOfService() throws Exception {
+        // Ignore does as it says
+        ignoreLoader.loadServiceProviders(EmptyParser.class);
+        // Warn will log, as we don't have any of this
+        warnLoader.loadServiceProviders(EmptyParser.class);
+        // Throw will object
+        try {
+            throwLoader.loadServiceProviders(EmptyParser.class);
+            fail("Shouldn't get here, no instances of the service");
+        } catch (RuntimeException expected) {}
+    }
 
     /**
      * Make sure that a configuration file can reference also a composite