Class ConfigData

java.lang.Object
net.storm.api.plugins.config.ConfigData

public class ConfigData extends Object
Thread-safe storage for configuration data with persistent file backing.

ConfigData manages configuration properties in memory with support for incremental patching and atomic file operations. It handles concurrent access from multiple threads and supports safe merging of changes from multiple client instances.

The class uses a patch-based approach for saving:

  1. Changes are tracked incrementally
  2. On save, the file is loaded, patches are applied, and the result is saved atomically
  3. This prevents data loss when multiple clients edit the same file

See Also:
  • Constructor Details

    • ConfigData

      public ConfigData(File configPath)
      Creates a new ConfigData instance and loads existing data from the file.

      If the file doesn't exist, an empty configuration is created.

      Parameters:
      configPath - the path to the configuration file
      Throws:
      RuntimeException - if the file exists but cannot be read
  • Method Details

    • getProperty

      public String getProperty(String key)
      Gets a configuration property value.
      Parameters:
      key - the property key
      Returns:
      the property value, or null if not set
    • setProperty

      public String setProperty(String key, String value)
      Sets a configuration property value.

      The change is tracked for later patching to the file.

      Parameters:
      key - the property key
      value - the value to set
      Returns:
      the previous value, or null if the key was not set
    • unset

      public String unset(String key)
      Removes a configuration property.

      The removal is tracked for later patching to the file.

      Parameters:
      key - the property key to remove
      Returns:
      the previous value, or null if the key was not set
    • putAll

      public void putAll(Map<String,String> values)
      Sets multiple configuration properties at once.

      All changes are tracked for later patching to the file.

      Parameters:
      values - the map of key-value pairs to set
    • keySet

      public Set<String> keySet()
      Gets all configuration keys.
      Returns:
      a set of all keys in the configuration
    • get

      public Map<String,String> get()
      Gets an unmodifiable view of all configuration properties.
      Returns:
      an unmodifiable map of all key-value pairs
    • swapChanges

      public Map<String,String> swapChanges()
      Atomically retrieves and clears pending changes.

      This method is used before saving to get the list of changes that need to be written to the file.

      Returns:
      a map of pending changes (value is null for deletions), or an empty map if no changes are pending
    • patch

      public void patch(Map<String,String> patch)
      Applies a patch of changes to the configuration file.

      This method performs a safe merge operation:

      1. Acquires a file lock
      2. Loads the current file contents
      3. Applies the patch changes
      4. Saves the result atomically
      This ensures multiple clients can safely edit the same configuration.

      Parameters:
      patch - the changes to apply (null values indicate deletion)