KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > LifecycleVisitor


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.Parameter;
12 import org.picocontainer.PicoContainer;
13 import org.picocontainer.PicoIntrospectionException;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23
24 /**
25  * @author Aslak Hellesøy
26  * @author Jörg Schaible
27  * @version $Revision: 1841 $
28  * @since 1.1
29  */

30 public class LifecycleVisitor extends AbstractPicoVisitor implements Serializable JavaDoc {
31
32     private transient Method JavaDoc method;
33     private Class JavaDoc type;
34     private boolean visitInInstantiationOrder;
35     private List JavaDoc componentInstances;
36     private ComponentMonitor componentMonitor;
37
38     public LifecycleVisitor(Method JavaDoc method, Class JavaDoc ofType, boolean visitInInstantiationOrder, ComponentMonitor componentMonitor) {
39         this.method = method;
40         this.type = ofType;
41         this.visitInInstantiationOrder = visitInInstantiationOrder;
42         this.componentMonitor = componentMonitor;
43         this.componentInstances = new ArrayList JavaDoc();
44     }
45
46     public LifecycleVisitor(Method JavaDoc method, Class JavaDoc ofType, boolean visiInInstantiationOrder) {
47         this(method, ofType, visiInInstantiationOrder, NullComponentMonitor.getInstance());
48     }
49
50     public Object JavaDoc traverse(Object JavaDoc node) {
51         componentInstances.clear();
52         try {
53             super.traverse(node);
54             if (!visitInInstantiationOrder) {
55                 Collections.reverse(componentInstances);
56             }
57             for (Iterator JavaDoc iterator = componentInstances.iterator(); iterator.hasNext();) {
58                 Object JavaDoc o = iterator.next();
59                 try {
60                     componentMonitor.invoking(method, o);
61                     long startTime = System.currentTimeMillis();
62                     method.invoke(o, null);
63                     componentMonitor.invoked(method, o, System.currentTimeMillis() - startTime);
64                 } catch (IllegalArgumentException JavaDoc e) {
65                     componentMonitor.invocationFailed(method, o, e);
66                     throw new PicoIntrospectionException("Can't call " + method.getName() + " on " + o, e);
67                 } catch (IllegalAccessException JavaDoc e) {
68                     componentMonitor.invocationFailed(method, o, e);
69                     throw new PicoIntrospectionException("Can't call " + method.getName() + " on " + o, e);
70                 } catch (InvocationTargetException JavaDoc e) {
71                     componentMonitor.invocationFailed(method, o, e);
72                     throw new PicoIntrospectionException("Failed when calling " + method.getName() + " on " + o, e.getTargetException());
73                 }
74             }
75         } finally {
76             componentInstances.clear();
77         }
78         return Void.TYPE;
79     }
80
81     public void visitContainer(PicoContainer pico) {
82         checkTraversal();
83         componentInstances.addAll(pico.getComponentInstancesOfType(type));
84     }
85
86     public void visitComponentAdapter(ComponentAdapter componentAdapter) {
87         checkTraversal();
88     }
89
90     public void visitParameter(Parameter parameter) {
91         checkTraversal();
92     }
93
94 }
95
Popular Tags