1 27 28 package com.adventnet.jmx; 29 30 import java.lang.reflect.Constructor ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.util.Arrays ; 34 import java.util.ArrayList ; 35 import java.util.Vector ; 36 37 import javax.management.Attribute ; 38 import javax.management.AttributeList ; 39 import javax.management.DynamicMBean ; 40 import javax.management.ObjectName ; 41 import javax.management.MBeanServer ; 42 import javax.management.MBeanServerFactory ; 43 import javax.management.MBeanRegistration ; 44 import javax.management.MBeanInfo ; 45 import javax.management.MBeanAttributeInfo ; 46 import javax.management.MBeanConstructorInfo ; 47 import javax.management.MBeanOperationInfo ; 48 import javax.management.MBeanParameterInfo ; 49 import javax.management.MBeanNotificationInfo ; 50 import javax.management.Notification ; 51 import javax.management.NotificationListener ; 52 import javax.management.NotificationFilter ; 53 import javax.management.NotificationBroadcaster ; 54 import javax.management.AttributeNotFoundException ; 55 import javax.management.ListenerNotFoundException ; 56 import javax.management.MBeanException ; 57 import javax.management.NotCompliantMBeanException ; 58 import javax.management.ReflectionException ; 59 import javax.management.RuntimeErrorException ; 60 import javax.management.RuntimeMBeanException ; 61 import javax.management.RuntimeOperationsException ; 62 import javax.management.InvalidAttributeValueException ; 63 64 70 public class DefaultDynamicMBean implements DynamicMBean , 71 NotificationBroadcaster , 72 NotificationListener , 73 NotificationFilter , 74 MBeanRegistration 75 { 76 public MBeanInfo mbeanInfo = null; 77 public Object object = null; 78 public Class clazz = null; 79 public Class iclazz = null; 80 public Method [] superInterfaceMeths = null; 81 public Vector readMeths = null; 82 public Vector writeMeths = null; 83 public Vector isIsMeths = null; 84 85 public DefaultDynamicMBean(Object object) throws Exception 86 { 87 this.object = object; 88 clazz = object.getClass(); 89 90 Class [] interfaces = clazz.getInterfaces(); 91 92 boolean flag = false; 93 94 if(interfaces == null) 95 { 96 clazz = clazz.getSuperclass(); 97 if(clazz == null) 98 throw new Exception ("NonJmxMBeanRegistrationException"); 99 if((interfaces = clazz.getInterfaces()) == null) 100 throw new Exception ("NonJmxMBeanRegistrationException"); 101 } 102 else 103 { 104 for(int i = 0;i<interfaces.length;i++) 105 { 106 if(interfaces[i].getName().equals(clazz.getName() + "MBean") ) 107 { 108 iclazz = interfaces[i]; 109 flag = true; 110 111 Class [] interClass = interfaces[i].getInterfaces(); 112 if(interClass != null) 113 { 114 if(interClass.length >0) 115 { 116 superInterfaceMeths = interClass[0].getMethods(); 117 } 118 } 119 break; 120 } 121 } 122 123 if(!flag) 124 { 125 clazz = clazz.getSuperclass(); 126 if(clazz == null) 127 throw new Exception ("NonJmxMBeanRegistrationException"); 128 if((interfaces = clazz.getInterfaces()) == null) 129 throw new Exception ("NonJmxMBeanRegistrationException"); 130 } 131 } 132 133 if(!flag) 134 { 135 for(int i = 0;i<interfaces.length;i++) 136 { 137 if(interfaces[i].getName().equals(clazz.getName() + "MBean") ) 138 { 139 iclazz = interfaces[i]; 140 flag = true; 141 break; 142 } 143 } 144 145 while(!flag) 146 { 147 flag = getFlagValue(clazz,flag); 148 if(clazz==null) 149 break; 150 } 151 } 152 153 if(!flag) 154 throw new Exception ("NonJmxMBeanRegistrationException"); 155 156 clazz = object.getClass(); 157 makeMBeanInfo(); 158 } 159 160 public Object getStandardMBeanObject() 161 { 162 return object; 163 } 164 165 protected void makeMBeanInfo() throws Exception 166 { 167 String className = clazz.getName(); 168 String description = className + " MBean"; 169 170 mbeanInfo = new MBeanInfo (className, 171 description, 172 getAttributes(), 173 getConstructors(), 174 getOperations(), 175 getNotifications()); 176 } 177 178 private MBeanAttributeInfo [] getAttributes() throws Exception 179 { 180 int length = 0; 181 int size = 0; 182 int pos = 0; 183 Method [] meths = iclazz.getMethods(); 186 readMeths = new Vector (); 187 writeMeths = new Vector (); 188 isIsMeths = new Vector (); 189 Vector readableVec= new Vector (); 190 Vector writableVec= new Vector (); 191 Vector isWriteStatus = new Vector (); 192 String [] types = null; 193 String [] names = null; 194 195 201 202 203 204 205 for(int i=0; i<meths.length; i++){ 206 String methName = meths[i].getName(); 208 211 if(methName.equals("get") || methName.equals("set") || methName.equals("is")){ 212 continue; 213 } 214 if((methName.startsWith("get") && meths[i].getParameterTypes().length == 0 ) && !(meths[i].getReturnType().getName().equals("void"))|| (methName.startsWith("is") && (meths[i].getReturnType().getName().equals("boolean") || meths[i].getReturnType().getName().equals("java.lang.Boolean")) && 215 meths[i].getParameterTypes().length == 0 )){ 216 if(methName.startsWith("get")){ 218 221 readMeths.add(meths[i]); 222 } 223 else{ 224 isIsMeths.add(meths[i]); 225 228 } 229 } 230 if(methName.startsWith("set") && meths[i].getParameterTypes().length ==1 && meths[i].getReturnType().getName().equals("void")){ 231 writeMeths.add(meths[i]); 232 235 } 236 237 } 238 239 240 for(int i=0; i<readMeths.size(); i++){ 241 boolean isMatched = false; 242 Method read = (Method )readMeths.get(i); 243 244 readMeths.remove(i); 245 if(isAttributeMethodOverloaded(read)){ 246 throw new NotCompliantMBeanException (); 247 } 248 readMeths.add(i,read); 249 for(int j=0;j<writeMeths.size(); j++){ 250 Method write = (Method )writeMeths.get(j); 251 writeMeths.remove(j); 252 if(isAttributeMethodOverloaded(write)){ 253 throw new NotCompliantMBeanException (); 254 } 255 writeMeths.add(j,write); 256 if(((Method )readMeths.get(i)).getName().substring(3).equals(((Method )writeMeths.get(j)).getName().substring(3))){ 257 if(!(read.getReturnType().getName().equals(write.getParameterTypes()[0].getName()))){ 258 throw new NotCompliantMBeanException (); 259 } 260 readableVec.add(new Boolean (true)); 261 writableVec.add(new Boolean (true)); 262 writeMeths.remove(j); 263 isMatched = true; 264 break; 265 } 266 } 267 if(!isMatched){ 268 readableVec.add(new Boolean (true)); 269 writableVec.add(new Boolean (false)); 270 } 271 } 272 273 for(int i=0;i<isIsMeths.size();i++){ 274 boolean isMatched = false; 275 Method is = (Method )isIsMeths.get(i); 276 isIsMeths.remove(i); 277 if(isAttributeMethodOverloaded(is)){ 278 throw new NotCompliantMBeanException (); 279 } 280 isIsMeths.add(i,is); 281 for(int j=0;j<writeMeths.size(); j++){ 282 Method write = (Method )writeMeths.get(j); 283 if(is.getName().substring(2).equals(write.getName().substring(3))){ 284 if(!(is.getReturnType().getName().equals(write.getParameterTypes()[0].getName()))){ 285 throw new NotCompliantMBeanException (); 286 } 287 290 isWriteStatus.add(new Boolean (true)); 291 isMatched = true; 292 writeMeths.remove(j); 293 break; 294 } 295 } 296 if(!isMatched){ 297 isWriteStatus.add(new Boolean (false)); 300 } 301 } 302 303 304 for(int i=0; i<writeMeths.size();i++){ 305 Method meth = (Method )writeMeths.get(i); 306 writeMeths.remove(i); 307 if(isAttributeMethodOverloaded(meth)){ 308 throw new NotCompliantMBeanException (); 309 } 310 writeMeths.add(i,meth); 311 readableVec.add(new Boolean (false)); 312 writableVec.add(new Boolean (true)); 313 } 316 size = readMeths.size()+writeMeths.size(); 317 types = new String [size]; 318 names = new String [readMeths.size()+writeMeths.size()+isIsMeths.size()]; 319 pos = 0; 320 321 322 for(int i=0; i< readMeths.size(); i++){ 323 names[i] = ((Method )readMeths.get(i)).getName().substring(3); 324 types[i] = (((Method )readMeths.get(i)).getReturnType().getName()); 326 } 327 for(int i=readMeths.size(); i<size; i++){ 328 types[i] = (((Method )writeMeths.get(pos)).getParameterTypes()[0].getName()); 330 names[i] = ((Method )writeMeths.get(pos)).getName().substring(3); 331 pos++; 332 } 333 334 MBeanAttributeInfo [] toRet = new MBeanAttributeInfo [names.length]; 335 String des = "This attribute is present in a Standard MBean"; 336 for(int i = 0; i<size;i++) 337 { 338 339 if(clazz.getName().equals("javax.management.MBeanServerDelegate")) 340 { 341 if(names[i].equals("MBeanServerId")) 342 des = "Specifies the ID of this JMX MBean Server"; 343 else if(names[i].equals("ImplementationName")) 344 des = "Specifies the JMX implementation name (the name of this product)."; 345 else if(names[i].equals("ImplementationVendor")) 346 des = "Specifies the JMX implementation vendor (the vendor of this product)"; 347 else if(names[i].equals("ImplementationVersion")) 348 des = "Specifies the JMX implementation version (the version of this product). "; 349 else if(names[i].equals("SpecificationName")) 350 des = "Specifies the full name of the JMX specification implemented by this product."; 351 else if(names[i].equals("SpecificationVendor")) 352 des = "Specifies the vendor of the JMX specification implemented by this product."; 353 else if(names[i].equals("SpecificationVersion")) 354 des = "Specifies the version of the JMX specification implemented by this product."; 355 } 356 else if(clazz.getName().equals("javax.management.loading.MLet")) 357 { 358 if(names[i].equals("URLs")) 359 des = "specifies the search path of URLs for loading classes and resources."; 360 } 361 362 toRet[i] = new MBeanAttributeInfo (names[i],types[i],des,((Boolean )readableVec.get(i)).booleanValue(),((Boolean )writableVec.get(i)).booleanValue(),false); 363 } 364 365 pos =0; 366 for(int i=size; i<toRet.length; i++){ 368 Method meth = (Method )isIsMeths.get(pos); 369 toRet[i] = new MBeanAttributeInfo (meth.getName().substring(2),meth.getReturnType().getName(),des,true,((Boolean )isWriteStatus.get(pos)).booleanValue(),true); 371 pos++; 372 } 374 375 return toRet; 376 } 377 378 private boolean isAttributeMethodOverloaded(Method toCheckMeth){ 379 String toCheckStr = toCheckMeth.getName(); 380 String methStr = null; 381 if(toCheckStr.startsWith("is")){ 382 toCheckStr = toCheckStr.substring(2); 383 } 384 else if(toCheckStr.startsWith("get")){ 385 toCheckStr = toCheckStr.substring(3); 386 } 387 if(toCheckMeth.getName().startsWith("is") || toCheckMeth.getName().startsWith("get")){ 388 for(int i=0; i<readMeths.size(); i++){ 389 Method meth = (Method )readMeths.get(i); 390 methStr = meth.getName(); 391 if(toCheckStr.equals(methStr.substring(3))){ 392 if(!(toCheckMeth.getReturnType().getName().equals(meth.getReturnType().getName())) 393 || (toCheckMeth.getDeclaringClass().getName().equals(meth.getDeclaringClass().getName()))){ 394 return true; 396 } 397 readMeths.remove(i); 398 } 400 } 401 for(int i=0; i<isIsMeths.size(); i++){ 402 Method meth = (Method )isIsMeths.get(i); 403 methStr = meth.getName(); 404 if(toCheckStr.equals(methStr.substring(2))){ 405 if(!(toCheckMeth.getReturnType().getName().equals(meth.getReturnType().getName())) 406 || (toCheckMeth.getDeclaringClass().getName().equals(meth.getDeclaringClass().getName()))) 407 { 408 return true; 410 } 411 isIsMeths.remove(i); 412 } 414 } 415 } 416 else if(toCheckMeth.getName().startsWith("set")){ 417 toCheckStr = toCheckStr.substring(3); 418 for(int i=0; i<writeMeths.size();i++){ 419 Method meth = (Method )writeMeths.get(i); 420 methStr = ((Method )writeMeths.get(i)).getName(); 421 if(toCheckStr.equals(methStr.substring(3))){ 422 if(!(toCheckMeth.getParameterTypes()[0].getName().equals(meth.getParameterTypes()[0].getName()))||(toCheckMeth.getDeclaringClass().getName().equals(meth.getDeclaringClass().getName()))){ 423 return true; 425 } 426 writeMeths.remove(i); 427 } 429 } 430 } 431 return false; 432 } 433 434 public MBeanConstructorInfo [] getConstructors() 435 { 436 Constructor [] constrs = clazz.getConstructors(); 437 MBeanConstructorInfo [] toRet = new MBeanConstructorInfo [constrs.length]; 438 439 for(int i=0;i<toRet.length;i++) 440 { 441 Class [] cparams = constrs[i].getParameterTypes(); 442 MBeanParameterInfo [] params = new MBeanParameterInfo [cparams.length]; 443 444 for(int j=0;j<params.length;j++) 445 { 446 params[j] = new MBeanParameterInfo ( ("param" + j ), 447 cparams[j].getName(), 448 "param to constructor"); 449 } 450 451 toRet[i] = new MBeanConstructorInfo (constrs[i].getName(), 452 constrs[i].toString(), 453 params); 454 } 455 456 return toRet; 457 } 458 459 public MBeanOperationInfo [] getOperations() 460 { 461 int length = 0; 462 int pos = 0; 463 464 Method [] meths = iclazz.getMethods(); 465 466 Vector operInfo = new Vector (); 468 MBeanOperationInfo mbOperInfo = null; 469 470 for(int i=0;i<meths.length; i++) 471 { 472 String methName = meths[i].getName(); 473 String returnType = meths[i].getReturnType().getName(); 474 Class [] mparams = meths[i].getParameterTypes(); 475 if(methName.startsWith("get") && !methName.equals("get")&& 476 (mparams.length ==0) &&!(returnType.equals("void"))){ 477 continue; 478 } 479 if(methName.startsWith("is") && !methName.equals("is")&& 480 (mparams.length ==0) &&(returnType.equals("boolean")|| returnType.equals("java.lang.Boolean"))){ 481 continue; 482 } 483 if(methName.startsWith("set") && !methName.equals("set")&& 484 (mparams.length == 1) && returnType.equals("void")){ 485 continue; 486 } 487 488 MBeanParameterInfo [] params = new MBeanParameterInfo [mparams.length]; 489 for(int j=0;j<params.length;j++) 491 { 492 params[j] = new MBeanParameterInfo (("param"+j),mparams[j].getName(),"param to method"); 495 } 496 String des = "Operation exposed for management"; 497 if(clazz.getName().equals("javax.management.loading.MLet")) 498 { 499 if(meths[i].getName().equals("addURL")) 500 des = "Appends the specified URL to the list of URLs to search for classes and resources."; 501 else if(meths[i].getName().equals("getMBeansFromURL")) 502 des = "Loads a text file containing MLET tags that define the MBeans to be added to the agent."; 503 } 504 505 mbOperInfo = new MBeanOperationInfo (meths[i].getName(),des,params,meths[i].getReturnType().getName(),MBeanOperationInfo.ACTION); 506 operInfo.add(mbOperInfo); 507 } 508 509 int size = operInfo.size(); 510 MBeanOperationInfo [] toRet = new MBeanOperationInfo [size]; 511 for(int i=0; i<size; i++){ 512 toRet[i] = (MBeanOperationInfo )operInfo.get(i); 513 } 514 return toRet; 515 } 516 517 public MBeanNotificationInfo [] getNotifications() 518 { 519 return null; 521 } 522 523 public MBeanInfo getMBeanInfo() 524 { 525 return mbeanInfo; 526 } 527 528 public Object getAttribute(String attribute) throws javax.management.AttributeNotFoundException , 530 javax.management.MBeanException , 531 javax.management.ReflectionException 532 { 533 try{ 534 535 Class e = null; 536 e = object.getClass(); 537 Method attrMethod = null; 538 Object [] arguments = new Object [0]; 539 540 try{ 541 MBeanAttributeInfo [] attrInfo = getMBeanInfo().getAttributes(); 543 boolean isAttributeFound = false; 544 for(int i=0; i<attrInfo.length; i++){ 545 if(attribute.equals(attrInfo[i].getName())){ 546 isAttributeFound = true; 547 } 548 } 549 if(!isAttributeFound){ 550 throw new AttributeNotFoundException (); 551 } 552 553 attrMethod = e.getMethod("get"+attribute,null); 554 557 return attrMethod.invoke(object,arguments); 558 }catch(Exception ee){ 559 560 if(ee instanceof NoSuchMethodException ){ 561 attrMethod = e.getMethod("is"+attribute,null); 562 return attrMethod.invoke(object,arguments); 563 } 564 throw ee; 565 } 566 }catch(Exception e){ 567 if(e instanceof AttributeNotFoundException ){ 568 throw (AttributeNotFoundException )e; 569 } 570 if(e instanceof NoSuchMethodException ) 571 throw new AttributeNotFoundException (); 572 573 if(e instanceof InvocationTargetException ) 574 { 575 576 InvocationTargetException ite = (InvocationTargetException )e; 577 Throwable th = ite.getTargetException(); 578 if(th instanceof Error ){ 579 Error er = (Error )th; 580 RuntimeErrorException ree = new RuntimeErrorException (er); 581 throw ree; 582 } 583 Exception e1 = (Exception )((InvocationTargetException )e).getTargetException(); 584 throw new ReflectionException (e1); 585 } 586 throw new ReflectionException (e); 587 } 588 } 589 590 public AttributeList getAttributes(String [] attributes) 591 { 593 AttributeList toRet = null; 594 if(attributes == null) 595 return toRet; 596 597 toRet = new AttributeList (); 598 for(int i = 0; i<attributes.length;i++) 599 { 600 Attribute attr = null; 601 602 try{ 603 attr = new Attribute (attributes[i], getAttribute(attributes[i])); 604 }catch(Exception e){ 605 attr = new Attribute (attributes[i], e); 606 } 607 toRet.add(attr); 608 } 609 610 return toRet; 611 } 612 613 public Object invoke(String actionName, Object [] params, String [] signature) 614 throws javax.management.MBeanException , 615 javax.management.ReflectionException 616 { 617 Class e = null; 618 try 619 { 620 e = object.getClass(); 621 622 MBeanOperationInfo [] opers = mbeanInfo.getOperations(); 623 624 MBeanOperationInfo oper = null; 625 for(int i=0;i<opers.length;i++) 626 { 627 if(opers[i].getName().equals(actionName)) 628 { 629 MBeanParameterInfo [] pars = opers[i].getSignature(); 630 if(pars == null) 631 break; 632 633 if(params != null) 634 { 635 if( !(pars.length == params.length) ) 636 { 637 if(i != (opers.length-1)){ 638 continue; 639 } 640 else{ 641 throw new RuntimeOperationsException (new IllegalArgumentException ()); 642 } 643 } 644 } 645 646 if(params != null && signature != null) 647 { 648 if(params.length != signature.length) 649 { 650 throw new ReflectionException (new IllegalArgumentException ()); 651 } 652 } 653 654 int j=0; 655 for(;j<pars.length;j++) 656 { 657 if(params[j] != null){ 658 String jmxParam = convertToJmxArrayType(signature[j]); 659 660 if(!pars[j].getType().equals(jmxParam)) 661 { 662 String temp = convertToWrapperType(pars[j].getType()); 663 664 if((temp == null) || (!(temp.equals(jmxParam)))) 665 continue; 666 } 667 } 668 669 670 671 } 672 if(j < pars.length) 673 continue; 674 675 oper = opers[i]; 676 break; 677 } 678 } 679 680 if(oper == null){ 681 682 692 693 Class [] paramClass = null; 694 if(params != null){ 695 if(params.length == 0 || params.length == 1){ 696 paramClass = new Class [params.length]; 697 for(int i=0; i<params.length;i++){ 698 paramClass[i] = params[i].getClass(); 699 } 700 } 701 else{ 702 throw new RuntimeOperationsException (new IllegalArgumentException ()); 704 } 705 } 706 Method attrMethod = null; 707 try{ 708 attrMethod = e.getMethod(actionName,paramClass); 709 }catch(NoSuchMethodException ne){ 710 if(paramClass.length == 1){ 711 Method [] meths = e.getMethods(); 712 for(int i=0;i< meths.length; i++){ 713 if(meths[i].getName().equals(actionName)){ 714 Class [] parameters = meths[i].getParameterTypes(); 715 if(parameters[0].isAssignableFrom(paramClass[0])){ 716 attrMethod = meths[i]; 717 return attrMethod.invoke(object,params); 718 } 719 } 720 } 721 if(attrMethod == null){ 722 paramClass[0] = getPrimitiveClass(paramClass[0].getName()); 723 attrMethod = e.getMethod(actionName,paramClass); 724 } 725 } 726 else{ 727 throw new MBeanException (new Exception ()); 728 } 729 } 730 if(attrMethod == null){ 731 throw new MBeanException (new Exception ()); 732 } 733 return attrMethod.invoke(object,params); 734 } 736 MBeanParameterInfo [] pars = oper.getSignature(); 737 Class parameterTypes[]=null; 738 if(params != null){ 739 parameterTypes = new Class [params.length]; 740 for(int i=0; i<params.length; i++) 741 { 742 Class clazz = getProperClass(pars[i].getType()); 743 if(clazz == null) 744 clazz = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 745 746 parameterTypes[i] = clazz; 747 } 748 } 749 750 Method attrMethod = e.getMethod(actionName,parameterTypes); 751 return attrMethod.invoke(object,params); 752 } 753 catch(ReflectionException re) 754 { 755 throw re; 756 } 757 catch(Exception ex) 758 { 759 if(ex instanceof InvocationTargetException ) 760 { 761 762 Exception ee = null; 763 InvocationTargetException ite = (InvocationTargetException )ex; 764 Throwable th = ite.getTargetException(); 765 if(th instanceof Error ){ 766 Error er = (Error )th; 767 RuntimeErrorException ree = new RuntimeErrorException (er); 768 throw ree; 769 } 770 else{ 771 if(th instanceof RuntimeException ){ 773 throw new RuntimeMBeanException ((RuntimeException )th); 774 } 775 if(th instanceof ReflectionException ){ 776 throw new MBeanException ((ReflectionException )th); 777 } 778 } 779 throw new MBeanException ((Exception )th); 780 } 781 if(ex instanceof RuntimeOperationsException ){ 782 throw (RuntimeOperationsException )ex; 783 } 784 if(ex instanceof RuntimeException ){ 785 throw new RuntimeMBeanException ((RuntimeException )ex); 786 } 787 788 if(ex instanceof ClassNotFoundException ) 789 { 790 791 try{ 792 Class [] classParams = new Class [params.length]; 793 for(int i=0; i<params.length; i++){ 794 if(isPrimitiveDataType(signature[i])) 795 classParams[i] = getProperClass(signature[i]); 796 else 797 classParams[i] = params[i].getClass(); 798 } 799 Method meth = e.getMethod(actionName,classParams); 801 return meth.invoke(object,params); 802 819 }catch(NoSuchMethodException ne){ 820 try 821 { 822 Class [] cParams = new Class [params.length]; 823 for(int i=0; i<params.length; i++){ 824 try{ 825 cParams[i] = DefaultLoaderRepositoryExt.loadClass(signature[i]); 826 }catch(Exception exc){ 827 if(isPrimitiveDataType(signature[i])) 830 cParams[i] = getProperClass(signature[i]); 831 else 832 cParams[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 833 } 834 } 835 Method meth = e.getMethod(actionName,cParams); 836 return meth.invoke(object,params); 837 }catch(NoSuchMethodException nee) 838 { 839 throw new ReflectionException (ne); 840 }catch(Exception eee) 841 { 842 throw new MBeanException (eee); 843 } 844 }catch(Exception ene){ 845 if(ene instanceof MBeanException ) 846 { 847 throw (MBeanException )ene; 848 } 849 850 throw new MBeanException (ene); 851 } 852 } 853 throw new MBeanException (ex); 854 } 856 } 857 858 private boolean isPrimitiveDataType(String type) 859 { 860 if(type.endsWith("int")) 861 return true; 862 else if(type.endsWith("long")) 863 return true; 864 else if(type.endsWith("byte")) 865 return true; 866 else if(type.endsWith("float")) 867 return true; 868 else if(type.endsWith("char")) 869 return true; 870 else if(type.endsWith("short")) 871 return true; 872 else if(type.endsWith("double")) 873 return true; 874 else if(type.endsWith("boolean")) 875 return true; 876 877 return false; 878 879 } 880 881 private boolean isPrimitiveArrayDataType(String type) 882 { 883 if(type.endsWith("int[]")) 884 return true; 885 else if(type.endsWith("long[]")) 886 return true; 887 else if(type.endsWith("byte[]")) 888 return true; 889 else if(type.endsWith("float[]")) 890 return true; 891 else if(type.endsWith("char[]")) 892 return true; 893 else if(type.endsWith("short[]")) 894 return true; 895 else if(type.endsWith("double[]")) 896 return true; 897 else if(type.endsWith("boolean[]")) 898 return true; 899 900 return false; 901 902 } 903 904 private String convertToWrapperType(String type) 905 { 906 if(type.endsWith("int")) 907 return "java.lang.Integer"; 908 else if(type.endsWith("int[]")) 909 return "[Ljava.lang.Integer;"; 910 else if(type.endsWith("long")) 911 return "java.lang.Long"; 912 else if(type.endsWith("long[]")) 913 return "[Ljava.lang.Long;"; 914 else if(type.endsWith("byte")) 915 return "java.lang.Byte"; 916 else if(type.endsWith("byte[]")) 917 return "[Ljava.lang.Byte;"; 918 else if(type.endsWith("float")) 919 return "java.lang.Float"; 920 else if(type.endsWith("float[]")) 921 return "[Ljava.lang.Float;"; 922 else if(type.endsWith("char")) 923 return "java.lang.Character"; 924 else if(type.endsWith("char[]")) 925 return "[Ljava.lang.Character;"; 926 else if(type.endsWith("short")) 927 return "java.lang.Short"; 928 else if(type.endsWith("short[]")) 929 return "[Ljava.lang.Short;"; 930 else if(type.endsWith("double")) 931 return "java.lang.Double"; 932 else if(type.endsWith("double[]")) 933 return "[Ljava.lang.Double;"; 934 else if(type.endsWith("boolean")) 935 return "java.lang.Boolean"; 936 else if(type.endsWith("boolean[]")) 937 return "[Ljava.lang.Boolean;"; 938 939 return null; 940 941 } 942 943 private Class getProperClass(String type) 944 { 945 if(type.endsWith("int[]")) 946 return (new int[0]).getClass(); 947 else if(type.endsWith("long[]")) 948 return (new long[0]).getClass(); 949 else if(type.endsWith("byte[]")) 950 return (new byte[0]).getClass(); 951 else if(type.endsWith("float[]")) 952 return (new float[0]).getClass(); 953 else if(type.endsWith("char[]")) 954 return (new char[0]).getClass(); 955 else if(type.endsWith("short[]")) 956 return (new short[0]).getClass(); 957 else if(type.endsWith("double[]")) 958 return (new double[0]).getClass(); 959 else if(type.endsWith("boolean[]")) 960 return (new boolean[0]).getClass(); 961 else if(type.endsWith("String[]")){ 962 return (new String [0]).getClass(); 963 } 964 965 if(type.endsWith("int")) 966 return int.class; 967 else if(type.endsWith("long")) 968 return long.class; 969 else if(type.endsWith("byte")) 970 return byte.class; 971 else if(type.endsWith("float")) 972 return float.class; 973 else if(type.endsWith("char")) 974 return char.class; 975 else if(type.endsWith("short")) 976 return short.class; 977 else if(type.endsWith("double")) 978 return double.class; 979 else if(type.endsWith("boolean")) 980 return boolean.class; 981 982 return null; 983 } 984 985 private Class getPrimitiveClass(String className){ 986 if(className.equals("java.lang.Integer")){ 987 return Integer.TYPE; 988 } 989 else if(className.equals("java.lang.Long")){ 990 return Long.TYPE; 991 } 992 else if(className.equals("java.lang.Short")){ 993 return Short.TYPE; 994 } 995 else if(className.equals("java.lang.Float")){ 996 return Float.TYPE; 997 } 998 else if(className.equals("java.lang.Double")){ 999 return Double.TYPE; 1000 } 1001 else if(className.equals("java.lang.Character")){ 1002 return Character.TYPE; 1003 } 1004 else if(className.equals("java.lang.Boolean")){ 1005 return Boolean.TYPE; 1006 } 1007 1008 return null; 1009 } 1010 1011 private String convertToJmxArrayType(String type) 1012 { 1013 if(type.equals("[I") ) 1014 return "int[]"; 1015 else if(type.equals("[J")) 1016 return "long[]"; 1017 else if(type.equals("[B")) 1018 return "byte[]"; 1019 else if(type.equals("[F")) 1020 return "float[]"; 1021 else if(type.equals("[C")) 1022 return "char[]"; 1023 else if(type.equals("[S")) 1024 return "short[]"; 1025 else if(type.equals("[D")) 1026 return "double[]"; 1027 else if(type.equals("[Z")) 1028 return "boolean[]"; 1029 1030 if(type.startsWith("[L") && type.endsWith(";")) 1031 return (type.substring(2, type.length() -1) + "[]"); 1032 1033 return type; 1034 } 1035 1036 public void setAttribute(Attribute attribute) 1037 throws javax.management.AttributeNotFoundException , 1039 javax.management.InvalidAttributeValueException , 1040 javax.management.MBeanException , 1041 javax.management.ReflectionException 1042 { 1043 1044 if(attribute == null) 1045 throw new RuntimeOperationsException (new IllegalArgumentException ("Null values not possible")); 1046 1047 try 1048 { 1049 Class e = null; 1050 try 1051 { 1052 e = Thread.currentThread().getContextClassLoader().loadClass(mbeanInfo.getClassName()); 1053 } 1054 catch(ClassNotFoundException cce) 1055 { 1056 MBeanServerImpl server = null; 1057 ArrayList list = MBeanServerFactory.findMBeanServer(null); 1058 if(list != null) 1059 server = (MBeanServerImpl)list.toArray()[0]; 1060 else throw cce; 1061 1062 Object loader = server.getLoaderTable().get(mbeanInfo.getClassName()); 1063 if(loader == null) 1064 throw cce; 1065 1066 e = Class.forName(mbeanInfo.getClassName(), true, (ClassLoader )loader); 1067 } 1068 1069 MBeanAttributeInfo [] attrs = mbeanInfo.getAttributes(); 1070 MBeanAttributeInfo attr = null; 1071 for(int i=0;i<attrs.length;i++) 1072 { 1073 1074 if(attrs[i].getName().equals(attribute.getName())) 1075 { 1076 attr = attrs[i]; 1077 break; 1078 } 1079 } 1080 1081 if(attr == null) 1082 throw new AttributeNotFoundException (attribute.getName()); 1083 1084 Class parameterTypes[]=new Class [1]; 1085 1086 Class clazz = getProperClass(attr.getType()); 1087 if(clazz == null){ 1088 clazz = attribute.getValue().getClass(); 1089 } 1090 parameterTypes[0] = clazz; 1091 1092 Method attrMethod = null; 1093 ArrayList methodList = new ArrayList (); 1094 ArrayList paramList = new ArrayList (); 1095 try 1096 { 1097 Method [] meths = e.getMethods(); 1098 Class [] params = null; 1099 parameterTypes[0] = null; 1100 for(int i=0; i<meths.length; i++) 1101 { 1102 Method meth = meths[i]; 1103 if(meth.getName().equals("set"+attribute.getName())) 1104 { 1105 params = meth.getParameterTypes(); 1106 if(params != null) 1108 { 1109 if(params[0].isAssignableFrom(clazz)){ 1110 parameterTypes[0] = params[0]; 1111 methodList.add(meth); 1112 paramList.add(parameterTypes[0]); 1113 } 1114 } 1115 } 1116 1117 if(params != null && params.length >0 && params[0] != null) 1118 { 1119 if(parameterTypes[0] == null) 1120 { 1121 throw new InvalidAttributeValueException (); 1122 } 1123 } 1124 } 1125 int size = methodList.size(); 1126 if(size == 0){ 1127 throw new NoSuchMethodException (); 1128 } 1129 else if(size == 1){ 1130 attrMethod = (Method )methodList.get(0); 1131 } 1132 else{ 1133 MBeanInfo info = getMBeanInfo(); 1134 Method [] mbeanMeths =iclazz.getMethods(); 1137 int mbeanMethsSize = mbeanMeths.length; 1138 boolean isFound = false; 1139 for(int i=0;i<size;i++){ 1140 Method meth = (Method )methodList.get(i); 1141 for(int j=0; j<mbeanMethsSize; j++){ 1142 if(meth.getName().equals(mbeanMeths[j].getName())){ 1143 if(Arrays.equals(meth.getExceptionTypes(),mbeanMeths[j].getExceptionTypes())){ attrMethod = meth; 1144 isFound = true; 1145 break; 1146 } 1147 } 1148 } 1149 if(isFound){ 1150 break; 1151 } 1152 } 1153 } 1154 if(attrMethod == null){ 1155 throw new NoSuchMethodException (); 1156 } 1157 } 1158 catch(Exception ee){ 1159 if(ee instanceof InvalidAttributeValueException ){ 1160 throw (InvalidAttributeValueException )ee; 1161 } 1162 if(ee instanceof NoSuchMethodException ){ 1163 throw new AttributeNotFoundException (); 1164 } 1165 throw new MBeanException (ee); 1167 } 1168 1169 try 1170 { 1171 attrMethod.invoke(object, new Object []{attribute.getValue()} ); 1172 }catch(Exception e2){ 1173 if(e2 instanceof AttributeNotFoundException ){ 1174 throw (AttributeNotFoundException )e2; 1175 } 1176 if(e2 instanceof InvocationTargetException ) 1177 { 1178 1179 InvocationTargetException ite = (InvocationTargetException )e2; 1180 Throwable th = ite.getTargetException(); 1181 if(th instanceof Error ){ 1182 Error er = (Error )th; 1183 RuntimeErrorException ree = new RuntimeErrorException (er); 1184 throw ree; 1185 } 1186 else{ 1187 if(th instanceof ReflectionException ){ 1188 throw (ReflectionException )th; 1189 } 1190 1191 if(th instanceof RuntimeException ){ 1192 throw new RuntimeMBeanException ((RuntimeException )th); 1193 } 1194 else{ 1195 throw new MBeanException ((Exception )th); 1196 } 1197 } 1198 } 1199 } 1200 } 1201 catch(ReflectionException e){ 1202 throw e; 1203 } 1204 catch(Exception e){ 1205 if(e instanceof InvalidAttributeValueException ){ 1206 throw (InvalidAttributeValueException )e; 1207 } 1208 if(e instanceof AttributeNotFoundException ){ 1209 throw (AttributeNotFoundException )e; 1210 } 1211 1212 if(e instanceof InvocationTargetException ) 1213 { 1214 1215 InvocationTargetException ite = (InvocationTargetException )e; 1216 Throwable th = ite.getTargetException(); 1217 if(th instanceof Error ){ 1218 Error er = (Error )th; 1219 RuntimeErrorException ree = new RuntimeErrorException (er); 1220 throw ree; 1221 } 1222 else{ 1223 if(th instanceof ReflectionException ){ 1224 throw (ReflectionException )th; 1225 } 1226 1227 if(th instanceof RuntimeException ){ 1228 throw new RuntimeMBeanException ((RuntimeException )th); 1229 } 1230 else{ 1231 throw new MBeanException ((Exception )th); 1232 } 1233 } 1234 } 1235 1236 if(e instanceof RuntimeMBeanException ){ 1237 throw (RuntimeMBeanException )e; 1238 } 1239 if(e instanceof MBeanException ){ 1240 throw (MBeanException )e; 1241 } 1242 } 1243 } 1244 1245 public AttributeList setAttributes(AttributeList attributes) 1246 { 1247 if(attributes == null) 1248 throw new RuntimeOperationsException (new IllegalArgumentException ("Null values not possible")); 1249 1250 Object [] array = attributes.toArray(); 1251 1252 if(array == null) 1253 return attributes; 1254 1255 for(int i = 0; i<array.length;i++) 1256 { 1257 Attribute attr = null; 1258 try 1259 { 1260 attr = (Attribute )array[i]; 1261 } 1262 catch(ClassCastException ce) 1263 { 1264 continue; 1265 } 1266 1267 try 1268 { 1269 setAttribute(attr); 1270 } 1271 catch(Exception e) 1272 { 1273 int index = attributes.indexOf(attr); 1274 attributes.remove(index); 1275 attr = new Attribute (attr.getName(), e); 1276 attributes.add(index, attr); 1277 } 1278 } 1279 1280 return attributes; 1281 } 1282 1283 public void addNotificationListener(NotificationListener listener, 1285 NotificationFilter filter, 1286 Object handback) 1287 throws IllegalArgumentException 1288 { 1289 if( !(object instanceof NotificationBroadcaster )) 1290 { 1291 throw new RuntimeOperationsException (new IllegalArgumentException ("The MBean does not implement NotificationBroadCaster")); 1292 } 1293 1294 ((NotificationBroadcaster )object).addNotificationListener(listener, 1295 filter, handback); 1296 } 1297 1298 public void removeNotificationListener(NotificationListener listener) 1299 throws ListenerNotFoundException 1300 { 1301 if( !(object instanceof NotificationBroadcaster )) 1302 { 1303 throw new RuntimeOperationsException (new IllegalArgumentException ("The MBean does not implement NotificationBroadCaster")); 1304 } 1305 1306 ((NotificationBroadcaster )object).removeNotificationListener(listener); 1307 } 1308 1309 public MBeanNotificationInfo [] getNotificationInfo() 1310 { 1311 if( !(object instanceof NotificationBroadcaster )) 1312 return null; 1313 1314 return ((NotificationBroadcaster )object).getNotificationInfo(); 1315 } 1316 1317 public boolean isNotificationEnabled(Notification notification) 1319 { 1320 if( !(object instanceof NotificationFilter )) 1321 return false; 1322 1323 return ((NotificationFilter )object).isNotificationEnabled(notification); 1324 } 1325 1326 public void handleNotification(Notification notification, Object handback) 1328 { 1329 if( !(object instanceof NotificationListener )) 1330 return; 1331 1332 ((NotificationListener )object).handleNotification(notification, handback); 1333 } 1334 1335 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 1337 { 1338 if( !(object instanceof MBeanRegistration )) 1339 return null; 1340 1341 return ((MBeanRegistration )object).preRegister(server, name); 1342 } 1343 1344 public void postRegister(java.lang.Boolean registrationDone) 1345 { 1346 if( !(object instanceof MBeanRegistration )) 1347 return; 1348 1349 ((MBeanRegistration )object).postRegister(registrationDone); 1350 } 1351 1352 public void preDeregister() throws Exception 1353 { 1354 if( !(object instanceof MBeanRegistration )) 1355 return; 1356 try 1357 { 1358 ((MBeanRegistration )object).preDeregister(); 1359 } 1360 catch(Exception e) 1361 { 1362 throw e; 1363 } 1364 } 1365 1366 public void postDeregister() 1367 { 1368 if( !(object instanceof MBeanRegistration )) 1369 return; 1370 1371 ((MBeanRegistration )object).postDeregister(); 1372 } 1373 1374 private boolean getFlagValue(Class mainClass,boolean value) 1375 { 1376 clazz = mainClass.getSuperclass(); 1377 1378 Class [] interfaces = clazz.getInterfaces(); 1379 for(int i = 0;i<interfaces.length;i++) 1380 { 1381 if(interfaces[i].getName().equals(clazz.getName() + "MBean") ) 1382 { 1383 iclazz = interfaces[i]; 1384 value = true; 1385 break; 1386 } 1387 1388 } 1389 1390 return value; 1391 } 1392} | Popular Tags |