KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > model > Describer


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.monitor.model;
5
6 import java.lang.reflect.InvocationTargetException JavaDoc;
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.apache.commons.beanutils.BeanUtils;
12 import org.apache.log4j.Logger;
13 import org.oddjob.Reserved;
14
15 /**
16  * Helper to describe the properties of a component for use in
17  * monitors.
18  *
19  * @author Rob Gordon.
20  */

21 public class Describer {
22     private static final Logger logger = Logger.getLogger(Describer.class);
23     
24     /**
25      * Describe the component. The component could be a
26      * remote proxy which is a DynaBean that does have
27      * a description property, or a local DyanBean such as
28      * variables which doesn't have a description property
29      * or a normal bean.
30      *
31      * @param component The compoennt.
32      * @return A map of the descriptions.
33      */

34     public static Map JavaDoc describe(Object JavaDoc component) {
35         if (component == null) {
36             throw new NullPointerException JavaDoc("Component must not be null.");
37         }
38         Map JavaDoc description = null;
39         try {
40             Method JavaDoc m = component.getClass().getMethod(Reserved.DESCRIBE_METHOD, new Class JavaDoc[0]);
41             description = (Map JavaDoc) m.invoke(component, new Object JavaDoc[0]);
42         } catch (Exception JavaDoc exception) {
43             // ignore
44
}
45         if (description != null) {
46             return description;
47         }
48
49         try {
50             return BeanUtils.describe(component);
51         } catch (InvocationTargetException JavaDoc e) {
52             logger.debug("Failed to describe:", e);
53             Throwable JavaDoc t = e.getTargetException();
54             Map JavaDoc map = new HashMap JavaDoc();
55             map.put("Error", t.getMessage());
56             return map;
57         } catch (Exception JavaDoc e) {
58             logger.debug("Failed to describe:", e);
59             Map JavaDoc map = new HashMap JavaDoc();
60             map.put("Failed to describe:", e.getMessage());
61             return map;
62         }
63     }
64 }
65
Popular Tags