KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > management > jmx > export > runtime > ModelMBeanAssembler


1 package org.objectweb.celtix.bus.management.jmx.export.runtime;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 import javax.management.Descriptor JavaDoc;
6 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
7
8 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedAttribute;
9 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedNotification;
10 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedNotifications;
11 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedOperation;
12 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedOperationParameter;
13 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedOperationParameters;
14 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedResource;
15
16
17 public class ModelMBeanAssembler {
18     private ModelMBeanInfoSupporter supporter = new ModelMBeanInfoSupporter();
19    
20     @SuppressWarnings JavaDoc("unchecked")
21     public ManagedResource getManagedResource(Class JavaDoc clazz) {
22         return (ManagedResource)clazz.getAnnotation(ManagedResource.class);
23     }
24
25     public ManagedAttribute getManagedAttribute(Method JavaDoc method) {
26         return method.getAnnotation(ManagedAttribute.class);
27     }
28
29     public ManagedOperation getManagedOperation(Method JavaDoc method) {
30         return method.getAnnotation(ManagedOperation.class);
31     }
32
33     public ManagedOperationParameter[] getManagedOperationParameters(Method JavaDoc method) {
34         ManagedOperationParameters params = method.getAnnotation(ManagedOperationParameters.class);
35         ManagedOperationParameter[] result = null;
36         if (params == null) {
37             result = new ManagedOperationParameter[0];
38         } else {
39             result = params.value();
40         }
41         return result;
42     }
43
44     @SuppressWarnings JavaDoc("unchecked")
45     public ManagedNotification[] getManagedNotifications(Class JavaDoc clazz) {
46         ManagedNotifications notificationsAnn =
47             (ManagedNotifications)clazz.getAnnotation(ManagedNotifications.class);
48         ManagedNotification[] result = null;
49         if (null == notificationsAnn) {
50             return new ManagedNotification[0];
51         }
52         result = notificationsAnn.value();
53         return result;
54     }
55
56     public String JavaDoc getAttributeName(String JavaDoc methodName) {
57         if (methodName.indexOf("set") == 0) {
58             return methodName.substring(3);
59         }
60         if (methodName.indexOf("get") == 0) {
61             return methodName.substring(3);
62         }
63         if (methodName.indexOf("is") == 0) {
64             return methodName.substring(2);
65         }
66         return null;
67     }
68     
69     public static boolean checkMethod(Method JavaDoc[] methods, String JavaDoc methodName) {
70         boolean result = false;
71         for (int i = 0; i < methods.length; i++) {
72             if (methods[i].getName().compareTo(methodName) == 0) {
73                 result = true;
74                 break;
75             }
76         }
77         return result;
78     }
79     
80     public static String JavaDoc getAttributeType(Method JavaDoc[] methods, String JavaDoc attributeName) {
81         String JavaDoc result = null;
82         String JavaDoc searchMethod = "get" + attributeName;
83         for (int i = 0; i < methods.length; i++) {
84             if (methods[i].getName().compareTo(searchMethod) == 0) {
85                 result = methods[i].getReturnType().getName();
86                 break;
87             }
88         }
89         // check it is "is " attribute
90
if (null == result) {
91             searchMethod = "is" + attributeName;
92             for (int i = 0; i < methods.length; i++) {
93                 if (methods[i].getName().compareTo(searchMethod) == 0) {
94                     result = methods[i].getReturnType().getName();
95                     break;
96                 }
97             }
98         }
99         return result;
100     }
101     
102     class ManagedAttributeInfo {
103         String JavaDoc fname;
104         String JavaDoc ftype;
105         String JavaDoc description;
106         boolean read;
107         boolean write;
108         boolean is;
109     };
110     
111     
112     //get the attribut information for the method
113
public ManagedAttributeInfo getAttributInfo(Method JavaDoc[] methods,
114                                                String JavaDoc attributName,
115                                                String JavaDoc attributType,
116                                                ManagedAttribute managedAttribute) {
117         ManagedAttributeInfo mai = new ManagedAttributeInfo();
118         mai.fname = attributName;
119         mai.ftype = attributType;
120         mai.description = managedAttribute.description();
121         mai.is = checkMethod(methods, "is" + attributName);
122         mai.write = checkMethod(methods, "set" + attributName);
123         
124         if (mai.is) {
125             mai.read = true;
126         } else {
127             mai.read = checkMethod(methods, "get" + attributName);
128         }
129         
130         return mai;
131         
132     }
133     
134     Method JavaDoc findMethodByName(Method JavaDoc methods[], String JavaDoc methodName) {
135         for (int i = 0; i < methods.length; i++) {
136             if (methods[i].getName().compareTo(methodName) == 0) {
137                 return methods[i];
138             } else {
139                 continue;
140             }
141                 
142         }
143         return null;
144         
145     }
146     
147     void addAttributeOperation(Method JavaDoc method) {
148         Descriptor JavaDoc operationDescriptor =
149             supporter.buildAttributeOperationDescriptor(method.getName());
150         
151         Class JavaDoc<?>[] types = method.getParameterTypes();
152         
153         String JavaDoc[] paramTypes = new String JavaDoc[types.length];
154         String JavaDoc[] paramNames = new String JavaDoc[types.length];
155         String JavaDoc[] paramDescs = new String JavaDoc[types.length];
156         
157         for (int j = 0; j < types.length; j++) {
158             paramTypes[j] = types[j].getName();
159             paramDescs[j] = "";
160             paramNames[j] = types[j].getName();
161         }
162        
163         supporter.addModelMBeanMethod(method.getName(),
164                                     paramTypes,
165                                     paramNames,
166                                     paramDescs,
167                                     "",
168                                     method.getReturnType().getName(),
169                                     operationDescriptor);
170     }
171     
172     public ModelMBeanInfo JavaDoc getModelMbeanInfo(Class JavaDoc clazz) {
173         supporter.clear();
174         ManagedResource mr = getManagedResource(clazz);
175         if (mr == null) {
176             // the class is not need to expose to jmx
177
return null;
178         }
179         // Clazz get all the method which should be managemed
180
Descriptor JavaDoc mbeanDescriptor = supporter.buildMBeanDescriptor(mr);
181         
182         // add the notification
183
ManagedNotification[] mns = getManagedNotifications(clazz);
184         for (int k = 0; k < mns.length; k++) {
185             supporter.addModelMBeanNotification(mns[k].notificationTypes(),
186                                           mns[k].name(),
187                                           mns[k].description(), null);
188         }
189         
190         Method JavaDoc[] methods = clazz.getDeclaredMethods();
191         
192         for (int i = 0; i < methods.length; i++) {
193             ManagedAttribute ma = getManagedAttribute(methods[i]);
194             //add Attribute to the ModelMBean
195
if (ma != null) {
196                 String JavaDoc attributeName = getAttributeName(methods[i].getName());
197                 if (!supporter.checkAttribute(attributeName)) {
198                     String JavaDoc attributeType = getAttributeType(methods, attributeName);
199                     ManagedAttributeInfo mai = getAttributInfo(methods,
200                                                                attributeName,
201                                                                attributeType,
202                                                                ma);
203                     Descriptor JavaDoc attributeDescriptor =
204                         supporter.buildAttributeDescriptor(ma,
205                                                          attributeName,
206                                                          mai.is, mai.read, mai.write);
207                 
208                     // should setup the description
209
supporter.addModelMBeanAttribute(mai.fname,
210                                                    mai.ftype,
211                                                    mai.read,
212                                                    mai.write,
213                                                    mai.is,
214                                                    mai.description,
215                                                    attributeDescriptor);
216                     
217                     Method JavaDoc method;
218                     // add the attribute methode to operation
219
if (mai.read) {
220                         if (mai.is) {
221                             method = findMethodByName(methods, "is" + attributeName);
222                         } else {
223                             method = findMethodByName(methods, "get" + attributeName);
224                         }
225                         addAttributeOperation(method);
226                     }
227                     if (mai.write) {
228                         method = findMethodByName(methods, "set" + attributeName);
229                         addAttributeOperation(method);
230                     }
231                 }
232               
233             } else {
234                 // add Operation to the ModelMBean
235
ManagedOperation mo = getManagedOperation(methods[i]);
236                 
237                 if (mo != null) {
238                     Class JavaDoc<?>[] types = methods[i].getParameterTypes();
239                     ManagedOperationParameter[] mop = getManagedOperationParameters(methods[i]);
240                     String JavaDoc[] paramTypes = new String JavaDoc[types.length];
241                     String JavaDoc[] paramNames = new String JavaDoc[types.length];
242                     String JavaDoc[] paramDescs = new String JavaDoc[types.length];
243                     
244                     for (int j = 0; j < types.length; j++) {
245                         paramTypes[j] = types[j].getName();
246                         if (j < mop.length) {
247                             paramDescs[j] = mop[j].description();
248                             paramNames[j] = mop[j].name();
249                         } else {
250                             paramDescs[j] = "";
251                             paramNames[j] = types[j].getName();
252                         }
253                     }
254                     Descriptor JavaDoc operationDescriptor =
255                         supporter.buildOperationDescriptor(mo, methods[i].getName());
256                     supporter.addModelMBeanMethod(methods[i].getName(),
257                                                 paramTypes,
258                                                 paramNames,
259                                                 paramDescs,
260                                                 mo.description(),
261                                                 methods[i].getReturnType().getName(),
262                                                 operationDescriptor);
263                 }
264             }
265             
266         }
267         return supporter.buildModelMBeanInfo(mbeanDescriptor);
268     }
269 }
270
Popular Tags