|
55 | 55 | * Shared value could be set only once (to prevent complex situations with state substitutions). It is advised |
56 | 56 | * to initialize shared value only in initialization phase (to avoid potential static access errors). |
57 | 57 | * <p> |
| 58 | + * If stored state object implements {@link AutoCloseable} it will be automatically closed on application shutdown |
| 59 | + * (which is closed in most cases under tests). This is mostly useful to guarantee resource cleanup after the test |
| 60 | + * (when it can't be managed directly). |
| 61 | + * <p> |
58 | 62 | * Objects available in shared state by default: {@link io.dropwizard.core.setup.Bootstrap}, |
59 | 63 | * {@link io.dropwizard.core.Configuration}, {@link Environment}, |
60 | 64 | * {@link ru.vyarus.dropwizard.guice.module.yaml.ConfigurationTree}, {@link com.google.inject.Injector}, |
@@ -312,7 +316,17 @@ public Set<String> getKeys() { |
312 | 316 | */ |
313 | 317 | @VisibleForTesting |
314 | 318 | public void shutdown() { |
315 | | - STATE.remove(application); |
| 319 | + getKeys().forEach(key -> { |
| 320 | + Object res = get(key); |
| 321 | + if (res instanceof AutoCloseable) { |
| 322 | + try { |
| 323 | + ((AutoCloseable) res).close(); |
| 324 | + } catch (Exception e) {} |
| 325 | + } |
| 326 | + }); |
| 327 | + if (application != null) { |
| 328 | + STATE.remove(application); |
| 329 | + } |
316 | 330 | } |
317 | 331 |
|
318 | 332 | @Override |
@@ -434,6 +448,38 @@ public static <V> V lookupOrFail(final Environment environment, |
434 | 448 | .orElseThrow(() -> new IllegalStateException(Strings.lenientFormat(message, args))); |
435 | 449 | } |
436 | 450 |
|
| 451 | + /** |
| 452 | + * Shortcut for {@link #lookup(Application, Class)} to get ot initialize shared state value. |
| 453 | + * |
| 454 | + * @param application application instance |
| 455 | + * @param key shared object key |
| 456 | + * @param defSupplier default value supplier (used when state is not exists to initialize it) |
| 457 | + * @param <V> shared object type |
| 458 | + * @return value (never null) |
| 459 | + * @throws IllegalStateException if value not available |
| 460 | + */ |
| 461 | + public static <V> V lookupOrCreate(final Application application, |
| 462 | + final Class<V> key, |
| 463 | + final Supplier<V> defSupplier) { |
| 464 | + return getOrFail(application, "State is not available yet").get(key, defSupplier); |
| 465 | + } |
| 466 | + |
| 467 | + /** |
| 468 | + * Shortcut for {@link #lookup(Environment, Class)} to get ot initialize shared state value. |
| 469 | + * |
| 470 | + * @param environment environment instance |
| 471 | + * @param key shared object key |
| 472 | + * @param defSupplier default value supplier (used when state is not exists to initialize it) |
| 473 | + * @param <V> shared object type |
| 474 | + * @return value (never null) |
| 475 | + * @throws IllegalStateException if value not available |
| 476 | + */ |
| 477 | + public static <V> V lookupOrCreate(final Environment environment, |
| 478 | + final Class<V> key, |
| 479 | + final Supplier<V> defSupplier) { |
| 480 | + return getOrFail(environment, "State is not available yet").get(key, defSupplier); |
| 481 | + } |
| 482 | + |
437 | 483 | /** |
438 | 484 | * Static lookup for entire application registry. |
439 | 485 | * |
@@ -501,6 +547,7 @@ public static SharedConfigurationState getOrFail(final Environment environment, |
501 | 547 | */ |
502 | 548 | @VisibleForTesting |
503 | 549 | public static void clear() { |
| 550 | + STATE.values().forEach(SharedConfigurationState::shutdown); |
504 | 551 | STATE.clear(); |
505 | 552 | } |
506 | 553 |
|
|
0 commit comments