| 1 22 package org.jboss.mx.server; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.lang.reflect.Constructor ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.security.ProtectionDomain ; 37 import java.security.AccessController ; 38 import java.security.PrivilegedAction ; 39 import java.security.PrivilegedExceptionAction ; 40 import java.security.PrivilegedActionException ; 41 42 import javax.management.Attribute ; 43 import javax.management.AttributeList ; 44 import javax.management.AttributeNotFoundException ; 45 import javax.management.InstanceAlreadyExistsException ; 46 import javax.management.InstanceNotFoundException ; 47 import javax.management.IntrospectionException ; 48 import javax.management.InvalidAttributeValueException ; 49 import javax.management.JMException ; 50 import javax.management.ListenerNotFoundException ; 51 import javax.management.MBeanException ; 52 import javax.management.MBeanInfo ; 53 import javax.management.MBeanParameterInfo ; 54 import javax.management.MBeanRegistrationException ; 55 import javax.management.MBeanServer ; 56 import javax.management.MBeanServerDelegate ; 57 import javax.management.NotCompliantMBeanException ; 58 import javax.management.NotificationBroadcaster ; 59 import javax.management.NotificationFilter ; 60 import javax.management.NotificationListener ; 61 import javax.management.ObjectInstance ; 62 import javax.management.ObjectName ; 63 import javax.management.OperationsException ; 64 import javax.management.QueryExp ; 65 import javax.management.ReflectionException ; 66 import javax.management.RuntimeErrorException ; 67 import javax.management.RuntimeMBeanException ; 68 import javax.management.RuntimeOperationsException ; 69 import javax.management.JMRuntimeException ; 70 import javax.management.MBeanPermission ; 71 import javax.management.MalformedObjectNameException ; 72 import javax.management.MBeanTrustPermission ; 73 import javax.management.loading.ClassLoaderRepository ; 74 import javax.management.modelmbean.DescriptorSupport ; 75 import javax.management.modelmbean.ModelMBean ; 76 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 77 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 78 import javax.management.modelmbean.ModelMBeanInfo ; 79 import javax.management.modelmbean.ModelMBeanInfoSupport ; 80 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 81 import javax.management.modelmbean.ModelMBeanOperationInfo ; 82 83 import org.jboss.logging.Logger; 84 import org.jboss.mx.loading.LoaderRepository; 85 import org.jboss.mx.modelmbean.ModelMBeanConstants; 86 import org.jboss.mx.modelmbean.RequiredModelMBeanInstantiator; 87 import org.jboss.mx.notification.MBeanServerListenerRegistry; 88 import org.jboss.mx.server.registry.MBeanEntry; 89 import org.jboss.mx.server.registry.MBeanRegistry; 90 import org.jboss.mx.service.ServiceConstants; 91 import org.jboss.mx.util.JMXExceptionDecoder; 92 import org.jboss.mx.util.PropertyAccess; 93 import org.jboss.util.NestedRuntimeException; 94 95 132 public class MBeanServerImpl 133 implements MBeanServer , ServerConstants, ServiceConstants, ModelMBeanConstants 134 { 135 137 140 private static final Object [] NOPARAMS = new Object [0]; 141 142 145 private static final String [] NOSIG = new String [0]; 146 147 149 152 protected MBeanServer outer = null; 153 154 157 protected MBeanRegistry registry = null; 158 159 162 private MBeanServerListenerRegistry listeners = new MBeanServerListenerRegistry(); 163 164 167 private ClassLoaderRepository classLoaderRepository; 168 169 171 174 private static Logger log = Logger.getLogger(MBeanServerImpl.class); 175 176 177 179 190 public MBeanServerImpl(String defaultDomain, MBeanServer outer, MBeanServerDelegate delegate) 191 { 192 if (outer == null) 194 this.outer = this; 195 else 196 this.outer = outer; 197 198 this.classLoaderRepository = getClassLoaderRepository(); 200 201 this.registry = createRegistry(defaultDomain); 203 204 try 208 { 209 HashMap valueMap = new HashMap (); 212 valueMap.put(JMI_DOMAIN, JMI_DOMAIN); 213 214 registry.registerMBean(delegate, 216 new ObjectName (MBEAN_SERVER_DELEGATE), 217 valueMap); 218 219 ModelMBean rmm = RequiredModelMBeanInstantiator.instantiate(); 221 rmm.setModelMBeanInfo(getRegistryManagementInterface()); 222 rmm.setManagedResource(registry, "ObjectReference"); 223 224 registry.registerMBean(rmm, new ObjectName (MBEAN_REGISTRY), valueMap); 226 227 232 ObjectName loaderName = new ObjectName (DEFAULT_LOADER_NAME); 234 registry.registerMBean(classLoaderRepository, loaderName, valueMap); 235 236 } 237 catch (Exception e) 238 { 239 throw new RuntimeException ("Cannot create MBeanServer", e); 240 } 241 } 242 243 245 public Object instantiate(String className) 246 throws ReflectionException , MBeanException  247 { 248 return instantiate(className, (ClassLoader ) null, NOPARAMS, NOSIG); 249 } 250 251 public Object instantiate(String className, Object [] params, String [] signature) 252 throws ReflectionException , MBeanException  253 { 254 return instantiate(className, (ClassLoader ) null, params, signature); 255 } 256 257 public Object instantiate(String className, ObjectName loaderName) 258 throws ReflectionException , MBeanException , InstanceNotFoundException  259 { 260 return instantiate(className, loaderName, NOPARAMS, NOSIG); 261 } 262 263 public Object instantiate(String className, ObjectName loaderName, Object [] params, String [] signature) 264 throws ReflectionException , MBeanException , InstanceNotFoundException  265 { 266 ClassLoader cl = null; 267 268 271 try 272 { 273 if (loaderName != null) 274 cl = (ClassLoader ) registry.get(loaderName).getResourceInstance(); 275 } 276 catch (ClassCastException e) 277 { 278 throw new ReflectionException (e, loaderName + " is not a class loader."); 279 } 280 281 if (cl == null) 282 cl = this.getClass().getClassLoader(); 283 if (cl == null) 284 cl = ClassLoader.getSystemClassLoader(); 285 286 return instantiate(className, cl, params, signature); 287 } 288 289 public ObjectInstance createMBean(String className, ObjectName name) 290 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException  291 { 292 try 293 { 294 Object mbean = instantiate(className); 295 return registerMBean(mbean, name, (ClassLoader ) null); 296 } 297 catch (SecurityException e) 298 { 299 throw e; 300 } 301 catch (ReflectionException refex) 302 { 303 if (refex.getCause() instanceof InstantiationException ) 305 throw new NotCompliantMBeanException ("Cannot instanciate MBean: " + className); 306 else 307 throw refex; 308 } 309 } 310 311 public ObjectInstance createMBean(String className, ObjectName name, Object [] params, String [] signature) 312 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException  313 { 314 try 315 { 316 Object mbean = instantiate(className, params, signature); 317 return registerMBean(mbean, name, (ClassLoader ) null); 318 } 319 catch (ReflectionException refex) 320 { 321 return handleExceptionOnCreate(refex, className); 322 } 323 } 324 325 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) 326 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException  327 { 328 try 329 { 330 Object mbean = instantiate(className, loaderName); 331 return registerMBean(mbean, name, loaderName); 332 } 333 catch (ReflectionException refex) 334 { 335 return handleExceptionOnCreate(refex, className); 336 } 337 } 338 339 public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object [] params, String [] signature) 340 throws ReflectionException , InstanceAlreadyExistsException , MBeanRegistrationException , MBeanException , NotCompliantMBeanException , InstanceNotFoundException  341 { 342 try 343 { 344 Object mbean = instantiate(className, loaderName, params, signature); 345 return registerMBean(mbean, name, loaderName); 346 } 347 catch (ReflectionException refex) 348 { 349 return handleExceptionOnCreate(refex, className); 350 } 351 } 352 353 357 private ObjectInstance handleExceptionOnCreate(ReflectionException refex, String className) 358 throws NotCompliantMBeanException , ReflectionException  359 { 360 if (refex.getCause() instanceof InstantiationException ) 361 throw new NotCompliantMBeanException ("Cannot instanciate MBean: " + className); 362 363 throw refex; 364 } 365 366 371 public ObjectInstance registerMBean(Object object, ObjectName name) 372 throws InstanceAlreadyExistsException , 373 MBeanRegistrationException , 374 NotCompliantMBeanException  375 { 376 return registerMBean(object, name, (ClassLoader ) null); 377 } 378 379 public void unregisterMBean(ObjectName name) 380 throws InstanceNotFoundException , MBeanRegistrationException  381 { 382 MBeanEntry entry = registry.get(name); 384 Object mbean = entry.getResourceInstance(); 385 name = entry.getObjectName(); 386 387 checkMBeanPermission(entry.getResourceClassName(), null, name, "unregisterMBean"); 388 389 try 390 { 391 final Object [] args = {name}; 392 final String [] sig = {ObjectName .class.getName()}; 393 try 394 { 395 AccessController.doPrivileged( 396 new PrivilegedExceptionAction () 397 { 398 public Object run() throws Exception  399 { 400 return invoke(new ObjectName (MBEAN_REGISTRY), 401 "unregisterMBean", args, sig); 402 } 403 } 404 ); 405 } 406 catch(PrivilegedActionException e) 407 { 408 throw e.getException(); 409 } 410 } 411 catch (Throwable t) 412 { 413 Throwable result = JMXExceptionDecoder.decodeToJMXException(t); 414 if (result instanceof InstanceNotFoundException ) 415 throw (InstanceNotFoundException ) result; 416 if (result instanceof MBeanRegistrationException ) 417 throw (MBeanRegistrationException ) result; 418 if ( result instanceof JMRuntimeException ) 419 throw (JMRuntimeException ) result; 420 if (result instanceof MBeanException ) 421 { 422 MBeanException e = (MBeanException ) result; 423 t = e.getTargetException(); 424 if (t instanceof InstanceNotFoundException ) 425 throw (InstanceNotFoundException ) t; 426 if (t instanceof MBeanRegistrationException ) 427 throw (MBeanRegistrationException ) t; 428 } 429 if (result instanceof RuntimeException ) 430 throw new RuntimeMBeanException ((RuntimeException ) result); 431 if (result instanceof Error ) 432 throw new RuntimeErrorException ((Error ) result); 433 434 throw new MBeanRegistrationException (new InvocationTargetException (t), "Cannot unregister MBean"); 436 } 437 438 if (mbean instanceof NotificationBroadcaster ) 440 listeners.remove(name); 441 } 442 443 public ObjectInstance getObjectInstance(ObjectName name) 444 throws InstanceNotFoundException  445 { 446 ObjectInstance oi = registry.getObjectInstance(name); 447 checkMBeanPermission(oi.getClassName(), null, name, 448 "getObjectInstance"); 449 450 return oi; 451 } 452 453 public Set queryMBeans(ObjectName name, QueryExp query) 454 { 455 checkMBeanPermission(null, null, null, "queryMBeans"); 457 458 Set result = new HashSet (); 460 if (query != null) 461 query.setMBeanServer(outer); 462 463 SecurityManager sm = System.getSecurityManager(); 464 List entries = registry.findEntries(name); 466 Iterator iterator = entries.iterator(); 467 while (iterator.hasNext()) 468 { 469 MBeanEntry entry = (MBeanEntry) iterator.next(); 471 ObjectName objectName = entry.getObjectName(); 472 if( sm != null ) 474 { 475 try 476 { 477 checkMBeanPermission(entry.getResourceClassName(), null, 478 objectName, "queryMBeans"); 479 } 480 catch(SecurityException e) 481 { 482 if( log.isTraceEnabled() ) 483 log.trace("Excluded mbean due to security: "+objectName); 484 continue; 485 } 486 } 487 if (queryMBean(objectName, query) == true) 489 { 490 try 491 { 492 ObjectInstance instance = registry.getObjectInstance(objectName); 493 result.add(instance); 494 } 495 catch (InstanceNotFoundException ignored) 496 { 497 } 498 } 499 } 500 501 return result; 502 } 503 504 public Set queryNames(ObjectName name, QueryExp query) 505 { 506 checkMBeanPermission(null, null, null, "queryNames"); 508 509 Set result = new HashSet (); 511 if (query != null) 512 query.setMBeanServer(outer); 513 514 SecurityManager sm = System.getSecurityManager(); 515 List entries = registry.findEntries(name); 517 Iterator iterator = entries.iterator(); 518 while (iterator.hasNext()) 519 { 520 MBeanEntry entry = (MBeanEntry) iterator.next(); 522 ObjectName objectName = entry.getObjectName(); 523 if( sm != null ) 525 { 526 try 527 { 528 checkMBeanPermission(entry.getResourceClassName(), null, 529 objectName, "queryNames"); 530 } 531 catch(SecurityException e) 532 { 533 if( log.isTraceEnabled() ) 534 log.trace("Excluded mbean due to security: "+objectName); 535 continue; 536 } 537 } 538 if (queryMBean(objectName, query) == true) 540 result.add(objectName); 541 } 542 543 return result; 544 } 545 546 public boolean isRegistered(ObjectName name) 547 { 548 return registry.contains(name); 549 } 550 551 public java.lang.Integer getMBeanCount() 552 { 553 return new Integer (registry.getSize()); 554 } 555 556 public Object getAttribute(ObjectName name, String attribute) 557 throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , ReflectionException  558 { 559 MBeanEntry entry = registry.get(name); 560 checkMBeanPermission(entry.getResourceClassName(), attribute, name, 561 "getAttribute"); 562 563 MBeanInvoker mbean = entry.getInvoker(); 564 565 return mbean.getAttribute(attribute); 566 } 567 568 public AttributeList getAttributes(ObjectName name, String [] attributes) 569 throws InstanceNotFoundException , ReflectionException  570 { 571 MBeanEntry entry = registry.get(name); 572 String className = entry.getResourceClassName(); 573 577 checkMBeanPermission(className, null, name, "getAttribute"); 578 579 MBeanInvoker mbean = entry.getInvoker(); 580 AttributeList list = mbean.getAttributes(attributes); 581 SecurityManager sm = System.getSecurityManager(); 582 if( sm != null ) 583 { 584 Iterator iter = list.iterator(); 586 while( iter.hasNext() ) 587 { 588 Attribute attr = (Attribute ) iter.next(); 589 String aname = attr.getName(); 590 try 591 { 592 checkMBeanPermission(className, aname, name, "getAttribute"); 593 } 594 catch(SecurityException e) 595 { 596 if( log.isTraceEnabled() ) 597 log.trace("Excluded attribute due to security: "+aname); 598 iter.remove(); 599 } 600 } 601 } 602 return list; 603 } 604 605 public void setAttribute(ObjectName name, Attribute attribute) 606 throws InstanceNotFoundException , AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException  607 { 608 MBeanEntry entry = registry.get(name); 609 String attributeName = null; 610 if (attribute != null) 611 attributeName = attribute.getName(); 612 checkMBeanPermission(entry.getResourceClassName(), attributeName, 613 name, "setAttribute"); 614 615 MBeanInvoker mbean = entry.getInvoker(); 616 617 mbean.setAttribute(attribute); 618 } 619 620 public AttributeList setAttributes(ObjectName name, AttributeList attributes) 621 throws InstanceNotFoundException , ReflectionException  622 { 623 MBeanEntry entry = registry.get(name); 624 625 String className = entry.getResourceClassName(); 626 630 checkMBeanPermission(className, null, name, "setAttribute"); 631 632 MBeanInvoker mbean = entry.getInvoker(); 633 AttributeList list = mbean.setAttributes(attributes); 634 SecurityManager sm = System.getSecurityManager(); 635 if( sm != null ) 636 { 637 Iterator iter = list.iterator(); 639 while( iter.hasNext() ) 640 { 641 Attribute attr = (Attribute ) iter.next(); 642 String aname = attr.getName(); 643 try 644 { 645 checkMBeanPermission(className, aname, name, "setAttribute"); 646 } 647 |