KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Original code by Paul Hammant *
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 JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * This class implements the default lifecycle based on
24  * <ul>
25  * <li>{@link org.picocontainer.Startable#start()}</li>
26  * <li>{@link org.picocontainer.Startable#stop()}</li>
27  * <li>{@link org.picocontainer.Disposable#dispose()}</li>
28  * </ul>
29  *
30  * It also allows custom lifecycle strategies to be plugged in via {@link #DefaultLifecycleManager(org.picocontainer.PicoVisitor, org.picocontainer.PicoVisitor, org.picocontainer.PicoVisitor)}.
31  *
32  * @author Paul Hammant
33  * @author J&ouml;rg Schaible
34  * @author Aslak Helles&oslash;y
35  * @version $Revision: 1870 $
36  */

37 public class DefaultLifecycleManager implements LifecycleManager, Serializable JavaDoc {
38
39     private ComponentMonitor componentMonitor;
40
41     protected static Method JavaDoc startMethod = null;
42     protected static Method JavaDoc stopMethod = null;
43     protected static Method JavaDoc disposeMethod = null;
44
45     private static Object JavaDoc[] emptyArray = new Object JavaDoc[0];
46
47     static {
48         try {
49             startMethod = Startable.class.getMethod("start", new Class JavaDoc[0]);
50             stopMethod = Startable.class.getMethod("stop", new Class JavaDoc[0]);
51             disposeMethod = Disposable.class.getMethod("dispose", new Class JavaDoc[0]);
52         } catch (NoSuchMethodException JavaDoc e) {
53         }
54     }
55
56     public DefaultLifecycleManager(ComponentMonitor componentMonitor) {
57         this.componentMonitor = componentMonitor;
58     }
59
60     public DefaultLifecycleManager() {
61         this.componentMonitor = NullComponentMonitor.getInstance();
62     }
63
64     public void start(PicoContainer node) {
65         List JavaDoc startables = node.getComponentInstancesOfType(Startable.class);
66         for (int i = 0; i < startables.size(); i++) {
67             doMethod(startMethod ,startables.get(i));
68         }
69     }
70
71     public void stop(PicoContainer node) {
72         List JavaDoc startables = node.getComponentInstancesOfType(Startable.class);
73         for (int i = startables.size() -1 ; 0 <= i; i--) {
74             doMethod(stopMethod ,startables.get(i));
75         }
76     }
77
78     public void dispose(PicoContainer node) {
79         List JavaDoc disposables = node.getComponentInstancesOfType(Disposable.class);
80         for (int i = disposables.size() -1 ; 0 <= i; i--) {
81             doMethod(disposeMethod, disposables.get(i));
82         }
83     }
84
85     protected void doMethod(Method JavaDoc method, Object JavaDoc instance) {
86         componentMonitor.invoking(method, instance);
87         try {
88             long beginTime = System.currentTimeMillis();
89             method.invoke(instance, emptyArray);
90             componentMonitor.invoked(method, instance, System.currentTimeMillis() - beginTime);
91         } catch (Exception JavaDoc e) {
92             invocationFailed(method, instance, e);
93         }
94     }
95
96     protected void invocationFailed(Method JavaDoc method, Object JavaDoc instance, Exception JavaDoc e) {
97         componentMonitor.invocationFailed(method, instance, e);
98         throw new org.picocontainer.PicoInitializationException("Method '" + method.getName()
99                 + "' failed on instance '" + instance+ "' for reason '" + e.getMessage() + "'", e);
100     }
101
102 }
103
Popular Tags