KickJava   Java API By Example, From Geeks To Geeks.

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


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.ComponentMonitor;
11 import org.picocontainer.Disposable;
12 import org.picocontainer.Startable;
13
14 import java.lang.reflect.Method JavaDoc;
15
16 /**
17  * Default lifecycle strategy. Starts and stops component if Startable,
18  * and disposes it if Disposable.
19  *
20  * @author Mauro Talevi
21  * @author Jörg Schaible
22  * @see Startable
23  * @see Disposable
24  */

25 public class DefaultLifecycleStrategy extends AbstractMonitoringLifecycleStrategy {
26
27     private static Method JavaDoc start, stop, dispose;
28     {
29         try {
30             start = Startable.class.getMethod("start", (Class JavaDoc[])null);
31             stop = Startable.class.getMethod("stop", (Class JavaDoc[])null);
32             dispose = Disposable.class.getMethod("dispose", (Class JavaDoc[])null);
33         } catch (NoSuchMethodException JavaDoc e) {
34         }
35     }
36
37     public DefaultLifecycleStrategy(ComponentMonitor monitor) {
38         super(monitor);
39     }
40
41     public void start(Object JavaDoc component) {
42         if (component != null && component instanceof Startable) {
43             long str = System.currentTimeMillis();
44             currentMonitor().invoking(start, component);
45             try {
46                 ((Startable) component).start();
47                 currentMonitor().invoked(start, component, System.currentTimeMillis() - str);
48             } catch (RuntimeException JavaDoc cause) {
49                 currentMonitor().lifecycleInvocationFailed(start, component, cause); // may re-throw
50
}
51         }
52     }
53
54     public void stop(Object JavaDoc component) {
55         if (component != null && component instanceof Startable) {
56             long str = System.currentTimeMillis();
57             currentMonitor().invoking(stop, component);
58             try {
59                 ((Startable) component).stop();
60                 currentMonitor().invoked(stop, component, System.currentTimeMillis() - str);
61             } catch (RuntimeException JavaDoc cause) {
62                 currentMonitor().lifecycleInvocationFailed(stop, component, cause); // may re-throw
63
}
64         }
65     }
66
67     public void dispose(Object JavaDoc component) {
68         if (component != null && component instanceof Disposable) {
69             long str = System.currentTimeMillis();
70             currentMonitor().invoking(dispose, component);
71             try {
72                 ((Disposable) component).dispose();
73                 currentMonitor().invoked(dispose, component, System.currentTimeMillis() - str);
74             } catch (RuntimeException JavaDoc cause) {
75                 currentMonitor().lifecycleInvocationFailed(dispose, component, cause); // may re-throw
76
}
77         }
78     }
79
80     public boolean hasLifecycle(Class JavaDoc type) {
81         return Startable.class.isAssignableFrom(type) || Disposable.class.isAssignableFrom(type);
82     }
83 }
84
Popular Tags