Class ComponentNameResolver

java.lang.Object
org.apache.tika.serialization.ComponentNameResolver

public final class ComponentNameResolver extends Object
Utility class that resolves friendly component names to classes using ComponentRegistry.

Supports friendly names like "pdf-parser" as well as fully qualified class names. Registries must be registered via registerRegistry(String, ComponentRegistry) before use.

Also stores ComponentConfig registrations for top-level component loading.

  • Method Details

    • registerRegistry

      public static void registerRegistry(String indexName, ComponentRegistry registry)
      Registers a ComponentRegistry for name resolution.
      Parameters:
      indexName - the index file name (e.g., "parsers", "detectors")
      registry - the registry to register
    • resolveClass

      public static Class<?> resolveClass(String name, ClassLoader classLoader) throws ClassNotFoundException
      Resolves a friendly name or FQCN to a Class. Searches all registered component registries, falling back to Class.forName.
      Parameters:
      name - friendly name or fully qualified class name
      classLoader - the class loader to use for FQCN fallback
      Returns:
      the resolved class
      Throws:
      ClassNotFoundException - if not found in any registry and not a valid FQCN
    • getFriendlyName

      public static String getFriendlyName(Class<?> clazz)
      Gets the friendly name for a class, or null if not registered.
      Parameters:
      clazz - the class to look up
      Returns:
      the friendly name, or null if not found
    • hasComponent

      public static boolean hasComponent(String name)
      Checks if a component with the given name is registered in any registry.
      Parameters:
      name - the component name to check
      Returns:
      true if the component is registered
    • getComponentInfo

      public static Optional<ComponentInfo> getComponentInfo(String name)
      Gets the component info for a given friendly name.
      Parameters:
      name - the friendly name to look up
      Returns:
      Optional containing the ComponentInfo, or empty if not found
    • hasImplementationsOf

      public static boolean hasImplementationsOf(Class<?> abstractType)
      Checks if any registered component implements or extends the given abstract type.

      This is used by TikaModule to determine if an abstract type (interface or abstract class) should use compact component serialization.

      Parameters:
      abstractType - the abstract type to check
      Returns:
      true if at least one registered component is assignable to this type
    • registerComponentConfig

      public static <T> void registerComponentConfig(ComponentConfig<T> config)
      Registers a ComponentConfig for top-level component loading.
      Parameters:
      config - the component configuration
    • getComponentConfig

      public static ComponentConfig<?> getComponentConfig(String jsonField)
      Gets component configuration by JSON field name.
      Parameters:
      jsonField - the JSON field name (e.g., "parsers")
      Returns:
      the component config, or null if not registered
    • getComponentConfig

      public static <T> ComponentConfig<T> getComponentConfig(Class<T> componentClass)
      Gets component configuration by component class.
      Parameters:
      componentClass - the component class (e.g., Parser.class)
      Returns:
      the component config, or null if not registered
    • hasComponentConfig

      public static boolean hasComponentConfig(String jsonField)
      Checks if a component config is registered for the given JSON field.
    • hasComponentConfig

      public static boolean hasComponentConfig(Class<?> componentClass)
      Checks if a component config is registered for the given class.
    • getComponentFields

      public static Set<String> getComponentFields()
      Gets all registered component JSON field names.
    • getContextKeyInterfaces

      public static Set<Class<?>> getContextKeyInterfaces()
      Returns the set of interfaces that use compact format serialization.
      Returns:
      unmodifiable set of context key interfaces
    • findContextKeyInterface

      public static Class<?> findContextKeyInterface(Class<?> type)
      Finds the appropriate context key interface for a given type. This is used to determine which interface should be used as the ParseContext key when storing instances of this type.
      Parameters:
      type - the type to find the context key for
      Returns:
      the interface to use as context key, or null if none found
    • usesCompactFormat

      public static boolean usesCompactFormat(Class<?> type)
      Checks if a type should use compact format serialization. Returns true if the type implements any of the registered context key interfaces.
      Parameters:
      type - the type to check
      Returns:
      true if the type uses compact format
    • determineContextKey

      public static Class<?> determineContextKey(ComponentInfo info)
      Determines the ParseContext key for a component.

      Resolution order:

      1. Explicit contextKey from .idx file (via @TikaComponent annotation)
      2. Auto-detect from implemented interfaces (using CONTEXT_KEY_INTERFACES)
      3. Fall back to the component class itself
      Parameters:
      info - the component info
      Returns:
      the class to use as ParseContext key
    • getContextKey

      public static Class<?> getContextKey(Class<?> clazz)
      Gets the contextKey for a class from the component registry. The contextKey is recorded in the .idx file by the annotation processor.
      Parameters:
      clazz - the class to check
      Returns:
      the contextKey class if specified, or null if not registered or no contextKey