View Javadoc
1   package com.atlassian.activeobjects.spi;
2   
3   import com.atlassian.tenancy.api.Tenant;
4   import com.atlassian.tenancy.api.TenantAccessor;
5   
6   import javax.annotation.Nonnull;
7   import javax.annotation.Nullable;
8   import java.util.Iterator;
9   
10  import static com.google.common.base.Preconditions.checkNotNull;
11  
12  /**
13   * Products that can't yet provide {@link com.atlassian.tenancy.api.TenantContext#getCurrentTenant()} that works in _all_
14   * threads should expose a {@link com.atlassian.activeobjects.spi.CompatibilityTenantContextImpl} in their SPI.
15   *
16   * @since 0.26.1
17   */
18  public class CompatibilityTenantContextImpl implements CompatibilityTenantContext {
19      private final TenantAccessor tenantAccessor;
20  
21      public CompatibilityTenantContextImpl(@Nonnull final TenantAccessor tenantAccessor) {
22          this.tenantAccessor = checkNotNull(tenantAccessor);
23      }
24  
25      /**
26       * Retrieve the current tenant.
27       *
28       * Hacky as hell for now; it just retrieves the first of the available tenants.
29       *
30       * We don't really know, yet, how we're going to identify the current tenant when there are multiple available,
31       * however it can be done in this one and only one place. Probably going to be a thread local...
32       *
33       * @return null if no tenant present
34       */
35      @Nullable
36      @Override
37      public Tenant getCurrentTenant() {
38          // just get the the first (and only) tenant
39          Iterator<Tenant> tenantIterator = tenantAccessor.getAvailableTenants().iterator();
40          if (tenantIterator.hasNext()) {
41              return tenantIterator.next();
42          } else {
43              return null;
44          }
45      }
46  }