java.lang.Object
io.helidon.service.registry.Services

public final class Services extends Object
Static access to the service registry.

Note: Using any methods on this class makes the service registry throw away optimization created via the Helidon Service Registry Maven Plugin (code generated binding) for services that use the contracts configured.

There is always a single instance of a registry available within an application that can be access through methods on this class.

This instance can be explicitly configured using registry(ServiceRegistry) in case you want to setup a registry instance yourself.

We support "late binding" of services using this class, such as by using set(Class, Object[]). These methods allow setting an explicit instance for a service normally built by the registry.

The set methods have the following limitations:

  • The contract parameter must be of one of the supported contracts by the service registry
  • The method MUST be called before the contract is used by any other service, to achieve consistency (i.e. calling ServiceRegistry.get(Class) must yield the same instance if the service is a singleton
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    add(ServiceDescriptor<?> descriptor)
    Add a custom service descriptor.
    static <T> void
    add(Class<T> contract, double weight, T instance)
    Add an explicit instance for the specified service contract.
    static <T> List<T>
    all(TypeName contract)
    Get all instances of the contract.
    static <T> List<T>
    all(Class<T> contract)
    Get all instances of the contract.
    static <T> Optional<T>
    first(TypeName contract)
    Get first instance of the contract from the registry, all an empty optional if none exist.
    static <T> Optional<T>
    first(Class<T> contract)
    Get first instance of the contract from the registry, all an empty optional if none exist.
    static <T> T
    get(TypeName contract)
    Get the first instance of the contract, expecting the contract is available.
    static <T> T
    get(Class<T> contract)
    Get the first instance of the contract, expecting the contract is available.
    static void
    Configure the application wide registry to be used by components that require static lookup of required services.
    static <T> void
    set(Class<T> contract, T... instances)
    Configure an explicit instance (explicit instances) for the specified service contract.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • registry

      public static void registry(ServiceRegistry registry)
      Configure the application wide registry to be used by components that require static lookup of required services.

      Note that this method MUST be called as one of the first things in your application, as all Helidon components that use other services require it (except for Helidon Logging and Common Config).

      Parameters:
      registry - registry instance to use
      Throws:
      NullPointerException - in case the registry is null
    • set

      @SafeVarargs public static <T> void set(Class<T> contract, T... instances)
      Configure an explicit instance (explicit instances) for the specified service contract. This method replaces all existing service providers and the registry will only use the provided instances.

      This method must be called before the contract is requested from the registry for the first time.

      This method only accepts contracts that are provided by one of the services in this registry.

      This method will only work if the underlying implementation of the service registry is provided by Helidon (i.e. it will not work on mocked types, and custom implementations).

      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to bind the instance under
      instances - instances to use
      Throws:
      ServiceRegistryException - in case the service contract was already used and cannot be re-bound
      NullPointerException - if either of the parameters is null
    • add

      public static <T> void add(Class<T> contract, double weight, T instance)
      Add an explicit instance for the specified service contract.

      This method has similar contract to set(Class, Object[]) except it adds the implementation, where the set method replaces all implementations.

      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to bind the instance under
      weight - weight of the instance (use Weighted.DEFAULT_WEIGHT for default)
      instance - instance to add
      Throws:
      ServiceRegistryException - in case the service contract was already used and cannot be re-bound
      NullPointerException - if either of the parameters is null
    • add

      public static void add(ServiceDescriptor<?> descriptor)
      Add a custom service descriptor.

      Note: it is recommended to use ServiceRegistryConfig.BuilderBase.addServiceDescriptor(ServiceDescriptor) as that service descriptor will be available when constructing the service registry. This method is only to be used when the service registry must be created prior to configuring the binding.

      Parameters:
      descriptor - descriptor to "late bind" to the service registry
      Throws:
      ServiceRegistryException - in case the service contract was already used and cannot be re-bound
      NullPointerException - if the parameter is null
    • get

      public static <T> T get(Class<T> contract)
      Get the first instance of the contract, expecting the contract is available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to find
      Returns:
      an instance of the contract, never null
      Throws:
      ServiceRegistryException - in case the contract is not available in the registry
      NullPointerException - if either of the parameters is null
      See Also:
    • get

      public static <T> T get(TypeName contract)
      Get the first instance of the contract, expecting the contract is available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to find
      Returns:
      an instance of the contract, never null
      Throws:
      ServiceRegistryException - in case the contract is not available in the registry
      NullPointerException - if either of the parameters is null
      See Also:
    • all

      public static <T> List<T> all(Class<T> contract)
      Get all instances of the contract.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to find
      Returns:
      all instances in the registry, may be empty
      Throws:
      NullPointerException - if either of the parameters is null
    • all

      public static <T> List<T> all(TypeName contract)
      Get all instances of the contract.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to find
      Returns:
      all instances in the registry, may be empty
      Throws:
      NullPointerException - if either of the parameters is null
    • first

      public static <T> Optional<T> first(Class<T> contract)
      Get first instance of the contract from the registry, all an empty optional if none exist.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to find
      Returns:
      first instance, or an empty optional
      Throws:
      NullPointerException - if either of the parameters is null
    • first

      public static <T> Optional<T> first(TypeName contract)
      Get first instance of the contract from the registry, all an empty optional if none exist.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to find
      Returns:
      first instance, or an empty optional
      Throws:
      NullPointerException - if either of the parameters is null