KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > meta > MBeanMetaHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: MBeanMetaHelper.java,v 1.5 2006/03/12 01:26:56 jluehe Exp $
26  * @author: alexkrav
27  *
28  * $Log: MBeanMetaHelper.java,v $
29  * Revision 1.5 2006/03/12 01:26:56 jluehe
30  * Renamed AS's org.apache.commons.* to com.sun.org.apache.commons.*, to avoid collisions with org.apache.commons.* packages bundled by webapps.
31  *
32  * Tests run: QL, Servlet TCK
33  *
34  * Revision 1.4 2005/12/25 03:47:37 tcfujii
35  * Updated copyright text and year.
36  *
37  * Revision 1.3 2005/08/16 22:19:31 kravtch
38  * M3: 1. ConfigMBeans: Support for generic getXXXNamesList() operation (request from management-rules).
39  * 2. MBeanRegistry: support for getElementPrintName() to provide readable element's description for validator's messages
40  * Submitted by: kravtch
41  * Reviewed by: Shreedhar
42  * Affected modules admin-core/admin
43  * Tests passed: QLT/EE + devtests
44  *
45  * Revision 1.2 2005/06/27 21:19:43 tcfujii
46  * Issue number: CDDL header updates.
47  *
48  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
49  * GlassFish first drop
50  *
51  * Revision 1.15 2004/11/14 07:04:21 tcfujii
52  * Updated copyright text and/or year.
53  *
54  * Revision 1.14 2004/05/22 00:35:07 kravtch
55  * "system-properties" backend support is added
56  * Reviewer: Sridatta
57  * Tests passed: QLT/CTS PE
58  *
59  * Revision 1.13 2004/02/20 03:56:14 qouyang
60  *
61  *
62  * First pass at code merge.
63  *
64  * Details for the merge will be published at:
65  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
66  *
67  * Revision 1.11.4.3 2004/02/02 07:25:19 tcfujii
68  * Copyright updates notices; reviewer: Tony Ng
69  *
70  * Revision 1.11.4.2 2003/12/23 01:51:45 kravtch
71  * Bug #4959186
72  * Reviewer: Sridatta
73  * Checked in PE8FCS_BRANCH
74  * (1) admin/admin-core/admin-cli/admin-gui/appserv-core/assembly-tool: switch to new domain name "ias:" -> "com.sun.appserv"
75  * (2) admin-core and admin-cli: switch to "dashed" attribute names
76  * (3) admin-core: support both "dashed"/"underscored" names for get/setAttributes
77  * (4) admin-gui: hook for reverse converting attribute names (temporary hack);
78  *
79  * Revision 1.11.4.1 2003/12/01 21:52:39 kravtch
80  * Bug #4939964
81  * Reviewer: Sridatta
82  * admin.config.ManagedConfigBean.createChildByType() now analyzes registryEntries.AttributeInfo for each "empty" valued attribute (similar to setAttribute(), but could not use MBeanAttributeInfo because MBean is not exists yet). If "emptyValueAllowed" field in registrEntry.AttributeInfo is not "true", then "empty" attribute will be ignored.
83  *
84  * Revision 1.11 2003/09/25 18:01:58 kravtch
85  * Bug #4927601
86  * Reviewer: Sreeni
87  * MBeanMetaHelper does not rejects custom operations which match to "standard" JMX names, but does it only if signature matches too.
88  *
89  * Revision 1.10 2003/09/05 06:06:10 kravtch
90  * Bug #4915176
91  * Reviewer: Sridatta
92  * - new custom mbean - DomainMBean implemented public AttributeList getDefaultCustomProperties(String mbeanTypeName, AttributeList attributeList) to fulfill the reques;
93  * - descriptor file is updated (classname for this mbean added)
94  * - mandatory getDefaultCustomProperties() removed from all MBeanInfos;
95  * - CLI name for http-listener.ssl repared in descriptors file;
96  * - non-neccessary logs suppressed;
97  *
98  * Revision 1.9 2003/08/20 15:36:42 kravtch
99  * typo correct getDefaulCustomProperties->getDefaultCustomProperties
100  *
101  * Revision 1.8 2003/08/15 23:08:28 kravtch
102  * DottedName Support (generation and call to manager)
103  * notifyRegisterMBean/UnregisterMBean are implemented;
104  * dotted name related opeartions are added to NaminDescriptor and NamingInfo
105  * removeChild support is added;
106  *
107  * Revision 1.7 2003/08/14 23:44:44 kravtch
108  * repare for attributes started from "default" they were treated as read-only because of conflicting with getDefaultXXXvalues.
109  * now introspector skips Static methods.
110  *
111  * Revision 1.6 2003/08/07 00:41:06 kravtch
112  * - new DTD related changes;
113  * - properties support added;
114  * - getDefaultAttributeValue() implemented for config MBeans;
115  * - merge Jsr77 and config activity in runtime mbeans;
116  *
117  * Revision 1.5 2003/07/29 18:59:35 kravtch
118  * MBeanRegistryEntry:
119  * - support for toFormatString();
120  * - instantiateMBean() method modified to instantiate runtime MBeans as well;
121  * MBeanRegistryFactory:
122  * - fixed bug in getRuntimeRegistry();
123  * MBeanNamingInfo:
124  * - less strict requirements for parm_list_array size in constructor (can be more then needed);
125  * BaseRuntimeMBean:
126  * - exception ClassCastException("Managed resource is not a Jsr77ModelBean") handling;
127  * ManagedJsr77MdlBean:
128  * - call managed bean bug fixed ( getDeclaredMethod()->getMethod())
129  * admin/dtds/runtime-mbeans-descriptors.xml - modified to represent new runtime mbeans;
130  *
131  * Revision 1.4 2003/07/18 20:14:44 kravtch
132  * 1. ALL config mbeans are now covered by descriptors.xml
133  * 2. new infrastructure for runtime mbeans is added
134  * 3. generic constructors added to jsr77Mdl beans (String[])
135  * 4. new test cases are added to admintest
136  * 5. MBeanRegistryFactory has now different methods to obtain admin/runtime registries
137  * 6. runtime-descriptors xml-file is added to build
138  *
139  * Revision 1.3 2003/06/25 20:03:40 kravtch
140  * 1. java file headers modified
141  * 2. properties handling api is added
142  * 3. fixed bug for xpathes containing special symbols;
143  * 4. new testcases added for jdbc-resource
144  * 5. introspector modified by not including base classes operations;
145  *
146  *
147 */

148
149 package com.sun.enterprise.admin.meta;
150
151 import java.util.Enumeration JavaDoc;
152 import java.util.Hashtable JavaDoc;
153 import java.util.List JavaDoc;
154 import java.text.CharacterIterator JavaDoc;
155 import java.text.StringCharacterIterator JavaDoc;
156
157 import java.lang.reflect.Method JavaDoc;
158 import java.lang.reflect.Modifier JavaDoc;
159
160 import com.sun.org.apache.commons.modeler.ManagedBean;
161 import com.sun.org.apache.commons.modeler.FeatureInfo;
162 import com.sun.org.apache.commons.modeler.FieldInfo;
163 import com.sun.org.apache.commons.modeler.AttributeInfo;
164 import com.sun.org.apache.commons.modeler.OperationInfo;
165 import com.sun.org.apache.commons.modeler.ParameterInfo;
166
167 //JMX imports
168
import javax.management.Descriptor JavaDoc;
169 import javax.management.ObjectName JavaDoc;
170 import javax.management.Attribute JavaDoc;
171 import javax.management.AttributeList JavaDoc;
172 import javax.management.DynamicMBean JavaDoc;
173 import javax.management.NotificationBroadcasterSupport JavaDoc;
174 import com.sun.enterprise.config.ConfigBeansFactory;
175
176 /**
177  *
178  */

179 public class MBeanMetaHelper implements MBeanMetaConstants
180 {
181     
182     static int ONLY_CHILD=1;
183     static int MULTY_CHILDS=2;
184     
185     static int SETTER_METHODTYPE = 1;
186     static int GETTER_METHODTYPE = 2;
187     /** Process the methods and extract 'attributes', methods, etc
188      *
189      */

190     //*****************************************************************************
191
public static void mergeWithConfigBean(ManagedBean managedBean, Class JavaDoc objectClass, int mode)
192     {
193         
194         if(objectClass==null)
195             return;
196         Hashtable JavaDoc attrs = new Hashtable JavaDoc();
197         Hashtable JavaDoc children = new Hashtable JavaDoc();
198         int shift = 0;
199         
200         //Introspect and get all the methods
201
Method JavaDoc[] methods = objectClass.getMethods();
202         for (int j = 0; j < methods.length; ++j)
203         {
204             String JavaDoc methodName=methods[j].getName();
205             
206             //----------GETTER----------------
207
if( methodName.startsWith("get") || methodName.startsWith("is"))
208             {
209                 shift = methodName.startsWith("is")?2:3;
210                 if( Modifier.isStatic(methods[j].getModifiers()) || !Modifier.isPublic(methods[j].getModifiers()) )
211                 {
212                     continue;
213                 }
214                 Class JavaDoc params[]=methods[j].getParameterTypes();
215                 if( params.length != 0 )
216                 {
217                     continue;
218                 }
219                 if( methods[j].getDeclaringClass() != objectClass )
220                     continue;
221                 
222                 Class JavaDoc ret=methods[j].getReturnType();
223                 if( ! supportedType( ret ) )
224                 {
225                     //maybe this is child
226
String JavaDoc childClassName = ret.getName();
227                     if(childClassName.endsWith("."+methodName.substring(shift)) ||
228                        childClassName.endsWith("."+methodName.substring(shift)+";"))
229                     {
230                         children.put(methodName.substring(shift), childClassName);
231                     }
232                     continue;
233                 }
234                 if((mode&EXPOSE_GETTERS)==0 )
235                     continue;
236 // if(methodName.startsWith( "getDefault" ))
237
// continue;
238

239                 AttrIntro ai = (AttrIntro)attrs.get(getAttrNameFromMethodName(methodName, true));
240                 if(ai==null)
241                 {
242                     ai = new AttrIntro();
243                     ai.name = getAttrNameFromMethodName(methodName, true);
244                     attrs.put(ai.name, ai);
245                 }
246                 if(ai.type!=null)
247                 {
248                     if(!ai.type.equals(ret.getName()))
249                         continue;
250                 }
251                 else
252                 {
253                     ai.type = ret.getName();
254                 }
255                 ai.getName = methodName;
256                 //----------SETTER----------------
257
} else if( methodName.startsWith( "set" ) )
258             {
259                 if((mode&EXPOSE_SETTERS)==0 )
260                     continue;
261
262                 Class JavaDoc params[]=methods[j].getParameterTypes();
263                 if( params.length != 1 )
264                 {
265                     continue;
266                 }
267                 if( ! supportedType( params[0] ) )
268                 {
269                     continue;
270                 }
271                 if( ! Modifier.isPublic( methods[j].getModifiers() ) )
272                 {
273                     continue;
274                 }
275                 if( methods[j].getDeclaringClass() != objectClass )
276                     continue;
277
278                 AttrIntro ai = (AttrIntro)attrs.get(getAttrNameFromMethodName(methodName, true));
279                 if(ai==null)
280                 {
281                     ai = new AttrIntro();
282                     ai.name = getAttrNameFromMethodName(methodName, true);
283                     attrs.put(ai.name, ai);
284                 }
285                 if(ai.type!=null)
286                 {
287                     if(!ai.type.equals(params[0].getName()))
288                         continue;
289                 }
290                 else
291                 {
292                     ai.type = params[0].getName();
293                 }
294                 ai.setName = methodName;
295                 if(methodName.startsWith( "setDefault" )) //???
296
{
297                     ai.setName = "set"+methodName.substring(3);
298                 }
299                 
300             } else
301             {
302                 continue;
303             }
304         }
305         
306         OperationInfo operationInfo;
307
308         //**** A T T R I B U T E S ******
309
// attrs.remove("x_path");
310
// attrs.remove("attribute_names");
311
// attrs.remove("monitoring_enabled");
312

313         if(attrs.size()>0)
314         {
315             AttributeInfo[] infos = managedBean.getAttributes();
316             Hashtable JavaDoc infosTable = new Hashtable JavaDoc();
317             for(int i=0; i<infos.length; i++)
318             {
319                 infosTable.put(infos[i].getName(), infos[i]);
320             }
321             
322             String JavaDoc key;
323             Enumeration JavaDoc keys = attrs.keys();
324             while(keys.hasMoreElements())
325             {
326                 key = (String JavaDoc)keys.nextElement();
327                 AttrIntro ai = (AttrIntro)attrs.get(key);
328                 AttributeInfo info = (AttributeInfo)infosTable.get(key);
329                 if(info==null)
330                 {
331                     ai.whereType = LOCATED_IN_CONFIGBEAN;
332                     info = ai.createAttributeInfo();
333                     managedBean.addAttribute(info);
334                     infosTable.put(key, info);
335                 }
336                 else
337                 {
338                     ai.mergeWithAttributeInfo(info);
339                 }
340             }
341
342             //getDefaultAttributeValue
343
operationInfo = createOperationInfo("getDefaultAttributeValue", "INFO",
344                    "java.lang.String",
345                    new ParameterInfo("attributeName", "java.lang.String", null),
346                    LOCATED_IN_CONFIGBEAN);
347             mergeWithOperationInfo(managedBean, operationInfo);
348         }
349         
350         //classnames values for method's types
351
String JavaDoc nameClass = "javax.management.ObjectName";
352         String JavaDoc attrClass = "javax.management.Attribute";
353         String JavaDoc namesClass = (new ObjectName JavaDoc[0]).getClass().getName();
354         String JavaDoc attrListClass = (new AttributeList JavaDoc()).getClass().getName();
355         String JavaDoc stringsClass = (new String JavaDoc[0]).getClass().getName();
356
357         FieldInfo field;
358         ParameterInfo param;
359
360         // **** P R O P E R T I E S ******
361
if(children.get("ElementProperty")!=null)
362         {
363             children.remove("ElementProperty");
364
365             
366             //getProperties
367
operationInfo = createOperationInfo("getProperties", "INFO",
368                    attrListClass, null, LOCATED_IN_CONFIGBEAN);
369             mergeWithOperationInfo(managedBean, operationInfo);
370             
371             //getDefaulCustomProperties
372
// operationInfo = createOperationInfo("getDefaultCustomProperties", "INFO",
373
// attrListClass, null, null);
374
// mergeWithOperationInfo(managedBean, operationInfo);
375

376             //getProperty
377
operationInfo = createOperationInfo("getPropertyValue", "INFO",
378                    "java.lang.Object",
379                    new ParameterInfo("propertyName", "java.lang.String", null),
380                    LOCATED_IN_CONFIGBEAN);
381             mergeWithOperationInfo(managedBean, operationInfo);
382             
383             //setProperty
384
operationInfo = createOperationInfo("setProperty", "ACTION",
385                    "void",
386                    new ParameterInfo("nameAndValue", attrClass, null),
387                    LOCATED_IN_CONFIGBEAN);
388             mergeWithOperationInfo(managedBean, operationInfo);
389         }
390
391         // **** S Y S T E M P R O P E R T I E S ******
392
if(children.get("SystemProperty")!=null)
393         {
394             children.remove("SystemProperty");
395             
396             //getSystemProperties
397
operationInfo = createOperationInfo("getSystemProperties", "INFO",
398                    attrListClass, null, LOCATED_IN_CONFIGBEAN);
399             mergeWithOperationInfo(managedBean, operationInfo);
400             
401             //getSystemProperty
402
operationInfo = createOperationInfo("getSystemPropertyValue", "INFO",
403                    "java.lang.Object",
404                    new ParameterInfo("propertyName", "java.lang.String", null),
405                    LOCATED_IN_CONFIGBEAN);
406             mergeWithOperationInfo(managedBean, operationInfo);
407             
408             //setSystemProperty
409
operationInfo = createOperationInfo("setSystemProperty", "ACTION",
410                    "void",
411                    new ParameterInfo("nameAndValue", attrClass, null),
412                    LOCATED_IN_CONFIGBEAN);
413             mergeWithOperationInfo(managedBean, operationInfo);
414         }
415
416         //**** C H I L D R E N ******
417

418         //children.remove("ElementProperty");
419

420         if(children.size()>0)
421         {
422             String JavaDoc key;
423             Enumeration JavaDoc keys = children.keys();
424             while(keys.hasMoreElements())
425             {
426                 key = (String JavaDoc)keys.nextElement();
427                 String JavaDoc clazz = (String JavaDoc)children.get(key);
428                 boolean bMulti = clazz.charAt(0)=='['?true:false;
429                 String JavaDoc childName = getAttrNameFromMethodName(key, true, '-');
430                 //getter
431
if((mode&EXPOSE_GETCHILD)!=0)
432                 {
433                     //get child (ObjectName)
434
operationInfo = createOperationInfo("get"+key, "INFO",
435                            bMulti?namesClass:nameClass,
436                            null, LOCATED_IN_CONFIGBEAN);
437                     addDataToChildOperInfo(childName, bMulti, operationInfo);
438                     mergeWithOperationInfo(managedBean, operationInfo);
439                     if(bMulti)
440                     {
441                         //get child NamesList(ObjectName)
442
operationInfo = createOperationInfo(
443                              "get"+key+GET_LISTNAMES_OP_SUFFIX,
444                              "INFO", stringsClass, null, null);
445                         addDataToChildOperInfo(childName, bMulti, operationInfo);
446                         mergeWithOperationInfo(managedBean, operationInfo);
447                     }
448                     
449                     //getChildByKey
450
if(bMulti)
451                     {
452                         String JavaDoc prefix = "get"+key+"By";
453                         for (int j = 0; j < methods.length; ++j)
454                         {
455                             String JavaDoc methodName=methods[j].getName();
456                             if(methodName.startsWith(prefix))
457                             {
458                                 operationInfo = createOperationInfo(methodName, "INFO",
459                                        nameClass,
460                                        new ParameterInfo("key", "java.lang.String", null),
461                                        LOCATED_IN_CONFIGBEAN);
462                                 addDataToChildOperInfo(childName, bMulti, operationInfo);
463                                 mergeWithOperationInfo(managedBean, operationInfo);
464                                 break;
465                             }
466                         }
467                     }
468                 }
469                 if((mode&EXPOSE_CREATECHILD)!=0)
470                 {
471                     //CreateChild
472
String JavaDoc prefix = bMulti?"add"+key:"set"+key;
473                     for (int j = 0; j < methods.length; ++j)
474                     {
475                         String JavaDoc methodName=methods[j].getName();
476                         if(methodName.startsWith(prefix))
477                         {
478                             operationInfo = createOperationInfo("create"+key, "ACTION_INFO",
479                                    nameClass,
480                                    new ParameterInfo("attribute_list", attrListClass, null),
481                                    LOCATED_IN_CONFIGBEAN);
482                             addDataToChildOperInfo(childName, bMulti, operationInfo);
483                             mergeWithOperationInfo(managedBean, operationInfo);
484                             break;
485                         }
486                     }
487                 }
488                 if((mode&EXPOSE_DESTROYCHILD)!=0)
489                 {
490                     if(!bMulti)
491                     {
492                         operationInfo = createOperationInfo("remove"+key,
493                                "ACTION", "void", null, LOCATED_IN_CONFIGBEAN);
494                         addDataToChildOperInfo(childName, bMulti, operationInfo);
495                         mergeWithOperationInfo(managedBean, operationInfo);
496                     }
497                     else
498                     {
499                         String JavaDoc prefix = "get"+key+"By";
500                         for (int j = 0; j < methods.length; ++j)
501                         {
502                             String JavaDoc methodName=methods[j].getName();
503                             if(methodName.startsWith(prefix))
504                             {
505                                 operationInfo = createOperationInfo("remove"+methodName.substring(3),
506                                        "ACTION", "void",
507                                        new ParameterInfo("key", "java.lang.String", null),
508                                        LOCATED_IN_CONFIGBEAN);
509                                 addDataToChildOperInfo(childName, bMulti, operationInfo);
510                                 mergeWithOperationInfo(managedBean, operationInfo);
511                                 break;
512                             }
513                         }
514                     }
515                 }
516             }
517         }
518     }
519
520     //*****************************************************************************
521
public static AttrIntro findOrCreateAttrInfo(Hashtable JavaDoc attrs, String JavaDoc attrName, String JavaDoc methodName, Class JavaDoc supposedTypeClass, String JavaDoc whereType, int methodType)
522     {
523         AttrIntro ai = (AttrIntro)attrs.get(attrName);
524         if(ai==null)
525         {
526             ai = new AttrIntro();
527             ai.name = attrName;
528             ai.type = supposedTypeClass.getName();
529             if((methodType&GETTER_METHODTYPE)!=0)
530                 ai.getName = methodName;
531             if((methodType&SETTER_METHODTYPE)!=0)
532                 ai.setName = methodName;
533             ai.getName = methodName;
534             ai.whereType = whereType;
535             attrs.put(ai.name, ai);
536         }
537         else
538            if(ai.type==null || ai.type.equals(supposedTypeClass.getName()))
539            {
540                 if((methodType&GETTER_METHODTYPE)!=0)
541                     ai.getName = methodName;
542                 if((methodType&SETTER_METHODTYPE)!=0)
543                     ai.setName = methodName;
544            }
545            else
546            {
547                return null;
548            }
549         return ai;
550     }
551     
552     //*****************************************************************************
553
public static void mergeWithRuntimeModelBean(ManagedBean managedBean, Class JavaDoc objectClass)
554     {
555         
556         if(objectClass==null)
557             return;
558         Hashtable JavaDoc attrs = new Hashtable JavaDoc();
559         
560         //Introspect and get all the methods
561
Method JavaDoc[] methods = objectClass.getMethods();
562         for (int j = 0; j < methods.length; ++j)
563         {
564             if( Modifier.isStatic(methods[j].getModifiers()))
565                 continue;
566             if( ! Modifier.isPublic( methods[j].getModifiers() ) )
567                 continue;
568             if( methods[j].getDeclaringClass() == Object JavaDoc.class )
569                 continue;
570             
571             String JavaDoc methodName=methods[j].getName();
572             Class JavaDoc params[]=methods[j].getParameterTypes();
573             Class JavaDoc ret=methods[j].getReturnType();
574             
575             //----------GETTER----------------
576
if( (methodName.startsWith("get") || methodName.startsWith("is")) &&
577 // !methodName.startsWith( "getDefault" ) &&
578
params.length == 0 &&
579                 supportedType( ret ) )
580             {
581                 findOrCreateAttrInfo(attrs, getAttrNameFromMethodName(methodName,false), methodName, ret, LOCATED_IN_RUNTIMEBEAN, GETTER_METHODTYPE);
582             }
583             //----------SETTER----------------
584
else if( methodName.startsWith( "set" ) &&
585                      params.length == 1 &&
586                      supportedType( params[0] ))
587             {
588                 findOrCreateAttrInfo(attrs, getAttrNameFromMethodName(methodName,false), methodName, params[0], LOCATED_IN_RUNTIMEBEAN, SETTER_METHODTYPE);
589                 
590             }
591             //----------OPERATIONS----------------
592
else
593             {
594                 OperationInfo operationInfo = getOperationInfo(methods[j], LOCATED_IN_RUNTIMEBEAN);
595                 mergeWithOperationInfo(managedBean, operationInfo);
596             }
597         }
598         
599         //**** A T T R I B U T E S ******
600
if(attrs.size()>0)
601         {
602             AttributeInfo[] infos = managedBean.getAttributes();
603             Hashtable JavaDoc infosTable = new Hashtable JavaDoc();
604             for(int i=0; i<infos.length; i++)
605             {
606                 infosTable.put(infos[i].getName(), infos[i]);
607             }
608             
609             String JavaDoc key;
610             Enumeration JavaDoc keys = attrs.keys();
611             while(keys.hasMoreElements())
612             {
613                 key = (String JavaDoc)keys.nextElement();
614                 AttrIntro ai = (AttrIntro)attrs.get(key);
615                 AttributeInfo info = (AttributeInfo)infosTable.get(key);
616                 if(info==null)
617                 {
618                     ai.whereType = LOCATED_IN_RUNTIMEBEAN;
619                     info = ai.createAttributeInfo();
620                     managedBean.addAttribute(info);
621                     infosTable.put(key, info);
622                 }
623                 else
624                 {
625                     ai.mergeWithAttributeInfo(info);
626                 }
627             }
628         }
629         
630     }
631
632     
633     //*****************************************************************************
634
private static Class JavaDoc forNameOrNull(String JavaDoc str)
635     {
636         try {
637             return Class.forName(str);
638         } catch (Exception JavaDoc e) {
639             return null;
640         }
641     }
642     //*****************************************************************************
643
private static boolean isMethodMatch(Method JavaDoc m, String JavaDoc name, Class JavaDoc[] clParams)
644     {
645         if(!name.equals(m.getName()))
646            return false;
647         Class JavaDoc[] cls = m.getParameterTypes();
648         if(clParams==null)
649             if(cls==null || cls.length==0)
650                 return true;
651             else
652                 return false;
653         if(cls==null)
654             if(clParams.length==0)
655                 return true;
656             else
657                 return false;
658          if(clParams.length!=cls.length)
659              return false;
660         for(int i=0; i<cls.length; i++)
661             if(!clParams[i].equals(cls[i]))
662                 return false;
663         return true;
664     }
665 static Class JavaDoc[] _clsStr = new Class JavaDoc[]{(new String JavaDoc()).getClass()};
666 static Class JavaDoc[] _clsStrArr = new Class JavaDoc[]{(new String JavaDoc[0]).getClass()};
667 static Class JavaDoc[] _clsAttr = new Class JavaDoc[]{forNameOrNull("javax.management.Attribute")};
668 static Class JavaDoc[] _clsAttrList = new Class JavaDoc[]{forNameOrNull("javax.management.AttributeList")};
669 static Class JavaDoc[] _clsServAndOname = new Class JavaDoc[]{forNameOrNull("javax.management.MBeanServer"), forNameOrNull("javax.management.ObjectName")};
670 static Class JavaDoc[] _clsModelMBI = new Class JavaDoc[]{forNameOrNull("javax.management.modelmbean.ModelMBeanInfo") };
671 static Class JavaDoc[] _clsBoolean = new Class JavaDoc[]{(new Boolean JavaDoc(true)).getClass()};
672 static Class JavaDoc[] _clsObjAndStr = new Class JavaDoc[]{(new Object JavaDoc()).getClass(), (new String JavaDoc()).getClass()};
673 static Class JavaDoc[] _clsInvokeParms = new Class JavaDoc[]{(new String JavaDoc()).getClass(), (new Object JavaDoc[0]).getClass(), (new String JavaDoc[0]).getClass()};
674     //*****************************************************************************
675
public static void mergeWithDynamicMBean(ManagedBean managedBean, Class JavaDoc objectClass)
676     {
677
678         if(objectClass==null)
679             return;
680         //Introspect and get all the methods
681
Method JavaDoc[] methods = objectClass.getMethods();
682         for (int j = 0; j < methods.length; ++j)
683         {
684             if( Modifier.isStatic(methods[j].getModifiers()))
685                 continue;
686             if( ! Modifier.isPublic( methods[j].getModifiers() ) )
687                 continue;
688
689             if( methods[j].getDeclaringClass() == Object JavaDoc.class )
690                 continue;
691             Class JavaDoc declaringClass = methods[j].getDeclaringClass();
692 // if( declaringClass == DynamicMBean.class )
693
// continue;
694
if( NotificationBroadcasterSupport JavaDoc.class.equals(declaringClass) )
695                 continue;
696             String JavaDoc methodName=methods[j].getName();
697             if( isMethodMatch(methods[j], "getAttribute", _clsStr) ||
698                 isMethodMatch(methods[j], "getAttributes", _clsStrArr) ||
699                 isMethodMatch(methods[j], "setAttribute", _clsAttr) ||
700                 isMethodMatch(methods[j], "setAttributes", _clsAttrList) ||
701                 isMethodMatch(methods[j], "preRegister", _clsServAndOname) ||
702                 isMethodMatch(methods[j], "postRegister", _clsBoolean) ||
703                 isMethodMatch(methods[j], "preDeregister", null) ||
704                 isMethodMatch(methods[j], "postDeregister", null)||
705                 isMethodMatch(methods[j], "setManagedResource", _clsObjAndStr) ||
706                 isMethodMatch(methods[j], "setModelMBeanInfo", _clsModelMBI)||
707                 isMethodMatch(methods[j], "getMBeanInfo", null) ||
708                 isMethodMatch(methods[j], "invoke", _clsInvokeParms) )
709                 continue;
710             OperationInfo operationInfo = getOperationInfo(methods[j], LOCATED_IN_MBEAN);
711             mergeWithOperationInfo(managedBean, operationInfo);
712         }
713         
714     }
715     
716     //*****************************************************************************
717
private static OperationInfo getOperationInfo(Method JavaDoc method, String JavaDoc whereType)
718     {
719         OperationInfo info = new OperationInfo();
720         info.setName(method.getName());
721         info.setReturnType(method.getReturnType().getName());
722         Class JavaDoc paramsClasses[]=method.getParameterTypes();
723         for(int k=0; k<paramsClasses.length; k++)
724         {
725             info.addParameter(new ParameterInfo("param"+(k+1), paramsClasses[k].getName(), null));
726         }
727         if (whereType!=null)
728             info.addField(newField(WHERE_LOCATED_FIELD_NAME, whereType));
729         return info;
730     }
731     
732     //*****************************************************************************
733
private static OperationInfo createOperationInfo(String JavaDoc name, String JavaDoc impact,
734           String JavaDoc returnType, ParameterInfo param,
735           String JavaDoc whereType)
736     {
737         OperationInfo info = new OperationInfo();
738         info.setName(name);
739         info.setImpact(impact);
740         if(returnType!=null)
741            info.setReturnType(returnType);
742         if(param!=null)
743             info.addParameter(param);
744         if (whereType!=null)
745             info.addField(newField(WHERE_LOCATED_FIELD_NAME, whereType));
746         return info;
747     }
748     //*****************************************************************************
749
private static void mergeWithOperationInfo(ManagedBean managedBean, OperationInfo info)
750     {
751         //OperationInfo[] infos = managedBean.getOperations();
752
//FIXME
753
managedBean.addOperation(info);
754     }
755             
756     private static String JavaDoc strArray[]=new String JavaDoc[0];
757     //*****************************************************************************
758
private static boolean supportedType( Class JavaDoc ret )
759     {
760         return ret == String JavaDoc.class ||
761         ret == Integer JavaDoc.class ||
762         ret == Integer.TYPE ||
763         ret == Long JavaDoc.class ||
764         ret == Long.TYPE ||
765         ret == java.io.File JavaDoc.class ||
766         ret == Boolean JavaDoc.class ||
767         ret == Boolean.TYPE ||
768         ret == strArray.getClass() || // XXX ???
769
ret == ObjectName JavaDoc.class
770         ;
771     }
772
773     //*****************************************************************************
774
private static String JavaDoc getAttrNameFromMethodName(String JavaDoc name, boolean bDecamelaze)
775     {
776         return getAttrNameFromMethodName(name, bDecamelaze, ATTRIBUTE_NAME_DELIMITER_SYMBOL);
777     }
778     //*****************************************************************************
779
private static String JavaDoc getAttrNameFromMethodName(String JavaDoc name, boolean bDecamelaze, char separatorSymbol)
780     {
781         if(name.startsWith("set") || name.startsWith("get"))
782             name = name.substring(3);
783         else
784             if(name.startsWith("is"))
785                 name = name.substring(2);
786         if(!bDecamelaze)
787             return name;
788         if(name.length()==0)
789             return name;
790         String JavaDoc attrName = name.toLowerCase();
791         char[] arr1 = name.toCharArray();
792         char[] arr2 = name.toLowerCase().toCharArray();
793         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(arr1.length*2);
794         buf.append(arr2[0]);
795         for(int i=1; i<arr1.length; i++)
796         {
797             if(arr1[i]!=arr2[i])
798                 buf.append(separatorSymbol);
799             buf.append(arr2[i]);
800         }
801         return buf.toString();
802     }
803     
804     //****************************************************************************************************
805
public static Class JavaDoc getConfigBeanClass(String JavaDoc xPath)
806     {
807         // get ConfigBean classname from XPath
808
String JavaDoc beanName = ConfigBeansFactory.getConfigBeanNameByXPath(xPath);
809         //getting the class object
810
try
811         {
812             Class JavaDoc cl = Class.forName("com.sun.enterprise.config.serverbeans."+beanName);
813             return cl;
814         }
815         catch(Exception JavaDoc e)
816         {
817             return null;
818         }
819     }
820     
821     //****************************************************************************************************
822
public static Class JavaDoc getRuntimeModelBeanClass(String JavaDoc j2eeType)
823     {
824         try
825         {
826             return Class.forName("com.sun.enterprise.management.model."+j2eeType+"Mdl");
827         }
828         catch(Exception JavaDoc e)
829         {
830 // e.printStackTrace();
831
return null;
832         }
833     }
834     
835     //****************************************************************************************************
836
public static String JavaDoc descriptorToString(FeatureInfo descr)
837     {
838         return descriptorToString(descr.getFields());
839     }
840     //****************************************************************************************************
841
public static String JavaDoc descriptorToString(List JavaDoc fields)
842     {
843         String JavaDoc str = "Descriptor[";
844         for(int i=0; i<fields.size(); i++)
845         {
846             FieldInfo field = (FieldInfo)fields.get(i);
847             str = str + field.getName() + "=" + field.getValue() + " ";
848         }
849         return str + "]";
850     }
851
852     //*******************************************************************************************
853
private static FieldInfo newField(String JavaDoc name, Object JavaDoc value)
854     {
855         FieldInfo info = new FieldInfo();
856         info.setName(name);
857         info.setValue(value);
858         return info;
859     }
860
861     //*******************************************************************************************
862
private static class AttrIntro
863     {
864         public String JavaDoc name = null;
865         public String JavaDoc type = null;
866         public String JavaDoc getName = null;
867         public String JavaDoc setName = null;
868         public String JavaDoc whereType = null;
869         
870         public AttributeInfo createAttributeInfo()
871         {
872             AttributeInfo ati = new AttributeInfo();
873             ati.setName(name);
874             ati.setType(type);
875             if(setName!=null)
876             {
877                 ati.setWriteable(true);
878                 ati.addField(newField(SETTER_FIELD_NAME, setName));
879             }
880             else
881                 ati.setWriteable(false);
882             if(getName!=null)
883             {
884                 ati.setReadable(true);
885                 ati.addField(newField(GETTER_FIELD_NAME, getName));
886             }
887             else
888                 ati.setReadable(false);
889             
890             if (whereType!=null)
891                 ati.addField(newField(WHERE_LOCATED_FIELD_NAME, whereType));
892             return ati;
893         }
894         
895         
896         public void mergeWithAttributeInfo(AttributeInfo ati)
897         {
898             ati.setType(type);
899             if(setName==null)
900                 ati.setWriteable(false);
901             if(getName==null)
902                 ati.setReadable(false);
903         }
904     }
905
906     //*******************************************************************************************
907
private static void addFieldToInfo(String JavaDoc name, String JavaDoc value, FeatureInfo info)
908     {
909         FieldInfo field = new FieldInfo();
910         field.setName(name);
911         field.setValue(value);
912         info.addField(field);
913     }
914     //*******************************************************************************************
915
private static void addDataToChildOperInfo(String JavaDoc childName, boolean bMulti, FeatureInfo info)
916     {
917         addFieldToInfo(CHILD_FIELD_NAME, childName, info);
918         if(bMulti)
919            addFieldToInfo(MULTI_FIELD_NAME, "true", info);
920     }
921
922     //*******************************************************************************************
923
// converts (if needed) the given name to corresponded MBean's Attribute name
924
public static String JavaDoc mapToMBeanAttributeName(String JavaDoc name)
925     {
926         if(ATTRIBUTE_NAME_DELIMITER_SYMBOL!='_' && name!=null)
927             return name.replace('_', ATTRIBUTE_NAME_DELIMITER_SYMBOL);
928         return name;
929     }
930     //*******************************************************************************************
931
// converts (if needed) the given name to corresponded ConfigBean Attribute name
932
public static String JavaDoc mapToConfigBeanAttributeName(String JavaDoc name)
933     {
934         if(name!=null)
935             return name.replace('_', '-');
936         return name;
937     }
938     //XPATH helpers
939
public static String JavaDoc cutAttributeTokenFromXPath(String JavaDoc xpath)
940    {
941        if(!xpath.endsWith("]") && !xpath.endsWith("/"))
942        {
943            int idx = xpath.lastIndexOf('/');
944            if(idx>0 && xpath.length()>idx+1 && xpath.charAt(idx+1)=='@')
945            {
946                return xpath.substring(0, idx);
947            }
948        }
949        return xpath;
950    }
951    public static String JavaDoc getMultipleElementKeyValue(String JavaDoc xpath)
952    {
953        if(xpath.endsWith("']"))
954        {
955            int idx = xpath.lastIndexOf('\'', xpath.length()-3);
956            if(idx>0)
957            {
958                return xpath.substring(idx+1, xpath.length()-2);
959            }
960        }
961        return null;
962    }
963    public static String JavaDoc extractLastElemNameFromXPath(String JavaDoc xpath)
964    {
965        if(xpath.endsWith("]"))
966        {
967            int idx = xpath.lastIndexOf('[');
968            if(idx>0)
969               xpath=xpath.substring(0,idx);
970        }
971        int idx = xpath.lastIndexOf('/');
972        if(idx>=0)
973            return xpath.substring(idx+1);
974        return null;
975    }
976    public static String JavaDoc cutLastElementFromXPath(String JavaDoc xpath)
977    {
978        if(xpath.endsWith("]"))
979        {
980            int idx = xpath.lastIndexOf('[');
981            if(idx>0)
982               xpath=xpath.substring(0,idx);
983        }
984        int idx = xpath.lastIndexOf('/');
985        if(idx>=0)
986            return xpath.substring(0, idx);
987        return null;
988    }
989 }
990
991
Popular Tags