Package org.apache.tika.config.loader
Class ComponentInstantiator
java.lang.Object
org.apache.tika.config.loader.ComponentInstantiator
Utility class for instantiating Tika components from JSON configuration.
Provides common logic for all component loaders to avoid code duplication.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanhasConfiguration(JsonConfig jsonConfig, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Checks if the JsonConfig contains actual configuration (non-empty JSON object with fields).static <T> Tinstantiate(Class<?> componentClass, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Instantiates a component from a JsonNode configuration.static <T> Tinstantiate(Class<?> componentClass, JsonConfig jsonConfig, ClassLoader classLoader, String componentTypeName, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Instantiates a component with JsonConfig constructor or falls back to zero-arg constructor.static <T> Tinstantiate(String typeName, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper objectMapper, ClassLoader classLoader) Instantiates a component by resolving a friendly name or FQCN to a class.static <T> TinstantiateComponent(String typeName, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper mapper, ClassLoader classLoader, Class<?> expectedType) Instantiates a Tika component with full special-case handling.
-
Constructor Details
-
ComponentInstantiator
public ComponentInstantiator()
-
-
Method Details
-
instantiate
public static <T> T instantiate(Class<?> componentClass, JsonConfig jsonConfig, ClassLoader classLoader, String componentTypeName, com.fasterxml.jackson.databind.ObjectMapper objectMapper) throws TikaConfigException Instantiates a component with JsonConfig constructor or falls back to zero-arg constructor.Instantiation strategy:
- Try constructor with JsonConfig parameter
- If not found and JSON config has actual configuration, throw error
- Otherwise fall back to zero-arg constructor via ServiceLoader
- Type Parameters:
T- the component type- Parameters:
componentClass- the component class to instantiatejsonConfig- the JSON configuration for the componentclassLoader- the class loader to usecomponentTypeName- the component type name (e.g., "Detector", "Parser") for error messagesobjectMapper- the Jackson ObjectMapper for parsing JSON- Returns:
- the instantiated component
- Throws:
TikaConfigException- if instantiation fails
-
instantiate
public static <T> T instantiate(Class<?> componentClass, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper objectMapper) throws TikaConfigException Instantiates a component from a JsonNode configuration.Instantiation strategy:
- Try constructor with JsonConfig parameter
- Fall back to Jackson bean deserialization if config is provided
- Fall back to zero-arg constructor if no config
- Type Parameters:
T- the component type- Parameters:
componentClass- the component class to instantiateconfigNode- the JSON configuration node (may be null or empty)objectMapper- the Jackson ObjectMapper for deserialization- Returns:
- the instantiated component
- Throws:
TikaConfigException- if instantiation fails
-
instantiate
public static <T> T instantiate(String typeName, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper objectMapper, ClassLoader classLoader) throws TikaConfigException Instantiates a component by resolving a friendly name or FQCN to a class.This is a convenience method that combines name resolution with instantiation.
- Type Parameters:
T- the component type- Parameters:
typeName- the component type name (friendly name like "pdf-parser" or FQCN)configNode- the JSON configuration node (may be null or empty)objectMapper- the Jackson ObjectMapper for deserializationclassLoader- the class loader for name resolution- Returns:
- the instantiated component
- Throws:
TikaConfigException- if instantiation fails or type name is unknown
-
instantiateComponent
public static <T> T instantiateComponent(String typeName, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper mapper, ClassLoader classLoader, Class<?> expectedType) throws TikaConfigException Instantiates a Tika component with full special-case handling.This is the primary entry point for component instantiation from JSON configuration. Handles:
- Type resolution via
ComponentNameResolver.resolveClass(java.lang.String, java.lang.ClassLoader) - Type compatibility validation against expectedType
- Special cases: DefaultParser/DefaultDetector rejection, MimeTypes singleton
_mime-include/_mime-excludeextraction and stripping- Three-step instantiation: JsonConfig ctor → readerForUpdating → no-arg
Initializable.initialize()callback- Parser MIME filter wrapping
- Parameters:
typeName- the component type name (friendly name or FQCN)configNode- the JSON configuration node (may be null)mapper- the ObjectMapper for deserializationclassLoader- the class loader for name resolutionexpectedType- the expected interface/base type (for validation), or null to skip- Returns:
- the instantiated component
- Throws:
TikaConfigException- if instantiation fails
- Type resolution via
-
hasConfiguration
public static boolean hasConfiguration(JsonConfig jsonConfig, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Checks if the JsonConfig contains actual configuration (non-empty JSON object with fields).- Parameters:
jsonConfig- the JSON configurationobjectMapper- the Jackson ObjectMapper for parsing JSON- Returns:
- true if there's meaningful configuration, false if empty or just "{}"
-