1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.picocontainer.defaults; |
9 |
| |
10 |
| import org.picocontainer.ComponentAdapter; |
11 |
| import org.picocontainer.Parameter; |
12 |
| import org.picocontainer.PicoContainer; |
13 |
| import org.picocontainer.PicoIntrospectionException; |
14 |
| |
15 |
| import java.lang.reflect.InvocationTargetException; |
16 |
| import java.lang.reflect.Method; |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collections; |
19 |
| import java.util.Iterator; |
20 |
| import java.util.List; |
21 |
| import java.io.Serializable; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class LifecycleVisitor extends AbstractPicoVisitor implements Serializable { |
31 |
| |
32 |
| private transient Method method; |
33 |
| private Class type; |
34 |
| private boolean visitInInstantiationOrder; |
35 |
| private List componentInstances; |
36 |
| private ComponentMonitor componentMonitor; |
37 |
| |
38 |
12
| public LifecycleVisitor(Method method, Class ofType, boolean visitInInstantiationOrder, ComponentMonitor componentMonitor) {
|
39 |
12
| this.method = method;
|
40 |
12
| this.type = ofType;
|
41 |
12
| this.visitInInstantiationOrder = visitInInstantiationOrder;
|
42 |
12
| this.componentMonitor = componentMonitor;
|
43 |
12
| this.componentInstances = new ArrayList();
|
44 |
| } |
45 |
| |
46 |
12
| public LifecycleVisitor(Method method, Class ofType, boolean visiInInstantiationOrder) {
|
47 |
12
| this(method, ofType, visiInInstantiationOrder, NullComponentMonitor.getInstance());
|
48 |
| } |
49 |
| |
50 |
12
| public Object traverse(Object node) {
|
51 |
12
| componentInstances.clear();
|
52 |
12
| try {
|
53 |
12
| super.traverse(node);
|
54 |
12
| if (!visitInInstantiationOrder) {
|
55 |
4
| Collections.reverse(componentInstances);
|
56 |
| } |
57 |
12
| for (Iterator iterator = componentInstances.iterator(); iterator.hasNext();) {
|
58 |
30
| Object o = iterator.next();
|
59 |
30
| try {
|
60 |
30
| componentMonitor.invoking(method, o);
|
61 |
30
| long startTime = System.currentTimeMillis();
|
62 |
30
| method.invoke(o, null);
|
63 |
24
| componentMonitor.invoked(method, o, System.currentTimeMillis() - startTime);
|
64 |
| } catch (IllegalArgumentException e) { |
65 |
2
| componentMonitor.invocationFailed(method, o, e);
|
66 |
2
| throw new PicoIntrospectionException("Can't call " + method.getName() + " on " + o, e);
|
67 |
| } catch (IllegalAccessException e) { |
68 |
2
| componentMonitor.invocationFailed(method, o, e);
|
69 |
2
| throw new PicoIntrospectionException("Can't call " + method.getName() + " on " + o, e);
|
70 |
| } catch (InvocationTargetException e) { |
71 |
2
| componentMonitor.invocationFailed(method, o, e);
|
72 |
2
| throw new PicoIntrospectionException("Failed when calling " + method.getName() + " on " + o, e.getTargetException());
|
73 |
| } |
74 |
| } |
75 |
| } finally { |
76 |
12
| componentInstances.clear();
|
77 |
| } |
78 |
6
| return Void.TYPE;
|
79 |
| } |
80 |
| |
81 |
18
| public void visitContainer(PicoContainer pico) {
|
82 |
18
| checkTraversal();
|
83 |
18
| componentInstances.addAll(pico.getComponentInstancesOfType(type));
|
84 |
| } |
85 |
| |
86 |
84
| public void visitComponentAdapter(ComponentAdapter componentAdapter) {
|
87 |
84
| checkTraversal();
|
88 |
| } |
89 |
| |
90 |
12
| public void visitParameter(Parameter parameter) {
|
91 |
12
| checkTraversal();
|
92 |
| } |
93 |
| |
94 |
| } |