1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.ArrayList;
26 import java.util.concurrent.CopyOnWriteArrayList;
27 import java.util.concurrent.atomic.AtomicBoolean;
28
29 import org.eclipse.aether.MultiRuntimeException;
30 import org.eclipse.aether.impl.RepositorySystemLifecycle;
31
32 import static java.util.Objects.requireNonNull;
33
34
35
36
37 @Singleton
38 @Named
39 public class DefaultRepositorySystemLifecycle implements RepositorySystemLifecycle {
40 private final AtomicBoolean shutdown;
41
42 private final CopyOnWriteArrayList<Runnable> onSystemEndedHandlers;
43
44 @Inject
45 public DefaultRepositorySystemLifecycle() {
46 this.shutdown = new AtomicBoolean(false);
47 this.onSystemEndedHandlers = new CopyOnWriteArrayList<>();
48 }
49
50 @Override
51 public void systemEnded() {
52 if (shutdown.compareAndSet(false, true)) {
53 final ArrayList<Exception> exceptions = new ArrayList<>();
54 for (Runnable onCloseHandler : onSystemEndedHandlers) {
55 try {
56 onCloseHandler.run();
57 } catch (Exception e) {
58 exceptions.add(e);
59 }
60 }
61 MultiRuntimeException.mayThrow("system on-close handler failures", exceptions);
62 }
63 }
64
65 @Override
66 public void addOnSystemEndedHandler(Runnable handler) {
67 requireNonNull(handler, "handler cannot be null");
68 requireNotShutdown();
69 onSystemEndedHandlers.add(0, handler);
70 }
71
72 private void requireNotShutdown() {
73 if (shutdown.get()) {
74 throw new IllegalStateException("repository system is already shut down");
75 }
76 }
77 }