KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > alternatives > RootVisitingLifecycleManager


1 package org.picocontainer.alternatives;
2
3 import org.picocontainer.LifecycleManager;
4 import org.picocontainer.PicoVisitor;
5 import org.picocontainer.Startable;
6 import org.picocontainer.Disposable;
7 import org.picocontainer.PicoContainer;
8 import org.picocontainer.defaults.ComponentMonitor;
9 import org.picocontainer.defaults.LifecycleVisitor;
10 import org.picocontainer.defaults.NullComponentMonitor;
11
12 import java.lang.reflect.Method JavaDoc;
13
14 /**
15  * @author Paul Hammant
16  * @version $Revision: 1870 $
17  */

18 public class RootVisitingLifecycleManager implements LifecycleManager {
19
20     private static final Method JavaDoc START;
21     private static final Method JavaDoc STOP;
22     private static final Method JavaDoc DISPOSE;
23     private PicoVisitor startVisitor;
24     private PicoVisitor stopVisitor;
25     private PicoVisitor disposeVisitor;
26
27     static {
28         try {
29             START = Startable.class.getMethod("start", null);
30             STOP = Startable.class.getMethod("stop", null);
31             DISPOSE = Disposable.class.getMethod("dispose", null);
32         } catch (NoSuchMethodException JavaDoc e) {
33             ///CLOVER:OFF
34
throw new InternalError JavaDoc(e.getMessage());
35             ///CLOVER:ON
36
}
37     }
38
39     /**
40      * Creates a lifecycle manager which will invoke lifecycle methods on components implementing:
41      * <ul>
42      * <li>{@link org.picocontainer.Startable#start()}</li>
43      * <li>{@link org.picocontainer.Startable#stop()}</li>
44      * <li>{@link org.picocontainer.Disposable#dispose()}</li>
45      * </ul>
46      *
47      * @param componentMonitor the monitor that will receive lifecycle events.
48      */

49     public RootVisitingLifecycleManager(ComponentMonitor componentMonitor) {
50         this(new LifecycleVisitor(START, Startable.class, true, componentMonitor),
51                 new LifecycleVisitor(STOP, Startable.class, false, componentMonitor),
52                 new LifecycleVisitor(DISPOSE, Disposable.class, false, componentMonitor));
53     }
54
55     /**
56      * Creates a lifecycle manager using pluggable lifecycle.
57      *
58      * @param startVisitor the visitor to use on start()
59      * @param stopVisitor the visitor to use on stop()
60      * @param disposeVisitor the visitor to use on dispose()
61      */

62     public RootVisitingLifecycleManager(PicoVisitor startVisitor, PicoVisitor stopVisitor, PicoVisitor disposeVisitor) {
63         this.startVisitor = startVisitor;
64         this.stopVisitor = stopVisitor;
65         this.disposeVisitor = disposeVisitor;
66     }
67
68     /**
69      * Creates a lifecycle manager with default visitors using a {@link org.picocontainer.defaults.NullComponentMonitor}.
70      */

71     public RootVisitingLifecycleManager() {
72         this(new NullComponentMonitor());
73     }
74
75     public void start(PicoContainer node) {
76         startVisitor.traverse(node);
77     }
78
79     public void stop(PicoContainer node) {
80         stopVisitor.traverse(node);
81     }
82
83     public void dispose(PicoContainer node) {
84         disposeVisitor.traverse(node);
85     }
86
87 }
88
Popular Tags