1 23 24 package com.sun.enterprise.admin.server.core.mbean.meta; 25 26 27 import com.sun.enterprise.admin.util.PropertiesStringSource; 28 29 import java.util.ArrayList ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Constructor ; 33 34 import javax.management.MBeanAttributeInfo ; 36 import javax.management.MBeanConstructorInfo ; 37 import javax.management.MBeanOperationInfo ; 38 import javax.management.MBeanParameterInfo ; 39 import javax.management.MBeanNotificationInfo ; 40 import javax.management.MBeanInfo ; 41 import javax.management.IntrospectionException ; 42 43 import com.sun.enterprise.admin.common.exception.MBeanConfigException; 46 47 import com.sun.enterprise.util.i18n.StringManager; 49 50 54 55 public class MBeanEasyConfig { 57 private static StringManager localStrings = 59 StringManager.getManager( MBeanEasyConfig.class ); 60 61 final static String DESCR_LINE_DELIMITER = ","; 62 63 final static String STRINGSOUREC_FILENAME = "MBeanConfig.properties"; 65 66 final static String EXCEPTION_LINE_EMPTY = localStrings.getString( "admin.server.core.mbean.meta.empty_description_line" ); 68 final static String EXCEPTION_WRONG_LINE_FORMAT = localStrings.getString( "admin.server.core.mbean.meta.wrong_fields_number_in_description_line" ); 69 final static String EXCEPTION_NAME_FLD_EMPTY = localStrings.getString( "admin.server.core.mbean.meta.empty_name_in_description_line" ); 70 final static String EXCEPTION_CLASSNAME_FLD_EMPTY = localStrings.getString( "admin.server.core.mbean.meta.empty_classname_in_description_line" ); 71 final static String EXCEPTION_UNKNOWN_RWTYPE = localStrings.getString( "admin.server.core.mbean.meta.unknown_rw_type_in_description_line" ); 72 final static String EXCEPTION_UNKNOWN_IMPACT_TYPE = localStrings.getString( "admin.server.core.mbean.meta.unknown_impact_type_in_description" ); 73 final static String EXCEPTION_OPER_NOT_FOUND = localStrings.getString( "admin.server.core.mbean.meta.unfound_operation_in_bean_class" ); 74 75 final static String EXCEPTION_WRONG_PARAM_FORMAT = localStrings.getString( "admin.server.core.mbean.meta.wrong_operation_parameter_in_description" ); 76 final static String EXCEPTION_WRONG_PARAM_DIM = localStrings.getString( "admin.server.core.mbean.meta.wrong_operation_parameter_dimension_in_description" ); 77 final static String EXCEPTION_WRONG_PARAM_CLASS = localStrings.getString( "admin.server.core.mbean.meta.wrong_parameter_attribute_class_in_description" ); 78 final static String EXCEPTION_EMPTY_PARAM_TYPE = localStrings.getString( "admin.server.core.mbean.meta.empty_parameter_attribute_type_in_description_line" ); 79 final static String TYPE_INFO = "INFO"; 81 final static String TYPE_ACTION = "ACTION"; 82 final static String TYPE_ACTION_INFO = "ACTION_INFO"; 83 final static String TYPE_UNKNOWN = "UNKNOWN"; 84 85 private MBeanAttributeInfo [] m_attrs = new MBeanAttributeInfo [0]; 87 private MBeanConstructorInfo [] m_cstrs = new MBeanConstructorInfo [0]; 88 private MBeanOperationInfo [] m_opers = new MBeanOperationInfo [0]; 89 private MBeanNotificationInfo [] m_notfs = new MBeanNotificationInfo [0]; 90 private Class m_beanClass = null; private String m_descr = null; 93 117 public MBeanEasyConfig(Class configuringMBeanClass, String [] attrDescr, String [] operDescr, String descr) throws MBeanConfigException 118 { 119 m_beanClass = configuringMBeanClass; m_descr = descr; if(m_descr==null || m_descr.length()==0) 122 m_descr = ""+getPureClassName(configuringMBeanClass)+".mbean"; 123 124 m_attrs = createAttributesInfo(m_beanClass, attrDescr); 126 m_cstrs = createConstructorsInfo(m_beanClass, null); 127 m_opers = createOperationsInfo(m_beanClass, operDescr); 128 } 130 131 139 public static MBeanAttributeInfo [] createAttributesInfo(Class configuringMBeanClass, String [] attrDescr) throws MBeanConfigException 140 { 141 if(attrDescr==null || attrDescr.length<1) 142 return new MBeanAttributeInfo [0]; 143 MBeanAttributeInfo [] infos = new MBeanAttributeInfo [attrDescr.length]; 144 String beanName = getPureClassName(configuringMBeanClass); 145 for(int i=0; i<attrDescr.length; i++) 147 { 148 infos[i] = parseAttributeDescrLine(beanName, attrDescr[i]); 149 } 150 return infos; 151 } 152 153 161 public static MBeanOperationInfo [] createOperationsInfo(Class configuringMBeanClass, String [] operDescr) throws MBeanConfigException 162 { 163 if(configuringMBeanClass==null) 164 return new MBeanOperationInfo [0]; 165 if(operDescr==null || operDescr.length<1) 166 return new MBeanOperationInfo [0]; 167 ArrayList arr = new ArrayList (); 168 for(int i=0; i<operDescr.length; i++) 169 { 170 MBeanOperationInfo [] overloads = parseOperationDescrLine(configuringMBeanClass, operDescr[i]); 171 for(int j=0; j<overloads.length; j++) 172 arr.add(overloads[j]); 173 } 174 175 MBeanOperationInfo [] infos = new MBeanOperationInfo [arr.size()]; 176 for(int i=0; i<infos.length; i++) 178 { 179 infos[i] = (MBeanOperationInfo )arr.get(i); 180 } 181 return infos; 182 } 183 184 185 191 public static MBeanConstructorInfo [] createConstructorsInfo(Class configuringMBeanClass, String constructorDescr) 192 { 193 if(configuringMBeanClass==null) 194 return new MBeanConstructorInfo [0]; 195 if(constructorDescr==null) 196 { 197 String nameDescr = getPureClassName(configuringMBeanClass)+".constructor"; 198 constructorDescr = getResourceString(nameDescr, nameDescr); 199 } 200 Constructor [] ctrs = configuringMBeanClass.getConstructors(); 202 MBeanConstructorInfo [] infos = new MBeanConstructorInfo [ctrs.length]; 203 for(int i=0; i<ctrs.length; i++) 204 { 205 infos[i] = new MBeanConstructorInfo (constructorDescr, ctrs[i]); 206 } 207 return infos; 208 } 209 210 214 public MBeanInfo getMBeanInfo() 215 { 216 if(m_beanClass==null) 217 return null; 218 else 219 return new MBeanInfo (m_beanClass.getName(), m_descr, 220 m_attrs, m_cstrs, m_opers, m_notfs); 221 222 } 223 224 228 public void setMBeanDescription(String beanDescription) 229 { 230 m_descr = beanDescription; } 232 233 237 public void setAttributesInfo(MBeanAttributeInfo [] attributesInfo) 238 { 239 if(attributesInfo==null) 240 attributesInfo = new MBeanAttributeInfo [0]; 241 m_attrs = attributesInfo; 242 } 243 244 248 public void setOperationsInfo(MBeanConstructorInfo [] constructorInfo) 249 { 250 if(constructorInfo ==null) 251 constructorInfo = new MBeanConstructorInfo [0]; 252 m_cstrs = constructorInfo; 253 } 254 255 259 public void setOperationsInfo(MBeanOperationInfo [] operationsInfo) 260 { 261 if(operationsInfo ==null) 262 operationsInfo = new MBeanOperationInfo [0]; 263 m_opers = operationsInfo; 264 } 265 266 270 public void setNotificationsInfo(MBeanNotificationInfo [] notificationsInfo) 271 { 272 if(notificationsInfo==null) 273 notificationsInfo = new MBeanNotificationInfo [0]; 274 m_notfs = notificationsInfo; 275 } 276 277 278 284 public static Object convertStringValueToProperType(String value, String targetType) throws MBeanConfigException 285 { 286 if(value==null) 287 return null; 288 289 Class cl; 290 try 291 { 292 if((cl=getPrimitiveClass(targetType))!=null || (cl=getRelatedPrimitiveClass(targetType))!=null) 293 { 294 if(cl==Byte.TYPE) 295 return new Byte (value); 296 if(cl==Character.TYPE) 297 return new Character (value.charAt(0)); 298 if(cl==Double.TYPE) 299 return new Double (value); 300 if(cl==Float.TYPE) 301 return new Float (value); 302 if(cl==Integer.TYPE) 303 return new Integer (value); 304 if(cl==Long.TYPE) 305 return new Long (value); 306 if(cl==Short.TYPE) 307 return new Short (value); 308 if(cl==Boolean.TYPE) 309 return new Boolean (value); 310 } 311 } 312 catch(java.lang.IllegalArgumentException e) 313 { 314 String msg = localStrings.getString( "admin.server.core.mbean.meta.convertstringvaluetopropertype_wrong_argument_type", e.getMessage() ); 315 throw new MBeanConfigException( msg ); 316 } 317 return value; 319 } 320 321 327 private static MBeanAttributeInfo parseAttributeDescrLine(String beanName, String descrLine) throws MBeanConfigException 328 { 329 String name; 331 String className; 332 String rwType; 333 String descr; 334 boolean bReadable = false, bWritable = false; 335 336 if(descrLine==null) { 337 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_exception_line_empty", EXCEPTION_LINE_EMPTY ); 338 throw new MBeanConfigException( msg ); 339 } 340 341 String [] flds = getLineFields(descrLine, 4); 343 if(flds.length<3 || flds.length>4) { 344 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_LINE_FORMAT, descrLine ); 345 throw new MBeanConfigException( msg ); 346 } 347 348 name = flds[0]; 350 if(name.length()==0) { 351 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_NAME_FLD_EMPTY, descrLine ); 352 throw new MBeanConfigException( msg ); 353 } 354 355 className = flds[1]; 357 if(className.length()==0) { 358 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_CLASSNAME_FLD_EMPTY, descrLine ); 359 throw new MBeanConfigException( msg ); 360 } 361 className = convertTypeToSignatureClass(className).getName(); 362 363 364 rwType = flds[2]; 366 if(rwType.equals("R")) 367 bReadable = true; 368 else 369 if(rwType.equals("W")) 370 bWritable = true; 371 else 372 if(rwType.equals("RW") || rwType.equals("WR")) 373 { 374 bWritable = true; 375 bReadable = true; 376 } 377 else { 378 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_UNKNOWN_RWTYPE, descrLine ); 379 throw new MBeanConfigException( msg ); 380 } 381 382 String defaultName = beanName+"."+name+".attribute"; 384 385 if(flds.length<4 || flds[3].length()==0) 386 descr = getResourceString(defaultName, defaultName); 387 else 388 descr = getResourceString(flds[3], defaultName); 389 390 return new MBeanAttributeInfo (name, className, descr, bReadable, bWritable, false); 391 } 392 393 402 private static MBeanOperationInfo [] parseOperationDescrLine(Class configuringMBeanClass, String descrLine) throws MBeanConfigException 403 { 404 String name; 406 String typeName; 407 int type; 408 String descr; 409 410 if(descrLine==null) { 411 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_exception_line_empty", EXCEPTION_LINE_EMPTY ); 412 throw new MBeanConfigException( msg ); 413 } 414 415 int idx1 = descrLine.indexOf('('); 417 if(idx1<=0 || idx1==descrLine.length()-1) { 418 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_PARAM_FORMAT, descrLine ); 419 throw new MBeanConfigException( msg ); 420 } 421 name = descrLine.substring(0, idx1).trim(); 422 if(name.length()==0) { 423 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_NAME_FLD_EMPTY, descrLine ); 424 throw new MBeanConfigException( msg ); 425 } 426 int idx2 = descrLine.indexOf(')', idx1+1); 427 if(idx2<=0) { 428 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_PARAM_FORMAT, descrLine ); 429 throw new MBeanConfigException( msg ); 430 } 431 432 Object [] params = decomposeParametersDescription(descrLine.substring(idx1+1,idx2)); 433 434 String [] flds = getLineFields(descrLine.substring(idx2), 3); 436 if(flds.length<2) { 437 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_LINE_FORMAT, descrLine ); 438 throw new MBeanConfigException( msg ); 439 } 440 441 442 typeName = flds[1]; 444 if(typeName.equals(TYPE_INFO)) 445 type = MBeanOperationInfo.INFO; 446 else 447 if(typeName.equals(TYPE_ACTION)) 448 type = MBeanOperationInfo.ACTION; 449 else 450 if(typeName.equals(TYPE_ACTION_INFO)) 451 type = MBeanOperationInfo.ACTION_INFO; 452 else 453 if(typeName.equals(TYPE_UNKNOWN)) 454 type = MBeanOperationInfo.UNKNOWN; 455 else { 456 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_UNKNOWN_IMPACT_TYPE, descrLine ); 457 throw new MBeanConfigException( msg ); 458 } 459 460 String beanName = getPureClassName(configuringMBeanClass); 462 String defaultName = beanName+"."+name+".operation"; 463 if(flds.length<3 || flds[2].length()==0) 464 descr = getResourceString(defaultName, defaultName); 465 else 466 descr = getResourceString(flds[2], defaultName); 467 468 469 Class [] signature = new Class [params.length/3]; 471 for(int i=0; i<signature.length; i++) 472 signature[i] = (Class )params[i*3]; 473 Method method; 474 try { 475 method = configuringMBeanClass.getMethod(name, signature); 476 } catch (NoSuchMethodException e) 477 { 478 String msg = localStrings.getString( "admin.server.core.mbean.meta.parseoperationdescrline_wrong_exception_format", EXCEPTION_OPER_NOT_FOUND, descrLine ); 479 throw new MBeanConfigException( msg ); 480 } 481 MBeanParameterInfo [] infos = new MBeanParameterInfo [signature.length]; 482 for(int i=0; i<signature.length; i++) 483 { 484 String pName = (String )params[i*3+1]; 485 if(pName==null) 486 pName = ""; String pDescr = (String )params[i*3+2]; 488 if(pDescr==null) { 490 if(pName.length()>0) 491 pDescr = beanName+"."+name+"."+pName+".parameter"; 492 else 493 pDescr = ""; 494 } 495 infos[i] = new MBeanParameterInfo (pName, signature[i].getName(), pDescr); 496 } 497 return new MBeanOperationInfo [] {new MBeanOperationInfo (name, descr, infos, method.getReturnType().getName(), type)}; 498 499 500 } 507 508 static private String [] getLineFields(String line, int nMax) 512 { 513 ArrayList flds = new ArrayList (); 514 int idx1=0, idx2=100; 515 if(line==null || line.length()==0 || nMax==0) 516 return null; 517 while(idx2>0 && nMax>flds.size() && idx1<line.length()) 518 { 519 idx2 = line.indexOf(DESCR_LINE_DELIMITER, idx1); 520 if(idx2<0) 521 flds.add(line.substring(idx1).trim()); 522 else 523 flds.add(line.substring(idx1, idx2).trim()); 524 idx1 = idx2 + 1; 525 if(idx2==line.length()) 526 flds.add(""); 527 } 528 String [] strs = new String [flds.size()]; 529 for(int i=0; i<strs.length; i++) 530 { 531 strs[i] = (String )flds.get(i); 532 } 533 return strs; 534 } 535 536 static String getResourceString(String resourceID, String nullReplacer) 538 { 539 return resourceID; 558 } 559 560 static private Method [] getMethodsForName(Class configuringMBeanClass, String name) 562 { 563 ArrayList overloads = new ArrayList (); 564 Method [] all = configuringMBeanClass.getMethods(); 565 if(all==null) 566 return null; 567 for(int i=0; i<all.length; i++) 568 { 569 if(name.equals(all[i].getName())) 570 overloads.add(all[i]); 571 } 572 if(overloads.size()==0) 573 return null; 574 Method [] ret = new Method [overloads.size()]; 575 for(int i=0; i<ret.length; i++) 576 ret[i] = (Method )overloads.get(i); 577 return ret; 578 } 579 580 581 static private Class convertTypeToSignatureClass(String type) throws MBeanConfigException 585 { 586 type = type.trim(); 587 int idx = type.indexOf("["); 588 if(idx==0 || type.length()==0) { 589 String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_EMPTY_PARAM_TYPE, type ); 590 throw new MBeanConfigException( msg ); 591 } 592 try 593 { 594 if(idx>0) 595 { String name = type.substring(0,idx).trim(); 597 if(name.length()==0) { 598 String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_EMPTY_PARAM_TYPE, type ); 599 throw new MBeanConfigException( msg ); 600 } 601 String code = getPrimitiveCode(name); 602 if(code==null) 603 { 604 if(name.indexOf('.')<0) 605 name = "java.lang."+name; 606 return Class.forName(getArrayPrefix(type, idx) + "L" + name + ";"); 607 } 608 else 609 return Class.forName(getArrayPrefix(type, idx) + code) ; 610 } 611 else 612 { 613 Class cl = getPrimitiveClass(type); 614 if(cl!=null) 615 return cl; 616 618 try { 619 return Class.forName(type); 620 } 621 catch(ClassNotFoundException e) 622 { 623 if(type.indexOf('.')>0) { 624 String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_WRONG_PARAM_CLASS, type ); 625 throw new MBeanConfigException( msg ); 626 } 627 } 628 return Class.forName("java.lang."+type); 630 } 631 } 632 catch(ClassNotFoundException e) 633 { 634 String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_WRONG_PARAM_CLASS, type ); 635 throw new MBeanConfigException( msg ); 636 } 637 638 } 639 640 static private String getArrayPrefix(String line, int idx) throws MBeanConfigException 642 { 643 String pref = ""; 644 int dimCheck = 0; 645 for( ; idx<line.length(); idx++) 646 { 647 if(line.charAt(idx)=='[') 648 pref = pref + "["; 649 else 650 if(line.charAt(idx)==']') 651 dimCheck++; 652 } 653 if(pref.length()!=dimCheck) { 654 String msg = localStrings.getString( "admin.server.core.mbean.meta.getarrayprefix_wrong_exception_format", EXCEPTION_WRONG_PARAM_DIM, line ); 655 throw new MBeanConfigException( msg ); 656 } 657 return pref; 658 } 659 660 static Object [][] convTable = new Object [][] { 662 {"byte", "B", Byte.TYPE, "java.lang.Byte"}, 663 {"char", "C", Character.TYPE, "java.lang.Character"}, 664 {"double", "D", Double.TYPE, "java.lang.Double"}, 665 {"float", "F", Float.TYPE, "java.lang.Float"}, 666 {"int", "I", Integer.TYPE, "java.lang.Integer"}, 667 {"long", "J", Long.TYPE, "java.lang.Long"}, 668 {"short", "S", Short.TYPE, "java.lang.Short"}, 669 {"boolean", "Z", Boolean.TYPE, "java.lang.Boolean"}, 670 {"void", "V", Void.TYPE, "?"} }; 671 672 static private Object findConvTableElemByType(String type, int idx) 674 { 675 for(int i=0; i<convTable.length; i++) 676 { 677 if(type.equals((String )convTable[i][0])) 678 return convTable[i][idx]; 679 } 680 return null; 681 } 682 683 static private Object findConvTableElemByAssoClassName(String assoClass, int idx) 685 { 686 for(int i=0; i<convTable.length; i++) 687 { 688 if(assoClass.equals((String )convTable[i][3])) 689 return convTable[i][idx]; 690 } 691 return null; 692 } 693 694 static private String getPrimitiveCode(String type) 696 { 697 return (String )findConvTableElemByType(type, 1); 698 } 699 700 static private Class getPrimitiveClass(String type) 702 { 703 return (Class )findConvTableElemByType(type, 2); 704 } 705 706 static private Class getRelatedPrimitiveClass(String className) 708 { 709 return (Class )findConvTableElemByAssoClassName(className, 2); 710 } 711 712 static private Object [] decomposeParametersDescription(String line) throws MBeanConfigException 716 { 717 ArrayList flds = new ArrayList (); 718 int idx1=0, idx2=100; 719 line = line.trim(); 720 int len = line.length(); 721 int parmEnd; 722 if(len==0) 723 return new Object [0]; 724 String type; 725 while(idx1<len) 726 { 727 if((parmEnd=line.indexOf(',', idx1))<0) 728 parmEnd = len; 729 idx2 = line.indexOf(' ', idx1); 731 if(idx2>parmEnd) 732 idx2=parmEnd; 733 if(idx2<0) 734 { 735 flds.add(convertTypeToSignatureClass(line.substring(idx1))); 736 idx2 = parmEnd; 737 } 738 else 739 flds.add(convertTypeToSignatureClass(line.substring(idx1,idx2))); 740 idx1 = idx2 + 1; 741 while(idx1<parmEnd && line.charAt(idx1)==' ') 742 idx1++; 743 if(idx1>=parmEnd) 744 { 745 flds.add(null); flds.add(null); if(idx1==parmEnd) 748 idx1++; 749 while(idx1<len && line.charAt(idx1)==' ') 750 idx1++; 751 continue; 752 } 753 754 idx2 = line.indexOf(' ', idx1); 756 if(idx2>parmEnd) 757 idx2=parmEnd; 758 if(idx2<0) 759 { 760 flds.add(line.substring(idx1).trim()); 761 idx2 = parmEnd; 762 } 763 else 764 flds.add(line.substring(idx1,idx2).trim()); 765 idx1 = idx2 + 1; 766 while(idx1<parmEnd && line.charAt(idx1)==' ') 767 idx1++; 768 if(idx1>=parmEnd) 769 { 770 flds.add(null); if(idx1==parmEnd) 772 idx1++; 773 while(idx1<len && line.charAt(idx1)==' ') 774 idx1++; 775 continue; 776 } 777 778 idx2 = line.indexOf(',', idx1); 780 if(idx2<0) 781 { 782 flds.add(line.substring(idx1).trim()); 783 idx2 = len; 784 } 785 else 786 flds.add(line.substring(idx1,idx2).trim()); 787 idx1 = idx2 + 1; 788 while(idx1<len && line.charAt(idx1)==' ') 789 idx1++; 790 } 791 792 Object [] strs = new Object [flds.size()]; 793 for(int i=0; i<strs.length; i++) 794 { 795 strs[i] = flds.get(i); 796 } 797 return strs; 798 } 799 private static String getPureClassName(Class configuringMBeanClass) 801 { 802 if(configuringMBeanClass==null) 803 return null; 804 String className = configuringMBeanClass.getName(); 805 int idx = className.lastIndexOf('.'); 806 if(idx>=0) 807 return className.substring(idx+1); 808 return className; 809 } 810 } 811 812 813 | Popular Tags |