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/12/13 18:15:19 UTC

[tika] branch TIKA-3945 created (now 811fe9327)

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

tallison pushed a change to branch TIKA-3945
in repository https://gitbox.apache.org/repos/asf/tika.git


      at 811fe9327 TIKA-3945 -- improve serialization of CompositePipesReporter

This branch includes the following new commits:

     new 811fe9327 TIKA-3945 -- improve serialization of CompositePipesReporter

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[tika] 01/01: TIKA-3945 -- improve serialization of CompositePipesReporter

Posted by ta...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 811fe9327a510f2c2b33c7968a73ee90ebdfbce9
Author: tallison <ta...@apache.org>
AuthorDate: Tue Dec 13 13:15:01 2022 -0500

    TIKA-3945 -- improve serialization of CompositePipesReporter
---
 .../java/org/apache/tika/config/ConfigBase.java    | 70 +++++++++++++++-------
 .../apache/tika/pipes/CompositePipesReporter.java  | 14 ++++-
 .../apache/tika/pipes/async/MockReporterTest.java  | 11 ++++
 .../{TIKA-3865.xml => TIKA-3865-deprecated.xml}    |  0
 .../org/apache/tika/pipes/async/TIKA-3865.xml      | 24 ++++----
 5 files changed, 81 insertions(+), 38 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/config/ConfigBase.java b/tika-core/src/main/java/org/apache/tika/config/ConfigBase.java
index 6af662dfc..734bc442c 100644
--- a/tika-core/src/main/java/org/apache/tika/config/ConfigBase.java
+++ b/tika-core/src/main/java/org/apache/tika/config/ConfigBase.java
@@ -219,28 +219,11 @@ public abstract class ConfigBase {
                 params = child.getChildNodes();
             } else if (child.getNodeType() == 1 && ! child.getLocalName().equals(exceptNodeName)) {
                 String itemName = child.getLocalName();
-                String setter = "set" + itemName.substring(0, 1).toUpperCase(Locale.US) +
-                        itemName.substring(1);
-                Class itemClass = null;
-                Method setterMethod = null;
-                for (Method method : object.getClass().getMethods()) {
-                    if (setter.equals(method.getName())) {
-                        Class<?>[] classes = method.getParameterTypes();
-                        if (classes.length == 1) {
-                            itemClass = classes[0];
-                            setterMethod = method;
-                            break;
-                        }
-                    }
-                }
-                if (itemClass == null) {
-                    throw new TikaConfigException("Couldn't find setter '" +
-                            setter + "' for " + itemName);
-                }
-                Object item = buildClass(child, itemName, itemClass);
-                setParams(itemClass.cast(item), child, new HashSet<>());
+                SetterClassPair setterClassPair = findSetterClassPair(object, itemName);
+                Object item = buildClass(child, itemName, setterClassPair.itemClass);
+                setParams(setterClassPair.itemClass.cast(item), child, new HashSet<>());
                 try {
-                    setterMethod.invoke(object, item);
+                    setterClassPair.setterMethod.invoke(object, item);
                 } catch (IllegalAccessException | InvocationTargetException e) {
                     throw new TikaConfigException("problem creating " + itemName, e);
                 }
@@ -278,6 +261,40 @@ public abstract class ConfigBase {
         }
     }
 
+    private static SetterClassPair findSetterClassPair(Object object, String itemName)
+            throws TikaConfigException {
+        String setter = "set" + itemName.substring(0, 1).toUpperCase(Locale.US) +
+                itemName.substring(1);
+        Class itemClass = null;
+        Method setterMethod = null;
+        for (Method method : object.getClass().getMethods()) {
+            if (setter.equals(method.getName())) {
+                Class<?>[] classes = method.getParameterTypes();
+                if (classes.length == 1) {
+                    itemClass = classes[0];
+                    setterMethod = method;
+                    return new SetterClassPair(setterMethod, itemClass);
+                }
+            }
+        }
+
+        String adder = "add" + itemName.substring(0, 1).toUpperCase(Locale.US) +
+                itemName.substring(1);
+        for (Method method : object.getClass().getMethods()) {
+            if (adder.equals(method.getName())) {
+                Class<?>[] classes = method.getParameterTypes();
+                if (classes.length == 1) {
+                    itemClass = classes[0];
+                    setterMethod = method;
+                    return new SetterClassPair(setterMethod, itemClass);
+                }
+            }
+        }
+        throw new TikaConfigException("Couldn't find setter '" +
+                setter + "' or adder '" + adder + "' for " + itemName +
+                " of class: " + object.getClass());
+    }
+
     private static boolean hasChildNodes(Node param) {
         if (!param.hasChildNodes()) {
             return false;
@@ -323,7 +340,7 @@ public abstract class ConfigBase {
 
         } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
                  IllegalAccessException e) {
-            throw new TikaConfigException("couldn't find class", e);
+            throw new TikaConfigException("couldn't build class for " + name, e);
         }
     }
 
@@ -523,4 +540,13 @@ public abstract class ConfigBase {
 
         return settings;
     }
+
+    private static class SetterClassPair {
+        private final Method setterMethod;
+        private final Class itemClass;
+        public SetterClassPair(Method setterMethod, Class itemClass) {
+            this.setterMethod = setterMethod;
+            this.itemClass = itemClass;
+        }
+    }
 }
diff --git a/tika-core/src/main/java/org/apache/tika/pipes/CompositePipesReporter.java b/tika-core/src/main/java/org/apache/tika/pipes/CompositePipesReporter.java
index 997e7ca18..6532e2c3c 100644
--- a/tika-core/src/main/java/org/apache/tika/pipes/CompositePipesReporter.java
+++ b/tika-core/src/main/java/org/apache/tika/pipes/CompositePipesReporter.java
@@ -17,6 +17,7 @@
 package org.apache.tika.pipes;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -29,7 +30,7 @@ import org.apache.tika.pipes.pipesiterator.TotalCountResult;
 
 public class CompositePipesReporter extends PipesReporter implements Initializable {
 
-    private List<PipesReporter> pipesReporters;
+    private List<PipesReporter> pipesReporters = new ArrayList<>();
 
     @Override
     public void report(FetchEmitTuple t, PipesResult result, long elapsed) {
@@ -70,12 +71,21 @@ public class CompositePipesReporter extends PipesReporter implements Initializab
         }
     }
 
+    /**
+     * 
+     * @param pipesReporters
+     * @deprecated use {@link CompositePipesReporter#addPipesReporter(PipesReporter)}
+     */
     @Field
+    @Deprecated
     public void setPipesReporters(List<PipesReporter> pipesReporters) {
         this.pipesReporters = pipesReporters;
     }
 
-
+    @Field
+    public void addPipesReporter(PipesReporter pipesReporter) {
+        this.pipesReporters.add(pipesReporter);
+    }
     public List<PipesReporter> getPipesReporters() {
         return pipesReporters;
     }
diff --git a/tika-core/src/test/java/org/apache/tika/pipes/async/MockReporterTest.java b/tika-core/src/test/java/org/apache/tika/pipes/async/MockReporterTest.java
index 9bfcd5591..a9bae3397 100644
--- a/tika-core/src/test/java/org/apache/tika/pipes/async/MockReporterTest.java
+++ b/tika-core/src/test/java/org/apache/tika/pipes/async/MockReporterTest.java
@@ -39,6 +39,17 @@ public class MockReporterTest {
         assertEquals("somethingOrOther", ((MockReporter)reporter).getEndpoint());
     }
 
+    @Test
+    public void testOlderCompositePipesReporter() throws Exception {
+        Path configPath = Paths.get(this.getClass().getResource("TIKA-3865-deprecated.xml").toURI());
+        AsyncConfig asyncConfig = AsyncConfig.load(configPath);
+        PipesReporter reporter = asyncConfig.getPipesReporter();
+        assertTrue(reporter instanceof CompositePipesReporter);
+        List<PipesReporter> reporters = ((CompositePipesReporter)reporter).getPipesReporters();
+        assertEquals("somethingOrOther1", ((MockReporter)reporters.get(0)).getEndpoint());
+        assertEquals("somethingOrOther2", ((MockReporter)reporters.get(1)).getEndpoint());
+    }
+
     @Test
     public void testCompositePipesReporter() throws Exception {
         Path configPath = Paths.get(this.getClass().getResource("TIKA-3865.xml").toURI());
diff --git a/tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865.xml b/tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865-deprecated.xml
similarity index 100%
copy from tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865.xml
copy to tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865-deprecated.xml
diff --git a/tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865.xml b/tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865.xml
index 04f7dfa6f..44733a495 100644
--- a/tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865.xml
+++ b/tika-core/src/test/resources/org/apache/tika/pipes/async/TIKA-3865.xml
@@ -26,20 +26,16 @@
       <numEmitters>1</numEmitters>
     </params>
     <pipesReporter class="org.apache.tika.pipes.CompositePipesReporter">
-      <params>
-        <pipesReporters class="org.apache.tika.pipes.PipesReporter">
-          <pipesReporter class="org.apache.tika.pipes.async.MockReporter">
-            <params>
-              <endpoint>somethingOrOther1</endpoint>
-            </params>
-          </pipesReporter>
-          <pipesReporter class="org.apache.tika.pipes.async.MockReporter">
-            <params>
-              <endpoint>somethingOrOther2</endpoint>
-            </params>
-          </pipesReporter>
-        </pipesReporters>
-      </params>
+      <pipesReporter class="org.apache.tika.pipes.async.MockReporter">
+        <params>
+          <endpoint>somethingOrOther1</endpoint>
+        </params>
+      </pipesReporter>
+      <pipesReporter class="org.apache.tika.pipes.async.MockReporter">
+        <params>
+          <endpoint>somethingOrOther2</endpoint>
+        </params>
+      </pipesReporter>
     </pipesReporter>
   </async>
 </properties>
\ No newline at end of file