Class ConfigurationHolder<T>
- Type Parameters:
T- the configuration type
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 Summary
ConstructorsModifierConstructorDescriptionprotectedConfigurationHolder(Supplier<T> loader) Protected constructor for subclasses.protectedProtected constructor for subclasses with save support. -
Method Summary
Modifier and TypeMethodDescriptionget()Gets the current configuration instance.@Nullable PathGets the path where this configuration is stored.Sets a callback to be invoked after each reload.reload()Reloads the configuration from disk.voidsave()Saves the current configuration to disk.voidSaves the current configuration to disk.
-
Constructor Details
-
ConfigurationHolder
-
ConfigurationHolder
Protected constructor for subclasses with save support.- Parameters:
loader- the function that loads the configurationconfigPath- the path to save the configuration toconfigClass- the configuration class for saving
-
-
Method Details
-
get
-
reload
-
onReload
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
-
getConfigPath
Gets the path where this configuration is stored.- Returns:
- the configuration path, or null if not set
-