View Javadoc
1   package com.atlassian.plugin.refimpl.tenant;
2   
3   import com.atlassian.tenancy.api.Tenant;
4   import com.atlassian.tenancy.api.TenantAccessor;
5   import com.atlassian.tenancy.api.TenantContext;
6   import com.atlassian.tenancy.api.TenantUnavailableException;
7   import com.google.common.base.Preconditions;
8   
9   import javax.annotation.Nullable;
10  import java.lang.reflect.InvocationTargetException;
11  import java.util.ArrayList;
12  import java.util.List;
13  import java.util.concurrent.Callable;
14  
15  /**
16   * Dummy single tenant accessor - always there
17   */
18  public class RefappTenantRegistry implements TenantAccessor, TenantContext {
19      private RefappTenant tenant;
20  
21      @Override
22      public Iterable<Tenant> getAvailableTenants() {
23          if (tenant != null) {
24              List<Tenant> list = new ArrayList<Tenant>();
25              list.add(tenant);
26              return list;
27          } else {
28              return new ArrayList<Tenant>();
29          }
30      }
31  
32      public List<RefappTenant> getRefappTenants() {
33          List<RefappTenant> list = new ArrayList<RefappTenant>();
34          if (tenant != null) {
35              list.add(tenant);
36          }
37          return list;
38      }
39  
40  
41      public void setTenant(RefappTenant tenant) {
42          this.tenant = tenant;
43      }
44  
45  
46      public boolean hasTenant() {
47          return (tenant != null);
48      }
49  
50  
51      @Nullable
52      @Override
53      public Tenant getCurrentTenant() {
54          return tenant;
55      }
56  
57  
58      @Override
59      public <T> T asTenant(final Tenant tenant, final Callable<T> callback)
60              throws TenantUnavailableException, InvocationTargetException {
61          Preconditions.checkNotNull(tenant);
62          Preconditions.checkNotNull(callback);
63  
64          if (tenant != this.tenant) {
65              throw new TenantUnavailableException();
66          }
67  
68          try {
69              return callback.call();
70          } catch (Exception e) {
71              throw new InvocationTargetException(e);
72          }
73      }
74  }