Class OpenFeatureAPI

java.lang.Object
dev.openfeature.sdk.OpenFeatureAPI
All Implemented Interfaces:
EventBus<OpenFeatureAPI>

public class OpenFeatureAPI extends Object implements EventBus<OpenFeatureAPI>
A global singleton which holds base configuration for the OpenFeature library. Configuration here will be shared across all Clients.
  • Constructor Details

    • OpenFeatureAPI

      protected OpenFeatureAPI()
  • Method Details

    • getInstance

      public static OpenFeatureAPI getInstance()
      Provisions the OpenFeatureAPI singleton (if needed) and returns it.
      Returns:
      The singleton instance.
    • getProviderMetadata

      public Metadata getProviderMetadata()
      Get metadata about the default provider.
      Returns:
      the provider metadata
    • getProviderMetadata

      public Metadata getProviderMetadata(String domain)
      Get metadata about a registered provider using the client name. An unbound or empty client name will return metadata from the default provider.
      Parameters:
      domain - an identifier which logically binds clients with providers
      Returns:
      the provider metadata
    • getClient

      public Client getClient()
      A factory function for creating new, OpenFeature client. Clients can contain their own state (e.g. logger, hook, context). Multiple clients can be used to segment feature flag configuration. All un-named or unbound clients use the default provider.
      Returns:
      a new client instance
    • getClient

      public Client getClient(String domain)
      A factory function for creating new domainless OpenFeature client. Clients can contain their own state (e.g. logger, hook, context). Multiple clients can be used to segment feature flag configuration. If there is already a provider bound to this domain, this provider will be used. Otherwise, the default provider is used until a provider is assigned to that domain.
      Parameters:
      domain - an identifier which logically binds clients with providers
      Returns:
      a new client instance
    • getClient

      public Client getClient(String domain, String version)
      A factory function for creating new domainless OpenFeature client. Clients can contain their own state (e.g. logger, hook, context). Multiple clients can be used to segment feature flag configuration. If there is already a provider bound to this domain, this provider will be used. Otherwise, the default provider is used until a provider is assigned to that domain.
      Parameters:
      domain - a identifier which logically binds clients with providers
      version - a version identifier
      Returns:
      a new client instance
    • setEvaluationContext

      public OpenFeatureAPI setEvaluationContext(EvaluationContext evaluationContext)
      Sets the global evaluation context, which will be used for all evaluations.
      Parameters:
      evaluationContext - the context
      Returns:
      api instance
    • getEvaluationContext

      public EvaluationContext getEvaluationContext()
      Gets the global evaluation context, which will be used for all evaluations.
      Returns:
      evaluation context
    • getTransactionContextPropagator

      public TransactionContextPropagator getTransactionContextPropagator()
      Return the transaction context propagator.
    • setTransactionContextPropagator

      public void setTransactionContextPropagator(TransactionContextPropagator transactionContextPropagator)
      Sets the transaction context propagator.
      Throws:
      IllegalArgumentException - if transactionContextPropagator is null
    • setTransactionContext

      public void setTransactionContext(EvaluationContext evaluationContext)
      Sets the transaction context using the registered transaction context propagator.
    • setProvider

      public void setProvider(FeatureProvider provider)
      Set the default provider.
    • setProvider

      public void setProvider(String domain, FeatureProvider provider)
      Add a provider for a domain.
      Parameters:
      domain - The domain to bind the provider to.
      provider - The provider to set.
    • setProviderAndWait

      public void setProviderAndWait(FeatureProvider provider) throws OpenFeatureError
      Sets the default provider and waits for its initialization to complete.

      Note: If the provider fails during initialization, an OpenFeatureError will be thrown. It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.

      Parameters:
      provider - the FeatureProvider to set as the default.
      Throws:
      OpenFeatureError - if the provider fails during initialization.
    • setProviderAndWait

      public void setProviderAndWait(String domain, FeatureProvider provider) throws OpenFeatureError
      Add a provider for a domain and wait for initialization to finish.

      Note: If the provider fails during initialization, an OpenFeatureError will be thrown. It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.

      Parameters:
      domain - The domain to bind the provider to.
      provider - The provider to set.
      Throws:
      OpenFeatureError - if the provider fails during initialization.
    • getProvider

      public FeatureProvider getProvider()
      Return the default provider.
    • getProvider

      public FeatureProvider getProvider(String domain)
      Fetch a provider for a domain. If not found, return the default.
      Parameters:
      domain - The domain to look for.
      Returns:
      A named FeatureProvider
    • addHooks

      public void addHooks(Hook... hooks)
      Adds hooks for globally, used for all evaluations. Hooks are run in the order they're added in the before stage. They are run in reverse order for all other stages.
      Parameters:
      hooks - The hook to add.
    • getHooks

      public List<Hook> getHooks()
      Fetch the hooks associated to this client.
      Returns:
      A list of Hooks.
    • clearHooks

      public void clearHooks()
      Removes all hooks.
    • shutdown

      public void shutdown()
      Shut down and reset the current status of OpenFeature API. This call cleans up all active providers and attempts to shut down internal event handling mechanisms. Once shut down is complete, API is reset and ready to use again.
    • onProviderReady

      public OpenFeatureAPI onProviderReady(Consumer<EventDetails> handler)
      Add a handler for the ProviderEvent.PROVIDER_READY event. Shorthand for EventBus.on(ProviderEvent, Consumer)
      Specified by:
      onProviderReady in interface EventBus<OpenFeatureAPI>
      Parameters:
      handler - behavior to add with this event
      Returns:
      this
    • onProviderConfigurationChanged

      public OpenFeatureAPI onProviderConfigurationChanged(Consumer<EventDetails> handler)
      Specified by:
      onProviderConfigurationChanged in interface EventBus<OpenFeatureAPI>
      Parameters:
      handler - behavior to add with this event
      Returns:
      this
    • onProviderStale

      public OpenFeatureAPI onProviderStale(Consumer<EventDetails> handler)
      Add a handler for the ProviderEvent.PROVIDER_ERROR event. Shorthand for EventBus.on(ProviderEvent, Consumer)
      Specified by:
      onProviderStale in interface EventBus<OpenFeatureAPI>
      Parameters:
      handler - behavior to add with this event
      Returns:
      this
    • onProviderError

      public OpenFeatureAPI onProviderError(Consumer<EventDetails> handler)
      Add a handler for the ProviderEvent.PROVIDER_STALE event. Shorthand for EventBus.on(ProviderEvent, Consumer)
      Specified by:
      onProviderError in interface EventBus<OpenFeatureAPI>
      Parameters:
      handler - behavior to add with this event
      Returns:
      this
    • on

      public OpenFeatureAPI on(ProviderEvent event, Consumer<EventDetails> handler)
      Add a handler for the specified ProviderEvent.
      Specified by:
      on in interface EventBus<OpenFeatureAPI>
      Parameters:
      event - event type
      handler - behavior to add with this event
      Returns:
      this
    • removeHandler

      public OpenFeatureAPI removeHandler(ProviderEvent event, Consumer<EventDetails> handler)
      Remove the previously attached handler by reference. If the handler doesn't exists, no-op.
      Specified by:
      removeHandler in interface EventBus<OpenFeatureAPI>
      Parameters:
      event - event type
      handler - to be removed
      Returns:
      this