Class ConfigurationHolder<T>

java.lang.Object
net.blockhost.commons.config.ConfigurationHolder<T>
Type Parameters:
T - the configuration type

public abstract class ConfigurationHolder<T> extends Object

A holder for configuration instances that supports dependency injection and reloading.

This class wraps a configuration instance and provides a way to reload it on demand while maintaining a consistent reference for consumers. This is particularly useful when the configuration needs to be injected into other services that should always use the latest configuration without holding stale references.

Extending for Plugin-Specific Holders

Create a plugin-specific holder by extending this class:

public class MyPluginConfigHolder extends ConfigurationHolder<MyPluginConfig> {
    public MyPluginConfigHolder(Path configPath, ConfigMigrator migrator) {
        super(() -> migrator.migrateAndLoad(configPath, MyPluginConfig.class, MyPluginConfig.CURRENT_VERSION));
    }
}

Then inject the specific holder type:

public class MyService {
    private final MyPluginConfigHolder config;

    public MyService(MyPluginConfigHolder config) {
        this.config = config;
    }

    public void doSomething() {
        MyPluginConfig cfg = config.get();  // No type parameter needed
    }
}

Thread Safety

This class is thread-safe. The configuration reference is stored in an AtomicReference, ensuring visibility across threads after reload.

See Also:
  • Constructor Details

    • ConfigurationHolder

      protected ConfigurationHolder(Supplier<T> loader)
      Protected constructor for subclasses.
      Parameters:
      loader - the function that loads the configuration
    • ConfigurationHolder

      protected ConfigurationHolder(Supplier<T> loader, Path configPath, Class<T> configClass)
      Protected constructor for subclasses with save support.
      Parameters:
      loader - the function that loads the configuration
      configPath - the path to save the configuration to
      configClass - the configuration class for saving
  • Method Details

    • get

      public T get()

      Gets the current configuration instance.

      This method always returns the most recent configuration, even after a reload() call from another thread.

      Returns:
      the current configuration
    • reload

      public T reload()

      Reloads the configuration from disk.

      This method reloads the configuration using the loader provided during construction. After this call, subsequent calls to get() will return the new configuration.

      This operation is thread-safe.

      Returns:
      the newly loaded configuration
    • onReload

      public ConfigurationHolder<T> onReload(Consumer<T> callback)

      Sets a callback to be invoked after each reload.

      This is useful for performing post-reload actions like reinitializing services that depend on the configuration.

      Parameters:
      callback - the callback to invoke after reload
      Returns:
      this holder for chaining
    • save

      public void save()
      Saves the current configuration to disk. Requires the holder to be created with path and class information.
      Throws:
      IllegalStateException - if path or configClass was not set during construction
    • save

      public void save(Path path, Class<T> configClass)
      Saves the current configuration to disk.
      Parameters:
      path - the path to save to
      configClass - the configuration class
    • getConfigPath

      public @Nullable Path getConfigPath()
      Gets the path where this configuration is stored.
      Returns:
      the configuration path, or null if not set