Class ComponentInstantiator

java.lang.Object
org.apache.tika.config.loader.ComponentInstantiator

public class ComponentInstantiator extends Object
Utility class for instantiating Tika components from JSON configuration. Provides common logic for all component loaders to avoid code duplication.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    hasConfiguration(JsonConfig jsonConfig, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Checks if the JsonConfig contains actual configuration (non-empty JSON object with fields).
    static <T> T
    instantiate(Class<?> componentClass, com.fasterxml.jackson.databind.JsonNode configNode, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Instantiates a component from a JsonNode configuration.
    static <T> T
    instantiate(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> T
    instantiate(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> T
    instantiateComponent(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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:

      1. Try constructor with JsonConfig parameter
      2. If not found and JSON config has actual configuration, throw error
      3. Otherwise fall back to zero-arg constructor via ServiceLoader
      Type Parameters:
      T - the component type
      Parameters:
      componentClass - the component class to instantiate
      jsonConfig - the JSON configuration for the component
      classLoader - the class loader to use
      componentTypeName - the component type name (e.g., "Detector", "Parser") for error messages
      objectMapper - 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:

      1. Try constructor with JsonConfig parameter
      2. Fall back to Jackson bean deserialization if config is provided
      3. Fall back to zero-arg constructor if no config
      Type Parameters:
      T - the component type
      Parameters:
      componentClass - the component class to instantiate
      configNode - 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 deserialization
      classLoader - 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:

      Parameters:
      typeName - the component type name (friendly name or FQCN)
      configNode - the JSON configuration node (may be null)
      mapper - the ObjectMapper for deserialization
      classLoader - the class loader for name resolution
      expectedType - the expected interface/base type (for validation), or null to skip
      Returns:
      the instantiated component
      Throws:
      TikaConfigException - if instantiation fails
    • 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 configuration
      objectMapper - the Jackson ObjectMapper for parsing JSON
      Returns:
      true if there's meaningful configuration, false if empty or just "{}"