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 void
    assertRequestMayConfigure(String typeName, Class<?> clazz, com.fasterxml.jackson.databind.JsonNode config, com.fasterxml.jackson.databind.ObjectMapper mapper)
    Refuses what per-request JSON may not build: a class of a request-blocked type, a class marked
    invalid reference
    OperatorOnly
    , or config naming an @OperatorOnly property.
    static boolean
    True if clazz binds its config through a public (JsonConfig) constructor.
    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(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, boolean trusted)
    Instantiates a Tika component with full special-case handling.
    static boolean
    isTrusted(com.fasterxml.jackson.databind.DeserializationContext ctxt)
    False if ctxt is reading per-request JSON (see withTrust(com.fasterxml.jackson.databind.ObjectReader, boolean)); true otherwise.
    static String
    The innermost message, which carries a refusal's reason through the wrapping layers.
    static com.fasterxml.jackson.databind.ObjectReader
    withTrust(com.fasterxml.jackson.databind.ObjectReader reader, boolean trusted)
    Carries the request's trust into the components a config nests: Jackson builds them through TikaModule, which reads UNTRUSTED back.

    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, 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, boolean trusted) 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
      trusted - false for per-request JSON: a request-blocked or @OperatorOnly class, or an @OperatorOnly property, is refused (see assertRequestMayConfigure(java.lang.String, java.lang.Class<?>, com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.ObjectMapper)), here and in every component the config nests
      Returns:
      the instantiated component
      Throws:
      TikaConfigException - if instantiation fails
    • assertRequestMayConfigure

      public static void assertRequestMayConfigure(String typeName, Class<?> clazz, com.fasterxml.jackson.databind.JsonNode config, com.fasterxml.jackson.databind.ObjectMapper mapper) throws TikaConfigException
      Refuses what per-request JSON may not build: a class of a request-blocked type, a class marked
      invalid reference
      OperatorOnly
      , or config naming an @OperatorOnly property. The class check runs before construction, since constructing a component can have side effects. A JsonConfig-constructor component's properties belong to its config class, so they are checked when the constructor reads its config, before that config object exists.
      Throws:
      TikaConfigException
    • withTrust

      public static com.fasterxml.jackson.databind.ObjectReader withTrust(com.fasterxml.jackson.databind.ObjectReader reader, boolean trusted)
      Carries the request's trust into the components a config nests: Jackson builds them through TikaModule, which reads UNTRUSTED back.
    • isTrusted

      public static boolean isTrusted(com.fasterxml.jackson.databind.DeserializationContext ctxt)
      False if ctxt is reading per-request JSON (see withTrust(com.fasterxml.jackson.databind.ObjectReader, boolean)); true otherwise.
    • reason

      public static String reason(Throwable t)
      The innermost message, which carries a refusal's reason through the wrapping layers.
    • hasJsonConfigConstructor

      public static boolean hasJsonConfigConstructor(Class<?> clazz)
      True if clazz binds its config through a public (JsonConfig) constructor.