18
18
19
19
import static com .google .common .base .Preconditions .checkArgument ;
20
20
import static com .google .common .base .Preconditions .checkNotNull ;
21
+ import static com .google .common .base .Preconditions .checkState ;
21
22
22
23
import com .google .api .client .json .JsonFactory ;
23
24
import com .google .api .core .ApiFuture ;
31
32
import com .google .firebase .internal .CallableOperation ;
32
33
import com .google .firebase .internal .NonNull ;
33
34
import com .google .firebase .internal .Nullable ;
35
+ import java .util .concurrent .atomic .AtomicBoolean ;
34
36
35
37
/**
36
38
* This class can be used to perform a variety of tenant-related operations, including creating,
40
42
*/
41
43
public final class TenantManager {
42
44
45
+ private final Object lock = new Object ();
46
+ private final AtomicBoolean destroyed = new AtomicBoolean (false );
47
+
43
48
private final FirebaseApp firebaseApp ;
44
49
private final FirebaseUserManager userManager ;
45
50
@@ -74,7 +79,7 @@ public ApiFuture<Tenant> getTenantAsync(@NonNull String tenantId) {
74
79
}
75
80
76
81
private CallableOperation <Tenant , FirebaseAuthException > getTenantOp (final String tenantId ) {
77
- // TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
82
+ checkNotDestroyed ();
78
83
checkArgument (!Strings .isNullOrEmpty (tenantId ), "tenantId must not be null or empty" );
79
84
return new CallableOperation <Tenant , FirebaseAuthException >() {
80
85
@ Override
@@ -142,7 +147,7 @@ public ApiFuture<ListTenantsPage> listTenantsAsync(@Nullable String pageToken, i
142
147
143
148
private CallableOperation <ListTenantsPage , FirebaseAuthException > listTenantsOp (
144
149
@ Nullable final String pageToken , final int maxResults ) {
145
- // TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
150
+ checkNotDestroyed ();
146
151
final TenantSource tenantSource = new DefaultTenantSource (userManager );
147
152
final PageFactory factory = new PageFactory (tenantSource , maxResults , pageToken );
148
153
return new CallableOperation <ListTenantsPage , FirebaseAuthException >() {
@@ -180,7 +185,7 @@ public ApiFuture<Tenant> createTenantAsync(@NonNull CreateRequest request) {
180
185
181
186
private CallableOperation <Tenant , FirebaseAuthException > createTenantOp (
182
187
final CreateRequest request ) {
183
- // TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
188
+ checkNotDestroyed ();
184
189
checkNotNull (request , "create request must not be null" );
185
190
return new CallableOperation <Tenant , FirebaseAuthException >() {
186
191
@ Override
@@ -218,7 +223,7 @@ public ApiFuture<Tenant> updateTenantAsync(@NonNull UpdateRequest request) {
218
223
219
224
private CallableOperation <Tenant , FirebaseAuthException > updateTenantOp (
220
225
final UpdateRequest request ) {
221
- // TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
226
+ checkNotDestroyed ();
222
227
checkNotNull (request , "update request must not be null" );
223
228
return new CallableOperation <Tenant , FirebaseAuthException >() {
224
229
@ Override
@@ -253,7 +258,7 @@ public ApiFuture<Void> deleteTenantAsync(String tenantId) {
253
258
}
254
259
255
260
private CallableOperation <Void , FirebaseAuthException > deleteTenantOp (final String tenantId ) {
256
- // TODO(micahstairs): Add a check to make sure the app has not been destroyed yet.
261
+ checkNotDestroyed ();
257
262
checkArgument (!Strings .isNullOrEmpty (tenantId ), "tenantId must not be null or empty" );
258
263
return new CallableOperation <Void , FirebaseAuthException >() {
259
264
@ Override
@@ -263,4 +268,17 @@ protected Void execute() throws FirebaseAuthException {
263
268
}
264
269
};
265
270
}
271
+
272
+ void checkNotDestroyed () {
273
+ synchronized (lock ) {
274
+ checkState (
275
+ !destroyed .get (),
276
+ "TenantManager instance is no longer alive. This happens when "
277
+ + "the parent FirebaseApp instance has been deleted." );
278
+ }
279
+ }
280
+
281
+ protected void destroy () {
282
+ destroyed .set (true );
283
+ }
266
284
}
0 commit comments