1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| package org.picocontainer.defaults; |
12 |
| |
13 |
| import org.picocontainer.Disposable; |
14 |
| import org.picocontainer.LifecycleManager; |
15 |
| import org.picocontainer.PicoContainer; |
16 |
| import org.picocontainer.Startable; |
17 |
| |
18 |
| import java.io.Serializable; |
19 |
| import java.lang.reflect.Method; |
20 |
| import java.util.List; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public class DefaultLifecycleManager implements LifecycleManager, Serializable { |
38 |
| |
39 |
| private ComponentMonitor componentMonitor; |
40 |
| |
41 |
| protected static Method startMethod = null; |
42 |
| protected static Method stopMethod = null; |
43 |
| protected static Method disposeMethod = null; |
44 |
| |
45 |
| private static Object[] emptyArray = new Object[0]; |
46 |
| |
47 |
| static { |
48 |
84
| try {
|
49 |
84
| startMethod = Startable.class.getMethod("start", new Class[0]);
|
50 |
84
| stopMethod = Startable.class.getMethod("stop", new Class[0]);
|
51 |
84
| disposeMethod = Disposable.class.getMethod("dispose", new Class[0]);
|
52 |
| } catch (NoSuchMethodException e) { |
53 |
| } |
54 |
| } |
55 |
| |
56 |
0
| public DefaultLifecycleManager(ComponentMonitor componentMonitor) {
|
57 |
0
| this.componentMonitor = componentMonitor;
|
58 |
| } |
59 |
| |
60 |
1012
| public DefaultLifecycleManager() {
|
61 |
1012
| this.componentMonitor = NullComponentMonitor.getInstance();
|
62 |
| } |
63 |
| |
64 |
142
| public void start(PicoContainer node) {
|
65 |
142
| List startables = node.getComponentInstancesOfType(Startable.class);
|
66 |
140
| for (int i = 0; i < startables.size(); i++) {
|
67 |
106
| doMethod(startMethod ,startables.get(i));
|
68 |
| } |
69 |
| } |
70 |
| |
71 |
94
| public void stop(PicoContainer node) {
|
72 |
94
| List startables = node.getComponentInstancesOfType(Startable.class);
|
73 |
94
| for (int i = startables.size() -1 ; 0 <= i; i--) {
|
74 |
64
| doMethod(stopMethod ,startables.get(i));
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
92
| public void dispose(PicoContainer node) {
|
79 |
92
| List disposables = node.getComponentInstancesOfType(Disposable.class);
|
80 |
92
| for (int i = disposables.size() -1 ; 0 <= i; i--) {
|
81 |
58
| doMethod(disposeMethod, disposables.get(i));
|
82 |
| } |
83 |
| } |
84 |
| |
85 |
228
| protected void doMethod(Method method, Object instance) {
|
86 |
228
| componentMonitor.invoking(method, instance);
|
87 |
228
| try {
|
88 |
228
| long beginTime = System.currentTimeMillis();
|
89 |
228
| method.invoke(instance, emptyArray);
|
90 |
228
| componentMonitor.invoked(method, instance, System.currentTimeMillis() - beginTime);
|
91 |
| } catch (Exception e) { |
92 |
0
| invocationFailed(method, instance, e);
|
93 |
| } |
94 |
| } |
95 |
| |
96 |
0
| protected void invocationFailed(Method method, Object instance, Exception e) {
|
97 |
0
| componentMonitor.invocationFailed(method, instance, e);
|
98 |
0
| throw new org.picocontainer.PicoInitializationException("Method '" + method.getName()
|
99 |
| + "' failed on instance '" + instance+ "' for reason '" + e.getMessage() + "'", e); |
100 |
| } |
101 |
| |
102 |
| } |