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(TypeName contract, Qualifier... qualifiers)
    Get all service instances matching the contract with the expectation that there may not be a match available.
    static <T> List<T>
    all(Class<T> contract)
    Get all instances of the contract.
    static <T> List<T>
    all(Class<T> contract, Qualifier... qualifiers)
    Get all service instances matching the contract with the expectation that there may not be a match available.
    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(TypeName contract, Qualifier... qualifiers)
    Get the first service instance matching the contract with the expectation that there may not be a match available.
    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> Optional<T>
    first(Class<T> contract, Qualifier... qualifiers)
    Get the first service instance matching the contract with the expectation that there may not be a match available.
    static <T> Optional<T>
    firstNamed(TypeName contract, String name)
    Get the first named service instance matching the contract with the expectation that there may not be a match available.
    static <T> Optional<T>
    firstNamed(Class<T> contract, String name)
    Get the first named service instance matching the contract with the expectation that there may not be a match available.
    static <T> T
    get(TypeName contract)
    Get the first instance of the contract, expecting the contract is available.
    static <T> T
    get(TypeName contract, Qualifier... qualifiers)
    Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.
    static <T> T
    get(Class<T> contract)
    Get the first instance of the contract, expecting the contract is available.
    static <T> T
    get(Class<T> contract, Qualifier... qualifiers)
    Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.
    static <T> T
    getNamed(TypeName contract, String name)
    Get the first named service instance matching the contract with the expectation that there is a match available.
    static <T> T
    getNamed(Class<T> contract, String name)
    Get the first named service instance matching the contract with the expectation that there is a match 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.
    static <T> void
    setNamed(Class<T> contract, T instance, String name)
    Set a named instance.
    static <T> void
    setQualified(Class<T> contract, T instance, Qualifier... qualifiers)
    Set a qualified instance.

    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
    • setQualified

      public static <T> void setQualified(Class<T> contract, T instance, Qualifier... qualifiers)
      Set a qualified instance.

      Rules are the same as for set(Class, Object[]).

      Type Parameters:
      T - type of the service
      Parameters:
      contract - contract to set
      instance - instance to use
      qualifiers - qualifier(s) to qualify the instance
    • setNamed

      public static <T> void setNamed(Class<T> contract, T instance, String name)
      Set a named instance.

      Rules are the same as for set(Class, Object[]).

      Type Parameters:
      T - type of the service
      Parameters:
      contract - contract to set
      instance - instance to use
      name - name qualifier to qualify the instance
    • 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:
    • getNamed

      public static <T> T getNamed(Class<T> contract, String name)
      Get the first named service instance matching the contract with the expectation that there is a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      name - name qualifier of the instance to get
      Returns:
      the best service instance matching the contract
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • get

      public static <T> T get(Class<T> contract, Qualifier... qualifiers)
      Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      qualifiers - qualifiers to find
      Returns:
      the best service instance matching the contract
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • 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:
    • getNamed

      public static <T> T getNamed(TypeName contract, String name)
      Get the first named service instance matching the contract with the expectation that there is a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      name - name qualifier of the instance to get
      Returns:
      the best service instance matching the contract
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • get

      public static <T> T get(TypeName contract, Qualifier... qualifiers)
      Get the first service instance matching the contract and qualifiers with the expectation that there is a match available.
      Type Parameters:
      T - type of the contract (we will "blindly" cast the result to the expected type, make sure you use the right one)
      Parameters:
      contract - contract to look-up
      qualifiers - qualifiers to find
      Returns:
      the best service instance matching the contract
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • all

      public static <T> List<T> all(Class<T> contract, Qualifier... qualifiers)
      Get all service instances matching the contract with the expectation that there may not be a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      qualifiers - qualifiers to find
      Returns:
      list of services matching the criteria, may be empty if none matched, or no instances were provided
    • 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, Qualifier... qualifiers)
      Get all service instances matching the contract with the expectation that there may not be a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      qualifiers - qualifiers to find
      Returns:
      list of services matching the criteria, may be empty if none matched, or no instances were provided
    • 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
    • firstNamed

      public static <T> Optional<T> firstNamed(Class<T> contract, String name)
      Get the first named service instance matching the contract with the expectation that there may not be a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      name - name qualifier of the instance to get
      Returns:
      the best service instance matching the contract
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • first

      public static <T> Optional<T> first(Class<T> contract, Qualifier... qualifiers)
      Get the first service instance matching the contract with the expectation that there may not be a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      qualifiers - qualifiers to find
      Returns:
      the best service instance matching the contract, or an empty Optional if none match
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • 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
    • firstNamed

      public static <T> Optional<T> firstNamed(TypeName contract, String name)
      Get the first named service instance matching the contract with the expectation that there may not be a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      name - name qualifier of the instance to get
      Returns:
      the best service instance matching the contract
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed
    • first

      public static <T> Optional<T> first(TypeName contract, Qualifier... qualifiers)
      Get the first service instance matching the contract with the expectation that there may not be a match available.
      Type Parameters:
      T - type of the contract
      Parameters:
      contract - contract to look-up
      qualifiers - qualifiers to find
      Returns:
      the best service instance matching the contract, or an empty Optional if none match
      Throws:
      ServiceRegistryException - if there is no service that could satisfy the lookup, or the resolution to instance failed