KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > mx > JavaBeanModelMBeanBuilder


1 /************ ***************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.mx;
10
11 import java.beans.BeanInfo JavaDoc;
12 import java.beans.Introspector JavaDoc;
13 import java.beans.MethodDescriptor JavaDoc;
14 import java.beans.PropertyDescriptor JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.management.Descriptor JavaDoc;
21 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
22 import javax.management.modelmbean.ModelMBeanConstructorInfo JavaDoc;
23 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
24 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
25 import javax.management.modelmbean.ModelMBeanNotificationInfo JavaDoc;
26 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
27
28 import org.apache.log4j.Logger;
29
30 /**
31  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
32  * @version $Revision: 1.2 $
33  */

34 public class JavaBeanModelMBeanBuilder
35 {
36
37    private final static String JavaDoc CURRENCY_TIME_LIMIT = "currencyTimeLimit";
38    private final static String JavaDoc GET_METHOD = "getMethod";
39    private final static String JavaDoc SET_METHOD = "setMethod";
40    private final static String JavaDoc PERSIST_POLICY = "persistPolicy";
41    private final static String JavaDoc ROLE = "role";
42
43    private static final Logger log = Logger.getLogger(JavaBeanModelMBeanBuilder.class);
44
45    private ArrayList JavaDoc mmais;
46    private ArrayList JavaDoc mmois;
47    private Class JavaDoc from;
48    private Class JavaDoc to;
49
50    public JavaBeanModelMBeanBuilder(Class JavaDoc from, Class JavaDoc to) throws Exception JavaDoc
51    {
52       // Get the BeanInfo
53
BeanInfo JavaDoc info = Introspector.getBeanInfo(from, to);
54
55       //
56
this.from = from;
57       this.to = to;
58       this.mmais = new ArrayList JavaDoc(info.getPropertyDescriptors().length);
59       this.mmois = new ArrayList JavaDoc(info.getMethodDescriptors().length);
60
61       // Keep track of property accessors methods
62
Map JavaDoc roles = new HashMap JavaDoc();
63
64       // Properties->Attributes
65
PropertyDescriptor JavaDoc[] propertyDescs = info.getPropertyDescriptors();
66       for (int i = 0;i < propertyDescs.length;i++)
67       {
68          PropertyDescriptor JavaDoc propertyDesc = propertyDescs[i];
69          String JavaDoc name = Character.toUpperCase(propertyDesc.getName().charAt(0)) + propertyDesc.getName().substring(1);
70          Method JavaDoc getter = propertyDesc.getReadMethod();
71          Method JavaDoc setter = propertyDesc.getWriteMethod();
72
73          // Mark the accessors methods
74
if (getter != null)
75          {
76             roles.put(getter, "getter");
77          }
78          if (setter != null)
79          {
80             roles.put(setter, "setter");
81          }
82
83          // Create the metadata
84
ModelMBeanAttributeInfo JavaDoc mmai = new ModelMBeanAttributeInfo JavaDoc(
85                name,
86                "Javabean introspected attribute",
87                getter,
88                setter);
89
90          // Complete the descriptor
91
Descriptor JavaDoc desc = mmai.getDescriptor();
92          desc.setField(CURRENCY_TIME_LIMIT, "0");
93          desc.setField(PERSIST_POLICY, "Never");
94          if (getter != null)
95          {
96             desc.setField(GET_METHOD, getter.getName());
97          }
98          if (setter != null)
99          {
100             desc.setField(SET_METHOD, setter.getName());
101          }
102          mmai.setDescriptor(desc);
103
104          // Finally add the metadata
105
mmais.add(mmai);
106       }
107
108       // Methods->Operations
109
MethodDescriptor JavaDoc[] methodDescs = info.getMethodDescriptors();
110       for (int i = 0;i < methodDescs.length;i++)
111       {
112          MethodDescriptor JavaDoc methodDesc = methodDescs[i];
113          Method JavaDoc method = methodDesc.getMethod();
114
115          // Create the metadata
116
ModelMBeanOperationInfo JavaDoc mmoi = new ModelMBeanOperationInfo JavaDoc("Javabean introspected method", method);
117
118          // Complete the descriptor
119
Descriptor JavaDoc desc = mmoi.getDescriptor();
120          String JavaDoc role = (String JavaDoc)roles.get(method);
121          desc.setField(ROLE, role != null ? role : "operation");
122          mmoi.setDescriptor(desc);
123
124          // Finally add the metadata
125
mmois.add(mmoi);
126       }
127    }
128
129    /**
130     * Remove an interface from the management interface.
131     */

132    public void remove(Class JavaDoc itf)
133    {
134       throw new UnsupportedOperationException JavaDoc("To be implemented if usefull, just a placeholder now");
135    }
136
137    /**
138     * Generates and returns the management interface.
139     */

140    public ModelMBeanInfo JavaDoc getInfo()
141    {
142       return new ModelMBeanInfoSupport JavaDoc(
143             from.getName(),
144             "Javabean model mbean",
145             (ModelMBeanAttributeInfo JavaDoc[])mmais.toArray(new ModelMBeanAttributeInfo JavaDoc[mmais.size()]),
146             new ModelMBeanConstructorInfo JavaDoc[0],
147             (ModelMBeanOperationInfo JavaDoc[])mmois.toArray(new ModelMBeanOperationInfo JavaDoc[mmois.size()]),
148             new ModelMBeanNotificationInfo JavaDoc[0]
149       );
150    }
151
152    public static final ModelMBeanInfo JavaDoc build(Class JavaDoc from, Class JavaDoc to) throws Exception JavaDoc
153    {
154       return new JavaBeanModelMBeanBuilder(from, to).getInfo();
155    }
156
157    public static final ModelMBeanInfo JavaDoc build(Object JavaDoc o) throws Exception JavaDoc
158    {
159       return new JavaBeanModelMBeanBuilder(o.getClass(), Object JavaDoc.class).getInfo();
160    }
161
162
163 }
164
Popular Tags