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