1 7 8 package com.sun.jmx.mbeanserver; 9 10 import java.util.Iterator ; 12 import java.util.ArrayList ; 13 import java.util.Set ; 14 import java.util.HashSet ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.lang.reflect.Method ; 17 import java.lang.reflect.Constructor ; 18 import java.io.OptionalDataException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.security.AccessController ; 25 import java.security.Permission ; 26 import java.security.PrivilegedExceptionAction ; 27 28 import javax.management.MBeanPermission ; 30 import javax.management.DynamicMBean ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.MBeanException ; 33 import javax.management.ReflectionException ; 34 import javax.management.MBeanAttributeInfo ; 35 import javax.management.MBeanInfo ; 36 import javax.management.QueryExp ; 37 import javax.management.NotificationListener ; 38 import javax.management.NotificationFilter ; 39 import javax.management.ListenerNotFoundException ; 40 import javax.management.IntrospectionException ; 41 import javax.management.OperationsException ; 42 import javax.management.MBeanNotificationInfo ; 43 import javax.management.JMRuntimeException ; 44 import javax.management.InstanceNotFoundException ; 45 import javax.management.NotCompliantMBeanException ; 46 import javax.management.MBeanRegistrationException ; 47 import javax.management.InstanceAlreadyExistsException ; 48 import javax.management.InvalidAttributeValueException ; 49 import javax.management.ObjectName ; 50 import javax.management.ObjectInstance ; 51 import javax.management.Attribute ; 52 import javax.management.AttributeList ; 53 import javax.management.RuntimeOperationsException ; 54 import javax.management.MBeanServer ; 55 import javax.management.MBeanServerDelegate ; 56 import javax.management.loading.ClassLoaderRepository ; 57 58 import com.sun.jmx.interceptor.DefaultMBeanServerInterceptor; 59 import com.sun.jmx.interceptor.MBeanServerInterceptor; 60 import com.sun.jmx.defaults.ServiceName; 61 import com.sun.jmx.trace.Trace; 62 63 88 public final class JmxMBeanServer 89 implements SunJmxMBeanServer { 90 91 92 private static final String dbgTag = "MBeanServer"; 93 94 private final MBeanInstantiator instantiator; 95 private final SecureClassLoaderRepository secureClr; 96 97 private final MetaData meta; 98 99 100 private final boolean interceptorsEnabled; 101 102 103 private final transient MBeanServer outerShell; 104 105 106 private transient MBeanServerInterceptor mbsInterceptor = null; 107 108 109 110 private final transient MBeanServerDelegate mBeanServerDelegateObject; 111 112 113 114 private transient ObjectName mBeanServerDelegateObjectName = null; 115 116 140 JmxMBeanServer(String domain, MBeanServer outer, 141 MBeanServerDelegate delegate) { 142 this(domain,outer,delegate,null,null,false); 143 } 144 145 169 JmxMBeanServer(String domain, MBeanServer outer, 170 MBeanServerDelegate delegate, boolean interceptors) { 171 this(domain,outer,delegate,null,null,false); 172 } 173 174 192 JmxMBeanServer(String domain, MBeanServer outer, 193 MBeanServerDelegate delegate, 194 MBeanInstantiator instantiator, 195 MetaData metadata, 196 boolean interceptors) { 197 198 if (instantiator == null) { 199 final ModifiableClassLoaderRepository 200 clr = new ClassLoaderRepositorySupport(); 201 instantiator = new MBeanInstantiatorImpl(clr); 202 } 203 this.secureClr = new 204 SecureClassLoaderRepository(instantiator.getClassLoaderRepository()); 205 if (metadata == null) 206 metadata = new MetaDataImpl(instantiator); 207 if (delegate == null) 208 delegate = new MBeanServerDelegateImpl(); 209 if (outer == null) 210 outer = this; 211 212 this.instantiator = instantiator; 213 this.meta = metadata; 214 this.mBeanServerDelegateObject = delegate; 215 this.outerShell = outer; 216 217 final Repository repository = new RepositorySupport(domain); 218 this.mbsInterceptor = 219 new DefaultMBeanServerInterceptor(outer, delegate, instantiator, 220 metadata, repository); 221 this.interceptorsEnabled = interceptors; 222 initialize(); 223 } 224 225 232 public boolean interceptorsEnabled() { 233 return interceptorsEnabled; 234 } 235 236 243 public MBeanInstantiator getMBeanInstantiator() { 244 if (interceptorsEnabled) return instantiator; 245 else throw new UnsupportedOperationException ( 246 "MBeanServerInterceptors are disabled."); 247 } 248 249 252 public MetaData getMetaData() { 253 254 return meta; 255 } 256 257 296 public ObjectInstance createMBean(String className, ObjectName name) 297 throws ReflectionException , InstanceAlreadyExistsException , 298 MBeanRegistrationException , MBeanException , 299 NotCompliantMBeanException { 300 301 return mbsInterceptor.createMBean(className, 302 cloneObjectName(name), 303 (Object []) null, 304 (String []) null); 305 } 306 307 348 public ObjectInstance createMBean(String className, ObjectName name, 349 ObjectName loaderName) 350 throws ReflectionException , InstanceAlreadyExistsException , 351 MBeanRegistrationException , MBeanException , 352 NotCompliantMBeanException , InstanceNotFoundException { 353 354 return mbsInterceptor.createMBean(className, 355 cloneObjectName(name), 356 loaderName, 357 (Object []) null, 358 (String []) null); 359 } 360 361 402 public ObjectInstance createMBean(String className, ObjectName name, 403 Object params[], String signature[]) 404 throws ReflectionException , InstanceAlreadyExistsException , 405 MBeanRegistrationException , MBeanException , 406 NotCompliantMBeanException { 407 408 return mbsInterceptor.createMBean(className, cloneObjectName(name), 409 params, signature); 410 } 411 412 456 public ObjectInstance createMBean(String className, ObjectName name, 457 ObjectName loaderName, Object params[], 458 String signature[]) 459 throws ReflectionException , InstanceAlreadyExistsException , 460 MBeanRegistrationException , MBeanException , 461 NotCompliantMBeanException , InstanceNotFoundException { 462 463 return mbsInterceptor.createMBean(className, cloneObjectName(name), 464 loaderName, params, signature); 465 } 466 467 493 public ObjectInstance registerMBean(Object object, ObjectName name) 494 throws InstanceAlreadyExistsException , MBeanRegistrationException , 495 NotCompliantMBeanException { 496 497 return mbsInterceptor.registerMBean(object, cloneObjectName(name)); 498 } 499 500 519 public void unregisterMBean(ObjectName name) 520 throws InstanceNotFoundException , MBeanRegistrationException { 521 mbsInterceptor.unregisterMBean(cloneObjectName(name)); 528 } 529 530 542 public ObjectInstance getObjectInstance(ObjectName name) 543 throws InstanceNotFoundException { 544 545 return mbsInterceptor.getObjectInstance(cloneObjectName(name)); 546 } 547 548 571 public Set queryMBeans(ObjectName name, QueryExp query) { 572 573 return mbsInterceptor.queryMBeans(cloneObjectName(name), query); 574 } 575 576 598 public Set queryNames(ObjectName name, QueryExp query) { 599 600 return mbsInterceptor.queryNames(cloneObjectName(name), query); 601 } 602 603 617 public boolean isRegistered(ObjectName name) { 618 619 return mbsInterceptor.isRegistered(name); 620 } 621 622 625 public Integer getMBeanCount() { 626 627 return mbsInterceptor.getMBeanCount(); 628 } 629 630 655 public Object getAttribute(ObjectName name, String attribute) 656 throws MBeanException , AttributeNotFoundException , 657 InstanceNotFoundException , ReflectionException { 658 659 return mbsInterceptor.getAttribute(cloneObjectName(name), attribute); 660 } 661 662 663 683 public AttributeList getAttributes(ObjectName name, String [] attributes) 684 throws InstanceNotFoundException , ReflectionException { 685 686 return mbsInterceptor.getAttributes(cloneObjectName(name), attributes); 687 688 } 689 690 717 public void setAttribute(ObjectName name, Attribute attribute) 718 throws InstanceNotFoundException , AttributeNotFoundException , 719 InvalidAttributeValueException , MBeanException , 720 ReflectionException { 721 722 mbsInterceptor.setAttribute(cloneObjectName(name), 723 cloneAttribute(attribute)); 724 } 725 726 747 public AttributeList setAttributes(ObjectName name, 748 AttributeList attributes) 749 throws InstanceNotFoundException , ReflectionException { 750 751 return mbsInterceptor.setAttributes(cloneObjectName(name), 752 cloneAttributeList(attributes)); 753 } 754 755 780 public Object invoke(ObjectName name, String operationName, 781 Object params[], String signature[]) 782 throws InstanceNotFoundException , MBeanException , 783 ReflectionException { 784 return mbsInterceptor.invoke(cloneObjectName(name), operationName, 785 params, signature); 786 } 787 788 793 public String getDefaultDomain() { 794 return mbsInterceptor.getDefaultDomain(); 795 } 796 797 public String [] getDomains() { 799 return mbsInterceptor.getDomains(); 800 } 801 802 816 public void addNotificationListener(ObjectName name, 817 NotificationListener listener, 818 NotificationFilter filter, 819 Object handback) 820 throws InstanceNotFoundException { 821 822 mbsInterceptor.addNotificationListener(cloneObjectName(name), listener, 823 filter, handback); 824 } 825 826 841 public void addNotificationListener(ObjectName name, ObjectName listener, 842 NotificationFilter filter, Object handback) 843 throws InstanceNotFoundException { 844 845 mbsInterceptor.addNotificationListener(cloneObjectName(name), listener, 846 filter, handback); 847 } 848 849 public void removeNotificationListener(ObjectName name, 850 NotificationListener listener) 851 throws InstanceNotFoundException , ListenerNotFoundException { 852 853 mbsInterceptor.removeNotificationListener(cloneObjectName(name), 854 listener); 855 } 856 857 public void removeNotificationListener(ObjectName name, 858 NotificationListener listener, 859 NotificationFilter filter, 860 Object handback) 861 throws InstanceNotFoundException , ListenerNotFoundException { 862 863 mbsInterceptor.removeNotificationListener(cloneObjectName(name), 864 listener, filter, handback); 865 } 866 867 public void removeNotificationListener(ObjectName name, 868 ObjectName listener) 869 throws InstanceNotFoundException , ListenerNotFoundException { 870 871 mbsInterceptor.removeNotificationListener(cloneObjectName(name), 872 listener); 873 } 874 875 public void removeNotificationListener(ObjectName name, 876 ObjectName listener, 877 NotificationFilter filter, 878 Object handback) 879 throws InstanceNotFoundException , ListenerNotFoundException { 880 881 mbsInterceptor.removeNotificationListener(cloneObjectName(name), 882 listener, filter, handback); 883 } 884 885 900 public MBeanInfo getMBeanInfo(ObjectName name) throws 901 InstanceNotFoundException , IntrospectionException , ReflectionException { 902 903 return mbsInterceptor.getMBeanInfo(cloneObjectName(name)); 904 } 905 906 929 public Object instantiate(String className) 930 throws ReflectionException , MBeanException { 931 932 933 checkMBeanPermission(className, null, null, "instantiate"); 934 935 return instantiator.instantiate(className); 936 } 937 938 965 public Object instantiate(String className, ObjectName loaderName) 966 throws ReflectionException , MBeanException , 967 InstanceNotFoundException { 968 969 970 checkMBeanPermission(className, null, null, "instantiate"); 971 972 ClassLoader myLoader = outerShell.getClass().getClassLoader(); 973 return instantiator.instantiate(className, loaderName, myLoader); 974 } 975 976 1003 public Object instantiate(String className, Object params[], 1004 String signature[]) 1005 throws ReflectionException , MBeanException { 1006 1007 1008 checkMBeanPermission(className, null, null, "instantiate"); 1009 1010 ClassLoader myLoader = outerShell.getClass().getClassLoader(); 1011 return instantiator.instantiate(className, params, signature, 1012 myLoader); 1013 } 1014 1015 1045 public Object instantiate(String className, ObjectName loaderName, 1046 Object params[], String signature[]) 1047 throws ReflectionException , MBeanException , 1048 InstanceNotFoundException { 1049 1050 1051 checkMBeanPermission(className, null, null, "instantiate"); 1052 1053 ClassLoader myLoader = outerShell.getClass().getClassLoader(); 1054 return instantiator.instantiate(className,loaderName,params,signature, 1055 myLoader); 1056 } 1057 1058 1071 public boolean isInstanceOf(ObjectName name, String className) 1072 throws InstanceNotFoundException { 1073 1074 return mbsInterceptor.isInstanceOf(cloneObjectName(name), className); 1075 } 1076 1077 1093 public ObjectInputStream deserialize(ObjectName name, byte[] data) 1094 throws InstanceNotFoundException , OperationsException { 1095 1096 1097 final ClassLoader loader = getClassLoaderFor(name); 1099 1100 return instantiator.deserialize(loader, data); 1101 } 1102 1103 1119 public ObjectInputStream deserialize(String className, byte[] data) 1120 throws OperationsException , ReflectionException { 1121 1122 if (className == null) { 1123 throw new RuntimeOperationsException ( 1124 new IllegalArgumentException (), 1125 "Null className passed in parameter"); 1126 } 1127 1128 1129 final ClassLoaderRepository clr = getClassLoaderRepository(); 1131 1132 Class theClass; 1133 try { 1134 if (clr == null) throw new ClassNotFoundException (className); 1135 theClass = clr.loadClass(className); 1136 } catch (ClassNotFoundException e) { 1137 throw new ReflectionException (e, 1138 "The given class could not be " + 1139 "loaded by the default loader " + 1140 "repository"); 1141 } 1142 1143 return instantiator.deserialize(theClass.getClassLoader(), data); 1144 } 1145 1146 1170 public ObjectInputStream deserialize(String className, 1171 ObjectName loaderName, 1172 byte[] data) throws 1173 InstanceNotFoundException , OperationsException , ReflectionException { 1174 1175 loaderName = cloneObjectName(loaderName); 1178 1179 1180 try { 1183 getClassLoader(loaderName); 1184 } catch (SecurityException e) { 1185 throw e; 1186 } catch (Exception e) { 1187 } 1188 1189 ClassLoader myLoader = outerShell.getClass().getClassLoader(); 1190 return instantiator.deserialize(className, loaderName, data, myLoader); 1191 } 1192 1193 1197 private void initialize() { 1198 if (instantiator == null) throw new 1199 IllegalStateException ("instantiator must not be null."); 1200 1201 try { 1203 mBeanServerDelegateObjectName = 1204 new ObjectName (ServiceName.DELEGATE) ; 1205 1206 AccessController.doPrivileged(new PrivilegedExceptionAction () { 1207 public Object run() throws Exception { 1208 mbsInterceptor.registerMBean(mBeanServerDelegateObject, 1209 mBeanServerDelegateObjectName); 1210 return null; 1211 } 1212 }); 1213 } catch (SecurityException e) { 1214 if (isDebugOn()) { 1215 debug("new", "Unexpected security exception occured: " + 1216 e); 1217 } 1218 mBeanServerDelegateObjectName = null; 1219 throw e; 1220 } catch (Exception e) { 1221 if (isDebugOn()) { 1222 debug("new", "Unexpected exception occured: " + 1223 e.getClass().getName()); 1224 } 1225 mBeanServerDelegateObjectName = null; 1226 throw new 1227 IllegalStateException ("Can't register delegate."); 1228 } 1229 1230 1231 1235 ClassLoader myLoader = outerShell.getClass().getClassLoader(); 1236 final ModifiableClassLoaderRepository loaders = 1237 instantiator.getClassLoaderRepository(); 1238 if (loaders != null) { 1239 loaders.addClassLoader(myLoader); 1240 1241 1256 ClassLoader systemLoader = ClassLoader.getSystemClassLoader(); 1257 if (systemLoader != myLoader) 1258 loaders.addClassLoader(systemLoader); 1259 } 1260 } 1261 1262 1269 public synchronized MBeanServerInterceptor getMBeanServerInterceptor() { 1270 if (interceptorsEnabled) return mbsInterceptor; 1271 else throw new UnsupportedOperationException ( 1272 "MBeanServerInterceptors are disabled."); 1273 } 1274 1275 1282 public synchronized void 1283 setMBeanServerInterceptor(MBeanServerInterceptor interceptor) { 1284 if (!interceptorsEnabled) throw new UnsupportedOperationException ( 1285 "MBeanServerInterceptors are disabled."); 1286 if (interceptor == null) throw new 1287 IllegalArgumentException ("MBeanServerInterceptor is null"); 1288 mbsInterceptor = interceptor; 1289 } 1290 1291 1298 public ClassLoader getClassLoaderFor(ObjectName mbeanName) 1299 throws InstanceNotFoundException { 1300 return mbsInterceptor.getClassLoaderFor(cloneObjectName(mbeanName)); 1301 } 1302 1303 1310 public ClassLoader getClassLoader(ObjectName loaderName) 1311 throws InstanceNotFoundException { 1312 return mbsInterceptor.getClassLoader(cloneObjectName(loaderName)); 1313 } 1314 1315 1319 public ClassLoaderRepository getClassLoaderRepository() { 1320 1321 checkMBeanPermission(null, null, null, "getClassLoaderRepository"); 1322 return secureClr; 1323 } 1324 1325 public MBeanServerDelegate getMBeanServerDelegate() { 1326 return mBeanServerDelegateObject; 1327 } 1328 1329 1331 1347 public static MBeanServerDelegate newMBeanServerDelegate() { 1348 return new MBeanServerDelegateImpl(); 1349 } 1350 1351 1391 public static MBeanServer newMBeanServer(String defaultDomain, 1392 MBeanServer outer, 1393 MBeanServerDelegate delegate, 1394 boolean interceptors) { 1395 return new JmxMBeanServer(defaultDomain,outer,delegate,interceptors); 1396 } 1397 1398 1401 1404 private ObjectName cloneObjectName(ObjectName name) { 1405 if (name != null) { 1406 return ObjectName.getInstance(name); 1407 } 1408 return name; 1409 } 1410 1411 1414 private Attribute cloneAttribute(Attribute attribute) { 1415 if (attribute != null) { 1416 if (!attribute.getClass().equals(Attribute .class)) { 1417 return new Attribute (attribute.getName(), attribute.getValue()); 1418 } 1419 } 1420 return attribute; 1421 } 1422 1423 1426 private AttributeList cloneAttributeList(AttributeList list) { 1427 if (list != null) { 1428 if (!list.getClass().equals(AttributeList .class)) { 1429 AttributeList newList = new AttributeList (list.size()); 1432 1433 for (Iterator i = list.iterator(); i.hasNext(); ) { 1436 Attribute attribute = (Attribute ) i.next(); 1437 newList.add(cloneAttribute(attribute)); 1438 } 1439 return newList; 1440 } else { 1441 for (int i = 0; i < list.size(); i++) { 1444 Attribute attribute = (Attribute ) list.get(i); 1445 if (!attribute.getClass().equals(Attribute .class)) { 1446 list.set(i, cloneAttribute(attribute)); 1447 } 1448 } 1449 return list; 1450 } 1451 } 1452 return list; 1453 } 1454 1455 1458 private static void checkMBeanPermission(String classname, 1459 String member, 1460 ObjectName objectName, 1461 String actions) 1462 throws SecurityException { 1463 SecurityManager sm = System.getSecurityManager(); 1464 if (sm != null) { 1465 Permission perm = new MBeanPermission (classname, 1466 member, 1467 objectName, 1468 actions); 1469 sm.checkPermission(perm); 1470 } 1471 } 1472 1473 1476 private boolean isTraceOn() { 1477 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); 1478 } 1479 1480 private void trace(String clz, String func, String info) { 1481 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info); 1482 } 1483 1484 private void trace(String func, String info) { 1485 trace(dbgTag, func, info); 1486 } 1487 1488 private boolean isDebugOn() { 1489 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); 1490 } 1491 1492 private void debug(String clz, String func, String info) { 1493 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info); 1494 } 1495 1496 private void debug(String func, String info) { 1497 debug(dbgTag, func, info); 1498 } 1499} 1500 | Popular Tags |