KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > lifecycle > ReflectionLifecycleStrategy


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.gems.lifecycle;
9
10 import org.picocontainer.ComponentMonitor;
11 import org.picocontainer.defaults.AbstractMonitoringLifecycleStrategy;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18
19 /**
20  * Reflection lifecycle strategy. Starts, stops, disposes of component if appropriate methods are
21  * present. The component may implement only one of the three methods.
22  *
23  * @author Paul Hammant
24  * @author Mauro Talevi
25  * @author Jörg Schaible
26  * @see org.picocontainer.Startable
27  * @see org.picocontainer.Disposable
28  * @see org.picocontainer.defaults.DefaultLifecycleStrategy
29  * @since 1.2
30  */

31 public class ReflectionLifecycleStrategy extends AbstractMonitoringLifecycleStrategy {
32
33     private final static int START = 0;
34     private final static int STOP = 1;
35     private final static int DISPOSE = 2;
36     private String JavaDoc[] methodNames;
37     private final transient Map JavaDoc methodMap = new HashMap JavaDoc();
38
39     /**
40      * Construct a ReflectionLifecycleStrategy.
41      *
42      * @param monitor the monitor to use
43      * @throws NullPointerException if the monitor is <code>null</code>
44      */

45     public ReflectionLifecycleStrategy(ComponentMonitor monitor) {
46         this(monitor, "start", "stop", "dispose");
47     }
48
49     /**
50      * Construct a ReflectionLifecycleStrategy with individual method names. Note, that a lifecycle
51      * method does not have any arguments.
52      *
53      * @param monitor the monitor to use
54      * @param startMethodName the name of the start method
55      * @param stopMethodName the name of the stop method
56      * @param disposeMethodName the name of the dispose method
57      * @throws NullPointerException if the monitor is <code>null</code>
58      */

59     public ReflectionLifecycleStrategy(
60             ComponentMonitor monitor, String JavaDoc startMethodName, String JavaDoc stopMethodName,
61             String JavaDoc disposeMethodName) {
62         super(monitor);
63         methodNames = new String JavaDoc[]{startMethodName, stopMethodName, disposeMethodName};
64     }
65
66     public void start(Object JavaDoc component) {
67         Method JavaDoc[] methods = init(component.getClass());
68         invokeMethod(component, methods[START]);
69     }
70
71     public void stop(Object JavaDoc component) {
72         Method JavaDoc[] methods = init(component.getClass());
73         invokeMethod(component, methods[STOP]);
74     }
75
76     public void dispose(Object JavaDoc component) {
77         Method JavaDoc[] methods = init(component.getClass());
78         invokeMethod(component, methods[DISPOSE]);
79     }
80
81     private void invokeMethod(Object JavaDoc component, Method JavaDoc method) {
82         if (component != null && method != null) {
83             try {
84                 long str = System.currentTimeMillis();
85                 currentMonitor().invoking(method, component);
86                 method.invoke(component, new Object JavaDoc[0]);
87                 currentMonitor().invoked(method, component, System.currentTimeMillis() - str);
88             } catch (IllegalAccessException JavaDoc e) {
89                 RuntimeException JavaDoc re = new ReflectionLifecycleException(method.getName(), e);
90                 currentMonitor().lifecycleInvocationFailed(method, component, re);
91                 throw re;
92             } catch (InvocationTargetException JavaDoc e) {
93                 RuntimeException JavaDoc re = new ReflectionLifecycleException(method.getName(), e);
94                 currentMonitor().lifecycleInvocationFailed(method, component, re);
95                 throw re;
96             }
97         }
98     }
99
100     /**
101      * {@inheritDoc} The component has a lifecylce if at least one of the three methods is present.
102      */

103     public boolean hasLifecycle(Class JavaDoc type) {
104         Method JavaDoc[] methods = init(type);
105         for (int i = 0; i < methods.length; i++) {
106             if (methods[i] != null) {
107                 return true;
108             }
109         }
110         return false;
111     }
112
113     private Method JavaDoc[] init(Class JavaDoc type) {
114         Method JavaDoc[] methods;
115         synchronized (methodMap) {
116             methods = (Method JavaDoc[])methodMap.get(type);
117             if (methods == null) {
118                 methods = new Method JavaDoc[methodNames.length];
119                 for (int i = 0; i < methods.length; i++) {
120                     try {
121                         methods[i] = type.getMethod(methodNames[i], new Class JavaDoc[0]);
122                     } catch (NoSuchMethodException JavaDoc e) {
123                         continue;
124                     }
125                 }
126                 methodMap.put(type, methods);
127             }
128         }
129         return methods;
130     }
131 }
132
Popular Tags