1 27 28 package com.adventnet.jmx; 29 30 import java.lang.reflect.Constructor ; 32 import java.lang.reflect.Modifier ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.io.ByteArrayInputStream ; 35 import java.io.IOException ; 36 import java.util.Set ; 37 import java.util.ArrayList ; 38 import java.util.Enumeration ; 39 import java.util.HashSet ; 40 import java.util.Hashtable ; 41 import java.util.StringTokenizer ; 42 43 import javax.management.Attribute ; 45 import javax.management.AttributeList ; 46 import javax.management.DynamicMBean ; 47 import javax.management.ObjectName ; 48 import javax.management.ObjectInstance ; 49 import javax.management.MBeanServer ; 50 import javax.management.MBeanServerDelegate ; 51 import javax.management.MBeanServerNotification ; 52 import javax.management.MBeanRegistration ; 53 import javax.management.MBeanInfo ; 54 import javax.management.Notification ; 55 import javax.management.NotificationListener ; 56 import javax.management.NotificationFilter ; 57 import javax.management.NotificationBroadcaster ; 58 import javax.management.QueryExp ; 59 import javax.management.AttributeNotFoundException ; 60 import javax.management.ListenerNotFoundException ; 61 import javax.management.InstanceNotFoundException ; 62 import javax.management.InstanceAlreadyExistsException ; 63 import javax.management.InvalidAttributeValueException ; 64 import javax.management.IntrospectionException ; 65 import javax.management.JMRuntimeException ; 66 import javax.management.MBeanException ; 67 import javax.management.MBeanRegistrationException ; 68 import javax.management.NotCompliantMBeanException ; 69 import javax.management.OperationsException ; 70 import javax.management.ReflectionException ; 71 import javax.management.RuntimeErrorException ; 72 import javax.management.RuntimeMBeanException ; 73 import javax.management.RuntimeOperationsException ; 74 75 import com.adventnet.agent.logging.Log; 76 import com.adventnet.agent.logging.LogFactory; 77 import com.adventnet.jmx.utils.JmxUtilities; 78 79 86 87 public class MBeanServerImpl implements MBeanServer 88 { 89 91 92 private String defaultDomain = new String ("DefaultDomain"); 93 94 95 private static ArrayList servers = new ArrayList (); 96 97 101 private Hashtable serverTable = null; 102 103 104 private MBeanServerDelegate delegate = null; 105 106 110 private Hashtable loaderTable = null; 111 112 113 private long sequenceNumber = 1; 114 115 private Log log=null; 116 117 120 public MBeanServerImpl() 121 { 122 this("defaultdomain"); 123 124 try 125 { 126 createLogger(); 127 } 128 catch(Exception e) 129 { 130 System.out.println("FATAL:Log Creation Failed"); 131 } 132 } 133 134 135 public MBeanServerImpl(String defaultDomain) 136 { 137 try 138 { 139 createLogger(); 140 } 141 catch(Exception e) 142 { 143 System.out.println("FATAL:Log Creation Failed"); 144 } 145 146 serverTable = new Hashtable (); 149 loaderTable = new Hashtable (); 150 151 delegate = new MBeanServerDelegate (); 152 153 try 154 { 155 registerMBean(delegate, new ObjectName ("JMImplementation:type=MBeanServerDelegate")); 156 } 157 catch(Exception e) 158 { 159 log.error("MBeanServerDelegate Registration Failed",e); 161 } 163 164 DefaultLoaderRepositoryExt.addLoader(Thread.currentThread().getContextClassLoader()); 168 169 this.defaultDomain = defaultDomain; 170 } 171 172 181 public boolean isRegistered(ObjectName name) 182 { 183 return serverTable.containsKey(name); 184 } 185 186 194 public boolean isInstanceOf(ObjectName name, String className) 195 throws InstanceNotFoundException 196 { 197 Object obj = serverTable.get(name); 198 Class clazz = null; 199 200 if(obj == null) 201 throw new InstanceNotFoundException (); 202 203 if(obj instanceof javax.management.modelmbean.RequiredModelMBean ) 204 { 205 try 206 { 207 MBeanInfo info = this.getMBeanInfo(name); 208 209 if(info.getClassName().equals(className)) 210 return true; 211 212 clazz = Thread.currentThread().getContextClassLoader() 213 .loadClass(className); 214 if(clazz.isInstance(obj)) 215 return true; 216 else 217 return false; 218 } 219 catch(Exception e1) 220 { 221 log.warn("RMM loading failed",e1); 223 return false; 224 } 225 } 226 else if(obj instanceof com.adventnet.jmx.DefaultDynamicMBean) 227 { 228 Object toCheck = ((DefaultDynamicMBean)(obj)).getStandardMBeanObject(); 229 clazz = null; 230 231 try 232 { 233 clazz = Thread.currentThread().getContextClassLoader().loadClass(className); 234 235 if(clazz != null && clazz.isInstance(toCheck)) 236 { 237 return true; 238 } 239 else 240 { 241 return false; 242 } 243 } 244 catch(Exception e1) 245 { 246 log.warn("Failed to loadClass"+toCheck.getClass().getName(),e1); 247 return false; 249 } 250 } 251 else 252 { 253 clazz = null; 254 255 try 256 { 257 clazz = Thread.currentThread().getContextClassLoader().loadClass(className); 258 } 259 catch(Exception e) 260 { 261 log.warn("isInstanceOf() : Failed to loadClass "+className); 262 return false; 263 } 264 if(clazz != null && clazz.isInstance(obj)) 265 { 266 return true; 267 } 268 else 269 { 270 return false; 271 } 272 } 273 } 274 275 278 public String getDefaultDomain() 279 { 280 return defaultDomain; 281 } 282 283 301 public Set queryMBeans(ObjectName name, QueryExp query) 302 { 303 return queryFromTable(name, query, false); 304 } 305 306 309 private Set queryFromTable(ObjectName name, QueryExp query, 310 boolean queryName) 311 { 312 boolean wildCard = false; 313 314 StringBuffer buff = new StringBuffer (); 315 316 HashSet querySet = new HashSet (); 317 318 try 319 { 320 if(name == null) 321 name = new ObjectName ("*:*"); 322 } 323 catch(Exception e1) 324 { 325 return querySet; 327 } 328 329 if(name != null && name.toString().length() != 0) 330 { 331 String strPattern = name.toString(); 332 333 if((strPattern.indexOf("*") != -1) || (strPattern.indexOf("?") != -1)) 334 { 335 wildCard = true; 336 } 337 338 int length = strPattern.length(); 339 for(int i=0;i<length;i++) 340 { 341 char ch = strPattern.charAt(i) ; 342 343 346 347 buff.append(ch); 348 } 349 } 350 351 String sname = buff.toString(); 352 353 StringTokenizer st = new StringTokenizer (sname, ":"); 354 355 if(st.countTokens() != 2) 356 return querySet; 357 358 String dom = st.nextToken(); 359 String des = st.nextToken(); 360 361 if(dom == null || des == null) 362 return querySet; 363 364 for(Enumeration e = serverTable.keys(); e.hasMoreElements();) 365 { 366 ObjectName obj = (ObjectName )e.nextElement(); 367 if(name == null || (name != null && name.toString().length() == 0) ) 368 { 369 if(queryName) 370 { 371 Object mbean = serverTable.get(obj); 372 try 373 { 374 if(query == null) 375 querySet.add(obj); 376 else 377 { 378 query.setMBeanServer(this); 379 if(query.apply(obj)) 380 querySet.add(obj); 381 } 382 } 383 catch(Exception e1) 384 { 385 if(!(e1 instanceof NullPointerException )) 386 log.warn("Null Pointer Exception",e1); 387 } 388 } 389 else 390 { 391 Object mbean = serverTable.get(obj); 392 String classname = null; 393 394 try 395 { 396 classname = ((DynamicMBean )mbean).getMBeanInfo().getClassName(); 397 } 398 catch(Exception ex) 399 { 400 } 401 402 try 403 { 404 if(query == null) 405 querySet.add(new ObjectInstance (obj, classname)); 406 else 407 { 408 query.setMBeanServer(this); 409 if(query.apply(obj)) 410 querySet.add(new ObjectInstance (obj, classname)); 411 } 412 } 413 catch(Exception e2) 414 { 415 if(!(e2 instanceof NullPointerException )) 416 log.warn("NullPointer Exception",e2); 417 } 418 } 419 } 420 else { 422 String sobj = obj.toString(); 423 424 StringTokenizer st1 = new StringTokenizer (sobj, ":"); 425 426 String lobj = st1.nextToken(); 427 String robj = st1.nextToken(); 428 429 boolean retVal1 = false; 430 boolean retVal2 = false; 431 432 retVal1 = JmxUtilities.checkPattern(lobj, dom, wildCard ); 433 434 if(retVal1) 435 retVal2 = JmxUtilities.checkPattern(robj, des, wildCard ); 436 437 if(retVal2) 438 { 439 if(queryName) 440 { 441 Object mbean = serverTable.get(obj); 442 443 try 444 { 445 if(query == null) 446 { 447 querySet.add(obj); 448 } 449 else 450 { 451 query.setMBeanServer(this); 452 if(query.apply(obj)) 453 querySet.add(obj); 454 } 455 } 456 catch(Exception e3) 457 { 458 if(!(e3 instanceof NullPointerException )) 459 log.warn("Null Pointer Exception",e3); 460 } 461 } 462 else 463 { 464 Object mbean = serverTable.get(obj); 465 String classname = null; 466 467 try 468 { 469 classname = ((DynamicMBean )mbean).getMBeanInfo().getClassName(); 470 } 471 catch(Exception ex) 472 { 473 } 474 475 try 476 { 477 if(query == null) 478 querySet.add(new ObjectInstance (obj, classname)); 479 else 480 { 481 query.setMBeanServer(this); 482 if(query.apply(obj)) 483 querySet.add(new ObjectInstance (obj, classname)); 484 } 485 } 486 catch(Exception e4) 487 { 488 if(!(e4 instanceof NullPointerException )) 489 log.warn("Null Pointer Exception",e4); 490 } 491 } 492 } 493 } 494 } 495 496 return querySet; 497 } 498 499 518 public Set queryNames(ObjectName name, QueryExp query) 519 { 520 return queryFromTable(name, query, true); 521 } 522 523 545 public ObjectInstance registerMBean(Object object, ObjectName name) 546 throws InstanceAlreadyExistsException , 547 MBeanRegistrationException , 548 NotCompliantMBeanException 549 { 550 try 551 { 552 ObjectName temp = name; 555 556 try 557 { 558 if(name.getDomain().equals("DefaultDomain") || name.getDomain().equals("")) 559 { 560 name = new ObjectName (defaultDomain+":"+name.getKeyPropertyListString()); 561 } 562 } 563 catch(Exception ee) 564 { 565 name = temp; 566 } 567 568 if(object == null) 569 throw new IllegalArgumentException (); 570 571 if(object instanceof DynamicMBean ) 572 { 573 try 574 { 575 MBeanInfo mbi = ((DynamicMBean )(object)).getMBeanInfo(); 576 log.trace("registerMBean:object instanceof RMM"); 577 if(mbi==null) 578 { 579 log.debug("MBeanInfo is Null"); 580 throw new NotCompliantMBeanException ("Exception occured while invoking the getMBeanInfo() of DynamicMBean"); 581 } 582 583 if(!isValidMBeanInfo(mbi)) 584 { 585 throw new NotCompliantMBeanException ("MBeanInfo is not proper "); 586 } 587 } 588 catch(Exception ee) 589 { 590 throw new NotCompliantMBeanException ("Exception occured while invoking the getMBeanInfo() of DynamicMBean"); 591 } 592 } 593 594 boolean isStandard = false; 596 Class className = object.getClass(); 597 String fileName = className.getName(); 598 Class [] interfaces = className.getInterfaces(); 599 for(int i=0; i<interfaces.length; i++) 600 { 601 if(interfaces[i].getName().equals(fileName+"MBean")) 602 { 603 isStandard = true; 604 break; 605 } 606 } 607 608 if(!isStandard) 610 { 611 Class superClass = className; 612 while(superClass != null) 614 { 615 superClass = superClass.getSuperclass(); 616 if(superClass == null) 617 { 618 break; 619 } 620 621 String superClassName = superClass.getName(); 622 interfaces = superClass.getInterfaces(); 623 624 for(int i=0; i<interfaces.length; i++) 625 { 626 if(interfaces[i].getName().equals(superClassName+"MBean")) 627 { 628 isStandard = true; 629 break; 630 } 631 } 632 } 633 } 634 635 if(object instanceof MBeanRegistration ) 636 { 637 log.trace("registerMBean:object instanceOf MBeanRegistration"); 638 ObjectName objName = null; 639 640 try 641 { 642 objName = ((MBeanRegistration )object).preRegister(this,name); 643 } 644 catch(RuntimeException re) 645 { 646 throw new RuntimeMBeanException (re); 647 } 648 catch(Exception e) 649 { 650 log.warn("MBeanRegistration Failed",e); 651 if(e instanceof MBeanRegistrationException ) 652 { 653 throw (MBeanRegistrationException )e; 654 } 655 656 throw new MBeanRegistrationException (e,e.getMessage()); 657 } 658 659 name = objName; 660 } 661 662 if(name == null) 663 { 664 try 665 { 666 if(object instanceof MBeanRegistration ) 667 ((MBeanRegistration )object).postRegister(new Boolean (false)); 668 } 669 catch(RuntimeException re) 670 { 671 throw new RuntimeMBeanException (re); 672 } 673 catch(Exception e) 674 { 675 throw new MBeanRegistrationException (e,e.getMessage()); 676 } 677 678 throw new RuntimeOperationsException (new IllegalArgumentException ("Object name is null")); 679 } 680 681 if(serverTable.get(name) != null) 682 { 683 try 684 { 685 if(object instanceof MBeanRegistration ) 686 ((MBeanRegistration )object).postRegister(new Boolean (false)); 687 688 throw new InstanceAlreadyExistsException (); 689 } 690 catch(Exception e) 691 { 692 693 if(e instanceof RuntimeException ) 694 { 695 throw new RuntimeMBeanException ((RuntimeException )e); 696 } 697 else if(e instanceof InstanceAlreadyExistsException ) 698 { 699 throw (InstanceAlreadyExistsException )e; 700 } 701 else 702 { 703 throw new MBeanRegistrationException (e); 704 } 705 } 706 } 707 708 709 if(!(name.toString().equals("JMImplementation:type=MBeanServerDelegate"))) 710 { 711 if(name.getDomain().equals("JMImplementation")) 712 { 713 throw new JMRuntimeException (); 714 } 715 } 716 717 String oname = object.getClass().getName(); 718 ClassLoader l = getClassLoader(object); 720 testForCompliance(oname,l); 721 if(isStandard) 723 { 724 log.trace("Object Not an instance of DynamicMBean"); 725 try 726 { 727 object = new DefaultDynamicMBean(object); 728 } 729 catch(Exception e) 730 { 731 try 732 { 733 if(object instanceof MBeanRegistration ) 734 ((MBeanRegistration )object).postRegister(new Boolean (false)); 735 } 736 catch(Exception poe) 737 { 738 throw new MBeanRegistrationException (poe,"Exception occured in postRegister"); 739 } 740 741 throw new NotCompliantMBeanException (); 742 } 743 } 744 745 if(name.isPattern()) 746 { 747 try 748 { 749 if(object instanceof MBeanRegistration ) 750 ((MBeanRegistration )object).postRegister(new Boolean (false)); 751 } 752 catch(RuntimeException re) 753 { 754 throw new RuntimeMBeanException (re); 755 } 756 catch(Exception poe) 757 { 758 throw new MBeanRegistrationException (poe,"Exception occured in postRegister"); 759 } 760 761 throw new RuntimeOperationsException (new IllegalArgumentException ("ObjectName pattern fails")); 762 } 763 764 serverTable.put(name, object); 765 loaderTable.put(oname, l); 766 767 try 768 { 769 if(object instanceof MBeanRegistration ) 770 ((MBeanRegistration )object).postRegister(new Boolean (true)); 771 } 772 catch(RuntimeException re) 773 { 774 throw new RuntimeMBeanException (re); 775 } 776 catch(Exception poe) 777 { 778 throw new MBeanRegistrationException (poe,"Exception occured in postRegister"); 779 } 780 catch(Throwable th) 781 { 782 if(th instanceof Error ) 783 { 784 throw new RuntimeErrorException ((Error )th); 785 } 786 Exception e = (Exception )th; 787 throw new MBeanRegistrationException (e,"Exception occured in postRegister"); 788 } 789 790 if(object instanceof ClassLoader ) 791 DefaultLoaderRepositoryExt.addLoader((ClassLoader )object); 792 793 if(object instanceof DefaultDynamicMBean && 794 ((DefaultDynamicMBean)object).getStandardMBeanObject() instanceof ClassLoader ) 795 { 796 DefaultLoaderRepositoryExt.addLoader((ClassLoader )((DefaultDynamicMBean)object).getStandardMBeanObject()); 797 } 798 799 Notification notif = new MBeanServerNotification ( 801 MBeanServerNotification.REGISTRATION_NOTIFICATION, 802 delegate.getClass().getName(), 803 sequenceNumber++, 804 name); 805 806 notif.setTimeStamp(System.currentTimeMillis()); 807 delegate.sendNotification(notif); 808 809 if(object instanceof DefaultDynamicMBean) 810 { 811 return new ObjectInstance (name, ((DefaultDynamicMBean)object).getStandardMBeanObject().getClass().getName()); 812 } 813 else 814 { 815 return new ObjectInstance (name, object.getClass().getName()); 816 } 817 } 818 catch(IllegalArgumentException ie) 819 { 820 throw new RuntimeOperationsException (ie); 821 } 822 } 823 824 838 public void unregisterMBean(ObjectName name) 839 throws InstanceNotFoundException , MBeanRegistrationException 840 { 841 log.trace("Unregistering MBean with object name " + name); 842 843 if(name == null) 844 throw new RuntimeOperationsException ( 845 new IllegalArgumentException ("ObjectName cannot be null!!!")); 846 847 Object object = null; 848 849 if((object = serverTable.get(name)) == null) 850 throw new InstanceNotFoundException (); 851 852 if(object instanceof MBeanRegistration ) 853 { 854 try 855 { 856 ((MBeanRegistration )object).preDeregister(); 857 } 858 catch(RuntimeException re) 859 { 860 throw new RuntimeMBeanException (re); 861 } 862 catch(Exception e) 863 { 864 log.warn("Exception while unregistering the MBean "); 865 if(e instanceof MBeanRegistrationException ) 866 { 867 throw (MBeanRegistrationException )e; 868 } 869 870 throw new MBeanRegistrationException (e); 871 } 872 } 873 874 if(name.toString().equals("JMImplementation:type=MBeanServerDelegate")) 875 throw new RuntimeOperationsException (new IllegalArgumentException ( 876 "MBeanServerDelegate Cannot be unregistered")); 877 878 Object o =serverTable.get(name); 879 serverTable.remove(name); 880 loaderTable.remove(name); 881 882 if(o instanceof DefaultDynamicMBean) 883 { 884 if(((DefaultDynamicMBean)o).getStandardMBeanObject() instanceof ClassLoader ) 885 { 886 DefaultLoaderRepositoryExt.removeLoader((ClassLoader )(((DefaultDynamicMBean)o).getStandardMBeanObject())); 887 } 888 } 889 890 if(o instanceof ClassLoader ) 891 { 892 DefaultLoaderRepositoryExt.removeLoader((ClassLoader )o); 893 } 894 895 if(object instanceof MBeanRegistration ) 896 { 897 try 898 { 899 ((MBeanRegistration )object).postDeregister(); 900 } 901 catch(Exception e) 902 { 903 if( e instanceof RuntimeException ) 904 { 905 RuntimeException rt = (RuntimeException )e; 906 throw new RuntimeMBeanException (rt); 907 } 908 } 909 catch(Error e) 910 { 911 throw new RuntimeErrorException (e); 912 } 913 } 914 915 Notification notif = new MBeanServerNotification ( 917 MBeanServerNotification.UNREGISTRATION_NOTIFICATION, 918 delegate.getClass().getName(), 919 sequenceNumber++, 920 name); 921 922 delegate.sendNotification(notif); 923 } 924 925 949 public Object getAttribute(ObjectName name, String attribute) 950 throws MBeanException , 951 AttributeNotFoundException , 952 InstanceNotFoundException , 953 ReflectionException 954 { 955 log.trace("getAttribute - getting value for attribute " + attribute 956 + " of MBean with ObjectName " + name); 957 958 if(name == null || attribute == null) 959 throw new RuntimeOperationsException ( 960 new IllegalArgumentException ("Null Values not possible")); 961 962 Object object = null; 963 if((object = serverTable.get(name)) == null) 964 throw new InstanceNotFoundException (); 965 966 if(object instanceof DynamicMBean ) 967 { 968 Object toRet = null; 969 970 synchronized(object) 971 { 972 try 973 { 974 toRet = ((DynamicMBean )object).getAttribute(attribute); 975 } 976 catch(AttributeNotFoundException ae) 977 { 978 throw ae; 979 } 980 catch(ReflectionException re) 981 { 982 Exception e = re.getTargetException(); 983 if(e instanceof InvocationTargetException ) 984 { 985 InvocationTargetException it = (InvocationTargetException )e; 986 Exception ee = (Exception )it.getTargetException(); 987 if(ee instanceof RuntimeException ) 988 { 989 RuntimeException rt = (RuntimeException )ee; 990 throw new RuntimeMBeanException (rt); 991 } 992 else 993 { 994 throw new MBeanException (ee); 995 } 996 } 997 998 throw new MBeanException (e); 999 } 1000 catch(Exception e) 1001 { 1002 if(e instanceof RuntimeErrorException ) 1003 { 1004 RuntimeErrorException rt = (RuntimeErrorException )e; 1005 throw rt; 1006 } 1007 throw new MBeanException (e); 1008 } 1009 } 1010 1011 return toRet; 1012 } 1013 1014 return null; 1015 } 1016 1017 1031 public AttributeList getAttributes(ObjectName name, String [] attributes) 1032 throws InstanceNotFoundException , ReflectionException 1033 { 1034 log.trace("getAttributes entered"); 1035 1036 int length = 0; 1037 1038 if(name == null || attributes == null) 1039 throw new RuntimeOperationsException ( 1040 new IllegalArgumentException ("Null values not possible")); 1041 1042 Object object = null; 1043 1044 if((object = serverTable.get(name)) == null) 1045 throw new InstanceNotFoundException (); 1046 1047 length = attributes.length; 1048 1049 for(int i=0 ; i<length ; i++) 1050 { 1051 if (attributes[i] == null) 1052 throw new RuntimeOperationsException (new IllegalArgumentException ("Null values not possible")); 1053 } 1054 1055 1056 if(object instanceof DynamicMBean ) 1057 { 1058 synchronized(object) 1059 { 1060 try 1061 { 1062 return ((DynamicMBean )object).getAttributes(attributes); 1063 } 1064 catch(Exception ex) 1065 { 1066 new ReflectionException (ex); 1067 } 1068 } 1069 } 1070 1071 return null; 1072 } 1073 1074 1099 public Object invoke(ObjectName name, String actionName, 1100 Object [] params, String [] signature) 1101 throws InstanceNotFoundException , 1102 MBeanException , 1103 ReflectionException 1104 { 1105 Object object = null; 1106 1107 if((object = serverTable.get(name)) == null) 1108 { 1109 throw new InstanceNotFoundException (); 1110 } 1111 1112 if(object instanceof DynamicMBean ) 1113 { 1114 synchronized(object) 1115 { 1116 Object retObj = null; 1117 if( object instanceof DefaultDynamicMBean) 1118 { 1119 try 1120 { 1121 retObj = ((DynamicMBean )object).invoke(actionName, params, signature); 1122 } 1123 catch(RuntimeOperationsException roe) 1124 { 1125 throw roe; 1126 } 1127 catch(RuntimeException re) 1128 { 1129 RuntimeException rt = (RuntimeException )re; 1130 if(rt instanceof RuntimeOperationsException ) 1131 { 1132 throw (RuntimeOperationsException )rt; 1133 } 1134 1135 if(rt instanceof RuntimeErrorException ) 1136 { 1137 throw (RuntimeErrorException )rt; 1138 } 1139 1140 if(rt instanceof RuntimeMBeanException ) 1141 { 1142 throw rt; 1143 } 1144 1145 throw new RuntimeMBeanException (rt); 1146 } 1147 catch(ReflectionException re) 1148 { 1149 throw re; 1150 } 1151 catch(MBeanException me) 1152 { 1153 Exception e = me.getTargetException(); 1154 1155 if(e instanceof RuntimeException ) 1156 { 1157 RuntimeException rt = (RuntimeException )e; 1158 throw new RuntimeMBeanException (rt); 1159 } 1160 1161 throw me; 1162 } 1163 1164 } 1165 else 1166 { 1167 try 1168 { 1169 retObj= ((DynamicMBean )object).invoke(actionName, params, signature); 1170 } 1171 catch(ReflectionException re) 1172 { 1173 Exception e = re.getTargetException(); 1174 if(e instanceof MBeanException ) 1175 { 1176 MBeanException mb = (MBeanException )e; 1177 throw mb; 1178 } 1179 else 1180 { 1181 throw re; 1182 } 1183 } 1184 } 1185 1186 return retObj; 1187 } 1188 } 1189 1190 return null; 1191 } 1192 1193 1219 public void setAttribute(ObjectName name, Attribute attribute) 1220 throws InstanceNotFoundException , 1221 AttributeNotFoundException , 1222 InvalidAttributeValueException , 1223 MBeanException , 1224 ReflectionException 1225 { 1226 log.trace("setAttribute - setting attribute"); 1227 1228 if(name == null || attribute == null) 1229 throw new RuntimeOperationsException ( 1230 new IllegalArgumentException ("Null Values not possible")); 1231 1232 Object object = null; 1233 if((object = serverTable.get(name)) == null) 1234 throw new InstanceNotFoundException ("No Mbean " + name + "present in the JMXServer"); 1235 1236 synchronized(object) 1237 { 1238 ((DynamicMBean )object).setAttribute(attribute); 1239 } 1240 } 1241 1242 1256 public AttributeList setAttributes(ObjectName name, AttributeList attributes) 1257 throws InstanceNotFoundException , ReflectionException 1258 { 1259 log.trace("setAttributes called for MBean with ObjectName " + name); 1260 1261 if(name == null || attributes == null) 1262 throw new RuntimeOperationsException (new IllegalArgumentException ("Either ObjectName / attributes is null")); 1263 1264 Object object = null; 1265 1266 if((object = serverTable.get(name)) == null) 1267 throw new InstanceNotFoundException (); 1268 1269 if(object instanceof DynamicMBean ) 1270 { 1271 synchronized(object) 1272 { 1273 return ((DynamicMBean )object).setAttributes(attributes); 1274 } 1275 } 1276 1277 return null; 1278 } 1279 1280 1295 public MBeanInfo getMBeanInfo(ObjectName name) 1296 throws InstanceNotFoundException , 1297 IntrospectionException , 1298 ReflectionException 1299 { 1300 log.trace("getting MBeanInfo for MBean with ObjectName " + name); 1301 1302 Object object = null; 1303 1304 if((object = serverTable.get(name)) == null) 1305 throw new InstanceNotFoundException (); 1306 1307 if(object instanceof DynamicMBean ) 1308 { 1309 synchronized(object) 1310 { 1311 MBeanInfo info = null; 1312 try 1313 { 1314 info = ((DynamicMBean )object).getMBeanInfo(); 1315 if(info == null) 1316 { 1317 throw new JMRuntimeException (); 1318 } 1319 } 1320 catch(Exception e) 1321 { 1322 if(e instanceof JMRuntimeException ) 1323 { 1324 throw (JMRuntimeException )e; 1325 } 1326 if(e instanceof RuntimeException ) 1327 { 1328 RuntimeException rt = (RuntimeException )e; 1329 throw new RuntimeMBeanException (rt); 1330 } 1331 } 1332 1333 return info; 1334 } 1335 } 1336 1337 return null; 1338 } 1339 1340 1350 public ObjectInstance getObjectInstance(ObjectName name) 1351 throws InstanceNotFoundException 1352 { 1353 if (name == null) 1354 throw new InstanceNotFoundException ("The specified ObjectName is null"); 1355 1356 if( ! isRegistered(name) ) 1357 throw new InstanceNotFoundException (name.toString()); 1358 1359 Object mbean = serverTable.get(name); 1360 String classname = null; 1361 1362 try 1363 { 1364 if(mbean instanceof javax.management.modelmbean.ModelMBean ) 1365 classname = "javax.management.ModelMBean"; 1366 else 1367 classname = ((DynamicMBean )mbean).getMBeanInfo().getClassName(); 1368 } 1369 catch(Exception ex) 1370 { 1371 } 1372 1373 try 1374 { 1375 return new ObjectInstance (name, classname); 1376 } 1377 catch(Exception ee) 1378 { 1379 log.error("ObjectInstance Creation failed",ee); 1380 return null; 1381 } 1382 } 1383 1384 1400 public void addNotificationListener(ObjectName name, 1401 NotificationListener listener, 1402 NotificationFilter filter, 1403 Object handback) 1404 throws InstanceNotFoundException 1405 { 1406 Object object = null; 1407 1408 if(name == null) 1409 { 1410 throw new RuntimeOperationsException (new IllegalArgumentException ( 1411 "ObjectName cannot be null!!!")); 1412 } 1413 1414 object = serverTable.get(name); 1415 1416 if(object == null) 1417 { 1418 throw new InstanceNotFoundException ("No object with the name " + 1419 name + " present in the JMXServer"); 1420 } 1421 1422 if( !(object instanceof NotificationBroadcaster )) 1423 { 1424 throw new RuntimeOperationsException (new IllegalArgumentException ( 1425 "MBean " + name + " does not implement NotificationBroadCaster")); 1426 } 1427 1428 if(listener == null) 1429 { 1430 throw new RuntimeOperationsException (new IllegalArgumentException ( 1431 "Null Listener is being passed !!!")); 1432 } 1433 1434 synchronized(object) 1435 { 1436 ((NotificationBroadcaster ) object) 1437 .addNotificationListener(listener, filter, handback); 1438 } 1439 } 1440 1441 1464 public void addNotificationListener(ObjectName name, 1465 ObjectName listener, 1466 NotificationFilter filter, 1467 Object handback) 1468 throws InstanceNotFoundException 1469 { 1470 Object object = null; 1471 1472 if(name == null) 1473 { 1474 throw new RuntimeOperationsException (new IllegalArgumentException ( 1475 "ObjectName cannot be null!!!")); 1476 } 1477 1478 object = serverTable.get(name); 1479 1480 if(object == null) 1481 { 1482 throw new InstanceNotFoundException ("No object with the name " + 1483 name + " present in the JMXServer"); 1484 } 1485 1486 if( !(object instanceof NotificationBroadcaster )) 1487 { 1488 throw new RuntimeOperationsException (new IllegalArgumentException ( 1489 "MBean " + name + " does not implement NotificationBroadCaster")); 1490 } 1491 1492 Object lobject = null; 1493 lobject = serverTable.get(listener); 1494 1495 if(lobject == null) 1496 { 1497 throw new InstanceNotFoundException ("No listener with the name " 1498 + listener + " present in the JMXServer!!!"); 1499 } 1500 1501 if(lobject instanceof DefaultDynamicMBean) 1502 { 1503 if(!(((DefaultDynamicMBean)(lobject)).getStandardMBeanObject() instanceof NotificationListener )) 1504 { 1505 throw new RuntimeOperationsException (new IllegalArgumentException ( 1506 "MBean " + name + " does not implement NotificationListener")); 1507 } 1508 } 1509 1510 if( !(lobject instanceof NotificationListener )) 1511 { 1512 throw new RuntimeOperationsException (new IllegalArgumentException ( 1513 "MBean " + name + " does not implement NotificationListener")); 1514 } 1515 1516 synchronized(object) 1517 { 1518 ((NotificationBroadcaster )object).addNotificationListener( (NotificationListener )lobject,filter,handback); 1519 } 1520 } 1521 1522 1554 public ObjectInstance createMBean(String className, ObjectName name) 1555 throws ReflectionException , 1556 InstanceAlreadyExistsException , 1557 MBeanRegistrationException , 1558 MBeanException , 1559 NotCompliantMBeanException 1560 { 1561 log.trace("createMBean called with className and ObjectName"); 1562 1563 Object obj = null; 1564 1565 if(className == null) 1566 throw new RuntimeOperationsException (new IllegalArgumentException ( 1567 "ClassName or ObjectName cannot be null")); 1568 1569 Class class_name = null; 1570 1571 try 1572 { 1573 class_name = Thread.currentThread().getContextClassLoader().loadClass(className); 1574 } 1575 catch(ClassNotFoundException cne) 1576 { 1577 try 1578 { 1579 class_name = DefaultLoaderRepositoryExt.loadClass(className); 1580 } 1581 catch(ClassNotFoundException cnee) 1582 { 1583 throw new ReflectionException (cnee, "The MBean class could not be loaded by the default loader repository"); 1584 } 1585 } 1586 1587 try 1588 { 1589 obj = class_name.newInstance(); 1590 } 1591 catch(InstantiationException ite) 1592 { 1593 if(Modifier.isAbstract(class_name.getModifiers())) 1594 { 1595 throw new NotCompliantMBeanException (); 1596 } 1597 try 1598 { 1599 Constructor cons = class_name.getConstructor(new Class [0]); 1600 } 1601 catch(NoSuchMethodException e) 1602 { 1603 throw new ReflectionException (e); 1604 } 1605 1606 throw new MBeanException (ite,"Exception occured while invoking the constructor of the MBean!!!"); 1607 } 1608 catch(IllegalAccessException iae) 1609 { 1610 throw new MBeanException (iae,"Exception occured while invoking the constructor of the MBean!!!"); 1611 } 1612 catch(Exception e) 1613 { 1614 if(e instanceof NotCompliantMBeanException ) 1615 { 1616 throw (NotCompliantMBeanException )e; 1617 } 1618 1619 throw new MBeanException (e, "General Exception occured. It might" + 1620 " be runtime errors while invoking the constructor"); 1621 } 1622 1623 return registerMBean(obj, name); 1624 } 1625 1626 1661 public ObjectInstance createMBean(String className, 1662 ObjectName name, 1663 Object [] params, 1664 String [] signature) 1665 throws ReflectionException , 1666 InstanceAlreadyExistsException , 1667 MBeanRegistrationException , 1668 MBeanException , 1669 NotCompliantMBeanException 1670 { 1671 log.trace("createMBean called with className, ObjectName, " + 1672 "parameters as Object array and signature as String array"); 1673 1674 Object obj = null; 1675 1676 if(className == null) 1677 { 1678 throw new RuntimeOperationsException (new IllegalArgumentException ( 1679 "ClassName or ObjectName cannot be null")); 1680 } 1681 1682 Class class_name = null; 1683 1684 try 1685 { 1686 class_name = DefaultLoaderRepositoryExt.loadClass(className); 1687 1688 if(params == null || signature == null) 1689 { 1690 return createMBean(className,name); 1691 } 1692 1693 Class [] sig = new Class [signature.length]; 1694 1695 for(int i = 0;i<signature.length;i++) 1696 { 1697 if((sig[i] = getThePrimitiveClassObject(signature[i])) == null) 1698 { 1699 try 1700 { 1701 sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 1702 } 1703 catch(ClassNotFoundException ee) 1704 { 1705 throw new ReflectionException (ee, "The MBean class " + 1706 "could not be loaded by the default loader repository"); 1707 } 1708 1709 try 1710 { 1711 if(obj instanceof DefaultDynamicMBean && 1712 ((DefaultDynamicMBean)obj).getStandardMBeanObject() instanceof ClassLoader ) 1713 { 1714 sig[i] = ((ClassLoader )((DefaultDynamicMBean)obj).getStandardMBeanObject()).loadClass(signature[i]); 1715 } 1716 } 1717 catch(ClassNotFoundException ce) 1718 { 1719 try 1720 { 1721 sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 1722 } 1723 catch(ClassNotFoundException ee) 1724 { 1725 throw new ReflectionException (ee, "The MBean class could not be loaded by the default loader repository"); 1726 } 1727 } 1728 1729 } 1730 } 1731 1732 Constructor constr = null; 1733 1734 constr = class_name.getConstructor(sig); 1735 obj = constr.newInstance(params); 1736 } 1737 catch(ClassNotFoundException cne) 1738 { 1739 throw new ReflectionException (cne, "The MBean class could not be loaded by the default loader repository"); 1740 } 1741 catch(NoSuchMethodException ne) 1742 { 1743 throw new ReflectionException (ne); 1744 } 1745 catch(IllegalArgumentException iae) 1746 { 1747 throw new ReflectionException (iae); 1748 } 1749 catch(InstantiationException ie) 1750 { 1751 try 1752 { 1753 Class [] classParams = new Class [params.length]; 1754 1755 for(int i=0; i<params.length; i++) 1756 { 1757 classParams[i] = params[i].getClass(); 1758 } 1759 1760 Constructor cons = class_name.getConstructor(classParams); 1761 } 1762 catch(NoSuchMethodException ne) 1763 { 1764 throw new ReflectionException (ne); 1765 } 1766 1767 throw new MBeanException (ie); 1768 } 1769 catch(Exception e) 1770 { 1771 if(e instanceof ReflectionException ) 1772 { 1773 throw (ReflectionException )e; 1774 } 1775 1776 throw new MBeanException (e, "Exception occured while calling the constructor of the MBean!!!"); 1777 } 1778 1779 return registerMBean(obj, name); 1780 } 1781 1782 1816 public ObjectInstance createMBean(String className, 1817 ObjectName name, 1818 ObjectName loaderName) 1819 throws ReflectionException , 1820 InstanceAlreadyExistsException , 1821 MBeanRegistrationException , 1822 MBeanException , 1823 NotCompliantMBeanException , 1824 InstanceNotFoundException 1825 { 1826 log.trace("createMBean called with className, ObjectName and " + 1827 "loaderName as ObjectName"); 1828 1829 Object object = null; 1830 Class clazz = null; 1831 Object obj = null; 1832 1833 if(className == null ) 1834 { 1835 throw new RuntimeOperationsException (new IllegalArgumentException ( 1836 "ClassName or ObjectName cannot be null")); 1837 } 1838 1839 try 1840 { 1841 if(loaderName == null) 1842 { 1843 clazz = getClassLoader(this).loadClass(className); 1845 } 1846 else if((object = serverTable.get(loaderName)) == null) 1847 { 1848 throw new InstanceNotFoundException (loaderName.toString()); 1851 } 1852 else if(object instanceof DefaultDynamicMBean && 1853 ((DefaultDynamicMBean)object).getStandardMBeanObject() instanceof ClassLoader ) 1854 { 1855 clazz = ((ClassLoader )((DefaultDynamicMBean)object).getStandardMBeanObject()).loadClass(className); 1856 } 1857 else if( !(object instanceof ClassLoader )) 1858 { 1859 throw new MBeanException (new Exception ("Invalid ClassLoader")); 1860 } 1861 else 1862 { 1863 clazz = ((ClassLoader )object).loadClass(className); 1864 } 1865 1866 obj = clazz.newInstance(); 1867 } 1868 catch(ClassNotFoundException cne) 1869 { 1870 throw new ReflectionException (cne, "The MBean class could not be loaded by the available class Loaders"); 1871 } 1872 catch(InstantiationException ite) 1873 { 1874 try 1875 { 1876 Constructor cons = clazz.getConstructor(new Class [0]); 1877 } 1878 catch(NoSuchMethodException ne) 1879 { 1880 throw new ReflectionException (ne); 1881 } 1882 1883 throw new MBeanException (ite); 1884 } 1885 catch(IllegalAccessException iae) 1886 { 1887 throw new MBeanException (iae,"Exception occured while invoking the constructor of the MBean!!!"); 1888 } 1889 catch(Exception e) 1890 { 1891 throw new MBeanException (e,"General Exception occured .It might be runtime errors while invoking the constructor"); 1892 } 1893 1894 return registerMBean(obj, name); 1895 } 1896 1897 1935 public ObjectInstance createMBean(String className, 1936 ObjectName name, 1937 ObjectName loaderName, 1938 Object [] params, 1939 String [] signature) 1940 throws ReflectionException , 1941 InstanceAlreadyExistsException , 1942 MBeanRegistrationException , 1943 MBeanException , 1944 RuntimeMBeanException , 1945 NotCompliantMBeanException , 1946 InstanceNotFoundException 1947 { 1948 log.trace("createMBean called with className, ObjectName, loaderName as " + 1949 "ObjectName, parameters as Object array and signature as String array"); 1950 1951 Object object = null; 1952 Class clazz = null; 1953 Object obj = null; 1954 1955 if(className == null ) 1956 { 1957 throw new RuntimeOperationsException (new IllegalArgumentException ( 1958 "ClassName or ObjectName cannot be null")); 1959 } 1960 1961 try 1962 { 1963 if(loaderName == null) 1964 { 1965 clazz = getClassLoader(this).loadClass(className); 1967 } 1968 else if((object = serverTable.get(loaderName)) == null) 1969 { 1970 throw new InstanceNotFoundException (loaderName.toString()); 1973 } 1974 else if(object instanceof DefaultDynamicMBean && 1975 ((DefaultDynamicMBean)object).getStandardMBeanObject() instanceof ClassLoader ) 1976 { 1977 clazz = ((ClassLoader )((DefaultDynamicMBean)object).getStandardMBeanObject()).loadClass(className); 1978 } 1979 else if( !(object instanceof ClassLoader )) 1980 { 1981 throw new MBeanException (new Exception ("Invalid ClassLoader ")); 1982 } 1983 else 1984 { 1985 clazz = ((ClassLoader )object).loadClass(className); 1986 } 1987 } 1988 catch(ClassNotFoundException cne) 1989 { 1990 throw new ReflectionException (cne, "The MBean class could not be " + 1991 "loaded by the available class Loaders"); 1992 } 1993 1994 if(params == null || signature == null) 1995 { 1996 return createMBean(className,name,loaderName); 1997 } 1998 1999 try 2000 { 2001 Class [] sig = new Class [signature.length]; 2002 for(int i = 0;i<signature.length;i++) 2003 { 2004 try 2005 { 2006 if((sig[i] = getThePrimitiveClassObject(signature[i])) == null) 2007 sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 2008 } 2009 catch(ClassNotFoundException eee) 2010 { 2011 try 2012 { 2013 DefaultLoaderRepositoryExt.loadClass(signature[i]); 2014 } 2015 catch(ClassNotFoundException ee) 2016 { 2017 throw new ReflectionException (ee, "The MBean class could not be loaded by the default loader repository"); 2018 } 2019 } 2020 } 2021 2022 Constructor constr = null; 2023 constr = clazz.getConstructor(sig); 2024 obj = constr.newInstance(params); 2025 } 2026 catch(InstantiationException ie) 2027 { 2028 try 2029 { 2030 Class [] classParams = new Class [params.length]; 2031 for(int i=0; i<params.length; i++){ 2032 classParams[i] = params[i].getClass(); 2033 } 2034 Constructor cons = clazz.getConstructor(classParams); 2035 } 2036 catch(NoSuchMethodException ne) 2037 { 2038 throw new ReflectionException (ne); 2039 } 2040 } 2041 catch(NoSuchMethodException nsme) 2042 { 2043 try 2044 { 2045 Class [] classParams = new Class [params.length]; 2046 for(int i=0; i<params.length; i++) 2047 { 2048 classParams[i] = params[i].getClass(); 2049 } 2050 2051 Constructor cons = clazz.getConstructor(classParams); 2052 obj = cons.newInstance(params); 2053 } 2054 catch(NoSuchMethodException ne) 2055 { 2056 throw new ReflectionException (ne); 2057 } 2058 catch(Exception ene) 2059 { 2060 throw new MBeanException (ene); 2061 } 2062 } 2063 catch(IllegalAccessException iae) 2064 { 2065 throw new MBeanException (iae,"Exception occured while invoking the constructor of the MBean!!!"); 2066 } 2067 catch(Exception e) 2068 { 2069 if(e instanceof ReflectionException ) 2070 { 2071 throw (ReflectionException )e; 2072 } 2073 2074 if(e instanceof RuntimeException ) 2075 { 2076 throw new RuntimeMBeanException ((RuntimeException )e); 2077 } 2078 2079 if(e instanceof MBeanException ) 2080 { 2081 throw (MBeanException )e; 2082 } 2083 2084 if(e instanceof InvocationTargetException ) 2085 { 2086 Exception ee = (Exception )((InvocationTargetException )e).getTargetException(); 2087 if(ee instanceof RuntimeException ) 2088 { 2089 throw new RuntimeMBeanException ((RuntimeException )ee); 2090 } 2091 else 2092 { 2093 throw new MBeanException (ee); 2094 } 2095 } 2096 2097 throw new ReflectionException (e,"General Exception occured. " + 2098 "It might be runtime errors while invoking the constructor"); 2099 } 2100 2101 return registerMBean(obj, name); 2102 } 2103 2104 2120 public java.io.ObjectInputStream deserialize(ObjectName name, byte[] data) 2121 throws InstanceNotFoundException , 2122 OperationsException 2123 { 2124 Object obj = serverTable.get(name); 2125 if(obj == null) 2126 throw new InstanceNotFoundException (name.toString()); 2127 2128 try 2129 { 2130 return deserialize(obj.getClass().getName(), data); 2131 } 2132 catch(ReflectionException re) 2133 { 2134 throw new OperationsException ("Exception while de-serializing the byte array data"); 2135 } 2136 } 2137 2138 2155 public java.io.ObjectInputStream deserialize(String className, byte[] data) 2156 throws OperationsException ,ReflectionException 2157 { 2158 return deserialize(className, null, data); 2159 } 2160 2161 2187 public java.io.ObjectInputStream deserialize(String className, 2188 ObjectName loaderName, 2189 byte[] data) 2190 throws InstanceNotFoundException , 2191 OperationsException , 2192 ReflectionException 2193 { 2194 Class clazz = null; 2195 2196 if(data == null || data.length == 0) 2197 throw new RuntimeOperationsException ( 2198 new IllegalArgumentException (), "invalid data"); 2199 2200 if(className == null) 2201 throw new RuntimeOperationsException ( 2202 new IllegalArgumentException (), "invalid className"); 2203 2204 if(loaderName != null) 2205 { 2206 Object obj = serverTable.get(loaderName); 2207 try 2208 { 2209 if(obj == null) 2210 { 2211 throw new RuntimeOperationsException (new IllegalArgumentException ("Invalid classloader "+loaderName)); 2212 } 2213 else if(obj instanceof DefaultDynamicMBean && 2214 ((DefaultDynamicMBean)obj).getStandardMBeanObject() instanceof ClassLoader ) 2215 { 2216 clazz = ((ClassLoader )((DefaultDynamicMBean)obj).getStandardMBeanObject()).loadClass(className); 2217 } 2218 else if(!(obj instanceof ClassLoader )) 2219 { 2220 throw new RuntimeOperationsException (new IllegalArgumentException ("Invalid classloader "+loaderName)); 2221 } 2222 else 2223 { 2224 clazz = ((ClassLoader )obj).loadClass(className); 2225 } 2226 } 2227 catch(ClassNotFoundException cnfe) 2228 { 2229 throw new ReflectionException (cnfe, "Loading failed by the loader " + loaderName); 2230 } 2231 } 2232 else 2233 { 2234 try 2235 { 2236 clazz = getClassLoader(this).loadClass(className); 2238 } 2239 catch(ClassNotFoundException cnfe) 2240 { 2241 throw new ReflectionException (cnfe, className + " MBean could not be loaded"); 2242 } 2243 } 2244 2245 ByteArrayInputStream bis = new ByteArrayInputStream (data); 2246 2247 ObjectInputStreamSupport oiss = null; 2248 try 2249 { 2250 oiss = new ObjectInputStreamSupport(bis, getClassLoader(clazz)); 2252 } 2253 catch(Exception e) 2254 { 2255 throw new OperationsException ("Exception while de-serializing the byte array data"); 2256 } 2257 2258 return oiss; 2259 } 2260 2261 2264 public Integer getMBeanCount() 2265 { 2266 return new Integer (serverTable.size()); 2267 } 2268 2269 2292 public Object getObjectInClassLoader(ObjectName name, byte[] entityBody) 2293 throws InstanceNotFoundException , 2294 ClassNotFoundException , 2295 java.io.OptionalDataException , 2296 java.io.IOException 2297 { 2299 Object object = null; 2300 Class clazz = null; 2301 2302 if(entityBody == null) 2303 throw new IOException ("entityBody is null"); 2304 2305 if(entityBody.length == 0) 2306 throw new IOException ("entityBody length is zero"); 2307 2308 return null; 2309 } 2310 2311 2326 public Object instantiate(String className) 2327 throws ReflectionException , MBeanException 2328 { 2329 if(className == null) 2330 throw new RuntimeOperationsException (new IllegalArgumentException ( 2331 "className cannot be null")); 2332 2333 Class class_name = null; 2334 2335 try 2336 { 2337 class_name = DefaultLoaderRepositoryExt.loadClass(className); 2338 return class_name.newInstance(); 2339 } 2340 catch(ClassNotFoundException e) 2341 { 2342 throw new ReflectionException (e); 2343 } 2344 catch(IllegalAccessException ie) 2345 { 2346 throw new ReflectionException (ie); 2347 } 2348 catch(InstantiationException ie) 2349 { 2350 try 2351 { 2352 Constructor cons = class_name.getConstructor(new Class [0]); 2353 } 2354 catch(NoSuchMethodException e) 2355 { 2356 throw new ReflectionException (e); 2357 } 2358 2359 throw new MBeanException (ie); 2360 } 2361 catch(Exception e1) 2362 { 2363 if(e1 instanceof ReflectionException ) 2364 { 2365 throw (ReflectionException )e1; 2366 } 2367 2368 throw new MBeanException (e1); 2369 } 2370 } 2371 2372 2394 public Object instantiate(String className, Object [] params, String [] signature) 2395 throws ReflectionException , MBeanException 2396 { 2397 if(className == null) 2398 throw new RuntimeOperationsException (new IllegalArgumentException ("className cannot be null")); 2399 2400 try 2401 { 2402 Class class_name = DefaultLoaderRepositoryExt.loadClass(className); 2403 2404 if(params == null || signature == null) 2405 { 2406 return instantiate(className); 2407 } 2408 2409 Class [] sig = new Class [signature.length]; 2410 for(int i = 0;i<signature.length;i++) 2411 { 2412 if((sig[i] = getThePrimitiveClassObject(signature[i])) == null) 2413 sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 2414 } 2415 2416 Constructor constr = class_name.getConstructor(sig); 2417 2418 return constr.newInstance(params); 2419 } 2420 catch(ClassNotFoundException e) 2421 { 2422 throw new ReflectionException (e); 2423 } 2424 catch(IllegalArgumentException ie) 2425 { 2426 throw new ReflectionException (ie); 2427 } 2428 catch(NoSuchMethodException ne) 2429 { 2430 throw new ReflectionException (ne); 2431 } 2432 catch(Exception e1) 2433 { 2434 throw new MBeanException (e1); 2435 } 2436 } 2437 2438 2459 public Object instantiate(String className, ObjectName loaderName) 2460 throws ReflectionException , 2461 MBeanException , 2462 InstanceNotFoundException 2463 { 2464 if(className == null ) 2465 throw new RuntimeOperationsException (new IllegalArgumentException ("className cannot be null")); 2466 2467 Object object = null; 2468 Object loaderObject = null; 2469 Class clazz = null; 2470 2471 try 2472 { 2473 object = serverTable.get(loaderName); 2474 if( (object instanceof DefaultDynamicMBean)) 2475 { 2476 loaderObject = ((DefaultDynamicMBean)(object)).getStandardMBeanObject(); 2477 2478 } 2479 else if( ! (object instanceof ClassLoader )) 2480 { 2481 throw new MBeanException (new Exception ("Invalid ClassLoader ")); 2482 } 2483 2484 if(loaderObject instanceof ClassLoader ) 2485 { 2486 clazz = ((ClassLoader )loaderObject).loadClass(className); 2487 } 2488 else 2489 { 2490 throw new MBeanException (new Exception ("Invalid ClassLoader ")); 2491 } 2492 } 2493 catch(ClassNotFoundException cnfe) 2494 { 2495 throw new ReflectionException (cnfe); 2496 } 2497 catch(Exception e) 2498 { 2499 try 2500 { 2501 clazz = getClassLoader(this).loadClass(className); 2502 } 2503 catch(ClassNotFoundException cnfe) 2504 { 2505 throw new ReflectionException (cnfe); 2506 } 2507 } 2508 2509 try 2510 { 2511 return clazz.newInstance(); 2512 } 2513 catch(IllegalAccessException ie) 2514 { 2515 throw new ReflectionException (ie); 2516 } 2517 catch(Exception e1) 2518 { 2519 throw new MBeanException (e1,"Exception occured while invoking the constructor of the MBean"); 2520 } 2521 } 2522 2523 2550 public Object instantiate(String className, ObjectName loaderName, 2551 Object [] params, String [] signature) 2552 throws ReflectionException , 2553 MBeanException , 2554 InstanceNotFoundException 2555 { 2556 if(className == null ) 2557 throw new RuntimeOperationsException (new IllegalArgumentException ("className cannot be null")); 2558 2559 Object object = null; 2560 Class clazz = null; 2561 Object loaderObject = null; 2562 2563 try 2564 { 2565 object = serverTable.get(loaderName); 2566 2567 if( (object instanceof DefaultDynamicMBean)) 2568 { 2569 loaderObject = ((DefaultDynamicMBean)(object)).getStandardMBeanObject(); 2570 } 2571 else if( ! (object instanceof ClassLoader )) 2572 { 2573 throw new MBeanException (new Exception ("Invalid ClassLoader ")); 2574 } 2575 2576 if(loaderObject instanceof ClassLoader ) 2577 { 2578 clazz = ((ClassLoader )loaderObject).loadClass(className); 2579 } 2580 else 2581 { 2582 throw new MBeanException (new Exception ("Invalid ClassLoader ")); 2583 } 2584 } 2585 catch(ClassNotFoundException cnfe) 2586 { 2587 throw new ReflectionException (cnfe); 2588 } 2589 catch(Exception e) 2590 { 2591 try 2592 { 2593 clazz = getClassLoader(this).loadClass(className); 2595 } 2596 catch(ClassNotFoundException cnfe) 2597 { 2598 throw new ReflectionException (cnfe); 2599 } 2600 } 2601 2602 if(params == null || signature == null) 2603 { 2604 return instantiate(className,loaderName); 2605 } 2606 2607 try 2608 { 2609 Class [] sig = new Class [signature.length]; 2610 2611 for(int i = 0;i<signature.length;i++) 2612 { 2613 if((sig[i] = getThePrimitiveClassObject(signature[i])) == null) 2614 sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 2615 } 2616 2617 Constructor constr = clazz.getConstructor(sig); 2618 return constr.newInstance(params); 2619 } 2620 catch(ClassNotFoundException cnfe) 2621 { 2622 throw new ReflectionException (cnfe); 2623 } 2624 catch(IllegalAccessException ie){ 2625 throw new ReflectionException (ie); 2626 } 2627 catch(Exception e1) 2628 { 2629 throw new MBeanException (e1); 2630 } 2631 } 2632 2633 2647 public boolean isInSameClassLoader(ObjectName name, Object object) 2648 throws InstanceNotFoundException 2649 { 2650 return false; 2651 } 2652 2653 2657 public void removeNotificationListener(ObjectName name, 2658 NotificationListener listener) 2659 throws InstanceNotFoundException , 2660 ListenerNotFoundException 2661 { 2662 Object object = null; 2663 2664 if(name == null) 2665 { 2666 throw new RuntimeOperationsException (new IllegalArgumentException ( 2667 "ObjectName cannot be null!!!")); 2668 } 2669 2670 object = serverTable.get(name); 2671 2672 if(object == null) 2673 { 2674 throw new InstanceNotFoundException ("No MBean with the Object Name "+listener+ " registered with MBeanServer"); 2675 } 2676 2677 if( !(object instanceof NotificationBroadcaster )) 2678 { 2679 throw new RuntimeOperationsException (new IllegalArgumentException ("The MBean"+name+" does not implement NotificationBroadCaster")); 2680 } 2681 2682 synchronized(object) 2683 { 2684 ((NotificationBroadcaster )object).removeNotificationListener(listener); 2685 } 2686 } 2687 2688 2712 public void removeNotificationListener(ObjectName name, ObjectName listener) 2713 throws InstanceNotFoundException , ListenerNotFoundException 2714 { 2715 Object object = null; 2716 2717 if(name == null) 2718 { 2719 throw new RuntimeOperationsException (new IllegalArgumentException ( 2720 "ObjectName cannot be null!!!")); 2721 } 2722 object = serverTable.get(name); 2723 2724 if(object == null) 2725 { 2726 throw new InstanceNotFoundException ("No MBean with the Object Name "+listener+ " registered with MBeanServer"); 2727 } 2728 2729 Object lobject = null; 2730 lobject = serverTable.get(listener); 2731 2732 if(lobject == null) 2733 { 2734 throw new InstanceNotFoundException ("No listener with the name "+listener+ " present in the JMXServer!!!"); 2735 } 2736 2737 if( !(lobject instanceof NotificationListener )) 2738 { 2739 throw new RuntimeOperationsException (new IllegalArgumentException ("The MBean"+name+" does not implement NotificationListener")); 2740 } 2741 2742 synchronized(object) 2743 { 2744 ((NotificationBroadcaster )object).removeNotificationListener( (NotificationListener )lobject); 2745 } 2746 } 2747 2748 2752 public Hashtable getLoaderTable() 2753 { 2754 return loaderTable; 2755 } 2756 2757 2759 private Class getThePrimitiveClassObject(String str) 2760 { 2761 if(str.indexOf("boolean") != -1) 2762 return Boolean.TYPE; 2763 else if(str.indexOf("int") != -1) 2764 return Integer.TYPE; 2765 else if(str.indexOf("double") != -1) 2766 return Double.TYPE; 2767 else if(str.indexOf("float") != -1) 2768 return Float.TYPE; 2769 else if(str.indexOf("short") != -1) 2770 return Short.TYPE; 2771 else if(str.indexOf("long") != -1) 2772 return Long.TYPE; 2773 else if(str.indexOf("byte") != -1) 2774 return Byte.TYPE; 2775 else if(str.indexOf("char") != -1) 2776 return Character.TYPE; 2777 else 2778 return null; 2779 } 2780 2781 private void testForCompliance(String className,ClassLoader l) 2782 throws NotCompliantMBeanException 2783 { 2784 Class cl = null; 2785 2786 try 2787 { 2788 cl = Class.forName(className,true,l); 2789 int i = cl.getModifiers(); 2790 if(Modifier.isAbstract(i)) 2791 throw new NotCompliantMBeanException ("The MBean cannot be a abstract class"); 2792 2793 java.lang.reflect.Constructor constructors[] = cl.getConstructors(); 2794 2795 if(constructors.length == 0) 2796 throw new NotCompliantMBeanException ("The MBean does not have a public constructor"); 2797 } 2798 catch(ClassNotFoundException ee) 2799 { 2800 log.warn("ClassNotFoundException",ee); 2801 } 2802 2803 Class superClass = null; 2804 superClass = cl; 2805 Class class1 = null; 2806 Class class2 = null; 2807 boolean flag = false; 2808 for(;superClass != null;superClass = superClass.getSuperclass()) 2809 { 2810 Class [] interfaces = superClass.getInterfaces(); 2811 boolean checkFlag = false; 2812 for(int j=0;j<interfaces.length;j++) 2813 { 2814 Class tempClass = checkForMBeanInterface(interfaces[j],superClass.getName()); 2815 if(tempClass != null) 2816 { 2817 flag = true; 2818 class1 = tempClass; 2819 class2 = superClass; 2820 } 2821 2822 if(checkForDynamicMBeanInterface(interfaces[j])) 2823 { 2824 flag = true; 2825 checkFlag = true; 2826 } 2827 2828 if(class1 != null && superClass.equals(class2) && checkFlag) 2829 throw new NotCompliantMBeanException (className + " implements javax.management.DynamicMBean and MBean interface"); 2830 } 2831 if(flag) 2832 break; 2833 } 2834 2835 if(!flag) 2836 throw new NotCompliantMBeanException (className + " does not implement the " + className+"MBean" + " interface or the DynamicMBean interface"); 2837 } 2838 2839 private Class checkForMBeanInterface(Class class1, String s) 2840 { 2841 if(class1.getName().compareTo(s + "MBean") == 0) 2842 return class1; 2843 2844 Class aclass[] = class1.getInterfaces(); 2845 for(int i = 0; i < aclass.length; i++) 2846 { 2847 if(aclass[i].getName().compareTo(s + "MBean") == 0) 2848 return aclass[i]; 2849 } 2850 2851 return null; 2852 } 2853 2854 2855 private boolean checkForDynamicMBeanInterface(Class class1) 2856 { 2857 if(class1.getName().compareTo("javax.management.DynamicMBean") == 0) 2858 return true; 2859 Class aclass[] = class1.getInterfaces(); 2860 for(int i = 0; i < aclass.length; i++) 2861 if(aclass[i].getName().compareTo("javax.management.DynamicMBean") == 0) 2862 return true; 2863 2864 return false; 2865 } 2866 2867 private boolean isValidMBeanInfo(MBeanInfo mbInfo) 2868 { 2869 if(mbInfo.getClassName() == null) 2870 { 2871 return false; 2872 } 2873 2874 2882 2883 return true; 2884 } 2885 2886 private ClassLoader getClassLoader(Object o) 2887 { 2888 return getClassLoader(o.getClass()); 2889 } 2890 2891 private ClassLoader getClassLoader(Class clazz) 2892 { 2893 ClassLoader cl = clazz.getClassLoader(); 2894 if(cl == null) 2895 { 2896 cl = Thread.currentThread().getContextClassLoader(); 2897 } 2898 return cl; 2899 } 2900 2901 private void createLogger() 2902 { 2903 try 2904 { 2905 log = LogFactory.getInstance("JMX"); 2906 } 2907 catch(Exception e) 2908 { 2909 System.out.println("FATAL:Log Creation Failed"); 2910 } 2911 } 2912} | Popular Tags |