KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > Lifecycle


1 package org.oddjob.arooa;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5
6 import org.apache.log4j.Logger;
7
8 /**
9  * Various utility methods used in the lifecycle of a component.
10  *
11  * @author Rob Gordon.
12  */

13 public class Lifecycle {
14     private static final Logger logger = Logger.getLogger(Lifecycle.class);
15         
16     public static boolean setContext(Object JavaDoc object, ArooaContext context) {
17         Class JavaDoc bean = object.getClass();
18         Method JavaDoc method = null;
19         try {
20             method = bean.getMethod("setContext",
21                     new Class JavaDoc[] { ArooaContext.class });
22         } catch (NoSuchMethodException JavaDoc e) {
23             return false;
24         }
25         try {
26             Object JavaDoc result =
27                 method.invoke(object, new Object JavaDoc[] { context });
28             if (result == null) {
29                 return true;
30             }
31             if (result instanceof Boolean JavaDoc) {
32                 return ((Boolean JavaDoc) result).booleanValue();
33             }
34             logger.debug("Unexpected return type [" + result.getClass().getName() + "]");
35             return true;
36         } catch (IllegalAccessException JavaDoc e) {
37             logger.debug(e);
38         } catch (InvocationTargetException JavaDoc e) {
39             logger.debug(e);
40         }
41         return false;
42     }
43     
44     /**
45      * If an object has an init method then invoke it.
46      *
47      * @param object The object.
48      */

49     public static void init(Object JavaDoc object) {
50         Class JavaDoc bean = object.getClass();
51         Method JavaDoc method = null;
52         try {
53             method = bean.getMethod("init", new Class JavaDoc[0]);
54         } catch (NoSuchMethodException JavaDoc e) {
55             return;
56         }
57         try {
58             method.invoke(object, new Object JavaDoc[0]);
59         } catch (IllegalAccessException JavaDoc e) {
60             // don't care
61
} catch (InvocationTargetException JavaDoc e) {
62             // don't care
63
}
64     }
65
66     /**
67      * Destroy an array of objects.
68      *
69      * @param objects The objects
70      */

71     public static void destroy(Object JavaDoc[] objects) {
72         
73         for (int i = 0; i < objects.length; ++i) {
74             destroy(objects[i]);
75         }
76     }
77     
78     /**
79      * If an object has a destroy method then call it.
80      *
81      * @param object The object.
82      */

83     public static void destroy(Object JavaDoc object) {
84         
85         Class JavaDoc bean = object.getClass();
86         Method JavaDoc destroy = null;
87         try {
88             destroy = bean.getMethod("destroy", new Class JavaDoc[0]);
89         } catch (NoSuchMethodException JavaDoc e) {
90             return;
91         }
92         try {
93             destroy.invoke(object, new Object JavaDoc[0]);
94         } catch (IllegalAccessException JavaDoc e) {
95             logger.error("Exception destroying [" + object + "]", e);
96         } catch (InvocationTargetException JavaDoc e) {
97             logger.error("Exception destroying [" + object + "]", e);
98         }
99     }
100     
101 }
102
Popular Tags