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/01/28 21:24:12 UTC
[tika] branch main updated: TIKA-3665 -- some incremental improvements. This does not solve the overall problem that defaults for dynamic are different for different constructors.
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 82ed730 TIKA-3665 -- some incremental improvements. This does not solve the overall problem that defaults for dynamic are different for different constructors.
82ed730 is described below
commit 82ed7304f16a452133f956ef2c27a52409f9bcbf
Author: tallison <ta...@apache.org>
AuthorDate: Fri Jan 28 16:23:50 2022 -0500
TIKA-3665 -- some incremental improvements. This does not solve the overall problem that defaults for dynamic are different for different constructors.
---
.../test/java/org/apache/tika/bundle/BundleIT.java | 2 +-
.../java/org/apache/tika/config/ServiceLoader.java | 9 ++++----
.../java/org/apache/tika/config/TikaActivator.java | 5 ----
.../org/apache/tika/utils/ServiceLoaderUtils.java | 27 ++++++++++++++++++++++
4 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/tika-bundles/tika-bundle-standard/src/test/java/org/apache/tika/bundle/BundleIT.java b/tika-bundles/tika-bundle-standard/src/test/java/org/apache/tika/bundle/BundleIT.java
index 0059558..1387a0e 100644
--- a/tika-bundles/tika-bundle-standard/src/test/java/org/apache/tika/bundle/BundleIT.java
+++ b/tika-bundles/tika-bundle-standard/src/test/java/org/apache/tika/bundle/BundleIT.java
@@ -305,7 +305,7 @@ public class BundleIT {
// Package extraction
ContentHandler handler = new BodyContentHandler();
- Parser parser = new AutoDetectParser(defaultParser);
+ Parser parser = new AutoDetectParser(contentTypeDetector, defaultParser);
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
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 644a8d1..61b19be 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
@@ -33,6 +33,7 @@ import java.util.Map;
import java.util.regex.Pattern;
import org.apache.tika.exception.TikaConfigException;
+import org.apache.tika.utils.ServiceLoaderUtils;
/**
* Internal utility class that Tika uses to look up service providers.
@@ -54,7 +55,7 @@ public class ServiceLoader {
* The default context class loader to use for all threads, or
* <code>null</code> to automatically select the context class loader.
*/
- private static volatile ClassLoader contextClassLoader = null;
+ private static volatile ClassLoader CONTEXT_CLASS_LOADER = null;
private final ClassLoader loader;
private final LoadErrorHandler handler;
private final InitializableProblemHandler initializableProblemHandler;
@@ -98,7 +99,7 @@ public class ServiceLoader {
* @see <a href="https://issues.apache.org/jira/browse/TIKA-441">TIKA-441</a>
*/
static ClassLoader getContextClassLoader() {
- ClassLoader loader = contextClassLoader;
+ ClassLoader loader = CONTEXT_CLASS_LOADER;
if (loader == null) {
loader = ServiceLoader.class.getClassLoader();
}
@@ -117,7 +118,7 @@ public class ServiceLoader {
* or <code>null</code> to automatically pick the loader
*/
public static void setContextClassLoader(ClassLoader loader) {
- contextClassLoader = loader;
+ CONTEXT_CLASS_LOADER = loader;
}
static void addService(Object reference, Object service, int rank) {
@@ -341,7 +342,7 @@ public class ServiceLoader {
}
}
if (!shouldExclude) {
- T instance = (T) klass.getConstructor().newInstance();
+ T instance = ServiceLoaderUtils.newInstance(klass, this);
if (instance instanceof Initializable) {
((Initializable) instance).initialize(Collections.EMPTY_MAP);
((Initializable) instance)
diff --git a/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java b/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
index d84a082..e076f1c 100644
--- a/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
+++ b/tika-core/src/main/java/org/apache/tika/config/TikaActivator.java
@@ -43,8 +43,6 @@ public class TikaActivator implements BundleActivator, ServiceTrackerCustomizer
private ServiceTracker parserTracker;
- private ServiceTracker zipDetectorTracker;
-
private BundleContext bundleContext;
//-----------------------------------------------------< BundleActivator >
@@ -53,17 +51,14 @@ public class TikaActivator implements BundleActivator, ServiceTrackerCustomizer
detectorTracker = new ServiceTracker(context, Detector.class.getName(), this);
parserTracker = new ServiceTracker(context, Parser.class.getName(), this);
- zipDetectorTracker = new ServiceTracker(context, "org.apache.tika.detect.zip.ZipContainerDetector", this);
detectorTracker.open();
parserTracker.open();
- zipDetectorTracker.open();
}
public void stop(BundleContext context) throws Exception {
parserTracker.close();
detectorTracker.close();
- zipDetectorTracker.close();
}
public Object addingService(ServiceReference reference) {
diff --git a/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java b/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
index 76afdb1..9e922c7 100644
--- a/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
+++ b/tika-core/src/main/java/org/apache/tika/utils/ServiceLoaderUtils.java
@@ -16,6 +16,8 @@
*/
package org.apache.tika.utils;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.tika.config.ServiceLoader;
@@ -58,4 +60,29 @@ public class ServiceLoaderUtils {
throw new RuntimeException(e);
}
}
+
+ /**
+ * Loads a class and instantiates it. If the class can be initialized
+ * with a ServiceLoader, the ServiceLoader constructor is used.
+ * Otherwise, a zero arg newInstance() is called.
+ *
+ * @param klass class to build
+ * @param loader service loader
+ * @param <T> service type
+ * @return instance of service
+ */
+ public static <T> T newInstance(Class klass, ServiceLoader loader) {
+ try {
+ try {
+ Constructor<T> constructor = klass.getDeclaredConstructor(ServiceLoader.class);
+ return constructor.newInstance(loader);
+ } catch (NoSuchMethodException e) {
+ return (T)klass.newInstance();
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
}