|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LifecycleVisitor.java | 100% | 100% | 100% | 100% |
|
1 |
/*****************************************************************************
|
|
2 |
* Copyright (C) PicoContainer Organization. All rights reserved. *
|
|
3 |
* ------------------------------------------------------------------------- *
|
|
4 |
* The software in this package is published under the terms of the BSD *
|
|
5 |
* style license a copy of which has been included with this distribution in *
|
|
6 |
* the LICENSE.txt file. *
|
|
7 |
*****************************************************************************/
|
|
8 |
package org.picocontainer.defaults;
|
|
9 |
|
|
10 |
import org.picocontainer.ComponentAdapter;
|
|
11 |
import org.picocontainer.Disposable;
|
|
12 |
import org.picocontainer.Parameter;
|
|
13 |
import org.picocontainer.PicoContainer;
|
|
14 |
import org.picocontainer.PicoIntrospectionException;
|
|
15 |
import org.picocontainer.Startable;
|
|
16 |
|
|
17 |
import java.lang.reflect.InvocationTargetException;
|
|
18 |
import java.lang.reflect.Method;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.Collections;
|
|
21 |
import java.util.Iterator;
|
|
22 |
import java.util.List;
|
|
23 |
|
|
24 |
|
|
25 |
/**
|
|
26 |
* @author Aslak Hellesøy
|
|
27 |
* @author Jörg Schaible
|
|
28 |
* @version $Revision: 1.9 $
|
|
29 |
* @since 1.1
|
|
30 |
*/
|
|
31 |
public class LifecycleVisitor extends AbstractPicoVisitor { |
|
32 |
|
|
33 |
private static final Method START; |
|
34 |
private static final Method STOP; |
|
35 |
private static final Method DISPOSE; |
|
36 |
static {
|
|
37 | 18 |
try {
|
38 | 18 |
START = Startable.class.getMethod("start", null); |
39 | 18 |
STOP = Startable.class.getMethod("stop", null); |
40 | 18 |
DISPOSE = Disposable.class.getMethod("dispose", null); |
41 |
} catch (NoSuchMethodException e) {
|
|
42 |
///CLOVER:OFF
|
|
43 |
throw new InternalError(e.getMessage()); |
|
44 |
///CLOVER:ON
|
|
45 |
} |
|
46 |
} |
|
47 |
|
|
48 |
private final Method method;
|
|
49 |
private final Class type;
|
|
50 |
private final boolean visitInInstantiationOrder; |
|
51 |
private final List componentInstances;
|
|
52 |
|
|
53 | 222 |
public LifecycleVisitor(Method method, Class ofType, boolean visitInInstantiationOrder) { |
54 | 222 |
this.method = method;
|
55 | 222 |
this.type = ofType;
|
56 | 222 |
this.visitInInstantiationOrder = visitInInstantiationOrder;
|
57 | 222 |
this.componentInstances = new ArrayList(); |
58 |
} |
|
59 |
|
|
60 | 222 |
public Object traverse(Object node) {
|
61 | 222 |
componentInstances.clear(); |
62 | 222 |
try {
|
63 | 222 |
super.traverse(node);
|
64 | 220 |
if (!visitInInstantiationOrder) {
|
65 | 130 |
Collections.reverse(componentInstances); |
66 |
} |
|
67 | 220 |
for (Iterator iterator = componentInstances.iterator(); iterator.hasNext();) {
|
68 | 212 |
Object o = iterator.next(); |
69 | 212 |
long startTime = System.currentTimeMillis();
|
70 | 212 |
try {
|
71 |
// if (PicoContainer.SHOULD_LOG) {
|
|
72 |
// System.out.print("PICO: Calling " + method.toString() + " on " + o + "... ");
|
|
73 |
// System.out.flush();
|
|
74 |
// }
|
|
75 | 212 |
method.invoke(o, null);
|
76 |
} catch (IllegalArgumentException e) {
|
|
77 | 2 |
throw new PicoIntrospectionException("Can't call " + method.getName() + " on " + o, e); |
78 |
} catch (IllegalAccessException e) {
|
|
79 | 2 |
throw new PicoIntrospectionException("Can't call " + method.getName() + " on " + o, e); |
80 |
} catch (InvocationTargetException e) {
|
|
81 | 2 |
throw new PicoIntrospectionException("Failed when calling " + method.getName() + " on " + o, e.getTargetException()); |
82 |
} finally {
|
|
83 |
// if (PicoContainer.SHOULD_LOG) {
|
|
84 |
// long endTime = System.currentTimeMillis();
|
|
85 |
// System.out.println("[" + (endTime - startTime) + "ms]");
|
|
86 |
// }
|
|
87 |
} |
|
88 |
} |
|
89 |
} finally {
|
|
90 | 222 |
componentInstances.clear(); |
91 |
} |
|
92 | 214 |
return Void.TYPE;
|
93 |
} |
|
94 |
|
|
95 | 314 |
public void visitContainer(PicoContainer pico) { |
96 | 314 |
checkTraversal(); |
97 | 314 |
componentInstances.addAll(pico.getComponentInstancesOfType(type)); |
98 |
} |
|
99 |
|
|
100 | 736 |
public void visitComponentAdapter(ComponentAdapter componentAdapter) { |
101 | 736 |
checkTraversal(); |
102 |
} |
|
103 |
|
|
104 | 12 |
public void visitParameter(Parameter parameter) { |
105 | 12 |
checkTraversal(); |
106 |
} |
|
107 |
|
|
108 |
/**
|
|
109 |
* Invoke the standard PicoContainer lifecycle for {@link Startable#start()}.
|
|
110 |
* @param node The node to start the traversal.
|
|
111 |
*/
|
|
112 | 84 |
public static void start(Object node) { |
113 | 84 |
new LifecycleVisitor(START, Startable.class, true).traverse(node);; |
114 |
} |
|
115 |
|
|
116 |
/**
|
|
117 |
* Invoke the standard PicoContainer lifecycle for {@link Startable#stop()}.
|
|
118 |
* @param node The node to start the traversal.
|
|
119 |
*/
|
|
120 | 64 |
public static void stop(Object node) { |
121 | 64 |
new LifecycleVisitor(STOP, Startable.class, false).traverse(node);; |
122 |
} |
|
123 |
|
|
124 |
/**
|
|
125 |
* Invoke the standard PicoContainer lifecycle for {@link Disposable#dispose()}.
|
|
126 |
* @param node The node to start the traversal.
|
|
127 |
*/
|
|
128 | 62 |
public static void dispose(Object node) { |
129 | 62 |
new LifecycleVisitor(DISPOSE, Disposable.class, false).traverse(node);; |
130 |
} |
|
131 |
|
|
132 |
} |
|
133 |
|
|