1 7 8 package javax.management.remote.rmi; 9 10 import java.io.InterruptedIOException ; 11 import java.io.IOException ; 12 import java.io.Serializable ; 13 import java.rmi.MarshalledObject ; 14 import java.rmi.server.RMIClientSocketFactory ; 15 import java.rmi.server.RMIServerSocketFactory ; 16 import java.rmi.server.UnicastRemoteObject ; 17 import java.rmi.server.Unreferenced ; 18 import java.rmi.NoSuchObjectException ; 19 import java.security.AccessControlContext ; 20 import java.security.AccessController ; 21 import java.security.Principal ; 22 import java.security.PrivilegedAction ; 23 import java.security.PrivilegedActionException ; 24 import java.security.PrivilegedExceptionAction ; 25 import java.security.ProtectionDomain ; 26 import java.util.Arrays ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Set ; 30 import java.util.Map ; 31 import java.rmi.UnmarshalException ; 32 33 import javax.management.Attribute ; 34 import javax.management.AttributeList ; 35 import javax.management.AttributeNotFoundException ; 36 import javax.management.InstanceAlreadyExistsException ; 37 import javax.management.InstanceNotFoundException ; 38 import javax.management.IntrospectionException ; 39 import javax.management.InvalidAttributeValueException ; 40 import javax.management.ListenerNotFoundException ; 41 import javax.management.MalformedObjectNameException ; 42 import javax.management.MBeanException ; 43 import javax.management.MBeanInfo ; 44 import javax.management.MBeanRegistrationException ; 45 import javax.management.MBeanServer ; 46 import javax.management.NotCompliantMBeanException ; 47 import javax.management.Notification ; 48 import javax.management.NotificationFilter ; 49 import javax.management.NotificationListener ; 50 import javax.management.ObjectInstance ; 51 import javax.management.ObjectName ; 52 import javax.management.QueryExp ; 53 import javax.management.ReflectionException ; 54 import javax.management.RuntimeOperationsException ; 55 import javax.management.loading.ClassLoaderRepository ; 56 import javax.management.remote.NotificationResult ; 57 import javax.management.remote.SubjectDelegationPermission ; 58 import javax.management.remote.TargetedNotification ; 59 import javax.management.remote.JMXServerErrorException ; 60 61 import javax.security.auth.Subject ; 62 63 import com.sun.jmx.remote.internal.ServerNotifForwarder; 64 import com.sun.jmx.remote.internal.ServerCommunicatorAdmin; 65 import com.sun.jmx.remote.internal.Unmarshal; 66 import com.sun.jmx.remote.security.JMXSubjectDomainCombiner; 67 import com.sun.jmx.remote.security.SubjectDelegator; 68 import com.sun.jmx.remote.util.CacheMap; 69 import com.sun.jmx.remote.util.ClassLoaderWithRepository; 70 import com.sun.jmx.remote.util.ClassLogger; 71 import com.sun.jmx.remote.util.EnvHelp; 72 import com.sun.jmx.remote.util.OrderClassLoaders; 73 74 81 public class RMIConnectionImpl implements RMIConnection , Unreferenced { 82 83 106 public RMIConnectionImpl(RMIServerImpl rmiServer, 107 String connectionId, 108 ClassLoader defaultClassLoader, 109 Subject subject, 110 Map <String ,?> env) { 111 if (rmiServer == null || connectionId == null) 112 throw new NullPointerException ("Illegal null argument"); 113 if (env == null) 114 env = Collections.EMPTY_MAP; 115 this.rmiServer = rmiServer; 116 this.connectionId = connectionId; 117 this.defaultClassLoader = defaultClassLoader; 118 119 this.subjectDelegator = new SubjectDelegator(); 120 this.subject = subject; 121 if (subject == null) { 122 this.acc = null; 123 } else { 124 this.acc = JMXSubjectDomainCombiner.getContext(subject); 125 } 126 this.mbeanServer = rmiServer.getMBeanServer(); 127 128 final ClassLoader dcl = defaultClassLoader; 129 this.classLoaderWithRepository = (ClassLoaderWithRepository) 130 AccessController.doPrivileged(new PrivilegedAction () { 131 public Object run() { 132 return new ClassLoaderWithRepository( 133 getClassLoaderRepository(), 134 dcl); 135 } 136 }); 137 138 serverCommunicatorAdmin = new 139 RMIServerCommunicatorAdmin(EnvHelp.getServerConnectionTimeout(env)); 140 141 this.env = env; 142 } 143 144 private synchronized ServerNotifForwarder getServerNotifFwd() { 145 if(serverNotifForwarder == null) 148 serverNotifForwarder = 149 new ServerNotifForwarder(mbeanServer, 150 env, 151 rmiServer.getNotifBuffer()); 152 153 return serverNotifForwarder; 154 } 155 156 public String getConnectionId() throws IOException { 157 return connectionId; 159 } 160 161 public void close() throws IOException { 162 final boolean debug = logger.debugOn(); 163 final String idstr = (debug?"["+this.toString()+"]":null); 164 165 synchronized(this) { 166 if (terminated) { 167 if (debug) logger.debug("close",idstr + " already terminated."); 168 return; 169 } 170 171 if (debug) logger.debug("close",idstr + " closing."); 172 173 terminated = true; 174 175 if (serverCommunicatorAdmin != null) { 176 serverCommunicatorAdmin.terminate(); 177 } 178 179 if (serverNotifForwarder != null) { 180 serverNotifForwarder.terminate(); 181 } 182 } 183 184 rmiServer.clientClosed(this); 185 186 if (debug) logger.debug("close",idstr + " closed."); 187 } 188 189 public void unreferenced() { 190 logger.debug("unreferenced", "called"); 191 try { 192 close(); 193 logger.debug("unreferenced", "done"); 194 } catch (IOException e) { 195 logger.fine("unreferenced", e); 196 } 197 } 198 199 203 public ObjectInstance createMBean(String className, 204 ObjectName name, 205 Subject delegationSubject) 206 throws 207 ReflectionException , 208 InstanceAlreadyExistsException , 209 MBeanRegistrationException , 210 MBeanException , 211 NotCompliantMBeanException , 212 IOException { 213 try { 214 final Object params[] = 215 new Object [] { className, name }; 216 217 if (logger.debugOn()) 218 logger.debug("createMBean(String,ObjectName)", 219 "connectionId=" + connectionId +", className=" + 220 className+", name=" + name); 221 222 return (ObjectInstance ) 223 doPrivilegedOperation( 224 CREATE_MBEAN, 225 params, 226 delegationSubject); 227 } catch (PrivilegedActionException pe) { 228 Exception e = extractException(pe); 229 if (e instanceof ReflectionException ) 230 throw (ReflectionException ) e; 231 if (e instanceof InstanceAlreadyExistsException ) 232 throw (InstanceAlreadyExistsException ) e; 233 if (e instanceof MBeanRegistrationException ) 234 throw (MBeanRegistrationException ) e; 235 if (e instanceof MBeanException ) 236 throw (MBeanException ) e; 237 if (e instanceof NotCompliantMBeanException ) 238 throw (NotCompliantMBeanException ) e; 239 if (e instanceof IOException ) 240 throw (IOException ) e; 241 throw newIOException("Got unexpected server exception: " + e, e); 242 } 243 } 244 245 public ObjectInstance createMBean(String className, 246 ObjectName name, 247 ObjectName loaderName, 248 Subject delegationSubject) 249 throws 250 ReflectionException , 251 InstanceAlreadyExistsException , 252 MBeanRegistrationException , 253 MBeanException , 254 NotCompliantMBeanException , 255 InstanceNotFoundException , 256 IOException { 257 try { 258 final Object params[] = 259 new Object [] { className, name, loaderName }; 260 261 if (logger.debugOn()) 262 logger.debug("createMBean(String,ObjectName,ObjectName)", 263 "connectionId=" + connectionId 264 +", className=" + className 265 +", name=" + name 266 +", loaderName=" + loaderName); 267 268 return (ObjectInstance ) 269 doPrivilegedOperation( 270 CREATE_MBEAN_LOADER, 271 params, 272 delegationSubject); 273 } catch (PrivilegedActionException pe) { 274 Exception e = extractException(pe); 275 if (e instanceof ReflectionException ) 276 throw (ReflectionException ) e; 277 if (e instanceof InstanceAlreadyExistsException ) 278 throw (InstanceAlreadyExistsException ) e; 279 if (e instanceof MBeanRegistrationException ) 280 throw (MBeanRegistrationException ) e; 281 if (e instanceof MBeanException ) 282 throw (MBeanException ) e; 283 if (e instanceof NotCompliantMBeanException ) 284 throw (NotCompliantMBeanException ) e; 285 if (e instanceof InstanceNotFoundException ) 286 throw (InstanceNotFoundException ) e; 287 if (e instanceof IOException ) 288 throw (IOException ) e; 289 throw newIOException("Got unexpected server exception: " + e, e); 290 } 291 } 292 293 public ObjectInstance createMBean(String className, 294 ObjectName name, 295 MarshalledObject params, 296 String signature[], 297 Subject delegationSubject) 298 throws 299 ReflectionException , 300 InstanceAlreadyExistsException , 301 MBeanRegistrationException , 302 MBeanException , 303 NotCompliantMBeanException , 304 IOException { 305 306 final Object [] values; 307 final boolean debug = logger.debugOn(); 308 309 if (debug) logger.debug( 310 "createMBean(String,ObjectName,Object[],String[])", 311 "connectionId=" + connectionId 312 +", unwrapping parameters using classLoaderWithRepository."); 313 314 values = nullIsEmpty((Object []) unwrap(params, 315 classLoaderWithRepository)); 316 317 try { 318 final Object params2[] = 319 new Object [] { className, name, values, 320 nullIsEmpty(signature) }; 321 322 if (debug) 323 logger.debug("createMBean(String,ObjectName,Object[],String[])", 324 "connectionId=" + connectionId 325 +", className=" + className 326 +", name=" + name 327 +", params=" + objects(values) 328 +", signature=" + strings(signature)); 329 330 return (ObjectInstance ) 331 doPrivilegedOperation( 332 CREATE_MBEAN_PARAMS, 333 params2, 334 delegationSubject); 335 } catch (PrivilegedActionException pe) { 336 Exception e = extractException(pe); 337 if (e instanceof ReflectionException ) 338 throw (ReflectionException ) e; 339 if (e instanceof InstanceAlreadyExistsException ) 340 throw (InstanceAlreadyExistsException ) e; 341 if (e instanceof MBeanRegistrationException ) 342 throw (MBeanRegistrationException ) e; 343 if (e instanceof MBeanException ) 344 throw (MBeanException ) e; 345 if (e instanceof NotCompliantMBeanException ) 346 throw (NotCompliantMBeanException ) e; 347 if (e instanceof IOException ) 348 throw (IOException ) e; 349 throw newIOException("Got unexpected server exception: " + e, e); 350 } 351 } 352 353 public ObjectInstance createMBean(String className, 354 ObjectName name, 355 ObjectName loaderName, 356 MarshalledObject params, 357 String signature[], 358 Subject delegationSubject) 359 throws 360 ReflectionException , 361 InstanceAlreadyExistsException , 362 MBeanRegistrationException , 363 MBeanException , 364 NotCompliantMBeanException , 365 InstanceNotFoundException , 366 IOException { 367 368 final Object [] values; 369 final boolean debug = logger.debugOn(); 370 371 if (debug) logger.debug( 372 "createMBean(String,ObjectName,ObjectName,Object[],String[])", 373 "connectionId=" + connectionId 374 +", unwrapping params with MBean extended ClassLoader."); 375 376 values = nullIsEmpty((Object []) unwrap(params, 377 getClassLoader(loaderName), 378 defaultClassLoader)); 379 380 try { 381 final Object params2[] = 382 new Object [] { className, name, loaderName, values, 383 nullIsEmpty(signature) }; 384 385 if (debug) logger.debug( 386 "createMBean(String,ObjectName,ObjectName,Object[],String[])", 387 "connectionId=" + connectionId 388 +", className=" + className 389 +", name=" + name 390 +", loaderName=" + loaderName 391 +", params=" + objects(values) 392 +", signature=" + strings(signature)); 393 394 return (ObjectInstance ) 395 doPrivilegedOperation( 396 CREATE_MBEAN_LOADER_PARAMS, 397 params2, 398 delegationSubject); 399 } catch (PrivilegedActionException pe) { 400 Exception e = extractException(pe); 401 if (e instanceof ReflectionException ) 402 throw (ReflectionException ) e; 403 if (e instanceof InstanceAlreadyExistsException ) 404 throw (InstanceAlreadyExistsException ) e; 405 if (e instanceof MBeanRegistrationException ) 406 throw (MBeanRegistrationException ) e; 407 if (e instanceof MBeanException ) 408 throw (MBeanException ) e; 409 if (e instanceof NotCompliantMBeanException ) 410 throw (NotCompliantMBeanException ) e; 411 if (e instanceof InstanceNotFoundException ) 412 throw (InstanceNotFoundException ) e; 413 if (e instanceof IOException ) 414 throw (IOException ) e; 415 throw newIOException("Got unexpected server exception: " + e, e); 416 } 417 } 418 419 public void unregisterMBean(ObjectName name, Subject delegationSubject) 420 throws 421 InstanceNotFoundException , 422 MBeanRegistrationException , 423 IOException { 424 try { 425 final Object params[] = new Object [] { name }; 426 427 if (logger.debugOn()) logger.debug("unregisterMBean", 428 "connectionId=" + connectionId 429 +", name="+name); 430 431 doPrivilegedOperation( 432 UNREGISTER_MBEAN, 433 params, 434 delegationSubject); 435 } catch (PrivilegedActionException pe) { 436 Exception e = extractException(pe); 437 if (e instanceof InstanceNotFoundException ) 438 throw (InstanceNotFoundException ) e; 439 if (e instanceof MBeanRegistrationException ) 440 throw (MBeanRegistrationException ) e; 441 if (e instanceof IOException ) 442 throw (IOException ) e; 443 throw newIOException("Got unexpected server exception: " + e, e); 444 } 445 } 446 447 public ObjectInstance getObjectInstance(ObjectName name, 448 Subject delegationSubject) 449 throws 450 InstanceNotFoundException , 451 IOException { 452 453 checkNonNull("ObjectName", name); 454 455 try { 456 final Object params[] = new Object [] { name }; 457 458 if (logger.debugOn()) logger.debug("getObjectInstance", 459 "connectionId=" + connectionId 460 +", name="+name); 461 462 return (ObjectInstance ) 463 doPrivilegedOperation( 464 GET_OBJECT_INSTANCE, 465 params, 466 delegationSubject); 467 } catch (PrivilegedActionException pe) { 468 Exception e = extractException(pe); 469 if (e instanceof InstanceNotFoundException ) 470 throw (InstanceNotFoundException ) e; 471 if (e instanceof IOException ) 472 throw (IOException ) e; 473 throw newIOException("Got unexpected server exception: " + e, e); 474 } 475 } 476 477 public Set <ObjectInstance > 478 queryMBeans(ObjectName name, 479 MarshalledObject query, 480 Subject delegationSubject) 481 throws IOException { 482 final QueryExp queryValue; 483 final boolean debug=logger.debugOn(); 484 485 if (debug) logger.debug("queryMBeans", 486 "connectionId=" + connectionId 487 +" unwrapping query with defaultClassLoader."); 488 489 queryValue = (QueryExp ) unwrap(query, defaultClassLoader); 490 491 try { 492 final Object params[] = new Object [] { name, queryValue }; 493 494 if (debug) logger.debug("queryMBeans", 495 "connectionId=" + connectionId 496 +", name="+name +", query="+query); 497 498 return (Set ) 499 doPrivilegedOperation( 500 QUERY_MBEANS, 501 params, 502 delegationSubject); 503 } catch (PrivilegedActionException pe) { 504 Exception e = extractException(pe); 505 if (e instanceof IOException ) 506 throw (IOException ) e; 507 throw newIOException("Got unexpected server exception: " + e, e); 508 } 509 } 510 511 public Set <ObjectName > 512 queryNames(ObjectName name, 513 MarshalledObject query, 514 Subject delegationSubject) 515 throws IOException { 516 final QueryExp queryValue; 517 final boolean debug=logger.debugOn(); 518 519 if (debug) logger.debug("queryNames", 520 "connectionId=" + connectionId 521 +" unwrapping query with defaultClassLoader."); 522 523 queryValue = (QueryExp ) unwrap(query, defaultClassLoader); 524 525 try { 526 final Object params[] = new Object [] { name, queryValue }; 527 528 if (debug) logger.debug("queryNames", 529 "connectionId=" + connectionId 530 +", name="+name +", query="+query); 531 532 return (Set ) 533 doPrivilegedOperation( 534 QUERY_NAMES, 535 params, 536 delegationSubject); 537 } catch (PrivilegedActionException pe) { 538 Exception e = extractException(pe); 539 if (e instanceof IOException ) 540 throw (IOException ) e; 541 throw newIOException("Got unexpected server exception: " + e, e); 542 } 543 } 544 545 public boolean isRegistered(ObjectName name, 546 Subject delegationSubject) throws IOException { 547 try { 548 final Object params[] = new Object [] { name }; 549 return ((Boolean ) 550 doPrivilegedOperation( 551 IS_REGISTERED, 552 params, 553 delegationSubject)).booleanValue(); 554 } catch (PrivilegedActionException pe) { 555 Exception e = extractException(pe); 556 if (e instanceof IOException ) 557 throw (IOException ) e; 558 throw newIOException("Got unexpected server exception: " + e, e); 559 } 560 } 561 562 public Integer getMBeanCount(Subject delegationSubject) 563 throws IOException { 564 try { 565 final Object params[] = new Object [] { }; 566 567 if (logger.debugOn()) logger.debug("getMBeanCount", 568 "connectionId=" + connectionId); 569 570 return (Integer ) 571 doPrivilegedOperation( 572 GET_MBEAN_COUNT, 573 params, 574 delegationSubject); 575 } catch (PrivilegedActionException pe) { 576 Exception e = extractException(pe); 577 if (e instanceof IOException ) 578 throw (IOException ) e; 579 throw newIOException("Got unexpected server exception: " + e, e); 580 } 581 } 582 583 public Object getAttribute(ObjectName name, 584 String attribute, 585 Subject delegationSubject) 586 throws 587 MBeanException , 588 AttributeNotFoundException , 589 InstanceNotFoundException , 590 ReflectionException , 591 IOException { 592 try { 593 final Object params[] = new Object [] { name, attribute }; 594 if (logger.debugOn()) logger.debug("getAttribute", 595 "connectionId=" + connectionId 596 +", name=" + name 597 +", attribute="+ attribute); 598 599 return (Object ) 600 doPrivilegedOperation( 601 GET_ATTRIBUTE, 602 params, 603 delegationSubject); 604 } catch (PrivilegedActionException pe) { 605 Exception e = extractException(pe); 606 if (e instanceof MBeanException ) 607 throw (MBeanException ) e; 608 if (e instanceof AttributeNotFoundException ) 609 throw (AttributeNotFoundException ) e; 610 if (e instanceof InstanceNotFoundException ) 611 throw (InstanceNotFoundException ) e; 612 if (e instanceof ReflectionException ) 613 throw (ReflectionException ) e; 614 if (e instanceof IOException ) 615 throw (IOException ) e; 616 throw newIOException("Got unexpected server exception: " + e, e); 617 } 618 } 619 620 public AttributeList getAttributes(ObjectName name, 621 String [] attributes, 622 Subject delegationSubject) 623 throws 624 InstanceNotFoundException , 625 ReflectionException , 626 IOException { 627 try { 628 final Object params[] = new Object [] { name, attributes }; 629 630 if (logger.debugOn()) logger.debug("getAttributes", 631 "connectionId=" + connectionId 632 +", name=" + name 633 +", attributes="+ strings(attributes)); 634 635 return (AttributeList ) 636 doPrivilegedOperation( 637 GET_ATTRIBUTES, 638 params, 639 delegationSubject); 640 } catch (PrivilegedActionException pe) { 641 Exception e = extractException(pe); 642 if (e instanceof InstanceNotFoundException ) 643 throw (InstanceNotFoundException ) e; 644 if (e instanceof ReflectionException ) 645 throw (ReflectionException ) e; 646 if (e instanceof IOException ) 647 throw (IOException ) e; 648 throw newIOException("Got unexpected server exception: " + e, e); 649 } 650 } 651 652 public void setAttribute(ObjectName name, 653 MarshalledObject attribute, 654 Subject delegationSubject) 655 throws 656 InstanceNotFoundException , 657 AttributeNotFoundException , 658 InvalidAttributeValueException , 659 MBeanException , 660 ReflectionException , 661 IOException { 662 final Attribute attr; 663 final boolean debug=logger.debugOn(); 664 665 if (debug) logger.debug("setAttribute", 666 "connectionId=" + connectionId 667 +" unwrapping attribute with MBean extended ClassLoader."); 668 669 attr = (Attribute ) unwrap(attribute, 670 getClassLoaderFor(name), 671 defaultClassLoader); 672 673 try { 674 final Object params[] = new Object [] { name, attr }; 675 676 if (debug) logger.debug("setAttribute", 677 "connectionId=" + connectionId 678 +", name="+name 679 +", attribute="+attr); 680 681 doPrivilegedOperation( 682 SET_ATTRIBUTE, 683 params, 684 delegationSubject); 685 } catch (PrivilegedActionException pe) { 686 Exception e = extractException(pe); 687 if (e instanceof InstanceNotFoundException ) 688 throw (InstanceNotFoundException ) e; 689 if (e instanceof AttributeNotFoundException ) 690 throw (AttributeNotFoundException ) e; 691 if (e instanceof InvalidAttributeValueException ) 692 throw (InvalidAttributeValueException ) e; 693 if (e instanceof MBeanException ) 694 throw (MBeanException ) e; 695 if (e instanceof ReflectionException ) 696 throw (ReflectionException ) e; 697 if (e instanceof IOException ) 698 throw (IOException ) e; 699 throw newIOException("Got unexpected server exception: " + e, e); 700 } 701 } 702 703 public AttributeList setAttributes(ObjectName name, 704 MarshalledObject attributes, 705 Subject delegationSubject) 706 throws 707 InstanceNotFoundException , 708 ReflectionException , 709 IOException { 710 final AttributeList attrlist; 711 final boolean debug=logger.debugOn(); 712 713 if (debug) logger.debug("setAttributes", 714 "connectionId=" + connectionId 715 +" unwrapping attributes with MBean extended ClassLoader."); 716 717 attrlist = 718 (AttributeList ) unwrap(attributes, 719 getClassLoaderFor(name), 720 defaultClassLoader); 721 722 try { 723 final Object params[] = new Object [] { name, attrlist }; 724 725 if (debug) logger.debug("setAttributes", 726 "connectionId=" + connectionId 727 +", name="+name 728 +", attributes="+attrlist); 729 730 return (AttributeList ) 731 doPrivilegedOperation( 732 SET_ATTRIBUTES, 733 params, 734 delegationSubject); 735 } catch (PrivilegedActionException pe) { 736 Exception e = extractException(pe); 737 if (e instanceof InstanceNotFoundException ) 738 throw (InstanceNotFoundException ) e; 739 if (e instanceof ReflectionException ) 740 throw (ReflectionException ) e; 741 if (e instanceof IOException ) 742 throw (IOException ) e; 743 throw newIOException("Got unexpected server exception: " + e, e); 744 } 745 } 746 747 public Object invoke(ObjectName name, 748 String operationName, 749 MarshalledObject params, 750 String signature[], 751 Subject delegationSubject) 752 throws 753 InstanceNotFoundException , 754 MBeanException , 755 ReflectionException , 756 IOException { 757 758 checkNonNull("ObjectName", name); 759 checkNonNull("Operation name", operationName); 760 761 final Object [] values; 762 final boolean debug=logger.debugOn(); 763 764 if (debug) logger.debug("invoke", 765 "connectionId=" + connectionId 766 +" unwrapping params with MBean extended ClassLoader."); 767 768 values = nullIsEmpty((Object []) unwrap(params, 769 getClassLoaderFor(name), 770 defaultClassLoader)); 771 772 try { 773 final Object params2[] = 774 new Object [] { name, operationName, values, 775 nullIsEmpty(signature) }; 776 777 if (debug) logger.debug("invoke", 778 "connectionId=" + connectionId 779 +", name="+name 780 +", operationName="+operationName 781 +", params="+objects(values) 782 +", signature="+strings(signature)); 783 784 return (Object ) 785 doPrivilegedOperation( 786 INVOKE, 787 params2, 788 delegationSubject); 789 } catch (PrivilegedActionException pe) { 790 Exception e = extractException(pe); 791 if (e instanceof InstanceNotFoundException ) 792 throw (InstanceNotFoundException ) e; 793 if (e instanceof MBeanException ) 794 throw (MBeanException ) e; 795 if (e instanceof ReflectionException ) 796 throw (ReflectionException ) e; 797 if (e instanceof IOException ) 798 throw (IOException ) e; 799 throw newIOException("Got unexpected server exception: " + e, e); 800 } 801 } 802 803 public String getDefaultDomain(Subject delegationSubject) 804 throws IOException { 805 try { 806 final Object params[] = new Object [] { }; 807 808 if (logger.debugOn()) logger.debug("getDefaultDomain", 809 "connectionId=" + connectionId); 810 811 return (String ) 812 doPrivilegedOperation( 813 GET_DEFAULT_DOMAIN, 814 params, 815 delegationSubject); 816 } catch (PrivilegedActionException pe) { 817 Exception e = extractException(pe); 818 if (e instanceof IOException ) 819 throw (IOException ) e; 820 throw newIOException("Got unexpected server exception: " + e, e); 821 } 822 } 823 824 public String [] getDomains(Subject delegationSubject) throws IOException { 825 try { 826 final Object params[] = new Object [] { }; 827 828 if (logger.debugOn()) logger.debug("getDomains", 829 "connectionId=" + connectionId); 830 831 return (String []) 832 doPrivilegedOperation( 833 GET_DOMAINS, 834 params, 835 delegationSubject); 836 } catch (PrivilegedActionException pe) { 837 Exception e = extractException(pe); 838 if (e instanceof IOException ) 839 throw (IOException ) e; 840 throw newIOException("Got unexpected server exception: " + e, e); 841 } 842 } 843 844 public MBeanInfo getMBeanInfo(ObjectName name, Subject delegationSubject) 845 throws 846 InstanceNotFoundException , 847 IntrospectionException , 848 ReflectionException , 849 IOException { 850 851 checkNonNull("ObjectName", name); 852 853 try { 854 final Object params[] = new Object [] { name }; 855 856 if (logger.debugOn()) logger.debug("getMBeanInfo", 857 "connectionId=" + connectionId 858 +", name="+name); 859 860 return (MBeanInfo ) 861 doPrivilegedOperation( 862 GET_MBEAN_INFO, 863 params, 864 delegationSubject); 865 } catch (PrivilegedActionException pe) { 866 Exception e = extractException(pe); 867 if (e instanceof InstanceNotFoundException ) 868 throw (InstanceNotFoundException ) e; 869 if (e instanceof IntrospectionException ) 870 throw (IntrospectionException ) e; 871 if (e instanceof ReflectionException ) 872 throw (ReflectionException ) e; 873 if (e instanceof IOException ) 874 throw (IOException ) e; 875 throw newIOException("Got unexpected server exception: " + e, e); 876 } 877 } 878 879 public boolean isInstanceOf(ObjectName name, 880 String className, 881 Subject delegationSubject) 882 throws InstanceNotFoundException , IOException { 883 884 checkNonNull("ObjectName", name); 885 886 try { 887 final Object params[] = new Object [] { name, className }; 888 889 if (logger.debugOn()) logger.debug("isInstanceOf", 890 "connectionId=" + connectionId 891 +", name="+name 892 +", className="+className); 893 894 return ((Boolean ) 895 doPrivilegedOperation( 896 IS_INSTANCE_OF, 897 params, 898 delegationSubject)).booleanValue(); 899 } catch (PrivilegedActionException pe) { 900 Exception e = extractException(pe); 901 if (e instanceof InstanceNotFoundException ) 902 throw (InstanceNotFoundException ) e; 903 if (e instanceof IOException ) 904 throw (IOException ) e; 905 throw newIOException("Got unexpected server exception: " + e, e); 906 } 907 } 908 909 public Integer [] addNotificationListeners(ObjectName [] names, 910 MarshalledObject [] filters, 911 Subject [] delegationSubjects) 912 throws InstanceNotFoundException , IOException { 913 914 if (names == null || filters == null) { 915 throw new IllegalArgumentException ("Got null arguments."); 916 } 917 918 Subject [] sbjs = (delegationSubjects != null) ? delegationSubjects : 919 new Subject [names.length]; 920 if (names.length != filters.length || filters.length != sbjs.length) { 921 final String msg = 922 "The value lengths of 3 parameters are not same."; 923 throw new IllegalArgumentException (msg); 924 } 925 926 for (int i=0; i<names.length; i++) { 927 if (names[i] == null) { 928 throw new IllegalArgumentException ("Null Object name."); 929 } 930 } 931 932 int i=0; 933 ClassLoader targetCl; 934 NotificationFilter [] filterValues = 935 new NotificationFilter [names.length]; 936 Object params[]; 937 Integer [] ids = new Integer [names.length]; 938 final boolean debug=logger.debugOn(); 939 940 try { 941 for (; i<names.length; i++) { 942 targetCl = getClassLoaderFor(names[i]); 943 944 if (debug) logger.debug("addNotificationListener"+ 945 "(ObjectName,NotificationFilter)", 946 "connectionId=" + connectionId + 947 " unwrapping filter with target extended ClassLoader."); 948 949 filterValues[i] = (NotificationFilter )unwrap(filters[i], 950 targetCl, defaultClassLoader); 951 952 if (debug) logger.debug("addNotificationListener"+ 953 "(ObjectName,NotificationFilter)", 954 "connectionId=" + connectionId 955 +", name=" + names[i] 956 +", filter=" + filterValues[i]); 957 958 ids[i] = (Integer ) 959 doPrivilegedOperation(ADD_NOTIFICATION_LISTENERS, 960 new Object [] { names[i], 961 filterValues[i] }, 962 sbjs[i]); 963 } 964 965 return ids; 966 } catch (Exception e) { 967 for (int j=0; j<i; j++) { 969 try { 970 getServerNotifFwd().removeNotificationListener(names[j], 971 ids[j]); 972 } catch (Exception eee) { 973 } 975 } 976 977 if (e instanceof PrivilegedActionException ) { 978 e = extractException(e); 979 } 980 981 if (e instanceof ClassCastException ) { 982 throw (ClassCastException ) e; 983 } else if (e instanceof IOException ) { 984 throw (IOException )e; 985 } else if (e instanceof InstanceNotFoundException ) { 986 throw (InstanceNotFoundException ) e; 987 } else if (e instanceof RuntimeException ) { 988 throw (RuntimeException ) e; 989 } else { 990 throw newIOException("Got unexpected server exception: "+e,e); 991 } 992 } 993 } 994 995 public void addNotificationListener(ObjectName name, 996 ObjectName listener, 997 MarshalledObject filter, 998 MarshalledObject handback, 999 Subject delegationSubject) 1000 throws InstanceNotFoundException , IOException { 1001 1002 checkNonNull("Target MBean name", name); 1003 checkNonNull("Listener MBean name", listener); 1004 1005 final NotificationFilter filterValue; 1006 final Object handbackValue; 1007 final boolean debug=logger.debugOn(); 1008 1009 final ClassLoader targetCl = getClassLoaderFor(name); 1010 1011 if (debug) logger.debug("addNotificationListener"+ 1012 "(ObjectName,ObjectName,NotificationFilter,Object)", 1013 "connectionId=" + connectionId 1014 +" unwrapping filter with target extended ClassLoader."); 1015 1016 filterValue = (NotificationFilter ) 1017 unwrap(filter, targetCl, defaultClassLoader); 1018 1019 if (debug) logger.debug("addNotificationListener"+ 1020 "(ObjectName,ObjectName,NotificationFilter,Object)", 1021 "connectionId=" + connectionId 1022 +" unwrapping handback with target extended ClassLoader."); 1023 1024 handbackValue = unwrap(handback, targetCl, defaultClassLoader); 1025 1026 try { 1027 final Object params[] = 1028 new Object [] { name, listener, filterValue, handbackValue }; 1029 1030 if (debug) logger.debug("addNotificationListener"+ 1031 "(ObjectName,ObjectName,NotificationFilter,Object)", 1032 "connectionId=" + connectionId 1033 +", name=" + name 1034 +", listenerName=" + listener 1035 +", filter=" + filterValue 1036 +", handback=" + handbackValue); 1037 1038 doPrivilegedOperation( 1039 ADD_NOTIFICATION_LISTENER_OBJECTNAME, 1040 params, 1041 delegationSubject); 1042 } catch (PrivilegedActionException pe) { 1043 Exception e = extractException(pe); 1044 if (e instanceof InstanceNotFoundException ) 1045 throw (InstanceNotFoundException ) e; 1046 if (e instanceof IOException ) 1047 throw (IOException ) e; 1048 throw newIOException("Got unexpected server exception: " + e, e); 1049 } 1050 } 1051 1052 public void removeNotificationListeners(ObjectName name, 1053 Integer [] listenerIDs, 1054 Subject delegationSubject) 1055 throws 1056 InstanceNotFoundException , 1057 ListenerNotFoundException , 1058 IOException { 1059 1060 if (name == null || listenerIDs == null) 1061 throw new IllegalArgumentException ("Illegal null parameter"); 1062 1063 for (int i = 0; i < listenerIDs.length; i++) { 1064 if (listenerIDs[i] == null) 1065 throw new IllegalArgumentException ("Null listener ID"); 1066 } 1067 1068 try { 1069 final Object params[] = new Object [] { name, listenerIDs }; 1070 1071 if (logger.debugOn()) logger.debug("removeNotificationListener"+ 1072 "(ObjectName,Integer[])", 1073 "connectionId=" + connectionId 1074 +", name=" + name 1075 +", listenerIDs=" + objects(listenerIDs)); 1076 1077 doPrivilegedOperation( 1078 REMOVE_NOTIFICATION_LISTENER, 1079 params, 1080 delegationSubject); 1081 } catch (PrivilegedActionException pe) { 1082 Exception e = extractException(pe); 1083 if (e instanceof InstanceNotFoundException ) 1084 throw (InstanceNotFoundException ) e; 1085 if (e instanceof ListenerNotFoundException ) 1086 throw (ListenerNotFoundException ) e; 1087 if (e instanceof IOException ) 1088 throw (IOException ) e; 1089 throw newIOException("Got unexpected server exception: " + e, e); 1090 } 1091 } 1092 1093 public void removeNotificationListener(ObjectName name, 1094 ObjectName listener, 1095 Subject delegationSubject) 1096 throws 1097 InstanceNotFoundException , 1098 ListenerNotFoundException , 1099 IOException { 1100 1101 checkNonNull("Target MBean name", name); 1102 checkNonNull("Listener MBean name", listener); 1103 1104 try { 1105 final Object params[] = new Object [] { name, listener }; 1106 1107 if (logger.debugOn()) logger.debug("removeNotificationListener"+ 1108 "(ObjectName,ObjectName)", 1109 "connectionId=" + connectionId 1110 +", name=" + name 1111 +", listenerName=" + listener); 1112 1113 doPrivilegedOperation( 1114 REMOVE_NOTIFICATION_LISTENER_OBJECTNAME, 1115 params, 1116 delegationSubject); 1117 } catch (PrivilegedActionException pe) { 1118 Exception e = extractException(pe); 1119 if (e instanceof InstanceNotFoundException ) 1120 throw (InstanceNotFoundException ) e; 1121 if (e instanceof ListenerNotFoundException ) 1122 throw (ListenerNotFoundException ) e; 1123 if (e instanceof IOException ) 1124 throw (IOException ) e; 1125 throw newIOException("Got unexpected server exception: " + e, e); 1126 } 1127 } 1128 1129 public void removeNotificationListener(ObjectName name, 1130 ObjectName listener, 1131 MarshalledObject filter, 1132 MarshalledObject handback, 1133 Subject delegationSubject) 1134 throws 1135 InstanceNotFoundException , 1136 ListenerNotFoundException , 1137 IOException { 1138 1139 checkNonNull("Target MBean name", name); 1140 checkNonNull("Listener MBean name", listener); 1141 1142 final NotificationFilter filterValue; 1143 final Object handbackValue; 1144 final boolean debug=logger.debugOn(); 1145 1146 final ClassLoader targetCl = getClassLoaderFor(name); 1147 1148 if (debug) logger.debug("removeNotificationListener"+ 1149 "(ObjectName,ObjectName,NotificationFilter,Object)", 1150 "connectionId=" + connectionId 1151 +" unwrapping filter with target extended ClassLoader."); 1152 1153 filterValue = (NotificationFilter ) 1154 unwrap(filter, targetCl, defaultClassLoader); 1155 1156 if (debug) logger.debug("removeNotificationListener"+ 1157 "(ObjectName,ObjectName,NotificationFilter,Object)", 1158 "connectionId=" + connectionId 1159 +" unwrapping handback with target extended ClassLoader."); 1160 1161 handbackValue = unwrap(handback, targetCl, defaultClassLoader); 1162 1163 try { 1164 final Object params[] = 1165 new Object [] { name, listener, filterValue, handbackValue }; 1166 1167 if (debug) logger.debug("removeNotificationListener"+ 1168 "(ObjectName,ObjectName,NotificationFilter,Object)", 1169 "connectionId=" + connectionId 1170 +", name=" + name 1171 +", listenerName=" + listener 1172 +", filter=" + filterValue 1173 +", handback=" + handbackValue); 1174 1175 doPrivilegedOperation( 1176 REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK, 1177 params, 1178 delegationSubject); 1179 } catch (PrivilegedActionException pe) { 1180 Exception e = extractException(pe); 1181 if (e instanceof InstanceNotFoundException ) 1182 throw (InstanceNotFoundException ) e; 1183 if (e instanceof ListenerNotFoundException ) 1184 throw (ListenerNotFoundException ) e; 1185 if (e instanceof IOException ) 1186 throw (IOException ) e; 1187 throw newIOException("Got unexpected server exception: " + e, e); 1188 } 1189 } 1190 1191 public NotificationResult fetchNotifications(long clientSequenceNumber, 1192 int maxNotifications, 1193 long timeout) 1194 throws IOException { 1195 1196 if (logger.debugOn()) logger.debug("fetchNotifications", 1197 "connectionId=" + connectionId 1198 +", timeout=" + timeout); 1199 1200 if (maxNotifications < 0 || timeout < 0) 1201 throw new IllegalArgumentException ("Illegal negative argument"); 1202 1203 final boolean serverTerminated = 1204 serverCommunicatorAdmin.reqIncoming(); 1205 try { 1206 if (serverTerminated) { 1207 return new NotificationResult (0L, 0L, 1211 new TargetedNotification [0]); 1212 1213 } 1214 1215 return getServerNotifFwd().fetchNotifs(clientSequenceNumber, 1216 timeout, maxNotifications); 1217 } finally { 1218 serverCommunicatorAdmin.rspOutgoing(); 1219 } 1220 } 1221 1222 1231 public String toString() { 1232 return super.toString() + ": connectionId=" + connectionId; 1233 } 1234 1235 1239 private class PrivilegedOperation implements PrivilegedExceptionAction { 1240 1241 public PrivilegedOperation(int operation, Object [] params) { 1242 this.operation = operation; 1243 this.params = params; 1244 } 1245 1246 public Object run() throws Exception { 1247 return doOperation(operation, params); 1248 } 1249 1250 private int operation; 1251 private Object [] params; 1252 } 1253 1254 private class RMIServerCommunicatorAdmin extends ServerCommunicatorAdmin { 1258 public RMIServerCommunicatorAdmin(long timeout) { 1259 super(timeout); 1260 } 1261 1262 protected void doStop() { 1263 try { 1264 close(); 1265 } catch (IOException ie) { 1266 logger.warning("RMIServerCommunicatorAdmin-doStop", 1267 "Failed to close: " + ie); 1268 logger.debug("RMIServerCommunicatorAdmin-doStop",ie); 1269 } 1270 } 1271 1272 } 1273 1274 1275 1279 private ClassLoaderRepository getClassLoaderRepository() { 1280 return (ClassLoaderRepository ) 1281 AccessController.doPrivileged(new PrivilegedAction () { 1282 public Object run() { 1283 return mbeanServer.getClassLoaderRepository(); 1284 } 1285 }); 1286 } 1287 1288 private ClassLoader getClassLoader(final ObjectName name) 1289 throws InstanceNotFoundException { 1290 try { 1291 return (ClassLoader ) 1292 AccessController.doPrivileged(new PrivilegedExceptionAction () { 1293 public Object run() throws InstanceNotFoundException { 1294 return mbeanServer.getClassLoader(name); 1295 } 1296 }); 1297 } catch (PrivilegedActionException pe) { 1298 throw (InstanceNotFoundException ) extractException(pe); 1299 } 1300 } 1301 1302 private ClassLoader getClassLoaderFor(final ObjectName name) 1303 throws InstanceNotFoundException { 1304 try { 1305 return (ClassLoader ) 1306 AccessController.doPrivileged(new PrivilegedExceptionAction () { 1307 public Object run() throws InstanceNotFoundException { 1308 return mbeanServer.getClassLoaderFor(name); 1309 } 1310 }); 1311 } catch (PrivilegedActionException pe) { 1312 throw (InstanceNotFoundException ) extractException(pe); 1313 } 1314 } 1315 1316 private Object doPrivilegedOperation(final int operation, 1317 final Object [] params, 1318 final Subject delegationSubject) 1319 throws PrivilegedActionException , IOException { 1320 1321 serverCommunicatorAdmin.reqIncoming(); 1322 try { 1323 1324 final AccessControlContext reqACC; 1325 if (delegationSubject == null) 1326 reqACC = acc; 1327 else { 1328 if (subject == null) { 1329 final String msg = 1330 "Subject delegation cannot be enabled unless " + 1331 "an authenticated subject is put in place"; 1332 throw new SecurityException (msg); 1333 } 1334 reqACC = 1335 subjectDelegator.delegatedContext(acc, 1336 delegationSubject); 1337 } 1338 1339 PrivilegedOperation op = 1340 new PrivilegedOperation(operation, params); 1341 if (reqACC == null) { 1342 try { 1343 return op.run(); 1344 } catch (Exception e) { 1345 if (e instanceof RuntimeException ) 1346 throw (RuntimeException ) e; 1347 throw new PrivilegedActionException (e); 1348 } 1349 } else { 1350 return AccessController.doPrivileged(op, reqACC); 1351 } 1352 } catch (Error e) { 1353 throw new JMXServerErrorException (e.toString(),e); 1354 } finally { 1355 serverCommunicatorAdmin.rspOutgoing(); 1356 } 1357 } 1358 1359 private Object doOperation(int operation, Object [] params) 1360 throws Exception { 1361 1362 switch (operation) { 1363 1364 case CREATE_MBEAN: 1365 return mbeanServer.createMBean((String )params[0], 1366 (ObjectName )params[1]); 1367 1368 case CREATE_MBEAN_LOADER: 1369 return mbeanServer.createMBean((String )params[0], 1370 (ObjectName )params[1], 1371 (ObjectName )params[2]); 1372 1373 case CREATE_MBEAN_PARAMS: 1374 return mbeanServer.createMBean((String )params[0], 1375 (ObjectName )params[1], 1376 (Object [])params[2], 1377 (String [])params[3]); 1378 1379 case CREATE_MBEAN_LOADER_PARAMS: 1380 return mbeanServer.createMBean((String )params[0], 1381 (ObjectName )params[1], 1382 (ObjectName )params[2], 1383 (Object [])params[3], 1384 (String [])params[4]); 1385 1386 case GET_ATTRIBUTE: 1387 return mbeanServer.getAttribute((ObjectName )params[0], 1388 (String )params[1]); 1389 1390 case GET_ATTRIBUTES: 1391 return mbeanServer.getAttributes((ObjectName )params[0], 1392 (String [])params[1]); 1393 1394 case GET_DEFAULT_DOMAIN: 1395 return mbeanServer.getDefaultDomain(); 1396 1397 case GET_DOMAINS: 1398 return mbeanServer.getDomains(); 1399 1400 case GET_MBEAN_COUNT: 1401 return mbeanServer.getMBeanCount(); 1402 1403 case GET_MBEAN_INFO: 1404 return mbeanServer.getMBeanInfo((ObjectName )params[0]); 1405 1406 case GET_OBJECT_INSTANCE: 1407 return mbeanServer.getObjectInstance((ObjectName )params[0]); 1408 1409 case INVOKE: 1410 return mbeanServer.invoke((ObjectName )params[0], 1411 (String )params[1], 1412 (Object [])params[2], 1413 (String [])params[3]); 1414 1415 case IS_INSTANCE_OF: 1416 return mbeanServer.isInstanceOf((ObjectName )params[0], 1417 (String )params[1]) 1418 ? Boolean.TRUE : Boolean.FALSE; 1419 1420 case IS_REGISTERED: 1421 return mbeanServer.isRegistered((ObjectName )params[0]) 1422 ? Boolean.TRUE : Boolean.FALSE; 1423 1424 case QUERY_MBEANS: 1425 return mbeanServer.queryMBeans((ObjectName )params[0], 1426 (QueryExp )params[1]); 1427 1428 case QUERY_NAMES: 1429 return mbeanServer.queryNames((ObjectName )params[0], 1430 (QueryExp )params[1]); 1431 1432 case SET_ATTRIBUTE: 1433 mbeanServer.setAttribute((ObjectName )params[0], 1434 (Attribute )params[1]); 1435 return null; 1436 1437 case SET_ATTRIBUTES: 1438 return mbeanServer.setAttributes((ObjectName )params[0], 1439 (AttributeList )params[1]); 1440 1441 case UNREGISTER_MBEAN: 1442 mbeanServer.unregisterMBean((ObjectName )params[0]); 1443 return null; 1444 1445 case ADD_NOTIFICATION_LISTENERS: 1446 return getServerNotifFwd().addNotificationListener( 1447 (ObjectName )params[0], 1448 (NotificationFilter )params[1]); 1449 1450 case ADD_NOTIFICATION_LISTENER_OBJECTNAME: 1451 mbeanServer.addNotificationListener((ObjectName )params[0], 1452 (ObjectName )params[1], 1453 (NotificationFilter )params[2], 1454 (Object )params[3]); 1455 return null; 1456 1457 case REMOVE_NOTIFICATION_LISTENER: 1458 getServerNotifFwd().removeNotificationListener( 1459 (ObjectName )params[0], 1460 (Integer [])params[1]); 1461 return null; 1462 1463 case REMOVE_NOTIFICATION_LISTENER_OBJECTNAME: 1464 mbeanServer.removeNotificationListener((ObjectName )params[0], 1465 (ObjectName )params[1]); 1466 return null; 1467 1468 case REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK: 1469 mbeanServer.removeNotificationListener( 1470 (ObjectName )params[0], 1471 (ObjectName )params[1], 1472 (NotificationFilter )params[2], 1473 (Object )params[3]); 1474 return null; 1475 1476 default: 1477 throw new IllegalArgumentException ("Invalid operation"); 1478 } 1479 } 1480 1481 1524 1525 private static final String unmarshalClassName = 1526 "com.sun.jmx.remote.internal.MOGet"; 1527 1528 private static boolean bootstrapLoaded = 1529 (RMIConnectionImpl .class.getClassLoader() == 1530 Object .class.getClassLoader()); 1531 1532 private static final Unmarshal unmarshal; 1533 static { 1534 final String byteCodeString = 1535 "\312\376\272\276\0\0\0.\0\30\12\0\4\0\16\12\0\17\0\20\7\0\21\7\0"+ 1536 "\22\7\0\23\1\0\6<init>\1\0\3()V\1\0\4Code\1\0\3get\1\0/(Ljava/r"+ 1537 "mi/MarshalledObject;)Ljava/lang/Object;\1\0\12Exceptions\7\0\24"+ 1538 "\7\0\25\14\0\6\0\7\7\0\26\14\0\11\0\27\1\0!com/sun/jmx/remote/i"+ 1539 "nternal/MOGet\1\0\20java/lang/Object\1\0%com/sun/jmx/remote/int"+ 1540 "ernal/Unmarshal\1\0\23java/io/IOException\1\0\40java/lang/Class"+ 1541 "NotFoundException\1\0\31java/rmi/MarshalledObject\1\0\24()Ljava"+ 1542 "/lang/Object;\0!\0\3\0\4\0\1\0\5\0\0\0\2\0\1\0\6\0\7\0\1\0\10\0"+ 1543 "\0\0\21\0\1\0\1\0\0\0\5*\267\0\1\261\0\0\0\0\0\1\0\11\0\12\0\2\0"+ 1544 "\10\0\0\0\21\0\1\0\2\0\0\0\5+\266\0\2\260\0\0\0\0\0\13\0\0\0\6\0"+ 1545 "\2\0\14\0\15\0\0"; 1546 if (bootstrapLoaded) 1547 unmarshal = null; 1548 else { 1549 final byte[] byteCode = 1550 NoCallStackClassLoader.stringToBytes(byteCodeString); 1551 final String [] otherClassNames = { 1552 Unmarshal.class.getName() 1553 }; 1554 final Class thisClass = RMIConnectionImpl .class; 1555 final ClassLoader thisClassLoader = thisClass.getClassLoader(); 1556 final PrivilegedExceptionAction action = 1557 new PrivilegedExceptionAction () { 1558 public Object run() throws Exception { 1559 final ProtectionDomain thisProtectionDomain = 1560 thisClass.getProtectionDomain(); 1561 ClassLoader cl = 1562 new NoCallStackClassLoader (unmarshalClassName, 1563 byteCode, 1564 otherClassNames, 1565 thisClassLoader, 1566 thisProtectionDomain); 1567 Class c = cl.loadClass(unmarshalClassName); 1568 return c.newInstance(); 1569 } 1570 }; 1571 try { 1572 unmarshal = (Unmarshal) AccessController.doPrivileged(action); 1573 } catch (PrivilegedActionException e) { 1574 Error error = new Error ("Internal error: " + e); 1575 EnvHelp.initCause(error, e); 1576 throw error; 1577 } 1578 } 1579 } 1580 1581 private static Object unwrap(final MarshalledObject mo, 1582 final ClassLoader cl) 1583 throws IOException { 1584 if (mo == null) { 1585 return null; 1586 } 1587 try { 1588 return AccessController.doPrivileged( 1589 new PrivilegedExceptionAction () { 1590 public Object run() 1591 throws IOException { 1592 final ClassLoader old = 1593 Thread.currentThread().getContextClassLoader(); 1594 Thread.currentThread().setContextClassLoader(cl); 1595 try { 1596 if (bootstrapLoaded) 1597 return mo.get(); 1598 else 1599 return unmarshal.get(mo); 1600 } catch (ClassNotFoundException cnfe) { 1601 throw new UnmarshalException (cnfe.toString(), cnfe); 1602 } finally { 1603 Thread.currentThread().setContextClassLoader(old); 1604 } 1605 } 1606 }); 1607 } catch (PrivilegedActionException pe) { 1608 Exception e = extractException(pe); 1609 if (e instanceof IOException ) { 1610 throw (IOException ) e; 1611 } 1612 if (e instanceof ClassNotFoundException ) { 1613 throw new UnmarshalException (e.toString(), e); 1614 } 1615 logger.warning("unwrap", "Failed to unmarshall object: " + e); 1616 logger.debug("unwrap", e); 1617 } 1618 return null; 1619 } 1620 1621 private static Object unwrap(final MarshalledObject mo, 1622 final ClassLoader cl1, 1623 final ClassLoader cl2) 1624 throws IOException { 1625 if (mo == null) { 1626 return null; 1627 } 1628 try { 1629 return AccessController.doPrivileged( 1630 new PrivilegedExceptionAction () { 1631 public Object run() 1632 throws IOException { 1633 return unwrap(mo, new OrderClassLoaders(cl1, cl2)); 1634 } 1635 }); 1636 } catch (PrivilegedActionException pe) { 1637 Exception e = extractException(pe); 1638 if (e instanceof IOException ) { 1639 throw (IOException ) e; 1640 } 1641 if (e instanceof ClassNotFoundException ) { 1642 throw new UnmarshalException (e.toString(), e); 1643 } 1644 logger.warning("unwrap", "Failed to unmarshall object: " + e); 1645 logger.debug("unwrap", e); 1646 } 1647 return null; 1648 } 1649 1650 1654 private static IOException newIOException(String message, 1655 Throwable cause) { 1656 final IOException x = new IOException (message); 1657 return (IOException ) EnvHelp.initCause(x,cause); 1658 } 1659 1660 1664 private static Exception extractException(Exception e) { 1665 while (e instanceof PrivilegedActionException ) { 1666 e = ((PrivilegedActionException )e).getException(); 1667 } 1668 return e; 1669 } 1670 1671 private static final Object [] NO_OBJECTS = new Object [0]; 1672 private static final String [] NO_STRINGS = new String [0]; 1673 1674 1682 private static Object [] nullIsEmpty(Object [] array) { 1683 return (array == null) ? NO_OBJECTS : array; 1684 } 1685 1686 private static String [] nullIsEmpty(String [] array) { 1687 return (array == null) ? NO_STRINGS : array; 1688 } 1689 1690 1697 private static void checkNonNull(String what, Object x) { 1698 if (x == null) { 1699 RuntimeException wrapped = 1700 new IllegalArgumentException (what + " must not be null"); 1701 throw new RuntimeOperationsException (wrapped); 1702 } 1703 } 1704 1705 1709 private final Subject subject; 1710 1711 private final SubjectDelegator subjectDelegator; 1712 1713 private final AccessControlContext acc; 1714 1715 private final RMIServerImpl rmiServer; 1716 1717 private final MBeanServer mbeanServer; 1718 1719 private final ClassLoader defaultClassLoader; 1720 1721 private final ClassLoaderWithRepository classLoaderWithRepository; 1722 1723 private boolean terminated = false; 1724 1725 private final String connectionId; 1726 1727 private final ServerCommunicatorAdmin serverCommunicatorAdmin; 1728 1731 private final static int 1732 ADD_NOTIFICATION_LISTENERS = 1; 1733 private final static int 1734 ADD_NOTIFICATION_LISTENER_OBJECTNAME = 2; 1735 private final static int 1736 CREATE_MBEAN = 3; 1737 private final static int 1738 CREATE_MBEAN_PARAMS = 4; 1739 private final static int 1740 CREATE_MBEAN_LOADER = 5; 1741 private final static int 1742 CREATE_MBEAN_LOADER_PARAMS = 6; 1743 private final static int 1744 GET_ATTRIBUTE = 7; 1745 private final static int 1746 GET_ATTRIBUTES = 8; 1747 private final static int 1748 GET_DEFAULT_DOMAIN = 9; 1749 private final static int 1750 GET_DOMAINS = 10; 1751 private final static int 1752 GET_MBEAN_COUNT = 11; 1753 private final static int 1754 GET_MBEAN_INFO = 12; 1755 private final static int 1756 GET_OBJECT_INSTANCE = 13; 1757 private final static int 1758 INVOKE = 14; 1759 private final static int 1760 IS_INSTANCE_OF = 15; 1761 private final static int 1762 IS_REGISTERED = 16; 1763 private final static int 1764 QUERY_MBEANS = 17; 1765 private final static int 1766 QUERY_NAMES = 18; 1767 private final static int 1768 REMOVE_NOTIFICATION_LISTENER = 19; 1769 private final static int 1770 REMOVE_NOTIFICATION_LISTENER_FILTER_HANDBACK = 20; 1771 private final static int 1772 REMOVE_NOTIFICATION_LISTENER_OBJECTNAME = 21; 1773 private final static int 1774 REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK = 22; 1775 private final static int 1776 SET_ATTRIBUTE = 23; 1777 private final static int 1778 SET_ATTRIBUTES = 24; 1779 private final static int 1780 UNREGISTER_MBEAN = 25; 1781 1782 private ServerNotifForwarder serverNotifForwarder; 1785 private Map env; 1786 1787 1790 private static String objects(final Object [] objs) { 1791 if (objs == null) 1792 return "null"; 1793 else 1794 return Arrays.asList(objs).toString(); 1795 } 1796 1797 private static String strings(final String [] strs) { 1798 return objects(strs); 1799 } 1800 1801 private static final ClassLogger logger = 1802 new ClassLogger("javax.management.remote.rmi", "RMIConnectionImpl"); 1803} 1804 | Popular Tags |