Class ConfigLoader

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

public class ConfigLoader extends Object
Loader for configuration objects from the "parse-context" section.

This class handles ParseContext components and configuration POJOs that are loaded into a ParseContext for use during parsing. All configurations loaded via ConfigLoader must be placed under the "parse-context" top-level node in the JSON.

For official Tika components and configurations (parsers, detectors, async, server, etc.), use the specific methods on TikaLoader or load directly from TikaJsonConfig.

Usage:

 TikaLoader loader = TikaLoader.load(configPath);

 // Load by explicit key
 MyConfig config = loader.configs().load("my-config", MyConfig.class);

 // Load by class name (auto-converts to kebab-case)
 MyConfig config = loader.configs().load(MyConfig.class);
 

JSON configuration example:

 {
   // Official Tika configs at root level (NOT loaded via configs())
   "parsers": [...],
   "detectors": [...],
   "pipes": {...},
   "server": {...},

   // ParseContext configs in "parse-context" (loaded via configs())
   "parse-context": {
     "embedded-limits": {
       "maxDepth": 10,
       "maxCount": 1000
     },
     "output-limits": {
       "writeLimit": 100000
     },
     "commons-digester-factory": {
       "algorithms": ["MD5", "SHA-256"]
     }
   }
 }
 
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if a configuration key exists in the JSON config.
    <T> T
    load(Class<T> clazz)
    Loads a configuration object using the class name converted to kebab-case.
    <T> T
    load(Class<T> clazz, T defaultValue)
    Loads a configuration object using the class name, with a default value.
    <T> T
    load(String key, Class<T> clazz)
    Loads a configuration object from the specified JSON key.
    <T> T
    load(String key, Class<T> clazz, T defaultValue)
    Loads a configuration object from the specified JSON key, with a default value.
    <T> T
    loadWithDefaults(Class<T> clazz, T defaultValue)
    Loads a configuration object by class name with defaults, merging JSON properties.
    <T> T
    loadWithDefaults(String key, Class<T> clazz, T defaultValue)
    Loads a configuration object by merging JSON properties into a copy of the default instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • load

      public <T> T load(Class<T> clazz) throws TikaConfigException
      Loads a configuration object using the class name converted to kebab-case.

      For example, MyAppConfig.class will look for key "my-app-config". Class name suffixes like "Config", "Configuration", "Settings" are stripped first.

      For interfaces, the JSON must specify the implementation (see load(String, Class)).

      Type Parameters:
      T - The type to load
      Parameters:
      clazz - The class to deserialize into (can be interface, abstract, or concrete)
      Returns:
      the deserialized object, or null if key not found in config
      Throws:
      TikaConfigException - if loading fails or class is not instantiable
    • load

      public <T> T load(Class<T> clazz, T defaultValue) throws TikaConfigException
      Loads a configuration object using the class name, with a default value.
      Type Parameters:
      T - The type to load
      Parameters:
      clazz - The class to deserialize into
      defaultValue - The value to return if key not found in config
      Returns:
      the deserialized object, or defaultValue if not present
      Throws:
      TikaConfigException - if loading fails or class is not instantiable
    • load

      public <T> T load(String key, Class<T> clazz) throws TikaConfigException
      Loads a configuration object from the specified JSON key.

      Supports two formats:

      • String value: treated as fully qualified class name to instantiate
      • Object: deserialized directly into the target class

      For tier-1 polymorphic types (Parser, Detector, MetadataFilter), use the wrapper object format with friendly names: {"pdf-parser": {...}}

      Type Parameters:
      T - The type to load
      Parameters:
      key - The JSON key to load from
      clazz - The class to deserialize into (can be interface, abstract, or concrete)
      Returns:
      the deserialized object, or null if key not found
      Throws:
      TikaConfigException - if loading fails or class cannot be instantiated
    • load

      public <T> T load(String key, Class<T> clazz, T defaultValue) throws TikaConfigException
      Loads a configuration object from the specified JSON key, with a default value.
      Type Parameters:
      T - The type to load
      Parameters:
      key - The JSON key to load from
      clazz - The class to deserialize into
      defaultValue - The value to return if key not found in config
      Returns:
      the deserialized object, or defaultValue if not present
      Throws:
      TikaConfigException - if loading fails or class is not instantiable
    • loadWithDefaults

      public <T> T loadWithDefaults(String key, Class<T> clazz, T defaultValue) throws TikaConfigException
      Loads a configuration object by merging JSON properties into a copy of the default instance.

      This allows partial configuration where only some properties are specified in JSON, and the rest retain their default values. The original defaultValue object is NOT modified.

      Example:

       MyConfig defaults = new MyConfig();
       defaults.setTimeout(30000);
       defaults.setRetries(2);
       defaults.setEnabled(false);
      
       // JSON: { "enabled": true }
       // Result: timeout=30000, retries=2, enabled=true (merged!)
       // Note: 'defaults' object remains unchanged
       MyConfig config = loader.configs().loadWithDefaults("my-config",
                                                            MyConfig.class,
                                                            defaults);
       
      Type Parameters:
      T - The type to load
      Parameters:
      key - The JSON key to load from
      clazz - The class type (not used for deserialization, but for type safety)
      defaultValue - The object with default values (will NOT be modified)
      Returns:
      a new object with defaults merged with JSON properties, or the original default if key not found
      Throws:
      TikaConfigException - if loading fails
    • loadWithDefaults

      public <T> T loadWithDefaults(Class<T> clazz, T defaultValue) throws TikaConfigException
      Loads a configuration object by class name with defaults, merging JSON properties.
      Type Parameters:
      T - The type to load
      Parameters:
      clazz - The class to deserialize into
      defaultValue - The object with default values to merge into
      Returns:
      the default object updated with JSON properties, or the original default if key not found
      Throws:
      TikaConfigException - if loading fails
    • hasKey

      public boolean hasKey(String key)
      Checks if a configuration key exists in the JSON config.
      Parameters:
      key - The JSON key to check
      Returns:
      true if the key exists and is not null