1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.inject.impl; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.inject.api.ILifecycleController; |
23 | |
import org.apache.commons.inject.api.ILifecycleListener; |
24 | |
import org.apache.commons.inject.util.Exceptions; |
25 | |
|
26 | 2 | public class DefaultLifecycleController implements ILifecycleController { |
27 | |
private static final int NOT_STARTED = 0; |
28 | |
private static final int STARTED = 1; |
29 | |
private static final int TERMINATED = 2; |
30 | |
|
31 | 2 | private int state = NOT_STARTED; |
32 | 2 | private List<ILifecycleListener> listeners = new ArrayList<ILifecycleListener>(); |
33 | 2 | private List<ILifecycleListener> initializedListeners = new ArrayList<ILifecycleListener>(); |
34 | |
|
35 | |
@Override |
36 | |
public synchronized void start() { |
37 | 2 | System.err.println("DefaultLifecycleController.start: -> " + state); |
38 | 2 | if (state == NOT_STARTED) { |
39 | 2 | for (ILifecycleListener listener : listeners) { |
40 | |
try { |
41 | 1 | listener.start(); |
42 | 1 | initializedListeners.add(listener); |
43 | 0 | } catch (Throwable t) { |
44 | 0 | throw Exceptions.show(t); |
45 | 1 | } |
46 | |
} |
47 | 2 | state = STARTED; |
48 | |
} |
49 | 2 | System.err.println("DefaultLifecycleController.start: <- " + state); |
50 | 2 | } |
51 | |
|
52 | |
@Override |
53 | |
public synchronized void shutdown() { |
54 | 2 | System.err.println("DefaultLifecycleController.shutdown: -> " + state); |
55 | 2 | if (state == STARTED) { |
56 | 2 | Throwable th = null; |
57 | |
|
58 | 7 | for (int i = initializedListeners.size()-1; i >= 0; i--) { |
59 | 5 | final ILifecycleListener listener = initializedListeners.get(i); |
60 | |
try { |
61 | 5 | listener.shutdown(); |
62 | 5 | listeners.remove(i); |
63 | 0 | } catch (Throwable t) { |
64 | 0 | if (th == null) { |
65 | 0 | th = t; |
66 | |
} |
67 | 5 | } |
68 | 5 | if (th != null) { |
69 | 0 | throw Exceptions.show(th); |
70 | |
} |
71 | |
} |
72 | 2 | state = TERMINATED; |
73 | |
} |
74 | 2 | System.err.println("DefaultLifecycleController.shutdown: <- " + state); |
75 | 2 | } |
76 | |
|
77 | |
@Override |
78 | |
public synchronized boolean add(ILifecycleListener pListener) { |
79 | 5 | System.err.println("DefaultLifecycleController.add: -> " + pListener + ", " + state); |
80 | 5 | if (state == STARTED) { |
81 | |
try { |
82 | 4 | pListener.start(); |
83 | 4 | initializedListeners.add(pListener); |
84 | 0 | } catch (Throwable t) { |
85 | 0 | throw Exceptions.show(t); |
86 | 4 | } |
87 | |
} |
88 | 5 | System.err.println("DefaultLifecycleController.add: <- " + listeners.size() + ", " + initializedListeners.size()); |
89 | 5 | return listeners.add(pListener); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public synchronized boolean remove(ILifecycleListener pListener) { |
94 | 0 | System.err.println("DefaultLifecycleController.remove: -> " + pListener + ", " + state); |
95 | 0 | boolean result = initializedListeners.remove(pListener); |
96 | 0 | if (state == TERMINATED && result) { |
97 | |
try { |
98 | 0 | pListener.shutdown(); |
99 | 0 | } catch (Throwable t) { |
100 | 0 | throw Exceptions.show(t); |
101 | 0 | } |
102 | |
} |
103 | 0 | System.err.println("DefaultLifecycleController.remove: <- " + result + ", " + listeners.size() + ", " + initializedListeners.size()); |
104 | 0 | return result; |
105 | |
} |
106 | |
} |