Class FileDataSourceBuilder

java.lang.Object
com.launchdarkly.sdk.server.integrations.FileDataSourceBuilder
All Implemented Interfaces:
DataSourceFactory

public final class FileDataSourceBuilder
extends java.lang.Object
implements DataSourceFactory
To use the file data source, obtain a new instance of this class with FileData.dataSource(); call the builder method filePaths(String...) to specify file path(s), and/or classpathResources(String...) to specify classpath data resources; then pass the resulting object to LDConfig.Builder.dataSource(DataSourceFactory).

For more details, see FileData.

Since:
4.12.0
  • Constructor Details

  • Method Details

    • filePaths

      public FileDataSourceBuilder filePaths​(java.lang.String... filePaths) throws java.nio.file.InvalidPathException
      Adds any number of source files for loading flag data, specifying each file path as a string. The files will not actually be loaded until the LaunchDarkly client starts up.

      Files will be parsed as JSON if their first non-whitespace character is '{'. Otherwise, they will be parsed as YAML.

      Parameters:
      filePaths - path(s) to the source file(s); may be absolute or relative to the current working directory
      Returns:
      the same factory object
      Throws:
      java.nio.file.InvalidPathException - if one of the parameters is not a valid file path
    • filePaths

      public FileDataSourceBuilder filePaths​(java.nio.file.Path... filePaths)
      Adds any number of source files for loading flag data, specifying each file path as a Path. The files will not actually be loaded until the LaunchDarkly client starts up.

      Files will be parsed as JSON if their first non-whitespace character is '{'. Otherwise, they will be parsed as YAML.

      Parameters:
      filePaths - path(s) to the source file(s); may be absolute or relative to the current working directory
      Returns:
      the same factory object
    • classpathResources

      public FileDataSourceBuilder classpathResources​(java.lang.String... resourceLocations)
      Adds any number of classpath resources for loading flag data. The resources will not actually be loaded until the LaunchDarkly client starts up.

      Files will be parsed as JSON if their first non-whitespace character is '{'. Otherwise, they will be parsed as YAML.

      Parameters:
      resourceLocations - resource location(s) in the format used by ClassLoader.getResource(); these are absolute paths, so for instance a resource called "data.json" in the package "com.mypackage" would have the location "/com/mypackage/data.json"
      Returns:
      the same factory object
    • autoUpdate

      public FileDataSourceBuilder autoUpdate​(boolean autoUpdate)
      Specifies whether the data source should watch for changes to the source file(s) and reload flags whenever there is a change. By default, it will not, so the flags will only be loaded once. This feature only works with real files, not with classpathResources(String...).

      Note that auto-updating will only work if all of the files you specified have valid directory paths at startup time; if a directory does not exist, creating it later will not result in files being loaded from it.

      The performance of this feature depends on what implementation of java.nio.file.WatchService is available in the Java runtime. On Linux and Windows, an implementation based on native filesystem APIs should be available. On MacOS, there is a long-standing known issue where due to the lack of such an implementation, it must use a file polling approach that can take up to 10 seconds to detect a change.

      Parameters:
      autoUpdate - true if flags should be reloaded whenever a source file changes
      Returns:
      the same factory object
    • duplicateKeysHandling

      public FileDataSourceBuilder duplicateKeysHandling​(FileData.DuplicateKeysHandling duplicateKeysHandling)
      Specifies how to handle keys that are duplicated across files.

      By default, data loading will fail if keys are duplicated across files (FileData.DuplicateKeysHandling.FAIL).

      Parameters:
      duplicateKeysHandling - specifies how to handle duplicate keys
      Returns:
      the same factory object
      Since:
      5.3.0
    • createDataSource

      public DataSource createDataSource​(ClientContext context, DataSourceUpdates dataSourceUpdates)
      Used internally by the LaunchDarkly client.
      Specified by:
      createDataSource in interface DataSourceFactory
      Parameters:
      context - allows access to the client configuration
      dataSourceUpdates - the component pushes data into the SDK via this interface
      Returns:
      an DataSource