Class TenantAccessor

java.lang.Object
com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor

public final class TenantAccessor extends Object
Accessor for retrieving the current Tenant.
  • Method Details

    • getTenantFacade

      @Nonnull public static TenantFacade getTenantFacade()
      Returns the TenantFacade instance.
      Returns:
      The TenantFacade instance, or null.
    • tryGetTenantFacade

      @Nonnull public static io.vavr.control.Try<TenantFacade> tryGetTenantFacade()
      Returns a Try of the TenantFacade instance.
      Returns:
      A Try of the TenantFacade instance.
    • setTenantFacade

      public static void setTenantFacade(@Nullable TenantFacade tenantFacade)
      Replaces the default TenantFacade instance.
      Parameters:
      tenantFacade - An instance of TenantFacade. Use null to reset the facade.
    • getCurrentTenant

      @Nonnull public static Tenant getCurrentTenant() throws TenantAccessException
      Returns the current Tenant.
      Returns:
      The current Tenant.
      Throws:
      TenantAccessException - If there is an issue while accessing the Tenant.
    • tryGetCurrentTenant

      @Nonnull public static io.vavr.control.Try<Tenant> tryGetCurrentTenant()
      Returns a Try of the current Tenant, or, if the Try is a failure, the global fallback.

      On SAP Business Technology Platform, the availability of a tenant is defined as follows:

      Tenant available Tenant not available
      SAP Business Technology Platform Cloud Foundry A request is present with an "Authorization" header that contains a valid JWT bearer with field "app_tid", "zid", or "zone_uuid" (legacy).
      As a fallback a JWT will be retrieved from a bound XSUAA instance.
      A request is not available, no "Authorization" header is present in the current request, the JWT bearer does not hold a field "app_tid", "zid", or "zone_uuid" (legacy), or there is no XSUAA service bound to this application.
      Returns:
      A Try of the current Tenant.
    • executeWithTenant

      @Nullable public static <T> T executeWithTenant(@Nonnull Tenant tenant, @Nonnull Callable<T> callable) throws ThreadContextExecutionException
      Execute the given Callable on behalf of a given tenant.
      Type Parameters:
      T - The type of the callable.
      Parameters:
      tenant - The tenant to execute on behalf of.
      callable - The callable to execute.
      Returns:
      The value computed by the callable.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code on behalf of the tenant.
    • executeWithTenant

      public static void executeWithTenant(@Nonnull Tenant tenant, @Nonnull Executable executable) throws ThreadContextExecutionException
      Execute the given Executable on behalf of a given tenant.
      Parameters:
      tenant - The tenant to execute on behalf of.
      executable - The operation to execute.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code on behalf of the tenant.
    • executeWithFallbackTenant

      @Nullable public static <T> T executeWithFallbackTenant(@Nonnull Supplier<Tenant> fallbackTenant, @Nonnull Callable<T> callable) throws ThreadContextExecutionException
      Execute the given Callable, using the given tenant as fallback if there is no other tenant available.
      Type Parameters:
      T - The type of the callable.
      Parameters:
      fallbackTenant - The tenant to fall back to.
      callable - The callable to execute.
      Returns:
      The value computed by the callable.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code on behalf of the tenant.
    • executeWithFallbackTenant

      public static void executeWithFallbackTenant(@Nonnull Supplier<Tenant> fallbackTenant, @Nonnull Executable executable) throws ThreadContextExecutionException
      Execute the given Executable, using the given tenant as fallback if there is no other tenant available.
      Parameters:
      fallbackTenant - The tenant to fall back to.
      executable - The operation to execute.
      Throws:
      ThreadContextExecutionException - If there is an issue while running the code on behalf of the tenant.
    • getFallbackTenant

      @Nullable public static Supplier<Tenant> getFallbackTenant()
      Global fallback Tenant. By default, no fallback is used, i.e., the fallback is null. A global fallback can be useful to ensure a safe fallback or to ease testing with a mocked tenant.
    • setFallbackTenant

      public static void setFallbackTenant(@Nullable Supplier<Tenant> fallbackTenant)
      Global fallback Tenant. By default, no fallback is used, i.e., the fallback is null. A global fallback can be useful to ensure a safe fallback or to ease testing with a mocked tenant.