summaryrefslogtreecommitdiff
path: root/gnu/javax/management/Server.java
blob: 5eecb6c2d9ce17aa9684a0f79c6ed7cab8de24d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
/* Server.java -- A GNU Classpath management server.
   Copyright (C) 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.javax.management;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.BadAttributeValueExpException;
import javax.management.BadBinaryOpValueExpException;
import javax.management.BadStringOperationException;
import javax.management.DynamicMBean;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidApplicationException;
import javax.management.InvalidAttributeValueException;
import javax.management.ListenerNotFoundException;
import javax.management.MalformedObjectNameException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanPermission;
import javax.management.MBeanRegistration;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerDelegate;
import javax.management.MBeanTrustPermission;
import javax.management.NotCompliantMBeanException;
import javax.management.Notification;
import javax.management.NotificationBroadcaster;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.OperationsException;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.RuntimeOperationsException;
import javax.management.StandardMBean;

import javax.management.loading.ClassLoaderRepository;

/**
 * This class provides an {@link javax.management.MBeanServer}
 * implementation for GNU Classpath.
 *
 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
 * @since 1.5
 */
public class Server
  implements MBeanServer
{

  /**
   * The registered beans, represented as a map of
   * {@link javax.management.ObjectName}s to
   * {@link java.lang.Object}s.
   */
  private final Map beans = new HashMap();

  /**
   * The default domain.
   */
  private String defaultDomain;

  /**
   * The outer server.
   */
  private MBeanServer outer;

  /**
   * The delegate bean.
   */
  private MBeanServerDelegate delegate;

  /**
   * The class loader repository.
   */
  private ClassLoaderRepository repository;

  /**
   * The map of listener delegates to the true
   * listener.
   */
  private Map listeners;

  /**
   * Constructs a new management server using the specified
   * default domain, delegate bean and outer server.
   *
   * @param domain the default domain to use for beans constructed
   *               with no specified domain.
   * @param outer an {@link javax.management.MBeanServer} to pass
   *              to beans implementing the {@link MBeanRegistration}
   *              interface, or <code>null</code> if <code>this</code>
   *              should be passed.
   * @param delegate the delegate bean for this server.
   */
  public Server(String defaultDomain, MBeanServer outer,
		MBeanServerDelegate delegate)
  {
    this.defaultDomain = defaultDomain;
    this.outer = outer;
    this.delegate = delegate;
  }

  /**
   * Checks for the necessary security privileges to perform an
   * operation.
   *
   * @param name the name of the bean being accessed.
   * @param member the name of the operation or attribute being
   *               accessed, or <code>null</code> if one is not
   *               involved.
   * @param action the action being performed.
   * @throws SecurityException if the action is denied.
   */
  private void checkSecurity(ObjectName name, String member,
			     String action)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      try
	{
	  MBeanInfo info = null;
	  if (name != null)
	    {
	      Object bean = getBean(name);
	      Method method = bean.getClass().getMethod("getMBeanInfo", null);
	      info = (MBeanInfo) method.invoke(bean, null);
	    }
	  sm.checkPermission(new MBeanPermission((info == null) ? 
						 null : info.getClassName(),
						 member, name, action));
	}
      catch (InstanceNotFoundException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to get bean.").initCause(e));
	}
      catch (NoSuchMethodException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to get bean info.").initCause(e));
	}
      catch (IllegalAccessException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to get bean info.").initCause(e));
	}
      catch (IllegalArgumentException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to get bean info.").initCause(e));
	}
      catch (InvocationTargetException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to get bean info.").initCause(e));
	}
  }

  /**
   * Retrieves the specified bean.
   *
   * @param name the name of the bean.
   * @return the bean.
   * @throws InstanceNotFoundException if the name of the management bean
   *                                   could not be resolved.
   */
  private Object getBean(ObjectName name)
    throws InstanceNotFoundException
  {
    ServerInfo bean = (ServerInfo) beans.get(name);
    if (bean == null)
      throw new InstanceNotFoundException("The bean, " + name +
					  ", was not found.");
    return bean.getObject();
  }

  /**
   * Registers the supplied listener with the specified management
   * bean.  Notifications emitted by the management bean are forwarded
   * to the listener via the server, which will convert an MBean
   * references in the source to a portable {@link ObjectName}
   * instance.  The notification is otherwise unchanged.
   *
   * @param name the name of the management bean with which the listener
   *             should be registered.
   * @param listener the listener which will handle notifications from
   *                 the bean.
   * @param filter the filter to apply to incoming notifications, or
   *               <code>null</code> if no filtering should be applied.
   * @param passback an object to be passed to the listener when a
   *                 notification is emitted.
   * @throws InstanceNotFoundException if the name of the management bean
   *                                   could not be resolved.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "addNotificationListener")</code>}.
   * @see #removeNotificationListener(ObjectName, NotificationListener)
   * @see #removeNotificationListener(ObjectName, NotificationListener,
   *                                  NotificationFilter, Object)
   * @see NotificationBroadcaster#addNotificationListener(NotificationListener,
   *                                                      NotificationFilter,
   *                                                      Object)
   */
  public void addNotificationListener(ObjectName name, NotificationListener listener,
				      NotificationFilter filter, Object passback)
    throws InstanceNotFoundException
  {
    Object bean = getBean(name);
    checkSecurity(name, null, "addNotificationListener");
    if (bean instanceof NotificationBroadcaster)
      {
	NotificationBroadcaster bbean = (NotificationBroadcaster) bean;
	if (listeners == null)
	  listeners = new HashMap();
	NotificationListener indirection = new ServerNotificationListener(bean, name,
									  listener);
	bbean.addNotificationListener(indirection, filter, passback);
	listeners.put(listener, indirection);
      }
  }

  /**
   * <p>
   * Registers the supplied listener with the specified management
   * bean.  Notifications emitted by the management bean are forwarded
   * to the listener via the server, which will convert any MBean
   * references in the source to portable {@link ObjectName}
   * instances.  The notification is otherwise unchanged.  
   * </p>
   * <p>
   * The listener that receives notifications will be the one that is
   * registered with the given name at the time this method is called.
   * Even if it later unregisters and ceases to use that name, it will
   * still receive notifications.
   * </p>
   *
   * @param name the name of the management bean with which the listener
   *             should be registered.
   * @param listener the name of the listener which will handle
   *                 notifications from the bean.
   * @param filter the filter to apply to incoming notifications, or
   *               <code>null</code> if no filtering should be applied.
   * @param passback an object to be passed to the listener when a
   *                 notification is emitted.
   * @throws InstanceNotFoundException if the name of the management bean
   *                                   could not be resolved.
   * @throws RuntimeOperationsException if the bean associated with the given
   *                                    object name is not a
   *                                    {@link NotificationListener}.  This
   *                                    exception wraps an
   *                                    {@link IllegalArgumentException}.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "addNotificationListener")</code>}.
   * @see #removeNotificationListener(ObjectName, NotificationListener)
   * @see #removeNotificationListener(ObjectName, NotificationListener,
   *                                  NotificationFilter, Object)
   * @see NotificationBroadcaster#addNotificationListener(NotificationListener,
   *                                                      NotificationFilter,
   *                                                      Object)
   */
  public void addNotificationListener(ObjectName name, ObjectName listener,
				      NotificationFilter filter, Object passback)
    throws InstanceNotFoundException
  {
    Object lbean = getBean(listener);
    if (!(lbean instanceof NotificationListener))
      {
	RuntimeException e =
	  new IllegalArgumentException("The supplied listener name does not " +
				       "correspond to a notification listener.");
	throw new RuntimeOperationsException(e);
      }
    addNotificationListener(name, ((NotificationListener) lbean), filter, passback);
  }

  /**
   * <p>
   * Instantiates a new instance of the specified management bean
   * using the default constructor and registers it with the server
   * under the supplied name.  The class is loaded using the
   * {@link javax.management.loading.ClassLoaderRepository default
   * loader repository} of the server. 
   * </p>
   * <p>
   * If the name supplied is <code>null</code>, then the bean is
   * expected to implement the {@link MBeanRegistration} interface.
   * The {@link MBeanRegistration#preRegister preRegister} method
   * of this interface will be used to obtain the name in this case.
   * </p>
   * <p>
   * This method is equivalent to calling {@link 
   * #createMBean(String, ObjectName, Object[], String[])
   * <code>createMBean(className, name, (Object[]) null,
   * (String[]) null)</code>} with <code>null</code> parameters
   * and signature.
   * </p>
   *
   * @param className the class of the management bean, of which
   *                  an instance should be created.
   * @param name the name to register the new bean with.
   * @return an {@link ObjectInstance} containing the {@link ObjectName}
   *         and Java class name of the created instance.
   * @throws ReflectionException if an exception occurs in creating
   *                             an instance of the bean.
   * @throws InstanceAlreadyExistsException if a matching instance
   *                                        already exists.
   * @throws MBeanRegistrationException if an exception occurs in
   *                                    calling the preRegister
   *                                    method.
   * @throws MBeanException if the bean's constructor throws an exception.
   * @throws NotCompliantMBeanException if the created bean is not
   *                                    compliant with the JMX specification.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> class name or object
   *                                    name or if the object name is a pattern.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply the
   *                           use of the <code>instantiate</code>
   *                           and <code>registerMBean</code> methods.
   * @see #createMBean(String, ObjectName, Object[], String[])
   */
  public ObjectInstance createMBean(String className, ObjectName name)
    throws ReflectionException, InstanceAlreadyExistsException,
	   MBeanRegistrationException, MBeanException,
	   NotCompliantMBeanException
  {
    return createMBean(className, name, (Object[]) null, (String[]) null);
  }
  
  /**
   * <p>
   * Instantiates a new instance of the specified management bean
   * using the given constructor and registers it with the server
   * under the supplied name.  The class is loaded using the
   * {@link javax.management.loading.ClassLoaderRepository default
   * loader repository} of the server. 
   * </p>
   * <p>
   * If the name supplied is <code>null</code>, then the bean is
   * expected to implement the {@link MBeanRegistration} interface.
   * The {@link MBeanRegistration#preRegister preRegister} method
   * of this interface will be used to obtain the name in this case.
   * </p>
   * 
   * @param className the class of the management bean, of which
   *                  an instance should be created.
   * @param name the name to register the new bean with.
   * @param params the parameters for the bean's constructor.
   * @param sig the signature of the constructor to use.
   * @return an {@link ObjectInstance} containing the {@link ObjectName}
   *         and Java class name of the created instance.
   * @throws ReflectionException if an exception occurs in creating
   *                             an instance of the bean.
   * @throws InstanceAlreadyExistsException if a matching instance
   *                                        already exists.
   * @throws MBeanRegistrationException if an exception occurs in
   *                                    calling the preRegister
   *                                    method.
   * @throws MBeanException if the bean's constructor throws an exception.
   * @throws NotCompliantMBeanException if the created bean is not
   *                                    compliant with the JMX specification.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> class name or object
   *                                    name or if the object name is a pattern.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply the
   *                           use of the <code>instantiate</code>
   *                           and <code>registerMBean</code> methods.
   */
  public ObjectInstance createMBean(String className, ObjectName name,
				    Object[] params, String[] sig)
    throws ReflectionException, InstanceAlreadyExistsException,
	   MBeanRegistrationException, MBeanException,
	   NotCompliantMBeanException
  {
    return registerMBean(instantiate(className, params, sig), name);
  } 

  /**
   * <p>
   * Instantiates a new instance of the specified management bean
   * using the default constructor and registers it with the server
   * under the supplied name.  The class is loaded using the
   * given class loader.  If this argument is <code>null</code>,
   * then the same class loader as was used to load the server
   * is used.
   * </p>
   * <p>
   * If the name supplied is <code>null</code>, then the bean is
   * expected to implement the {@link MBeanRegistration} interface.
   * The {@link MBeanRegistration#preRegister preRegister} method
   * of this interface will be used to obtain the name in this case.
   * </p>
   * <p>
   * This method is equivalent to calling {@link 
   * #createMBean(String, ObjectName, ObjectName, Object[], String)
   * <code>createMBean(className, name, loaderName, (Object[]) null,
   * (String) null)</code>} with <code>null</code> parameters
   * and signature.
   * </p>
   *
   * @param className the class of the management bean, of which
   *                  an instance should be created.
   * @param name the name to register the new bean with.
   * @param loaderName the name of the class loader.
   * @return an {@link ObjectInstance} containing the {@link ObjectName}
   *         and Java class name of the created instance.
   * @throws ReflectionException if an exception occurs in creating
   *                             an instance of the bean.
   * @throws InstanceAlreadyExistsException if a matching instance
   *                                        already exists.
   * @throws MBeanRegistrationException if an exception occurs in
   *                                    calling the preRegister
   *                                    method.
   * @throws MBeanException if the bean's constructor throws an exception.
   * @throws NotCompliantMBeanException if the created bean is not
   *                                    compliant with the JMX specification.
   * @throws InstanceNotFoundException if the specified class loader is not
   *                                   registered with the server.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> class name or object
   *                                    name or if the object name is a pattern.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply the
   *                           use of the <code>instantiate</code>
   *                           and <code>registerMBean</code> methods.
   * @see #createMBean(String, ObjectName, ObjectName, Object[], String[])
   */
  public ObjectInstance createMBean(String className, ObjectName name, 
				    ObjectName loaderName)
    throws ReflectionException, InstanceAlreadyExistsException,
	   MBeanRegistrationException, MBeanException,
	   NotCompliantMBeanException, InstanceNotFoundException
  {
    return createMBean(className, name, loaderName, (Object[]) null,
		       (String[]) null);
  }

  /**
   * <p>
   * Instantiates a new instance of the specified management bean
   * using the given constructor and registers it with the server
   * under the supplied name.  The class is loaded using the
   * given class loader.  If this argument is <code>null</code>,
   * then the same class loader as was used to load the server
   * is used. 
   * </p>
   * <p>
   * If the name supplied is <code>null</code>, then the bean is
   * expected to implement the {@link MBeanRegistration} interface.
   * The {@link MBeanRegistration#preRegister preRegister} method
   * of this interface will be used to obtain the name in this case.
   * </p>
   * 
   * @param className the class of the management bean, of which
   *                  an instance should be created.
   * @param name the name to register the new bean with.
   * @param loaderName the name of the class loader.
   * @param params the parameters for the bean's constructor.
   * @param sig the signature of the constructor to use.
   * @return an {@link ObjectInstance} containing the {@link ObjectName}
   *         and Java class name of the created instance.
   * @throws ReflectionException if an exception occurs in creating
   *                             an instance of the bean.
   * @throws InstanceAlreadyExistsException if a matching instance
   *                                        already exists.
   * @throws MBeanRegistrationException if an exception occurs in
   *                                    calling the preRegister
   *                                    method.
   * @throws MBeanException if the bean's constructor throws an exception.
   * @throws NotCompliantMBeanException if the created bean is not
   *                                    compliant with the JMX specification.
   * @throws InstanceNotFoundException if the specified class loader is not
   *                                   registered with the server.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> class name or object
   *                                    name or if the object name is a pattern.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply the
   *                           use of the <code>instantiate</code>
   *                           and <code>registerMBean</code> methods.
   */
  public ObjectInstance createMBean(String className, ObjectName name,
				    ObjectName loaderName, Object[] params,
				    String[] sig)
    throws ReflectionException, InstanceAlreadyExistsException,
	   MBeanRegistrationException, MBeanException,
	   NotCompliantMBeanException, InstanceNotFoundException
  {
    return registerMBean(instantiate(className, loaderName, params, sig),
			 name);
  }

  /**
   * Deserializes a byte array using the class loader of the specified
   * management bean as its context.
   *
   * @param name the name of the bean whose class loader should be used.
   * @param data the byte array to be deserialized.
   * @return the deserialized object stream.
   * @deprecated {@link #getClassLoaderFor(ObjectName)} should be used
   *             to obtain the class loader of the bean, which can then
   *             be used to perform deserialization in the user's code.
   * @throws InstanceNotFoundException if the specified bean is not
   *                                   registered with the server.
   * @throws OperationsException if any I/O error is thrown by the
   *                             deserialization process.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "getClassLoaderFor")</code>
   */
  public ObjectInputStream deserialize(ObjectName name, byte[] data)
    throws InstanceNotFoundException, OperationsException
  {
    try
      {
	return new ServerInputStream(new ByteArrayInputStream(data),
				     getClassLoaderFor(name));
      }
    catch (IOException e)
      {
	throw new OperationsException("An I/O error occurred: " + e);
      }
  }

  /**
   * Deserializes a byte array using the same class loader for its context
   * as was used to load the given class.  This class loader is obtained by
   * loading the specified class using the {@link
   * javax.management.loading.ClassLoaderRepository Class Loader Repository}
   * and then using the class loader of the resulting {@link Class} instance.
   *
   * @param name the name of the class which should be loaded to obtain the
   *             class loader.
   * @param data the byte array to be deserialized.
   * @return the deserialized object stream.
   * @deprecated {@link #getClassLoaderRepository} should be used
   *             to obtain the class loading repository, which can then
   *             be used to obtain the {@link Class} instance and deserialize
   *             the array using its class loader.
   * @throws OperationsException if any I/O error is thrown by the
   *                             deserialization process.
   * @throws ReflectionException if an error occurs in obtaining the
   *                             {@link Class} instance.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(null, null, null,
   *                           "getClassLoaderRepository")</code>
   */
  public ObjectInputStream deserialize(String name, byte[] data)
    throws OperationsException, ReflectionException
  {
    try
      {
	Class c = getClassLoaderRepository().loadClass(name);
	return new ServerInputStream(new ByteArrayInputStream(data),
					   c.getClassLoader());
      }
    catch (IOException e)
      {
	throw new OperationsException("An I/O error occurred: " + e);
      }
    catch (ClassNotFoundException e)
      {
	throw new ReflectionException(e, "The class could not be found.");
      }
  }

  /**
   * Deserializes a byte array using the same class loader for its context
   * as was used to load the given class.  The name of the class loader to
   * be used is supplied, and may be <code>null</code> if the server's
   * class loader should be used instead.
   *
   * @param name the name of the class which should be loaded to obtain the
   *             class loader.
   * @param loader the name of the class loader to use, or <code>null</code>
   *               if the class loader of the server should be used.
   * @param data the byte array to be deserialized.
   * @return the deserialized object stream.
   * @deprecated {@link #getClassLoader(ObjectName} can be used to obtain
   *              the named class loader and deserialize the array.
   * @throws InstanceNotFoundException if the specified class loader is not
   *                                   registered with the server.
   * @throws OperationsException if any I/O error is thrown by the
   *                             deserialization process.
   * @throws ReflectionException if an error occurs in obtaining the
   *                             {@link Class} instance.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, loader,
   *                           "getClassLoader")</code>
   */
  public ObjectInputStream deserialize(String name, ObjectName loader, byte[] data)
    throws InstanceNotFoundException, ReflectionException,
	   OperationsException
  {
    try
      {
	Class c = getClassLoader(loader).loadClass(name);
	return new ServerInputStream(new ByteArrayInputStream(data),
					   c.getClassLoader());
      }
    catch (IOException e)
      {
	throw new OperationsException("An I/O error occurred: " + e);
      }
    catch (ClassNotFoundException e)
      {
	throw new ReflectionException(e, "The class could not be found.");
      }
  }

  /**
   * Returns the value of the supplied attribute from the specified
   * management bean.
   *
   * @param bean the bean to retrieve the value from.
   * @param name the name of the attribute to retrieve.
   * @return the value of the attribute.
   * @throws AttributeNotFoundException if the attribute could not be
   *                                    accessed from the bean.
   * @throws MBeanException if the management bean's accessor throws
   *                        an exception.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws ReflectionException if an exception was thrown in trying
   *                             to invoke the bean's accessor.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> bean or attribute
   *                                    name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, name, bean,
   *                           "getAttribute")</code>}.
   * @see DynamicMBean#getAttribute(String)
   */
  public Object getAttribute(ObjectName bean, String name)
    throws MBeanException, AttributeNotFoundException,
	   InstanceNotFoundException, ReflectionException
  {
    if (bean == null || name == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("One of the supplied arguments was null.");
	throw new RuntimeOperationsException(e);
      }
    Object abean = getBean(bean);
    checkSecurity(bean, name, "getAttribute");
    if (abean instanceof DynamicMBean)
      return ((DynamicMBean) abean).getAttribute(name);
    else
      try
	{
	  return new StandardMBean(abean, null).getAttribute(name);
	}
      catch (NotCompliantMBeanException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to create dynamic bean.").initCause(e));
	}
  }


  /**
   * Returns the values of the named attributes from the specified
   * management bean.
   *
   * @param bean the bean to retrieve the value from.
   * @param names the names of the attributes to retrieve.
   * @return the values of the attributes.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws ReflectionException if an exception was thrown in trying
   *                             to invoke the bean's accessor.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> bean or attribute
   *                                    name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, bean,
   *                           "getAttribute")</code>}.  Additionally,
   *                           for an attribute name, <code>n</code>, the
   *                           caller's permission must imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, n, bean,
   *                           "getAttribute")</code>} or that attribute will
   *                           not be included.
   *                           
   * @see DynamicMBean#getAttributes(String[])
   */
  public AttributeList getAttributes(ObjectName bean, String[] names)
    throws InstanceNotFoundException, ReflectionException
  {
    if (bean == null || names == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("One of the supplied arguments was null.");
	throw new RuntimeOperationsException(e);
      }
    Object abean = getBean(bean);
    checkSecurity(bean, null, "getAttribute");
    AttributeList list = new AttributeList(names.length);
    for (int a = 0; a < names.length; ++a)
      {
	if (names[a] == null)
	  {
	    RuntimeException e =
	      new IllegalArgumentException("Argument " + a + " was null.");
	    throw new RuntimeOperationsException(e);
	  }
	checkSecurity(bean, names[a], "getAttribute");
	try
	  {
	    Object value;
	    if (abean instanceof DynamicMBean)
	      value = ((DynamicMBean) abean).getAttribute(names[a]);
	    else
	      try
		{
		  value = new StandardMBean(abean, null).getAttribute(names[a]);
		}
	      catch (NotCompliantMBeanException e)
		{
		  throw (Error) 
		    (new InternalError("Failed to create dynamic bean.").initCause(e));
		}
	    list.add(new Attribute(names[a], value));
	  }
	catch (AttributeNotFoundException e)
	  {
	    /* Ignored */
	  }
	catch (MBeanException e)
	  {
	    /* Ignored */
	  }
      }
    return list;
  }


  /**
   * Returns the specified class loader.  If the specified value is
   * <code>null</code>, then the class loader of the server will be
   * returned.  If <code>l</code> is the requested class loader,
   * and <code>r</code> is the actual class loader returned, then
   * either <code>l</code> and <code>r</code> will be identical,
   * or they will at least return the same class from
   * {@link ClassLoader#loadClass(String)} for any given string.
   * They may not be identical due to one or the other
   * being wrapped in another class loader (e.g. for security).
   *
   * @param name the name of the class loader to return.
   * @return the class loader.
   * @throws InstanceNotFoundException if the class loader can not
   *                                   be found.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "getClassLoader")</code>
   */
  public ClassLoader getClassLoader(ObjectName name)
    throws InstanceNotFoundException
  {
    if (name == null)
      {
	checkSecurity(null, null, "getClassLoader");
	return getClass().getClassLoader();
      }
    Object bean = getBean(name);
    checkSecurity(name, null, "getClassLoader");
    return (ClassLoader) bean;
  }
 
  /**
   * Returns the class loader of the specified management bean.  If
   * <code>l</code> is the requested class loader, and <code>r</code>
   * is the actual class loader returned, then either <code>l</code>
   * and <code>r</code> will be identical, or they will at least
   * return the same class from {@link ClassLoader#loadClass(String)}
   * for any given string.  They may not be identical due to one or
   * the other being wrapped in another class loader (e.g. for
   * security).
   *
   * @param name the name of the bean whose class loader should be
   *             returned.
   * @return the class loader.
   * @throws InstanceNotFoundException if the bean is not registered
   *                                   with the server.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "getClassLoaderFor")</code>
   */
  public ClassLoader getClassLoaderFor(ObjectName name)
    throws InstanceNotFoundException
  {
    Object bean = getBean(name);
    checkSecurity(name, null, "getClassLoaderFor");
    return bean.getClass().getClassLoader();
  }    

  /**
   * Returns the class loader repository used by this server.
   *
   * @return the class loader repository.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(null, null, null,
   *                           "getClassLoaderRepository")</code>
   */
  public ClassLoaderRepository getClassLoaderRepository()
  {
    return repository;
  }

  /**
   * Returns the default domain this server applies to beans that have
   * no specified domain.
   *
   * @return the default domain.
   */
  public String getDefaultDomain()
  {
    return defaultDomain;
  }


  /**
   * Returns an array containing all the domains used by beans registered
   * with this server.  The ordering of the array is undefined.
   *
   * @return the list of domains.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(null, null, name,
   *                           "getDomains")</code>}.  Additionally,
   *                           for an domain, <code>d</code>, the
   *                           caller's permission must imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(null, null,
   *                           new ObjectName("d:x=x"), "getDomains")</code>}
   *                           or that domain will not be included.  Note
   *                           that "x=x" is an arbitrary key-value pair
   *                           provided to satisfy the constructor.
   * @see ObjectName#getDomain()
   */
  public String[] getDomains()
  {
    checkSecurity(null, null, "getDomains");
    Set domains = new HashSet();
    Iterator iterator = beans.keySet().iterator();
    while (iterator.hasNext())
      {
	String d  = ((ObjectName) iterator.next()).getDomain();
	try
	  {
	    checkSecurity(new ObjectName(d + ":x=x"), null, "getDomains");
	    domains.add(d);
	  }
	catch (MalformedObjectNameException e)
	  {
	    /* Ignored */
	  }
      }
    return (String[]) domains.toArray(new String[domains.size()]);
  }

  /**
   * Returns the number of management beans registered with this server.
   * This may be less than the real number if the caller's access is
   * restricted.
   *
   * @return the number of registered beans.
   */
  public Integer getMBeanCount()
  {
    return Integer.valueOf(beans.size());
  }

  /**
   * Returns information on the given management bean.
   *
   * @param name the name of the management bean.
   * @return an instance of {@link MBeanInfo} for the bean.
   * @throws IntrospectionException if an exception occurs in examining
   *                                the bean.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws ReflectionException if an exception occurs when trying
   *                             to invoke {@link DynamicMBean#getMBeanInfo()}
   *                             on the bean.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "getMBeanInfo")</code>}.
   * @see DynamicMBean#getMBeanInfo()
   */
  public MBeanInfo getMBeanInfo(ObjectName name)
    throws InstanceNotFoundException, IntrospectionException,
	   ReflectionException
  {
    Object bean = getBean(name);
    checkSecurity(name, null, "getMBeanInfo");
    try
      {
	Method method = bean.getClass().getMethod("getMBeanInfo", null);
	return (MBeanInfo) method.invoke(bean, null);
      }
    catch (NoSuchMethodException e)
      {
	throw new IntrospectionException("The getMBeanInfo method " + 
					 "could not be found.");
      }
    catch (IllegalAccessException e)
      {
	throw new ReflectionException(e, "Failed to call getMBeanInfo");
      }
    catch (IllegalArgumentException e)
      {
	throw new ReflectionException(e, "Failed to call getMBeanInfo");
      }
    catch (InvocationTargetException e)
      {
	throw new ReflectionException(e, "The method threw an exception");
      }
  }

  /**
   * Returns the {@link ObjectInstance} created for the specified
   * management bean on registration.
   *
   * @param name the name of the bean.
   * @return the corresponding {@link ObjectInstance} instance.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "getObjectInstance")</code>
   * @see #createMBean(String, ObjectName)
   */
  public ObjectInstance getObjectInstance(ObjectName name)
    throws InstanceNotFoundException
  {
    ServerInfo bean = (ServerInfo) beans.get(name);
    if (bean == null)
      throw new InstanceNotFoundException("The bean, " + name +
					  ", was not found.");
    return bean.getInstance();
  }

  /**
   * <p>
   * Creates an instance of the specified class using the list of
   * class loaders from the {@link
   * javax.management.loading.ClassLoaderRepository Class Loader
   * Repository}.  The class should have a public constructor
   * with no arguments.  A reference to the new instance is returned,
   * but the instance is not yet registered with the server.
   * </p>
   * <p>
   * This method is equivalent to calling {@link 
   * #instantiate(String, Object[], String[])
   * <code>instantiate(name, (Object[]) null, (String[]) null)</code>}
   * with <code>null</code> parameters and signature.
   * </p>
   *
   * @param name the name of the class of bean to be instantiated.
   * @return an instance of the given class.
   * @throws ReflectionException if an exception is thrown during
   *                             loading the class or calling the
   *                             constructor.
   * @throws MBeanException if the constructor throws an exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, null,
   *                           "instantiate")</code>}.
   * @see #instantiate(String, Object[], String[])
   */
  public Object instantiate(String name)
    throws ReflectionException, MBeanException
  {
    return instantiate(name, (Object[]) null, (String[]) null);
  }

  /**
   * Creates an instance of the specified class using the list of
   * class loaders from the {@link
   * javax.management.loading.ClassLoaderRepository Class Loader
   * Repository}.  The class should have a public constructor
   * matching the supplied signature.  A reference to the new
   * instance is returned, but the instance is not yet
   * registered with the server.
   *
   * @param name the name of the class of bean to be instantiated.
   * @param params the parameters for the constructor.
   * @param sig the signature of the constructor.
   * @return an instance of the given class.
   * @throws ReflectionException if an exception is thrown during
   *                             loading the class or calling the
   *                             constructor.
   * @throws MBeanException if the constructor throws an exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, null,
   *                           "instantiate")</code>}.
   */
  public Object instantiate(String name, Object[] params, String[] sig)
    throws ReflectionException, MBeanException
  {
    checkSecurity(null, null, "instantiate");
    if (name == null)
      {
	RuntimeException e = 
	  new IllegalArgumentException("The name was null.");
	throw new RuntimeOperationsException(e);
      }
    Class[] sigTypes = new Class[sig.length];
    for (int a = 0; a < sigTypes.length; ++a)
      {
	try 
	  {
	    sigTypes[a] = repository.loadClass(sig[a]);
	  }
	catch (ClassNotFoundException e)
	  {
	    throw new ReflectionException(e, "The class, " + sigTypes[a] + 
					  ", in the method signature " +
					  "could not be loaded.");
	  }
      }
    try
      {
	Constructor cons =
	  repository.loadClass(name).getConstructor(sigTypes);
	return cons.newInstance(params);
      }
    catch (ClassNotFoundException e)
      {
	throw new ReflectionException(e, "The class, " + name + 
				      ", of the constructor " +
				      "could not be loaded.");
      }
    catch (NoSuchMethodException e)
      {
	throw new ReflectionException(e, "The method, " + name +
				      ", could not be found.");
      }
    catch (IllegalAccessException e)
      {
	throw new ReflectionException(e, "Failed to instantiate the object");
      }
    catch (InstantiationException e)
      {
	throw new ReflectionException(e, "Failed to instantiate the object");
      }
    catch (InvocationTargetException e)
      {
	throw new MBeanException((Exception) e.getCause(), "The constructor "
				 + name + " threw an exception");
      }
  }

  /**
   * <p>
   * Creates an instance of the specified class using the supplied
   * class loader.  If the class loader given is <code>null</code>,
   * then the class loader of the server will be used.  The class
   * should have a public constructor with no arguments.  A reference
   * to the new instance is returned, but the instance is not yet
   * registered with the server.
   * </p>
   * <p>
   * This method is equivalent to calling {@link 
   * #instantiate(String, ObjectName, Object[], String[])
   * <code>instantiate(name, loaderName, (Object[]) null,
   * (String[]) null)</code>} with <code>null</code> parameters
   * and signature.
   * </p>
   *
   * @param name the name of the class of bean to be instantiated.
   * @param loaderName the name of the class loader to use.
   * @return an instance of the given class.
   * @throws InstanceNotFoundException if the class loader is not
   *                                   registered with the server.
   * @throws ReflectionException if an exception is thrown during
   *                             loading the class or calling the
   *                             constructor.
   * @throws MBeanException if the constructor throws an exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, null,
   *                           "instantiate")</code>}.
   * @see #instantiate(String, Object[], String[])
   */
  public Object instantiate(String name, ObjectName loaderName)
    throws InstanceNotFoundException, ReflectionException,
	   MBeanException
  {
    return instantiate(name, loaderName);
  }

  /**
   * Creates an instance of the specified class using the supplied
   * class loader.  If the class loader given is <code>null</code>,
   * then the class loader of the server will be used.  The class
   * should have a public constructor matching the supplied
   * signature.  A reference to the new instance is returned,
   * but the instance is not yet registered with the server.
   *
   * @param name the name of the class of bean to be instantiated.
   * @param loaderName the name of the class loader to use.
   * @param params the parameters for the constructor.
   * @param sig the signature of the constructor.
   * @return an instance of the given class.
   * @throws InstanceNotFoundException if the class loader is not
   *                                   registered with the server.
   * @throws ReflectionException if an exception is thrown during
   *                             loading the class or calling the
   *                             constructor.
   * @throws MBeanException if the constructor throws an exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, null,
   *                           "instantiate")</code>}.
   */
  public Object instantiate(String name, ObjectName loaderName,
			    Object[] params, String[] sig)
    throws InstanceNotFoundException, ReflectionException,
	   MBeanException
  {
    checkSecurity(null, null, "instantiate");
    if (name == null)
      {
	RuntimeException e = 
	  new IllegalArgumentException("The name was null.");
	throw new RuntimeOperationsException(e);
      }
    ClassLoader loader = getClassLoader(loaderName);
    Class[] sigTypes = new Class[sig.length];
    for (int a = 0; a < sig.length; ++a)
      {
	try 
	  {
	    sigTypes[a] = Class.forName(sig[a], true, loader);
	  }
	catch (ClassNotFoundException e)
	  {
	    throw new ReflectionException(e, "The class, " + sig[a] + 
					  ", in the method signature " +
					  "could not be loaded.");
	  }
      }
    try
      {
	Constructor cons =
	  Class.forName(name, true, loader).getConstructor(sigTypes);
	return cons.newInstance(params);
      }
    catch (ClassNotFoundException e)
      {
	throw new ReflectionException(e, "The class, " + name + 
				      ", of the constructor " +
				      "could not be loaded.");
      }
    catch (NoSuchMethodException e)
      {
	throw new ReflectionException(e, "The method, " + name +
				      ", could not be found.");
      }
    catch (IllegalAccessException e)
      {
	throw new ReflectionException(e, "Failed to instantiate the object");
      }
    catch (InstantiationException e)
      {
	throw new ReflectionException(e, "Failed to instantiate the object");
      }
    catch (InvocationTargetException e)
      {
	throw new MBeanException((Exception) e.getCause(), "The constructor "
				 + name + " threw an exception");
      }
  }

  /**
   * Invokes the supplied operation on the specified management
   * bean.  The class objects specified in the signature are loaded
   * using the same class loader as was used for the management bean.
   *
   * @param bean the management bean whose operation should be invoked.
   * @param name the name of the operation to invoke.
   * @param params the parameters of the operation.
   * @param sig the signature of the operation.
   * @return the return value of the method.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws MBeanException if the method invoked throws an exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> name.
   * @throws ReflectionException if an exception is thrown in invoking the
   *                             method.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, name, bean,
   *                           "invoke")</code>}.
   * @see DynamicMBean#invoke(String, Object[], String[])
   */
  public Object invoke(ObjectName bean, String name, Object[] params, String[] sig)
    throws InstanceNotFoundException, MBeanException,
	   ReflectionException
  {
    if (bean == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("The bean was null.");
	throw new RuntimeOperationsException(e);
      }
    Object abean = getBean(bean);
    checkSecurity(bean, name, "invoke");
    if (abean instanceof DynamicMBean)
      return ((DynamicMBean) abean).invoke(name, params, sig);
    else
      try
	{
	  return new StandardMBean(abean, null).invoke(name, params, sig);
	}
      catch (NotCompliantMBeanException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to create dynamic bean.").initCause(e));
	}
  }

  /**
   * <p>
   * Returns true if the specified management bean is an instance
   * of the supplied class.
   * </p>
   * <p>
   * A bean, B, is an instance of a class, C, if either of the following
   * conditions holds:
   * </p>
   * <ul>
   * <li>The class name in B's {@link MBeanInfo} is equal to the supplied
   * name.</li>
   * <li>Both the class of B and C were loaded by the same class loader,
   * and B is assignable to C.</li>
   * </ul>
   * 
   * @param name the name of the management bean.
   * @param className the name of the class to test if <code>name</code> is
   *                  an instance of.
   * @return true if either B is directly an instance of the named class,
   *         or B is assignable to the class, given that both it and B's
   *         current class were loaded using the same class loader.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "isInstanceOf")</code>
   */
  public boolean isInstanceOf(ObjectName name, String className)
    throws InstanceNotFoundException
  {
    Object bean = getBean(name);
    checkSecurity(name, null, "isInstanceOf");
    MBeanInfo info;
    if (bean instanceof DynamicMBean)
      info = ((DynamicMBean) bean).getMBeanInfo();
    else
      try
	{
	  info = new StandardMBean(bean, null).getMBeanInfo();
	}
      catch (NotCompliantMBeanException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to create dynamic bean.").initCause(e));
	}
    if (info.getClassName().equals(className))
      return true;
    Class bclass = bean.getClass();
    try
      {
	Class oclass = Class.forName(className);
	return (bclass.getClassLoader().equals(oclass.getClassLoader()) &&
		oclass.isAssignableFrom(bclass));  
      }
    catch (ClassNotFoundException e)
      {
	return false;
      }
  }

  /**
   * Returns true if the specified management bean is registered with
   * the server.
   *
   * @param name the name of the management bean.
   * @return true if the bean is registered.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> bean name.
   */
  public boolean isRegistered(ObjectName name)
  {
    if (name == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("The name was null.");
	throw new RuntimeOperationsException(e);
      }
    return beans.containsKey(name);
  }

  /**
   * <p>
   * Returns a set of {@link ObjectInstance}s matching the specified
   * criteria.  The full set of beans registered with the server
   * are passed through two filters:
   * </p>
   * <ol>
   * <li>Pattern matching is performed using the supplied
   * {@link ObjectName}.</li>
   * <li>The supplied query expression is applied.</li>
   * </ol>
   * <p>
   * If both the object name and the query expression are <code>null</code>,
   * or the object name has no domain and no key properties,
   * no filtering will be performed and all beans are returned. 
   * </p>
   *
   * @param name an {@link ObjectName} to use as a filter.
   * @param query a query expression to apply to each of the beans that match
   *              the given object name.
   * @return a set of {@link ObjectInstance}s matching the filtered beans.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(null, null, name,
   *                           "queryMBeans")</code>}.  Additionally,
   *                           for an bean, <code>b</code>, the
   *                           caller's permission must imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, b, name,
   *                           "queryMBeans")</code>} or that bean will
   *                           not be included.  Such an exception may also
   *                           arise from the execution of the query, in which
   *                           case that particular bean will again be excluded.
   */
  public Set queryMBeans(ObjectName name, QueryExp query)
  {
    checkSecurity(name, null, "queryMBeans");
    Set results = new HashSet();
    Iterator iterator = beans.entrySet().iterator();
    while (iterator.hasNext())
      {
	Map.Entry entry = (Map.Entry) iterator.next();
	ObjectName nextName = (ObjectName) entry.getKey();
	checkSecurity(name, nextName.toString(), "queryMBeans");
	try
	  {
	    if ((name == null || name.apply(nextName)) &&
		(query == null || query.apply(nextName)))
	      results.add(((ServerInfo) entry.getValue()).getInstance());
	  }
	catch (BadStringOperationException e)
	  {
	    /* Ignored -- assume false result */
	  }
	catch (BadBinaryOpValueExpException e)
	  {
	    /* Ignored -- assume false result */
	  }
	catch (BadAttributeValueExpException e)
	  {
	    /* Ignored -- assume false result */
	  }
	catch (InvalidApplicationException e)
	  {
	    /* Ignored -- assume false result */
	  }
      }
    return results;
  }
 
  /**
   * <p>
   * Returns a set of {@link ObjectName}s matching the specified
   * criteria.  The full set of beans registered with the server
   * are passed through two filters:
   * </p>
   * <ol>
   * <li>Pattern matching is performed using the supplied
   * {@link ObjectName}.</li>
   * <li>The supplied query expression is applied.</li>
   * </ol>
   * <p>
   * If both the object name and the query expression are <code>null</code>,
   * or the object name has no domain and no key properties,
   * no filtering will be performed and all beans are returned. 
   * </p>
   *
   * @param name an {@link ObjectName} to use as a filter.
   * @param query a query expression to apply to each of the beans that match
   *              the given object name.
   * @return a set of {@link ObjectName}s matching the filtered beans.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(null, null, name,
   *                           "queryNames")</code>}.  Additionally,
   *                           for an name, <code>n</code>, the
   *                           caller's permission must imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, n, name,
   *                           "queryNames")</code>} or that name will
   *                           not be included.  Such an exception may also
   *                           arise from the execution of the query, in which
   *                           case that particular bean will again be excluded.
   *                           Note that these permissions are implied if the
   *                           <code>queryMBeans</code> permissions are available.
   */
  public Set queryNames(ObjectName name, QueryExp query)
  {
    checkSecurity(name, null, "queryNames");
    Set results = new HashSet();
    Iterator iterator = beans.entrySet().iterator();
    while (iterator.hasNext())
      {
	Map.Entry entry = (Map.Entry) iterator.next();
	ObjectName nextName = (ObjectName) entry.getKey();
	checkSecurity(name, nextName.toString(), "queryNames");
	try
	  {
	    if ((name == null || name.apply(nextName)) &&
		(query == null || query.apply(nextName)))
	      results.add(nextName);
	  }
	catch (BadStringOperationException e)
	  {
	    /* Ignored -- assume false result */
	  }
	catch (BadBinaryOpValueExpException e)
	  {
	    /* Ignored -- assume false result */
	  }
	catch (BadAttributeValueExpException e)
	  {
	    /* Ignored -- assume false result */
	  }
	catch (InvalidApplicationException e)
	  {
	    /* Ignored -- assume false result */
	  }
      }
    return results;
  }

  /**
   * Registers the supplied instance with the server, using the specified
   * {@link ObjectName}.  If the name given is <code>null</code>, then
   * the bean supplied is expected to implement the {@link MBeanRegistration}
   * interface and provide the name via the 
   * {@link MBeanRegistration#preRegister preRegister} method
   * of this interface.
   *
   * @param obj the object to register with the server.
   * @param name the name under which to register the object,
   *             or <code>null</code> if the {@link MBeanRegistration}
   *             interface should be used.
   * @return an {@link ObjectInstance} containing the supplied
   *         {@link ObjectName} along with the name of the bean's class.
   * @throws InstanceAlreadyExistsException if a matching instance
   *                                        already exists.
   * @throws MBeanRegistrationException if an exception occurs in
   *                                    calling the preRegister
   *                                    method.
   * @throws NotCompliantMBeanException if the created bean is not
   *                                    compliant with the JMX specification.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> object.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "registerMBean")</code>}.  <code>className</code>
   *                           here corresponds to the result of
   *                           {@link MBeanInfo#getClassName()} for objects of
   *                           this class.  If this check succeeds, a check
   *                           is also made on its
   *                           {@link java.security.ProtectionDomain} to ensure
   *                           it implies {@link MBeanTrustPermission(String)
   *                           <code>MBeanTrustPermission("register")</code>}.
   *                           The use of the {@link MBeanRegistration} interface
   *                           results in another {@link MBeanPermission} check
   *                           being made on the returned {@link ObjectName}.
   */
  public ObjectInstance registerMBean(Object obj, ObjectName name)
    throws InstanceAlreadyExistsException, MBeanRegistrationException,
	   NotCompliantMBeanException
  {  
    SecurityManager sm = System.getSecurityManager();
    Class cl = obj.getClass();
    String className = cl.getName();
    if (sm != null)
      {
	sm.checkPermission(new MBeanPermission(className, null, name,
					       "registerMBean"));
	if (!(cl.getProtectionDomain().implies(new MBeanTrustPermission("register"))))
	  throw new SecurityException("The protection domain of the object's class" +
				      "does not imply the trust permission," +
				      "register");
      }
    if (obj == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("The object was null.");
	throw new RuntimeOperationsException(e);
      }
    MBeanRegistration register = null;
    if (obj instanceof MBeanRegistration)
      register = (MBeanRegistration) obj;
    if (name == null)
      {
	if (register == null)
	  {
	    RuntimeException e =
	      new IllegalArgumentException("The name was null and " +
					   "the bean does not implement " +
					   "MBeanRegistration.");
	    throw new RuntimeOperationsException(e);
	  }
	try
	  {
	    name = register.preRegister(this, null);
	    if (sm != null)
	      sm.checkPermission(new MBeanPermission(className, null, name,
						     "registerMBean"));
	  }
	catch (SecurityException e)
	  {
	    register.postRegister(Boolean.FALSE);
	    throw e;
	  }
	catch (Exception e)
	  {
	    register.postRegister(Boolean.FALSE);
	    throw new MBeanRegistrationException(e, "Pre-registration failed.");
	  }
      }
    if (beans.containsKey(name))
      {
	if (register != null)
	  register.postRegister(Boolean.FALSE);
	throw new InstanceAlreadyExistsException(name + "is already registered.");
      }
    ObjectInstance obji = new ObjectInstance(name, className);
    beans.put(name, new ServerInfo(obji, obj));
    if (register != null)
      register.postRegister(Boolean.TRUE);
    return obji;
  }

  /**
   * Removes the specified listener from the list of recipients
   * of notifications from the supplied bean.  This includes all
   * combinations of filters and passback objects registered for
   * this listener.  For more specific removal of listeners, see
   * {@link #removeNotificationListener(ObjectName,
   * NotificationListener,NotificationFilter,Object)}
   *
   * @param name the name of the management bean from which the
   *             listener should be removed.
   * @param listener the listener to remove.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws ListenerNotFoundException if the specified listener
   *                                   is not registered with the bean.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "removeNotificationListener")</code>}.
   * @see #addNotificationListener(NotificationListener, NotificationFilter,
   *                               java.lang.Object)
   * @see NotificationBroadcaster#removeNotificationListener(NotificationListener)
   */
  public void removeNotificationListener(ObjectName name,
					 NotificationListener listener)
    throws InstanceNotFoundException, ListenerNotFoundException
  {
    Object bean = getBean(name);
    checkSecurity(name, null, "removeNotificationListener");
    if (bean instanceof NotificationBroadcaster)
      {
	NotificationBroadcaster bbean = (NotificationBroadcaster) bean;
	NotificationListener indirection = (NotificationListener)
	  listeners.get(listener);
	if (indirection == null)
	  bbean.removeNotificationListener(listener);
	else
	  {
	    bbean.removeNotificationListener(indirection);
	    listeners.remove(listener);
	  }
      }
  }

  /**
   * Removes the specified listener from the list of recipients
   * of notifications from the supplied bean.  Only the first instance with
   * the supplied filter and passback object is removed.
   * <code>null</code> is used as a valid value for these parameters,
   * rather than as a way to remove all registration instances for
   * the specified listener; for this behaviour instead, see
   * {@link #removeNotificationListener(ObjectName, NotificationListener)}.
   *
   * @param name the name of the management bean from which the
   *             listener should be removed.
   * @param listener the listener to remove.
   * @param filter the filter of the listener to remove.
   * @param passback the passback object of the listener to remove.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws ListenerNotFoundException if the specified listener
   *                                   is not registered with the bean.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "removeNotificationListener")</code>}.
   * @see #addNotificationListener(ObjectName, NotificationListener,
   *                               NotificationFilter, Object)
   * @see NotificationEmitter#removeNotificationListener(NotificationListener,
   *                                                     NotificationFilter,
   *                                                     Object)
   */
  public void removeNotificationListener(ObjectName name,
					 NotificationListener listener,
					 NotificationFilter filter,
					 Object passback)
    throws InstanceNotFoundException, ListenerNotFoundException
  {
    Object bean = getBean(name);
    checkSecurity(name, null, "removeNotificationListener");
    if (bean instanceof NotificationEmitter)
      {
	NotificationEmitter bbean = (NotificationEmitter) bean;
	NotificationListener indirection = (NotificationListener)
	  listeners.get(listener);
	if (indirection == null)
	  bbean.removeNotificationListener(listener, filter, passback);
	else
	  {
	    bbean.removeNotificationListener(indirection, filter, passback);
	    listeners.remove(listener);
	  }
      }
  }

  /**
   * Removes the specified listener from the list of recipients
   * of notifications from the supplied bean.  This includes all
   * combinations of filters and passback objects registered for
   * this listener.  For more specific removal of listeners, see
   * {@link #removeNotificationListener(ObjectName,
   * ObjectName,NotificationFilter,Object)}
   *
   * @param name the name of the management bean from which the
   *             listener should be removed.
   * @param listener the name of the listener to remove.
   * @throws InstanceNotFoundException if a name doesn't match a registered
   *                                   bean.
   * @throws ListenerNotFoundException if the specified listener
   *                                   is not registered with the bean.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "removeNotificationListener")</code>}.
   * @see #addNotificationListener(NotificationListener, NotificationFilter,
   *                               java.lang.Object)
   * @see NotificationBroadcaster#removeNotificationListener(NotificationListener)
   */
  public void removeNotificationListener(ObjectName name, ObjectName listener)
    throws InstanceNotFoundException, ListenerNotFoundException
  {
    Object lbean = getBean(listener);
    if (!(lbean instanceof NotificationListener))
      {
	RuntimeException e =
	  new IllegalArgumentException("The supplied listener name does not " +
				       "correspond to a notification listener.");
	throw new RuntimeOperationsException(e);
      }
    removeNotificationListener(name, ((NotificationListener) lbean));
  }

  /**
   * Removes the specified listener from the list of recipients
   * of notifications from the supplied bean.  Only the first instance with
   * the supplied filter and passback object is removed.
   * <code>null</code> is used as a valid value for these parameters,
   * rather than as a way to remove all registration instances for
   * the specified listener; for this behaviour instead, see
   * {@link #removeNotificationListener(ObjectName, ObjectName)}.
   *
   * @param name the name of the management bean from which the
   *             listener should be removed.
   * @param listener the name of the listener to remove.
   * @param filter the filter of the listener to remove.
   * @param passback the passback object of the listener to remove.
   * @throws InstanceNotFoundException if a name doesn't match a registered
   *                                   bean.
   * @throws ListenerNotFoundException if the specified listener
   *                                   is not registered with the bean.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "removeNotificationListener")</code>}.
   * @see #addNotificationListener(ObjectName, NotificationListener,
   *                               NotificationFilter, Object)
   * @see NotificationEmitter#removeNotificationListener(NotificationListener,
   *                                                     NotificationFilter,
   *                                                     Object)
   */
  public void removeNotificationListener(ObjectName name,
				  ObjectName listener,
				  NotificationFilter filter,
				  Object passback)
    throws InstanceNotFoundException, ListenerNotFoundException
  {
    Object lbean = getBean(listener);
    if (!(lbean instanceof NotificationListener))
      {
	RuntimeException e =
	  new IllegalArgumentException("The supplied listener name does not " +
				       "correspond to a notification listener.");
	throw new RuntimeOperationsException(e);
      }
    removeNotificationListener(name, ((NotificationListener) lbean), filter,
			       passback);
  }

  /**
   * Sets the value of the specified attribute of the supplied
   * management bean.  
   *
   * @param name the name of the management bean.
   * @param attribute the attribute to set.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws AttributeNotFoundException if the attribute does not
   *                                    correspond to an attribute
   *                                    of the bean.
   * @throws InvalidAttributeValueException if the value is invalid
   *                                        for this particular
   *                                        attribute of the bean.
   * @throws MBeanException if setting the attribute causes
   *                        the bean to throw an exception (which
   *                        becomes the cause of this exception).
   * @throws ReflectionException if an exception occurred in trying
   *                             to use the reflection interface
   *                             to lookup the attribute.  The
   *                             thrown exception is the cause of
   *                             this exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> bean or attribute
   *                                    name.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, name, bean,
   *                           "setAttribute")</code>}.
   * @see #getAttribute(ObjectName, String)
   * @see DynamicMBean#setAttribute(Attribute)
   */
  public void setAttribute(ObjectName name, Attribute attribute)
    throws InstanceNotFoundException, AttributeNotFoundException,
	   InvalidAttributeValueException, MBeanException,
	   ReflectionException
  {
    if (attribute == null || name == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("One of the supplied arguments was null.");
	throw new RuntimeOperationsException(e);
      }
    Object bean = getBean(name);
    checkSecurity(name, attribute.getName(), "setAttribute");
    if (bean instanceof DynamicMBean)
      ((DynamicMBean) bean).setAttribute(attribute);
    else
      try
	{
	  new StandardMBean(bean, null).setAttribute(attribute);
	}
      catch (NotCompliantMBeanException e)
	{
	  throw (Error) 
	    (new InternalError("Failed to create dynamic bean.").initCause(e));
	}
  }

  /**
   * Sets the value of each of the specified attributes
   * of the supplied management bean to that specified by
   * the {@link Attribute} object.  The returned list contains
   * the attributes that were set and their new values.
   *
   * @param name the name of the management bean.
   * @param attributes the attributes to set.
   * @return a list of the changed attributes.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws ReflectionException if an exception occurred in trying
   *                             to use the reflection interface
   *                             to lookup the attribute.  The
   *                             thrown exception is the cause of
   *                             this exception.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> bean or attribute
   *                                    list.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, bean,
   *                           "setAttribute")</code>}.  Additionally,
   *                           for an attribute name, <code>n</code>, the
   *                           caller's permission must imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, n, bean,
   *                           "setAttribute")</code>} or that attribute will
   *                           not be included.
   * @see #getAttributes(ObjectName, String[])
   * @see DynamicMBean#setAttributes(AttributeList)
   */
  public AttributeList setAttributes(ObjectName name, AttributeList attributes)
    throws InstanceNotFoundException, ReflectionException
  {
    if (name == null || attributes == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("One of the supplied arguments was null.");
	throw new RuntimeOperationsException(e);
      }
    Object abean = getBean(name);
    checkSecurity(name, null, "setAttribute");
    AttributeList list = new AttributeList(attributes.size());
    Iterator it = attributes.iterator();
    while (it.hasNext())
      {
	try
	  {
	    Attribute attrib = (Attribute) it.next();
	    if (attrib == null)
	      {
		RuntimeException e =
		  new IllegalArgumentException("An attribute was null.");
		throw new RuntimeOperationsException(e);
	      }
	    checkSecurity(name, attrib.getName(), "setAttribute");
	    if (abean instanceof DynamicMBean)
	      ((DynamicMBean) abean).setAttribute(attrib);
	    else
	      try
		{
		  new StandardMBean(abean, null).setAttribute(attrib);
		}
	      catch (NotCompliantMBeanException e)
		{
		  throw (Error) 
		    (new InternalError("Failed to create dynamic bean.").initCause(e));
		}
	    list.add(attrib);
	  }
	catch (AttributeNotFoundException e)
	  {
	    /* Ignored */
	  }
	catch (InvalidAttributeValueException e)
	  {
	    /* Ignored */
	  }
	catch (MBeanException e)
	  {
	    /* Ignored */
	  }
      }
    return list;
  }

  /**
   * Unregisters the specified management bean.  Following this operation,
   * the bean instance is no longer accessible from the server via this
   * name.  Prior to unregistering the bean, the
   * {@link MBeanRegistration#preDeregister()} method will be called if
   * the bean implements the {@link MBeanRegistration} interface.
   *
   * @param name the name of the management bean.
   * @throws InstanceNotFoundException if the bean can not be found.
   * @throws MBeanRegistrationException if an exception occurs in
   *                                    calling the preDeregister
   *                                    method.
   * @throws RuntimeOperationsException if an {@link IllegalArgumentException}
   *                                    is thrown by the server due to a
   *                                    <code>null</code> bean name or a
   *                                    request being made to unregister the
   *                                    {@link MBeanServerDelegate} bean.
   * @throws SecurityException if a security manager exists and the
   *                           caller's permissions don't imply {@link
   *                           MBeanPermission(String,String,ObjectName,String)
   *                           <code>MBeanPermission(className, null, name,
   *                           "unregisterMBean")</code>}.
   */ 
  public void unregisterMBean(ObjectName name)
    throws InstanceNotFoundException, MBeanRegistrationException
  {
    if (name == null)
      {
	RuntimeException e =
	  new IllegalArgumentException("The name was null.");
	throw new RuntimeOperationsException(e);
      }
    Object bean = getBean(name);    
    checkSecurity(name, null, "unregisterMBean");
    if (bean == delegate)
      {
	RuntimeException e =
	  new IllegalArgumentException("The delegate can not be unregistered.");
	throw new RuntimeOperationsException(e);
      }	
    MBeanRegistration register = null;
    if (bean instanceof MBeanRegistration)
      {
	register = (MBeanRegistration) bean;
	try
	  {
	    register.preDeregister();
	  }
	catch (Exception e)
	  {
	    throw new MBeanRegistrationException(e, "Pre-deregistration failed.");
	  }
      }
    beans.remove(name);
    if (register != null)
      register.postDeregister();
  }

  /**
   * Input stream which deserializes using the given classloader.
   */
  private class ServerInputStream
    extends ObjectInputStream
  {

    private ClassLoader cl;

    public ServerInputStream(InputStream is, ClassLoader cl)
      throws IOException, StreamCorruptedException
    {
      super(is);
      this.cl = cl;
    }

    protected Class resolveClass(ObjectStreamClass osc)
      throws ClassNotFoundException, IOException
    {
      try
	{
	  return Class.forName(osc.getName(), true, cl);
	}
      catch (ClassNotFoundException e)
	{
	  return super.resolveClass(osc);
	}
    }

  }

  /**
   * Holder for information on registered beans.
   */
  private class ServerInfo
  {  
    private ObjectInstance instance;

    private Object object;

    public ServerInfo(ObjectInstance instance, Object object)
    {
      this.instance = instance;
      this.object = object;
    }

    public Object getObject()
    {
      return object;
    }

    public ObjectInstance getInstance()
    {
      return instance;
    }
  }

  /**
   * Notification listener which removes direct references
   * to beans.
   */
  private class ServerNotificationListener
    implements NotificationListener
  {

    /**
     * The bean from which notifications are emitted.
     */
    Object bean;

    /**
     * The {@link ObjectName} of the emitting bean.
     */
    ObjectName name;

    /**
     * The real {@link NotificationListener}.
     */
    NotificationListener listener;

    /**
     * Constructs a new {@link ServerNotificationListener} replacing
     * occurrences of <code>bean</code> with its name.
     *
     * @param bean the bean emitting notifications.
     * @param name the object name of the emitting bean.
     * @param listener the listener events eventually reach.
     */
    public ServerNotificationListener(Object bean, ObjectName name,
				      NotificationListener listener)
    {
      this.bean = bean;
      this.name = name;
      this.listener = listener;
    }

    /**
     * Replace a direct reference to <code>bean</code> with its
     * object reference, if necessary, before calling the listener.
     *
     * @param notif the notification being emitted.
     * @param handback an object that will be returned to the notification
     *                 listener when an event occurs.
     */
    public void handleNotification(Notification notif, Object handback)
    {
      if (notif.getSource() == bean)
	notif.setSource(name);
      listener.handleNotification(notif, handback);
    }

  }

}