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 voidassertRequestMayConfigure(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, or config naming aninvalid reference
OperatorOnly@OperatorOnlyproperty.static booleanhasJsonConfigConstructor(Class<?> clazz) True ifclazzbinds its config through a public(JsonConfig)constructor.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(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, boolean trusted) Instantiates a Tika component with full special-case handling.static booleanisTrusted(com.fasterxml.jackson.databind.DeserializationContext ctxt) False ifctxtis reading per-request JSON (seewithTrust(com.fasterxml.jackson.databind.ObjectReader, boolean)); true otherwise.static StringThe innermost message, which carries a refusal's reason through the wrapping layers.static com.fasterxml.jackson.databind.ObjectReaderwithTrust(com.fasterxml.jackson.databind.ObjectReader reader, boolean trusted) Carries the request's trust into the components a config nests: Jackson builds them throughTikaModule, which readsUNTRUSTEDback.
-
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:
- 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, 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:
- 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 skiptrusted- false for per-request JSON: a request-blocked or@OperatorOnlyclass, or an@OperatorOnlyproperty, is refused (seeassertRequestMayConfigure(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
- Type resolution via
-
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, or config naming aninvalid reference
OperatorOnly@OperatorOnlyproperty. The class check runs before construction, since constructing a component can have side effects. AJsonConfig-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 throughTikaModule, which readsUNTRUSTEDback. -
isTrusted
public static boolean isTrusted(com.fasterxml.jackson.databind.DeserializationContext ctxt) False ifctxtis reading per-request JSON (seewithTrust(com.fasterxml.jackson.databind.ObjectReader, boolean)); true otherwise. -
reason
The innermost message, which carries a refusal's reason through the wrapping layers. -
hasJsonConfigConstructor
True ifclazzbinds its config through a public(JsonConfig)constructor.
-