rustorb-core 0.5.0

ORB core of rustorb: connections, request dispatch and POA
Documentation
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
2162
2163
2164
//! The POA: object adapters, their policies, POA managers and object keys.
//!
//! Object keys follow `docs/poa-design.md`:
//!
//! | POA | Key |
//! | --- | --- |
//! | PERSISTENT | `\xff` POA path `\x00` ObjectId (the omniORB format) |
//! | TRANSIENT | `\xff` POA path `\xfe` boot id (8 bytes) ObjectId |
//! | RootPOA | `\xfe` boot id ObjectId |
//!
//! The POA path is the names from below the RootPOA down, separated by
//! `\xff`. The boot id is a random number chosen at each start of the ORB,
//! so TRANSIENT references die with the process. System generated
//! ObjectIds are the boot id and a 4-byte big-endian serial number.

use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt;
use std::panic::{self, AssertUnwindSafe};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, Weak};
use std::thread::{self, ThreadId};

use rustorb_giop::{IiopProfile, TaggedComponent};

use crate::pool::Job;
use crate::servant::{Cookie, ServantLocator};
use crate::server::Server;
use crate::trace;
use crate::{
    AdapterActivator, CompletionStatus, ObjectRef, Orb, PolicyError, PolicyValue, Servant,
    ServantActivator, ServantManager, ServantManagerError, SystemException, SystemExceptionKind,
    minor,
};

/// `PortableServer::ObjectId`.
pub type ObjectId = Vec<u8>;

const PATH_START: u8 = 0xff;
const PATH_SEPARATOR: u8 = 0xff;
const PERSISTENT_END: u8 = 0x00;
const TRANSIENT_END: u8 = 0xfe;
/// Length of the boot id in TRANSIENT keys and system generated ids.
pub(crate) const BOOT_ID_LEN: usize = 8;

/// `PortableServer::ThreadPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ThreadPolicy {
    /// `ORB_CTRL_MODEL`: requests run on the upcall thread pool, in
    /// parallel and in no particular order.
    OrbCtrl,
    /// `SINGLE_THREAD_MODEL`: requests to the POA run one at a time, in
    /// the order they arrive, on any upcall thread. A servant may call an
    /// object of its own POA in the same ORB: the call runs at once on the
    /// servant's thread.
    SingleThread,
    /// `MAIN_THREAD_MODEL`: requests to the POA run one at a time on a
    /// thread that is in [`Orb::run`] or [`Orb::perform_work`], shared with
    /// every other POA with this policy.
    MainThread,
}

/// `PortableServer::LifespanPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Lifespan {
    /// References are valid while this ORB runs.
    Transient,
    /// References stay valid across restarts of a server that listens on
    /// the same address and recreates the POA.
    Persistent,
}

/// `PortableServer::IdUniquenessPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IdUniqueness {
    /// A servant is active under one ObjectId at most.
    Unique,
    /// A servant may be active under several ObjectIds.
    Multiple,
}

/// `PortableServer::IdAssignmentPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IdAssignment {
    /// The application chooses the ObjectIds.
    User,
    /// The POA generates the ObjectIds.
    System,
}

/// `PortableServer::ImplicitActivationPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ImplicitActivation {
    /// `servant_to_reference` activates an inactive servant.
    Implicit,
    /// Servants are activated explicitly only.
    NoImplicit,
}

/// `PortableServer::ServantRetentionPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ServantRetention {
    /// Active servants are kept in the active object map.
    Retain,
    /// No active object map: every request goes to the default servant or
    /// to a [`ServantLocator`].
    NonRetain,
}

/// `PortableServer::RequestProcessingPolicyValue`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RequestProcessing {
    /// Requests go to servants in the active object map only.
    ActiveObjectMapOnly,
    /// Requests for objects not in the active object map go to the
    /// default servant ([`Poa::set_servant`]).
    UseDefaultServant,
    /// Requests for objects not in the active object map go to the
    /// servant manager ([`Poa::set_servant_manager`]).
    UseServantManager,
}

/// `BiDirPolicy::BidirectionalPolicyValue` (docs/bidir-giop.md).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Bidirectional {
    /// `NORMAL`: no bidirectional GIOP.
    Normal,
    /// `BOTH`: on a POA, its objects take requests that come over the
    /// connections this ORB opened (callbacks over bidirectional GIOP); on
    /// the ORB, it offers and accepts bidirectional connections.
    Both,
}

/// A POA policy, given to [`Poa::create_poa`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Policy {
    /// `ThreadPolicy`.
    Thread(ThreadPolicy),
    /// `LifespanPolicy`.
    Lifespan(Lifespan),
    /// `IdUniquenessPolicy`.
    IdUniqueness(IdUniqueness),
    /// `IdAssignmentPolicy`.
    IdAssignment(IdAssignment),
    /// `ImplicitActivationPolicy`.
    ImplicitActivation(ImplicitActivation),
    /// `ServantRetentionPolicy`.
    ServantRetention(ServantRetention),
    /// `RequestProcessingPolicy`.
    RequestProcessing(RequestProcessing),
    /// `BiDirPolicy::BidirectionalPolicy`.
    Bidirectional(Bidirectional),
}

/// The policies of one POA.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Policies {
    /// Thread policy.
    pub thread: ThreadPolicy,
    /// Lifespan policy.
    pub lifespan: Lifespan,
    /// ObjectId uniqueness policy.
    pub id_uniqueness: IdUniqueness,
    /// ObjectId assignment policy.
    pub id_assignment: IdAssignment,
    /// Implicit activation policy.
    pub implicit_activation: ImplicitActivation,
    /// Servant retention policy.
    pub servant_retention: ServantRetention,
    /// Request processing policy.
    pub request_processing: RequestProcessing,
    /// Bidirectional policy: whether requests that come over a connection
    /// this ORB opened reach the POA's objects.
    pub bidirectional: Bidirectional,
}

impl Default for Policies {
    /// The policies of a POA created with no policies.
    fn default() -> Policies {
        Policies {
            thread: ThreadPolicy::OrbCtrl,
            lifespan: Lifespan::Transient,
            id_uniqueness: IdUniqueness::Unique,
            id_assignment: IdAssignment::System,
            implicit_activation: ImplicitActivation::NoImplicit,
            servant_retention: ServantRetention::Retain,
            request_processing: RequestProcessing::ActiveObjectMapOnly,
            bidirectional: Bidirectional::Normal,
        }
    }
}

impl Policies {
    /// The policies of the RootPOA.
    pub(crate) fn root() -> Policies {
        Policies {
            implicit_activation: ImplicitActivation::Implicit,
            ..Policies::default()
        }
    }

    /// The defaults overridden by `list`. Fails with the index of the first
    /// policy that is repeated, or of one inconsistent with the others.
    fn from_list(list: &[Policy]) -> Result<Policies, u16> {
        let mut p = Policies::default();
        let mut seen = Vec::new();
        let index = |i: usize| u16::try_from(i).unwrap_or(u16::MAX);
        for (i, policy) in list.iter().enumerate() {
            let kind = std::mem::discriminant(policy);
            if seen.contains(&kind) {
                return Err(index(i));
            }
            seen.push(kind);
            match *policy {
                Policy::Thread(v) => p.thread = v,
                Policy::Lifespan(v) => p.lifespan = v,
                Policy::IdUniqueness(v) => p.id_uniqueness = v,
                Policy::IdAssignment(v) => p.id_assignment = v,
                Policy::ImplicitActivation(v) => p.implicit_activation = v,
                Policy::ServantRetention(v) => p.servant_retention = v,
                Policy::RequestProcessing(v) => p.request_processing = v,
                Policy::Bidirectional(v) => p.bidirectional = v,
            }
        }
        // The index of the last policy of a kind in the list, as the one in
        // conflict with the defaults or with the policies before it.
        let position = |f: fn(&Policy) -> bool| index(list.iter().rposition(f).unwrap_or(0));
        if p.implicit_activation == ImplicitActivation::Implicit
            && (p.id_assignment != IdAssignment::System
                || p.servant_retention != ServantRetention::Retain)
        {
            return Err(position(|q| matches!(q, Policy::ImplicitActivation(_))));
        }
        if p.servant_retention == ServantRetention::NonRetain
            && p.request_processing == RequestProcessing::ActiveObjectMapOnly
        {
            return Err(position(|q| {
                matches!(
                    q,
                    Policy::ServantRetention(_) | Policy::RequestProcessing(_)
                )
            }));
        }
        if p.request_processing == RequestProcessing::UseDefaultServant
            && p.id_uniqueness != IdUniqueness::Multiple
        {
            return Err(position(|q| {
                matches!(q, Policy::RequestProcessing(_) | Policy::IdUniqueness(_))
            }));
        }
        Ok(p)
    }
}

/// The exceptions of the `PortableServer::POA` and `POAManager`
/// operations, and system exceptions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PoaError {
    /// `AdapterAlreadyExists`.
    AdapterAlreadyExists,
    /// `AdapterNonExistent`.
    AdapterNonExistent,
    /// `InvalidPolicy`, with the index of the offending policy.
    InvalidPolicy(u16),
    /// `ServantAlreadyActive`.
    ServantAlreadyActive,
    /// `ObjectAlreadyActive`.
    ObjectAlreadyActive,
    /// `ServantNotActive`.
    ServantNotActive,
    /// `ObjectNotActive`.
    ObjectNotActive,
    /// `WrongPolicy`.
    WrongPolicy,
    /// `WrongAdapter`.
    WrongAdapter,
    /// `POAManager::AdapterInactive`.
    AdapterInactive,
    /// `POAManagerFactory::ManagerAlreadyExists`.
    ManagerAlreadyExists,
    /// `CORBA::PolicyError`, from `POAManagerFactory::create_POAManager`.
    Policy(PolicyError),
    /// `POA::NoServant`: no default servant was set.
    NoServant,
    /// `Current::NoContext`: the thread is not running a request.
    NoContext,
    /// A system exception, such as `OBJECT_NOT_EXIST` for a destroyed POA.
    System(SystemException),
}

impl fmt::Display for PoaError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PoaError::AdapterAlreadyExists => {
                f.write_str("PortableServer::POA::AdapterAlreadyExists")
            }
            PoaError::AdapterNonExistent => f.write_str("PortableServer::POA::AdapterNonExistent"),
            PoaError::InvalidPolicy(i) => {
                write!(f, "PortableServer::POA::InvalidPolicy (index {i})")
            }
            PoaError::ServantAlreadyActive => {
                f.write_str("PortableServer::POA::ServantAlreadyActive")
            }
            PoaError::ObjectAlreadyActive => {
                f.write_str("PortableServer::POA::ObjectAlreadyActive")
            }
            PoaError::ServantNotActive => f.write_str("PortableServer::POA::ServantNotActive"),
            PoaError::ObjectNotActive => f.write_str("PortableServer::POA::ObjectNotActive"),
            PoaError::WrongPolicy => f.write_str("PortableServer::POA::WrongPolicy"),
            PoaError::WrongAdapter => f.write_str("PortableServer::POA::WrongAdapter"),
            PoaError::AdapterInactive => f.write_str("PortableServer::POAManager::AdapterInactive"),
            PoaError::ManagerAlreadyExists => {
                f.write_str("PortableServer::POAManagerFactory::ManagerAlreadyExists")
            }
            PoaError::Policy(e) => e.fmt(f),
            PoaError::NoServant => f.write_str("PortableServer::POA::NoServant"),
            PoaError::NoContext => f.write_str("PortableServer::Current::NoContext"),
            PoaError::System(e) => e.fmt(f),
        }
    }
}

impl std::error::Error for PoaError {}

impl From<SystemException> for PoaError {
    fn from(e: SystemException) -> Self {
        PoaError::System(e)
    }
}

fn sys(kind: SystemExceptionKind, minor: u32) -> SystemException {
    SystemException::new(kind, minor, CompletionStatus::No)
}

/// `OBJECT_NOT_EXIST` for a request whose key names no active object.
pub(crate) fn no_such_object() -> SystemException {
    sys(SystemExceptionKind::OBJECT_NOT_EXIST, minor::NO_SUCH_OBJECT)
}

// --- upcall bookkeeping ------------------------------------------------------

thread_local! {
    static UPCALL_DEPTH: Cell<usize> = const { Cell::new(0) };
    static MAIN_DEPTH: Cell<usize> = const { Cell::new(0) };
    static CURRENT: RefCell<Vec<CurrentEntry>> = const { RefCell::new(Vec::new()) };
}

/// The request a thread is running, for [`PoaCurrent`].
#[derive(Clone)]
struct CurrentEntry {
    poa: Poa,
    id: ObjectId,
    servant: Arc<dyn Servant>,
}

/// Makes a request the current one of the thread while it lives.
pub(crate) struct CurrentGuard(());

impl CurrentGuard {
    pub(crate) fn enter(poa: &Poa, id: &[u8], servant: &Arc<dyn Servant>) -> CurrentGuard {
        CURRENT.with(|c| {
            c.borrow_mut().push(CurrentEntry {
                poa: poa.clone(),
                id: id.to_vec(),
                servant: Arc::clone(servant),
            });
        });
        CurrentGuard(())
    }
}

impl Drop for CurrentGuard {
    fn drop(&mut self) {
        CURRENT.with(|c| c.borrow_mut().pop());
    }
}

/// `PortableServer::Current`: the POA and ObjectId of the request the
/// calling thread is running. Outside a request, every method fails with
/// [`PoaError::NoContext`].
#[derive(Debug, Clone, Copy, Default)]
pub struct PoaCurrent;

impl PoaCurrent {
    fn with<T>(f: impl FnOnce(&CurrentEntry) -> T) -> Result<T, PoaError> {
        CURRENT.with(|c| c.borrow().last().map(f).ok_or(PoaError::NoContext))
    }

    /// `get_POA`: the POA of the target object.
    pub fn get_poa() -> Result<Poa, PoaError> {
        PoaCurrent::with(|e| e.poa.clone())
    }

    /// `get_object_id`: the ObjectId of the target object.
    pub fn get_object_id() -> Result<ObjectId, PoaError> {
        PoaCurrent::with(|e| e.id.clone())
    }

    /// `get_reference`: a reference to the target object.
    pub fn get_reference() -> Result<ObjectRef, PoaError> {
        let (poa, id, servant) =
            PoaCurrent::with(|e| (e.poa.clone(), e.id.clone(), Arc::clone(&e.servant)))?;
        poa.reference(&id, servant.repository_id())
    }

    /// `get_servant`: the servant running the request.
    pub fn get_servant() -> Result<Arc<dyn Servant>, PoaError> {
        PoaCurrent::with(|e| Arc::clone(&e.servant))
    }
}

/// Marks the current thread as one that runs MAIN_THREAD_MODEL requests
/// (in `Orb::run` or `Orb::perform_work`) while it lives.
pub(crate) struct MainThreadGuard(());

impl MainThreadGuard {
    pub(crate) fn enter() -> MainThreadGuard {
        MAIN_DEPTH.with(|d| d.set(d.get() + 1));
        MainThreadGuard(())
    }
}

impl Drop for MainThreadGuard {
    fn drop(&mut self) {
        MAIN_DEPTH.with(|d| d.set(d.get() - 1));
    }
}

/// Whether the current thread runs MAIN_THREAD_MODEL requests.
pub(crate) fn is_main_thread() -> bool {
    MAIN_DEPTH.with(Cell::get) > 0
}

/// Whether the current thread is running a servant.
pub(crate) fn in_upcall() -> bool {
    UPCALL_DEPTH.with(Cell::get) > 0
}

/// Marks the current thread as running a servant while it lives.
pub(crate) struct UpcallGuard(());

impl UpcallGuard {
    pub(crate) fn enter() -> UpcallGuard {
        UPCALL_DEPTH.with(|d| d.set(d.get() + 1));
        UpcallGuard(())
    }
}

impl Drop for UpcallGuard {
    fn drop(&mut self) {
        UPCALL_DEPTH.with(|d| d.set(d.get() - 1));
    }
}

/// A count of requests in progress that can be waited on.
#[derive(Debug, Default)]
pub(crate) struct Activity {
    count: Mutex<usize>,
    idle: Condvar,
}

impl Activity {
    pub(crate) fn enter(self: &Arc<Self>) -> ActivityGuard {
        *self.count.lock().unwrap() += 1;
        ActivityGuard(Arc::clone(self))
    }

    pub(crate) fn wait_idle(&self) {
        let mut count = self.count.lock().unwrap();
        while *count > 0 {
            count = self.idle.wait(count).unwrap();
        }
    }
}

pub(crate) struct ActivityGuard(Arc<Activity>);

impl Drop for ActivityGuard {
    fn drop(&mut self) {
        let mut count = self.0.count.lock().unwrap();
        *count -= 1;
        if *count == 0 {
            self.0.idle.notify_all();
        }
    }
}

/// Fails with `BAD_INV_ORDER` (would deadlock) when a servant asks to wait
/// for the completion of requests.
fn check_wait(wait_for_completion: bool) -> Result<(), SystemException> {
    if wait_for_completion && in_upcall() {
        return Err(sys(
            SystemExceptionKind::BAD_INV_ORDER,
            minor::WOULD_DEADLOCK,
        ));
    }
    Ok(())
}

// --- POA manager -------------------------------------------------------------

/// `PortableServer::POAManager::State`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PoaManagerState {
    /// Requests are queued until the manager becomes active.
    Holding,
    /// Requests are processed.
    Active,
    /// Requests are refused with `TRANSIENT`.
    Discarding,
    /// Requests are refused with `OBJ_ADAPTER`; the manager cannot become
    /// active again.
    Inactive,
}

/// What happens to a request that reached its POA manager.
pub(crate) enum Disposition {
    /// Run it. The dispatch is called on whichever thread admitted the
    /// request (an I/O thread, or the one activating the manager), so it
    /// must only hand the request on, not run user code.
    Run,
    /// Answer it with this exception.
    Reject(SystemException),
}

pub(crate) type Dispatch = Box<dyn FnOnce(Disposition) + Send>;

/// Requests a holding POA manager keeps at most; more are refused with
/// `TRANSIENT`.
const MAX_HELD_REQUESTS: usize = 4096;

/// `PortableServer::POAManager`: switches the processing of requests for
/// the POAs it manages. A manager starts in the holding state.
///
/// A cheap handle; clones manage the same POAs.
#[derive(Clone)]
pub struct PoaManager {
    pub(crate) inner: Arc<ManagerInner>,
}

pub(crate) struct ManagerInner {
    /// `get_id`: unique among the managers that exist in the ORB.
    id: String,
    state: Mutex<ManagerState>,
    activity: Arc<Activity>,
    server: Weak<Server>,
    /// The POAs the manager manages.
    poas: Mutex<ManagedPoas>,
}

#[derive(Default)]
struct ManagedPoas {
    /// For `deactivate(etherealize)`.
    list: Vec<Weak<PoaInner>>,
    /// The POAs not yet destroyed.
    live: usize,
    /// The last of its POAs was destroyed: the manager is gone from the
    /// `POAManagerFactory` and cannot take new POAs.
    destroyed: bool,
}

struct ManagerState {
    state: PoaManagerState,
    held: VecDeque<Dispatch>,
}

impl fmt::Debug for PoaManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PoaManager")
            .field("id", &self.inner.id)
            .field("state", &self.state())
            .finish_non_exhaustive()
    }
}

impl PartialEq for PoaManager {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl Eq for PoaManager {}

impl PoaManager {
    /// A new manager in the holding state, not yet registered with the
    /// factory.
    pub(crate) fn new(server: &Arc<Server>, id: String) -> PoaManager {
        let inner = Arc::new(ManagerInner {
            id,
            state: Mutex::new(ManagerState {
                state: PoaManagerState::Holding,
                held: VecDeque::new(),
            }),
            activity: Arc::default(),
            server: Arc::downgrade(server),
            poas: Mutex::default(),
        });
        server.register_manager(&inner);
        PoaManager { inner }
    }

    /// `get_id`: the identity given to
    /// [`create_poa_manager`](PoaManagerFactory::create_poa_manager), or
    /// the one the ORB chose for a manager made by `create_POA`
    /// (`"RootPOAManager"` for the RootPOA's).
    pub fn get_id(&self) -> &str {
        &self.inner.id
    }

    /// The current state.
    pub fn state(&self) -> PoaManagerState {
        self.inner.state.lock().unwrap().state
    }

    /// Starts processing requests, including those held so far. Fails
    /// with `AdapterInactive` after [`deactivate`](PoaManager::deactivate).
    pub fn activate(&self) -> Result<(), PoaError> {
        let held = {
            let mut st = self.inner.state.lock().unwrap();
            if st.state == PoaManagerState::Inactive {
                return Err(PoaError::AdapterInactive);
            }
            st.state = PoaManagerState::Active;
            trace::info!("POA manager active");
            std::mem::take(&mut st.held)
        };
        for dispatch in held {
            dispatch(Disposition::Run);
        }
        Ok(())
    }

    /// Queues new requests until the manager is activated again. With
    /// `wait_for_completion`, returns once the requests in progress have
    /// finished (`BAD_INV_ORDER` when called from a servant).
    pub fn hold_requests(&self, wait_for_completion: bool) -> Result<(), PoaError> {
        check_wait(wait_for_completion)?;
        self.set(PoaManagerState::Holding)?;
        if wait_for_completion {
            self.inner.activity.wait_idle();
        }
        Ok(())
    }

    /// Refuses new requests with `TRANSIENT`, and the held ones too.
    pub fn discard_requests(&self, wait_for_completion: bool) -> Result<(), PoaError> {
        check_wait(wait_for_completion)?;
        self.set(PoaManagerState::Discarding)?;
        if wait_for_completion {
            self.inner.activity.wait_idle();
        }
        Ok(())
    }

    /// Refuses new requests with `OBJ_ADAPTER` for good, and the held ones
    /// too. With `etherealize`, the objects of the POAs it manages that
    /// have a [`ServantActivator`] are deactivated and etherealized once
    /// their requests have finished (before this returns with
    /// `wait_for_completion`).
    pub fn deactivate(&self, etherealize: bool, wait_for_completion: bool) -> Result<(), PoaError> {
        check_wait(wait_for_completion)?;
        self.set(PoaManagerState::Inactive)?;
        if etherealize {
            let poas: Vec<_> = self
                .inner
                .poas
                .lock()
                .unwrap()
                .list
                .iter()
                .filter_map(Weak::upgrade)
                .collect();
            let mut work = Vec::new();
            for poa in poas {
                work.extend(poa.take_for_etherealize());
            }
            if let Some(server) = self.inner.server.upgrade() {
                server.etherealize(work, wait_for_completion);
            }
        }
        if wait_for_completion {
            self.inner.activity.wait_idle();
        }
        Ok(())
    }

    fn set(&self, state: PoaManagerState) -> Result<(), PoaError> {
        let held = {
            let mut st = self.inner.state.lock().unwrap();
            if st.state == PoaManagerState::Inactive {
                return Err(PoaError::AdapterInactive);
            }
            st.state = state;
            trace::info!("POA manager {state:?}");
            if state == PoaManagerState::Holding {
                return Ok(());
            }
            std::mem::take(&mut st.held)
        };
        let e = refusal(state);
        for dispatch in held {
            dispatch(Disposition::Reject(e.clone()));
        }
        Ok(())
    }
}

// --- POA manager factory -----------------------------------------------------

/// `PortableServer::POAManagerFactory` (CORBA 3): creates POA managers
/// with an identity, to share among POAs, and finds those that exist in
/// the ORB. Got from any POA with
/// [`the_poa_manager_factory`](Poa::the_poa_manager_factory).
///
/// A manager exists from its creation (explicit, or implicit in
/// `create_POA`) until the last of its POAs is destroyed. One created
/// here that never had a POA exists until the ORB is destroyed.
#[derive(Clone)]
pub struct PoaManagerFactory {
    orb: Orb,
}

impl fmt::Debug for PoaManagerFactory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PoaManagerFactory").finish_non_exhaustive()
    }
}

impl PoaManagerFactory {
    fn server(&self) -> Result<Arc<Server>, PoaError> {
        self.orb
            .server()
            .ok_or_else(|| sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into())
    }

    /// `create_POAManager`: a new manager, in the holding state, with the
    /// identity `id` (the empty string is a valid identity). Fails with
    /// `ManagerAlreadyExists` if a manager with that identity exists in
    /// the ORB, and with `PolicyError` (`UNSUPPORTED_POLICY`) for any
    /// policy: none of the policies rustorb knows applies to POA managers.
    pub fn create_poa_manager(
        &self,
        id: &str,
        policies: &[PolicyValue],
    ) -> Result<PoaManager, PoaError> {
        if !policies.is_empty() {
            return Err(PoaError::Policy(PolicyError {
                reason: PolicyError::UNSUPPORTED_POLICY,
            }));
        }
        self.server()?.create_manager(id)
    }

    /// `list`: every manager that exists in the ORB, whether created here
    /// or by `create_POA`, in the order they were created.
    pub fn list(&self) -> Vec<PoaManager> {
        self.server().map(|s| s.list_managers()).unwrap_or_default()
    }

    /// `find`: the manager with the identity `id`, or `None` (a nil
    /// reference).
    pub fn find(&self, id: &str) -> Option<PoaManager> {
        self.server().ok()?.find_manager(id)
    }
}

impl ManagerInner {
    pub(crate) fn id(&self) -> &str {
        &self.id
    }
}

/// The exception for a request refused in `state`.
fn refusal(state: PoaManagerState) -> SystemException {
    match state {
        PoaManagerState::Inactive => sys(SystemExceptionKind::OBJ_ADAPTER, minor::POA_INACTIVE),
        _ => sys(SystemExceptionKind::TRANSIENT, minor::POA_DISCARDING),
    }
}

impl ManagerInner {
    /// Runs, holds or refuses a request according to the state.
    pub(crate) fn dispatch(&self, dispatch: Dispatch) {
        let mut st = self.state.lock().unwrap();
        match st.state {
            PoaManagerState::Active => {
                drop(st);
                dispatch(Disposition::Run);
            }
            PoaManagerState::Holding if st.held.len() < MAX_HELD_REQUESTS => {
                st.held.push_back(dispatch);
            }
            PoaManagerState::Holding => {
                drop(st);
                dispatch(Disposition::Reject(sys(SystemExceptionKind::TRANSIENT, 0)));
            }
            state => {
                drop(st);
                dispatch(Disposition::Reject(refusal(state)));
            }
        }
    }

    /// Whether a `LocateRequest` may answer `OBJECT_HERE`.
    pub(crate) fn is_inactive(&self) -> bool {
        self.state.lock().unwrap().state == PoaManagerState::Inactive
    }

    pub(crate) fn activity(&self) -> &Arc<Activity> {
        &self.activity
    }

    /// Takes a new POA; fails with `OBJECT_NOT_EXIST` once the manager
    /// was destroyed with the last of its POAs.
    fn add_poa(&self, poa: &Arc<PoaInner>) -> Result<(), PoaError> {
        let mut poas = self.poas.lock().unwrap();
        if poas.destroyed {
            return Err(sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into());
        }
        poas.list.retain(|p| p.strong_count() > 0);
        poas.list.push(Arc::downgrade(poa));
        poas.live += 1;
        Ok(())
    }

    /// One of its POAs was destroyed. With the last one, the manager is
    /// destroyed too: it leaves the factory, and its identity may be used
    /// again.
    fn poa_destroyed(&self) {
        let mut poas = self.poas.lock().unwrap();
        poas.live = poas.live.saturating_sub(1);
        if poas.live > 0 || poas.destroyed {
            return;
        }
        poas.destroyed = true;
        trace::info!("POA manager {:?} destroyed", self.id);
        // Still under the lock, so that no POA joins it meanwhile.
        if let Some(server) = self.server.upgrade() {
            server.factory_remove(self);
        }
    }

    /// Stops the manager at ORB shutdown: held requests are dropped
    /// without a reply (the connections are closed with `CloseConnection`,
    /// which tells clients they were not processed).
    pub(crate) fn shut_down(&self) {
        let held = {
            let mut st = self.state.lock().unwrap();
            st.state = PoaManagerState::Inactive;
            std::mem::take(&mut st.held)
        };
        drop(held);
    }
}

// --- POA -----------------------------------------------------------------------

/// `PortableServer::POA`: an object adapter, with its active object map
/// and its child POAs.
///
/// A cheap handle; clones refer to the same POA.
#[derive(Clone)]
pub struct Poa {
    pub(crate) orb: Orb,
    pub(crate) inner: Arc<PoaInner>,
}

pub(crate) struct PoaInner {
    name: String,
    /// Names from below the RootPOA down to this POA; empty for the
    /// RootPOA.
    path: Vec<String>,
    policies: Policies,
    pub(crate) manager: PoaManager,
    parent: Option<Weak<PoaInner>>,
    /// The key up to the ObjectId.
    key_prefix: Vec<u8>,
    boot_id: [u8; BOOT_ID_LEN],
    activity: Arc<Activity>,
    state: Mutex<PoaState>,
    /// Signalled when an `incarnate` or `unknown_adapter` call ends.
    changed: Condvar,
    /// The queue of a SINGLE_THREAD_MODEL POA.
    serial: Mutex<Serial>,
    /// The tagged components the IOR interceptors added to its
    /// references.
    pub(crate) components: Vec<TaggedComponent>,
}

#[derive(Default)]
struct PoaState {
    destroyed: bool,
    children: HashMap<String, Arc<PoaInner>>,
    active: HashMap<ObjectId, Arc<dyn Servant>>,
    /// The ObjectId of each active servant, by address, for UNIQUE_ID.
    ids: HashMap<usize, ObjectId>,
    next_id: u32,
    default_servant: Option<Arc<dyn Servant>>,
    servant_manager: Option<ServantManager>,
    activator: Option<Arc<dyn AdapterActivator>>,
    /// ObjectIds whose `incarnate` call is under way.
    incarnating: HashSet<ObjectId>,
    /// Child names whose `unknown_adapter` call is under way.
    activating: HashSet<String>,
    /// Requests running on each object that was found in the active object
    /// map or incarnated.
    running: HashMap<ObjectId, usize>,
    /// Deactivated objects to etherealize once their last request ends.
    deactivated: HashMap<ObjectId, Arc<dyn Servant>>,
}

/// The queue of a SINGLE_THREAD_MODEL POA: at most one of its requests
/// runs at a time.
#[derive(Default)]
struct Serial {
    queue: VecDeque<Job>,
    /// Whether a request of the POA is running or about to.
    busy: bool,
    /// The thread running it.
    owner: Option<ThreadId>,
}

/// How a colocated call may enter a SINGLE_THREAD_MODEL POA.
pub(crate) enum SerialEntry {
    /// The thread already runs a request of the POA.
    Reentrant,
    /// The POA was idle and is now held by the thread until the guard is
    /// dropped.
    Acquired(SerialGuard),
    /// Another thread runs a request of the POA: queue the call.
    Busy,
}

pub(crate) struct SerialGuard {
    poa: Arc<PoaInner>,
    server: Arc<Server>,
}

impl Drop for SerialGuard {
    fn drop(&mut self) {
        self.poa.release_serial(&self.server);
    }
}

/// An object to etherealize: its POA, ObjectId and servant.
pub(crate) struct Etherealization {
    poa: Arc<PoaInner>,
    activator: Arc<dyn ServantActivator>,
    id: ObjectId,
    servant: Arc<dyn Servant>,
    cleanup_in_progress: bool,
}

impl Etherealization {
    pub(crate) fn poa(&self) -> &Arc<PoaInner> {
        &self.poa
    }

    /// Calls `etherealize`. `remaining` says whether the servant is still
    /// active under another ObjectId; `None` looks it up in the active
    /// object map.
    pub(crate) fn run(self, orb: &Orb, remaining: Option<bool>) {
        let remaining = remaining.unwrap_or_else(|| {
            let st = self.poa.state.lock().unwrap();
            st.active.values().any(|s| same_servant(s, &self.servant))
        });
        let adapter = Poa {
            orb: orb.clone(),
            inner: self.poa,
        };
        let _upcall = UpcallGuard::enter();
        let result = panic::catch_unwind(AssertUnwindSafe(|| {
            self.activator.etherealize(
                &self.id,
                &adapter,
                self.servant,
                self.cleanup_in_progress,
                remaining,
            );
        }));
        if result.is_err() {
            trace::warning!("servant activator panicked in etherealize");
        }
    }
}

/// Runs `etherealizations` in order, telling each whether its servant
/// still has a later one.
pub(crate) fn etherealize_all(orb: &Orb, etherealizations: Vec<Etherealization>) {
    let mut rest = etherealizations;
    while !rest.is_empty() {
        let e = rest.remove(0);
        let remaining = rest.iter().any(|r| same_servant(&r.servant, &e.servant));
        e.run(orb, Some(remaining));
    }
}

/// The servant found for a request, and what to do when it has run.
pub(crate) struct Resolved {
    pub(crate) servant: Arc<dyn Servant>,
    /// Counts the request against its object while it runs (RETAIN).
    _object: Option<ObjectGuard>,
    /// The locator and cookie for `postinvoke` (NON_RETAIN).
    locator: Option<(Arc<dyn ServantLocator>, Cookie)>,
}

impl Resolved {
    /// Calls `postinvoke` if a servant locator provided the servant.
    pub(crate) fn finish(self, adapter: &Poa, id: &[u8], operation: &str) {
        if let Some((locator, cookie)) = self.locator {
            let servant = self.servant;
            let result = panic::catch_unwind(AssertUnwindSafe(|| {
                locator.postinvoke(id, adapter, operation, cookie, servant);
            }));
            if result.is_err() {
                trace::warning!("servant locator panicked in postinvoke");
            }
        }
    }
}

pub(crate) struct ObjectGuard {
    poa: Arc<PoaInner>,
    server: Weak<Server>,
    id: ObjectId,
}

impl Drop for ObjectGuard {
    fn drop(&mut self) {
        let pending = {
            let mut st = self.poa.state.lock().unwrap();
            let Some(count) = st.running.get_mut(&self.id) else {
                return;
            };
            *count -= 1;
            if *count > 0 {
                return;
            }
            st.running.remove(&self.id);
            if st.destroyed {
                return;
            }
            let Some(servant) = st.deactivated.remove(&self.id) else {
                return;
            };
            let Some(ServantManager::Activator(activator)) = st.servant_manager.clone() else {
                return;
            };
            Etherealization {
                poa: Arc::clone(&self.poa),
                activator,
                id: std::mem::take(&mut self.id),
                servant,
                cleanup_in_progress: false,
            }
        };
        if let Some(server) = self.server.upgrade() {
            server.etherealize_later(pending);
        }
    }
}

impl fmt::Debug for Poa {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Poa")
            .field("name", &self.inner.name)
            .field("path", &self.inner.path)
            .field("policies", &self.inner.policies)
            .finish_non_exhaustive()
    }
}

impl PartialEq for Poa {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl Eq for Poa {}

fn servant_addr(servant: &Arc<dyn Servant>) -> usize {
    Arc::as_ptr(servant).cast::<()>() as usize
}

fn same_servant(a: &Arc<dyn Servant>, b: &Arc<dyn Servant>) -> bool {
    servant_addr(a) == servant_addr(b)
}

/// The objects taken out of a POA, in ObjectId order, and its servant
/// activator.
type Drained = (
    Vec<(ObjectId, Arc<dyn Servant>)>,
    Option<Arc<dyn ServantActivator>>,
);

/// Empties the active object map, deactivated objects included.
fn drain(st: &mut PoaState) -> Drained {
    st.ids.clear();
    let mut entries: Vec<_> = std::mem::take(&mut st.active)
        .into_iter()
        .chain(std::mem::take(&mut st.deactivated))
        .collect();
    entries.sort_by(|a, b| a.0.cmp(&b.0));
    let activator = match &st.servant_manager {
        Some(ServantManager::Activator(a)) => Some(Arc::clone(a)),
        _ => None,
    };
    (entries, activator)
}

fn catch<T>(
    what: &str,
    f: impl FnOnce() -> Result<T, ServantManagerError>,
) -> Result<T, ServantManagerError> {
    let _upcall = UpcallGuard::enter();
    panic::catch_unwind(AssertUnwindSafe(f)).unwrap_or_else(|_| {
        trace::warning!("servant manager panicked in {what}");
        Err(SystemException::new(SystemExceptionKind::UNKNOWN, 0, CompletionStatus::Maybe).into())
    })
}

impl PoaInner {
    #[allow(clippy::too_many_arguments)]
    fn new(
        name: String,
        path: Vec<String>,
        policies: Policies,
        manager: PoaManager,
        parent: Option<Weak<PoaInner>>,
        key_prefix: Vec<u8>,
        boot_id: [u8; BOOT_ID_LEN],
        components: Vec<TaggedComponent>,
    ) -> Result<Arc<PoaInner>, PoaError> {
        let poa = Arc::new(PoaInner {
            components,
            name,
            path,
            policies,
            manager,
            parent,
            key_prefix,
            boot_id,
            activity: Arc::default(),
            state: Mutex::default(),
            changed: Condvar::new(),
            serial: Mutex::default(),
        });
        poa.manager.inner.add_poa(&poa)?;
        Ok(poa)
    }

    pub(crate) fn new_root(
        manager: PoaManager,
        boot_id: [u8; BOOT_ID_LEN],
        components: Vec<TaggedComponent>,
    ) -> Result<Arc<PoaInner>, PoaError> {
        let mut key_prefix = vec![TRANSIENT_END];
        key_prefix.extend_from_slice(&boot_id);
        PoaInner::new(
            "RootPOA".into(),
            Vec::new(),
            Policies::root(),
            manager,
            None,
            key_prefix,
            boot_id,
            components,
        )
    }

    fn new_child(
        parent: &Arc<PoaInner>,
        name: &str,
        manager: PoaManager,
        policies: Policies,
        components: Vec<TaggedComponent>,
    ) -> Result<Arc<PoaInner>, PoaError> {
        let mut path = parent.path.clone();
        path.push(name.to_owned());
        let mut key_prefix = vec![PATH_START];
        for (i, n) in path.iter().enumerate() {
            if i > 0 {
                key_prefix.push(PATH_SEPARATOR);
            }
            key_prefix.extend_from_slice(n.as_bytes());
        }
        match policies.lifespan {
            Lifespan::Persistent => key_prefix.push(PERSISTENT_END),
            Lifespan::Transient => {
                key_prefix.push(TRANSIENT_END);
                key_prefix.extend_from_slice(&parent.boot_id);
            }
        }
        PoaInner::new(
            name.to_owned(),
            path,
            policies,
            manager,
            Some(Arc::downgrade(parent)),
            key_prefix,
            parent.boot_id,
            components,
        )
    }

    pub(crate) fn policies(&self) -> &Policies {
        &self.policies
    }

    /// Names from below the RootPOA down to this POA.
    pub(crate) fn path(&self) -> &[String] {
        &self.path
    }

    pub(crate) fn activity(&self) -> &Arc<Activity> {
        &self.activity
    }

    pub(crate) fn key(&self, id: &[u8]) -> Vec<u8> {
        let mut key = self.key_prefix.clone();
        key.extend_from_slice(id);
        key
    }

    /// The child `name`, or `Err(true)` if there is none but an adapter
    /// activator could create it.
    pub(crate) fn child(&self, name: &str) -> Result<Arc<PoaInner>, bool> {
        let st = self.state.lock().unwrap();
        if st.destroyed {
            return Err(false);
        }
        st.children.get(name).cloned().ok_or(st.activator.is_some())
    }

    /// The child `name`, created by the adapter activator if there is none.
    /// Calls user code: only on an upcall thread or in a colocated call.
    pub(crate) fn find_or_activate_child(
        self: &Arc<Self>,
        orb: &Orb,
        name: &str,
    ) -> Option<Arc<PoaInner>> {
        let mut st = self.state.lock().unwrap();
        loop {
            if st.destroyed {
                return None;
            }
            if let Some(child) = st.children.get(name) {
                return Some(Arc::clone(child));
            }
            let activator = st.activator.clone()?;
            if st.activating.contains(name) {
                st = self.changed.wait(st).unwrap();
                continue;
            }
            st.activating.insert(name.to_owned());
            drop(st);
            let parent = Poa {
                orb: orb.clone(),
                inner: Arc::clone(self),
            };
            let created = {
                let _upcall = UpcallGuard::enter();
                panic::catch_unwind(AssertUnwindSafe(|| {
                    activator.unknown_adapter(&parent, name)
                }))
                .unwrap_or_else(|_| {
                    trace::warning!("adapter activator panicked in unknown_adapter");
                    false
                })
            };
            st = self.state.lock().unwrap();
            st.activating.remove(name);
            self.changed.notify_all();
            if !created {
                return None;
            }
            return st.children.get(name).cloned();
        }
    }

    /// Whether a `LocateRequest` for `id` finds an object: one in the
    /// active object map, or any with a default servant or a servant
    /// manager (which are not asked).
    pub(crate) fn has_object(&self, id: &[u8]) -> bool {
        let st = self.state.lock().unwrap();
        !st.destroyed
            && (st.active.contains_key(id)
                || self.policies.request_processing != RequestProcessing::ActiveObjectMapOnly)
    }

    pub(crate) fn is_destroyed(&self) -> bool {
        self.state.lock().unwrap().destroyed
    }

    /// The servant for a request to `id`: from the active object map, the
    /// default servant or the servant manager. Calls user code.
    pub(crate) fn resolve(
        self: &Arc<Self>,
        adapter: &Poa,
        server: &Arc<Server>,
        id: &[u8],
        operation: &str,
    ) -> Result<Resolved, ServantManagerError> {
        let retain = self.policies.servant_retention == ServantRetention::Retain;
        let mut st = self.state.lock().unwrap();
        loop {
            if st.destroyed {
                return Err(no_such_object().into());
            }
            if retain {
                if let Some(servant) = st.active.get(id).cloned() {
                    return Ok(self.resolved(&mut st, server, id, servant));
                }
            }
            match self.policies.request_processing {
                RequestProcessing::ActiveObjectMapOnly => return Err(no_such_object().into()),
                RequestProcessing::UseDefaultServant => {
                    let servant = st.default_servant.clone().ok_or_else(|| {
                        sys(SystemExceptionKind::OBJ_ADAPTER, minor::NO_DEFAULT_SERVANT)
                    })?;
                    return Ok(Resolved {
                        servant,
                        _object: None,
                        locator: None,
                    });
                }
                RequestProcessing::UseServantManager => {}
            }
            match st.servant_manager.clone() {
                None => {
                    return Err(
                        sys(SystemExceptionKind::OBJ_ADAPTER, minor::NO_SERVANT_MANAGER).into(),
                    );
                }
                Some(ServantManager::Locator(locator)) => {
                    drop(st);
                    let (servant, cookie) =
                        catch("preinvoke", || locator.preinvoke(id, adapter, operation))?;
                    return Ok(Resolved {
                        servant,
                        _object: None,
                        locator: Some((locator, cookie)),
                    });
                }
                Some(ServantManager::Activator(activator)) => {
                    if st.incarnating.contains(id) {
                        st = self.changed.wait(st).unwrap();
                        continue;
                    }
                    st.incarnating.insert(id.to_vec());
                    drop(st);
                    let result = catch("incarnate", || activator.incarnate(id, adapter));
                    st = self.state.lock().unwrap();
                    st.incarnating.remove(id);
                    self.changed.notify_all();
                    let servant = result?;
                    if st.destroyed {
                        return Err(no_such_object().into());
                    }
                    match self.activate(&mut st, id.to_vec(), Arc::clone(&servant)) {
                        Ok(()) => return Ok(self.resolved(&mut st, server, id, servant)),
                        // Activated by the application meanwhile.
                        Err(PoaError::ObjectAlreadyActive) => continue,
                        Err(_) => {
                            return Err(sys(
                                SystemExceptionKind::OBJ_ADAPTER,
                                minor::SERVANT_ALREADY_ACTIVE,
                            )
                            .into());
                        }
                    }
                }
            }
        }
    }

    fn resolved(
        self: &Arc<Self>,
        st: &mut PoaState,
        server: &Arc<Server>,
        id: &[u8],
        servant: Arc<dyn Servant>,
    ) -> Resolved {
        *st.running.entry(id.to_vec()).or_insert(0) += 1;
        Resolved {
            servant,
            _object: Some(ObjectGuard {
                poa: Arc::clone(self),
                server: Arc::downgrade(server),
                id: id.to_vec(),
            }),
            locator: None,
        }
    }

    /// Marks this POA and its descendants destroyed, children first, and
    /// drops their servants. With `etherealize`, returns the objects to
    /// etherealize.
    pub(crate) fn destroy_tree(self: &Arc<Self>, etherealize: bool) -> Vec<Etherealization> {
        let (children, drained, rest) = {
            let mut st = self.state.lock().unwrap();
            if st.destroyed {
                return Vec::new();
            }
            st.destroyed = true;
            let children = std::mem::take(&mut st.children);
            let drained = drain(&mut st);
            // Dropped once the lock is released.
            let rest = (
                st.default_servant.take(),
                st.servant_manager.take(),
                st.activator.take(),
            );
            (children, drained, rest)
        };
        drop(rest);
        let mut all = Vec::new();
        for child in children.into_values() {
            all.extend(child.destroy_tree(etherealize));
        }
        self.manager.inner.poa_destroyed();
        all.extend(self.etherealizations(drained, etherealize));
        all
    }

    /// Deactivates every object for `POAManager::deactivate(etherealize)`.
    fn take_for_etherealize(self: &Arc<Self>) -> Vec<Etherealization> {
        let drained = {
            let mut st = self.state.lock().unwrap();
            if st.destroyed {
                return Vec::new();
            }
            drain(&mut st)
        };
        self.etherealizations(drained, true)
    }

    fn etherealizations(
        self: &Arc<Self>,
        (entries, activator): Drained,
        etherealize: bool,
    ) -> Vec<Etherealization> {
        let Some(activator) = activator.filter(|_| etherealize) else {
            return Vec::new();
        };
        entries
            .into_iter()
            .map(|(id, servant)| Etherealization {
                poa: Arc::clone(self),
                activator: Arc::clone(&activator),
                id,
                servant,
                cleanup_in_progress: true,
            })
            .collect()
    }

    fn generate_id(&self, st: &mut PoaState) -> ObjectId {
        let serial = st.next_id;
        st.next_id = st.next_id.wrapping_add(1);
        let mut id = self.boot_id.to_vec();
        id.extend_from_slice(&serial.to_be_bytes());
        id
    }

    fn activate(
        &self,
        st: &mut PoaState,
        id: ObjectId,
        servant: Arc<dyn Servant>,
    ) -> Result<(), PoaError> {
        if st.active.contains_key(&id) {
            return Err(PoaError::ObjectAlreadyActive);
        }
        if self.policies.id_uniqueness == IdUniqueness::Unique {
            let addr = servant_addr(&servant);
            if st.ids.contains_key(&addr) {
                return Err(PoaError::ServantAlreadyActive);
            }
            st.ids.insert(addr, id.clone());
        }
        trace::debug!(
            "activated object {} in POA {:?}",
            trace::IdSummary(&id),
            self.name
        );
        st.active.insert(id, servant);
        Ok(())
    }

    // --- SINGLE_THREAD_MODEL ---------------------------------------------------

    /// Queues `job` behind the other requests of this POA.
    pub(crate) fn run_serially(self: &Arc<Self>, server: &Arc<Server>, job: Job) {
        let mut serial = self.serial.lock().unwrap();
        serial.queue.push_back(job);
        if !serial.busy {
            serial.busy = true;
            drop(serial);
            self.submit_serial(server);
        }
    }

    fn submit_serial(self: &Arc<Self>, server: &Arc<Server>) {
        let poa = Arc::clone(self);
        let srv = Arc::clone(server);
        server.submit(Box::new(move || poa.run_one_serially(&srv)));
    }

    /// Runs the first queued request; on an upcall thread.
    fn run_one_serially(self: &Arc<Self>, server: &Arc<Server>) {
        let job = {
            let mut serial = self.serial.lock().unwrap();
            let Some(job) = serial.queue.pop_front() else {
                serial.busy = false;
                return;
            };
            serial.owner = Some(thread::current().id());
            job
        };
        let _ = panic::catch_unwind(AssertUnwindSafe(job));
        self.release_serial(server);
    }

    fn release_serial(self: &Arc<Self>, server: &Arc<Server>) {
        let mut serial = self.serial.lock().unwrap();
        serial.owner = None;
        if serial.queue.is_empty() {
            serial.busy = false;
        } else {
            drop(serial);
            self.submit_serial(server);
        }
    }

    /// Lets a colocated call run on the calling thread if that keeps the
    /// requests of the POA one at a time.
    pub(crate) fn enter_serially(self: &Arc<Self>, server: &Arc<Server>) -> SerialEntry {
        let me = thread::current().id();
        let mut serial = self.serial.lock().unwrap();
        if serial.owner == Some(me) {
            SerialEntry::Reentrant
        } else if serial.busy {
            SerialEntry::Busy
        } else {
            serial.busy = true;
            serial.owner = Some(me);
            SerialEntry::Acquired(SerialGuard {
                poa: Arc::clone(self),
                server: Arc::clone(server),
            })
        }
    }
}

/// How a request's object key splits up.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ParsedKey<'a> {
    /// A key made by a POA of this format: the path of POA names, the boot
    /// id for a TRANSIENT POA, and the ObjectId.
    Poa {
        path: Vec<&'a str>,
        boot_id: Option<&'a [u8]>,
        id: &'a [u8],
    },
    /// Any other key, looked up among the objects registered by name.
    Simple(&'a [u8]),
    /// A key that starts like a POA key but is cut short or not UTF-8.
    Invalid,
}

pub(crate) fn parse_key(key: &[u8]) -> ParsedKey<'_> {
    fn transient<'a>(rest: &'a [u8], path: Vec<&'a str>) -> ParsedKey<'a> {
        if rest.len() < BOOT_ID_LEN {
            return ParsedKey::Invalid;
        }
        let (boot_id, id) = rest.split_at(BOOT_ID_LEN);
        ParsedKey::Poa {
            path,
            boot_id: Some(boot_id),
            id,
        }
    }
    match key.first() {
        Some(&TRANSIENT_END) => transient(&key[1..], Vec::new()),
        Some(&PATH_START) => {
            let rest = &key[1..];
            let Some(end) = rest
                .iter()
                .position(|&b| b == PERSISTENT_END || b == TRANSIENT_END)
            else {
                return ParsedKey::Invalid;
            };
            let mut path = Vec::new();
            for name in rest[..end].split(|&b| b == PATH_SEPARATOR) {
                match std::str::from_utf8(name) {
                    Ok(name) if !name.is_empty() => path.push(name),
                    _ => return ParsedKey::Invalid,
                }
            }
            if rest[end] == PERSISTENT_END {
                ParsedKey::Poa {
                    path,
                    boot_id: None,
                    id: &rest[end + 1..],
                }
            } else {
                transient(&rest[end + 1..], path)
            }
        }
        _ => ParsedKey::Simple(key),
    }
}

impl Poa {
    fn state(&self) -> Result<MutexGuard<'_, PoaState>, PoaError> {
        let st = self.inner.state.lock().unwrap();
        if st.destroyed {
            return Err(sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into());
        }
        Ok(st)
    }

    fn server(&self) -> Result<Arc<Server>, PoaError> {
        self.orb
            .server()
            .ok_or_else(|| sys(SystemExceptionKind::OBJECT_NOT_EXIST, 0).into())
    }

    fn handle(&self, inner: Arc<PoaInner>) -> Poa {
        Poa {
            orb: self.orb.clone(),
            inner,
        }
    }

    /// `the_name`.
    pub fn the_name(&self) -> &str {
        &self.inner.name
    }

    /// `the_parent`; `None` for the RootPOA.
    pub fn the_parent(&self) -> Option<Poa> {
        let parent = self.inner.parent.as_ref()?.upgrade()?;
        Some(self.handle(parent))
    }

    /// `the_children`, in no particular order.
    pub fn the_children(&self) -> Vec<Poa> {
        let st = self.inner.state.lock().unwrap();
        st.children
            .values()
            .map(|c| self.handle(Arc::clone(c)))
            .collect()
    }

    /// `the_POAManager`.
    pub fn the_poa_manager(&self) -> PoaManager {
        self.inner.manager.clone()
    }

    /// `the_POAManagerFactory`: the ORB's factory of POA managers, the
    /// same from every POA.
    pub fn the_poa_manager_factory(&self) -> PoaManagerFactory {
        PoaManagerFactory {
            orb: self.orb.clone(),
        }
    }

    /// The policies of the POA.
    pub fn policies(&self) -> Policies {
        self.inner.policies
    }

    /// `create_POA`: a child POA named `name`, managed by `manager` or, if
    /// `None`, by a new manager with an identity the ORB chooses. Policies
    /// not in `policies` take their default values (not those of this
    /// POA). Inconsistent policies fail with `InvalidPolicy`, a name
    /// containing NUL with `BAD_PARAM`, and a manager destroyed with the
    /// last of its POAs with `OBJECT_NOT_EXIST`.
    pub fn create_poa(
        &self,
        name: &str,
        manager: Option<&PoaManager>,
        policies: &[Policy],
    ) -> Result<Poa, PoaError> {
        if name.is_empty() || name.contains('\0') {
            return Err(sys(SystemExceptionKind::BAD_PARAM, 0).into());
        }
        let policies = Policies::from_list(policies).map_err(PoaError::InvalidPolicy)?;
        let server = self.server()?;
        // The IOR interceptors run before the POA's lock is taken.
        let components = if self.state()?.children.contains_key(name) {
            Vec::new()
        } else {
            let mut path = self.inner.path.clone();
            path.push(name.to_owned());
            crate::pi::establish_components(&self.orb, &policies, &path)
        };
        let mut st = self.state()?;
        if st.children.contains_key(name) {
            return Err(PoaError::AdapterAlreadyExists);
        }
        let manager = match manager {
            Some(m) => m.clone(),
            None => server.create_implicit_manager(),
        };
        if policies.lifespan == Lifespan::Persistent && !server.has_fixed_port() {
            trace::warning!(
                "PERSISTENT POA {name:?} on an endpoint without a fixed port: \
                 its references will not survive a restart"
            );
        }
        let child = PoaInner::new_child(&self.inner, name, manager, policies, components)?;
        trace::info!("created POA {name:?} under {:?}", self.inner.name);
        st.children.insert(name.to_owned(), Arc::clone(&child));
        Ok(self.handle(child))
    }

    /// `find_POA`: the child `name`. With `activate_it`, a missing child
    /// is asked of the adapter activator, if the POA has one.
    pub fn find_poa(&self, name: &str, activate_it: bool) -> Result<Poa, PoaError> {
        drop(self.state()?);
        let child = if activate_it {
            self.inner.find_or_activate_child(&self.orb, name)
        } else {
            self.inner.child(name).ok()
        };
        child
            .map(|c| self.handle(c))
            .ok_or(PoaError::AdapterNonExistent)
    }

    /// `the_activator`.
    pub fn the_activator(&self) -> Option<Arc<dyn AdapterActivator>> {
        self.inner.state.lock().unwrap().activator.clone()
    }

    /// `the_activator` (set): the adapter activator for unknown children,
    /// or none.
    pub fn set_the_activator(
        &self,
        activator: Option<Arc<dyn AdapterActivator>>,
    ) -> Result<(), PoaError> {
        let old = std::mem::replace(&mut self.state()?.activator, activator);
        drop(old);
        Ok(())
    }

    /// `get_servant_manager` (USE_SERVANT_MANAGER only).
    pub fn get_servant_manager(&self) -> Result<Option<ServantManager>, PoaError> {
        if self.inner.policies.request_processing != RequestProcessing::UseServantManager {
            return Err(PoaError::WrongPolicy);
        }
        Ok(self.state()?.servant_manager.clone())
    }

    /// `set_servant_manager` (USE_SERVANT_MANAGER only): a
    /// [`ServantActivator`] with RETAIN, a [`ServantLocator`] with
    /// NON_RETAIN (otherwise `OBJ_ADAPTER`). It can be set once
    /// (`BAD_INV_ORDER` after that).
    pub fn set_servant_manager(&self, manager: ServantManager) -> Result<(), PoaError> {
        let p = self.inner.policies;
        if p.request_processing != RequestProcessing::UseServantManager {
            return Err(PoaError::WrongPolicy);
        }
        let fits = matches!(
            (&manager, p.servant_retention),
            (ServantManager::Activator(_), ServantRetention::Retain)
                | (ServantManager::Locator(_), ServantRetention::NonRetain)
        );
        if !fits {
            return Err(sys(SystemExceptionKind::OBJ_ADAPTER, minor::NO_SERVANT_MANAGER).into());
        }
        let mut st = self.state()?;
        if st.servant_manager.is_some() {
            return Err(sys(
                SystemExceptionKind::BAD_INV_ORDER,
                minor::SERVANT_MANAGER_ALREADY_SET,
            )
            .into());
        }
        st.servant_manager = Some(manager);
        Ok(())
    }

    /// `get_servant`: the default servant (USE_DEFAULT_SERVANT only).
    pub fn get_servant(&self) -> Result<Arc<dyn Servant>, PoaError> {
        if self.inner.policies.request_processing != RequestProcessing::UseDefaultServant {
            return Err(PoaError::WrongPolicy);
        }
        self.state()?
            .default_servant
            .clone()
            .ok_or(PoaError::NoServant)
    }

    /// `set_servant`: the default servant (USE_DEFAULT_SERVANT only), which
    /// runs the requests for objects not in the active object map.
    pub fn set_servant(&self, servant: Arc<dyn Servant>) -> Result<(), PoaError> {
        if self.inner.policies.request_processing != RequestProcessing::UseDefaultServant {
            return Err(PoaError::WrongPolicy);
        }
        let old = self.state()?.default_servant.replace(servant);
        drop(old);
        Ok(())
    }

    /// `destroy`: destroys the POA and its descendants and drops their
    /// servants. Requests already running finish; later ones get
    /// `OBJECT_NOT_EXIST`. With `etherealize`, the objects of POAs with a
    /// [`ServantActivator`] are etherealized once their requests have
    /// finished. With `wait_for_completion`, returns once the running
    /// requests of this POA have finished and the objects are etherealized
    /// (`BAD_INV_ORDER` when called from a servant).
    pub fn destroy(&self, etherealize: bool, wait_for_completion: bool) -> Result<(), PoaError> {
        check_wait(wait_for_completion)?;
        trace::info!("destroying POA {:?}", self.inner.name);
        if let Some(parent) = self.inner.parent.as_ref().and_then(Weak::upgrade) {
            let mut st = parent.state.lock().unwrap();
            if st
                .children
                .get(&self.inner.name)
                .is_some_and(|c| Arc::ptr_eq(c, &self.inner))
            {
                st.children.remove(&self.inner.name);
            }
        }
        let work = self.inner.destroy_tree(etherealize);
        if wait_for_completion {
            self.inner.activity.wait_idle();
        }
        if let Some(server) = self.orb.server() {
            server.etherealize(work, wait_for_completion);
        }
        Ok(())
    }

    fn check_retain(&self) -> Result<(), PoaError> {
        if self.inner.policies.servant_retention != ServantRetention::Retain {
            return Err(PoaError::WrongPolicy);
        }
        Ok(())
    }

    /// `activate_object`: activates `servant` under a new ObjectId
    /// (SYSTEM_ID and RETAIN only).
    pub fn activate_object(&self, servant: Arc<dyn Servant>) -> Result<ObjectId, PoaError> {
        self.check_retain()?;
        if self.inner.policies.id_assignment != IdAssignment::System {
            return Err(PoaError::WrongPolicy);
        }
        let mut st = self.state()?;
        let id = self.inner.generate_id(&mut st);
        self.inner.activate(&mut st, id.clone(), servant)?;
        Ok(id)
    }

    /// `activate_object_with_id` (RETAIN only).
    pub fn activate_object_with_id(
        &self,
        id: &[u8],
        servant: Arc<dyn Servant>,
    ) -> Result<(), PoaError> {
        self.check_retain()?;
        let mut st = self.state()?;
        self.inner.activate(&mut st, id.to_vec(), servant)
    }

    /// `deactivate_object` (RETAIN only): requests already running finish;
    /// later ones get `OBJECT_NOT_EXIST`, or incarnate the object again
    /// with a [`ServantActivator`]. The activator's `etherealize` is called
    /// on an upcall thread once the running requests have finished; this
    /// does not wait for it.
    pub fn deactivate_object(&self, id: &[u8]) -> Result<(), PoaError> {
        self.check_retain()?;
        let (servant, now) = {
            let mut st = self.state()?;
            let servant = st.active.remove(id).ok_or(PoaError::ObjectNotActive)?;
            trace::debug!(
                "deactivated object {} in POA {:?}",
                trace::IdSummary(id),
                self.inner.name
            );
            let addr = servant_addr(&servant);
            if st.ids.get(&addr).is_some_and(|i| i == id) {
                st.ids.remove(&addr);
            }
            match st.servant_manager.clone() {
                Some(ServantManager::Activator(activator)) => {
                    if st.running.contains_key(id) {
                        st.deactivated.insert(id.to_vec(), servant);
                        (None, None)
                    } else {
                        (None, Some((activator, servant)))
                    }
                }
                _ => (Some(servant), None),
            }
        };
        drop(servant);
        if let Some((activator, servant)) = now {
            let work = Etherealization {
                poa: Arc::clone(&self.inner),
                activator,
                id: id.to_vec(),
                servant,
                cleanup_in_progress: false,
            };
            if let Some(server) = self.orb.server() {
                server.etherealize_later(work);
            }
        }
        Ok(())
    }

    /// `servant_to_id`: the ObjectId of an active servant (UNIQUE_ID), a
    /// new one with IMPLICIT_ACTIVATION, or, for the default servant inside
    /// a request it runs, the ObjectId of that request.
    pub fn servant_to_id(&self, servant: &Arc<dyn Servant>) -> Result<ObjectId, PoaError> {
        let p = self.inner.policies;
        let retain = p.servant_retention == ServantRetention::Retain;
        let default_servant = p.request_processing == RequestProcessing::UseDefaultServant;
        if !default_servant
            && !(retain
                && (p.id_uniqueness == IdUniqueness::Unique
                    || p.implicit_activation == ImplicitActivation::Implicit))
        {
            return Err(PoaError::WrongPolicy);
        }
        let mut st = self.state()?;
        if retain && p.id_uniqueness == IdUniqueness::Unique {
            if let Some(id) = st.ids.get(&servant_addr(servant)) {
                return Ok(id.clone());
            }
        }
        if retain && p.implicit_activation == ImplicitActivation::Implicit {
            let id = self.inner.generate_id(&mut st);
            self.inner
                .activate(&mut st, id.clone(), Arc::clone(servant))?;
            return Ok(id);
        }
        let is_default = st
            .default_servant
            .as_ref()
            .is_some_and(|d| same_servant(d, servant));
        drop(st);
        if is_default {
            let current = CURRENT.with(|c| {
                c.borrow()
                    .last()
                    .filter(|e| e.poa == *self && same_servant(&e.servant, servant))
                    .map(|e| e.id.clone())
            });
            if let Some(id) = current {
                return Ok(id);
            }
        }
        Err(PoaError::ServantNotActive)
    }

    /// `servant_to_reference`, activating the servant like
    /// [`servant_to_id`](Poa::servant_to_id).
    pub fn servant_to_reference(&self, servant: &Arc<dyn Servant>) -> Result<ObjectRef, PoaError> {
        let id = self.servant_to_id(servant)?;
        self.reference(&id, servant.repository_id())
    }

    /// `id_to_servant` (RETAIN or USE_DEFAULT_SERVANT): the servant active
    /// under `id`, or else the default servant.
    pub fn id_to_servant(&self, id: &[u8]) -> Result<Arc<dyn Servant>, PoaError> {
        let p = self.inner.policies;
        let retain = p.servant_retention == ServantRetention::Retain;
        let default_servant = p.request_processing == RequestProcessing::UseDefaultServant;
        if !retain && !default_servant {
            return Err(PoaError::WrongPolicy);
        }
        let st = self.state()?;
        let active = retain.then(|| st.active.get(id).cloned()).flatten();
        active
            .or_else(|| st.default_servant.clone().filter(|_| default_servant))
            .ok_or(PoaError::ObjectNotActive)
    }

    /// `id_to_reference` (RETAIN only): a reference to the active object
    /// `id`, with the servant's repository id as type id.
    pub fn id_to_reference(&self, id: &[u8]) -> Result<ObjectRef, PoaError> {
        self.check_retain()?;
        let servant = self
            .state()?
            .active
            .get(id)
            .cloned()
            .ok_or(PoaError::ObjectNotActive)?;
        self.reference(id, servant.repository_id())
    }

    /// `create_reference`: a reference with a new ObjectId and the type id
    /// `intf`, without activating anything (SYSTEM_ID only).
    pub fn create_reference(&self, intf: &str) -> Result<ObjectRef, PoaError> {
        if self.inner.policies.id_assignment != IdAssignment::System {
            return Err(PoaError::WrongPolicy);
        }
        let id = {
            let mut st = self.state()?;
            self.inner.generate_id(&mut st)
        };
        self.reference(&id, intf)
    }

    /// `create_reference_with_id`: a reference to `id` with the type id
    /// `intf`, without activating anything.
    pub fn create_reference_with_id(&self, id: &[u8], intf: &str) -> Result<ObjectRef, PoaError> {
        drop(self.state()?);
        self.reference(id, intf)
    }

    /// `reference_to_id`: the ObjectId of a reference made by this POA
    /// (`WrongAdapter` for any other).
    pub fn reference_to_id(&self, reference: &ObjectRef) -> Result<ObjectId, PoaError> {
        drop(self.state()?);
        let prefix = &self.inner.key_prefix;
        reference
            .ior()
            .iiop_profiles()
            .filter_map(Result::ok)
            .map(|p: IiopProfile| p.object_key)
            .find(|key| key.starts_with(prefix))
            .map(|key| key[prefix.len()..].to_vec())
            .ok_or(PoaError::WrongAdapter)
    }

    /// `reference_to_servant`.
    pub fn reference_to_servant(
        &self,
        reference: &ObjectRef,
    ) -> Result<Arc<dyn Servant>, PoaError> {
        let id = self.reference_to_id(reference)?;
        self.id_to_servant(&id)
    }

    pub(crate) fn reference(&self, id: &[u8], type_id: &str) -> Result<ObjectRef, PoaError> {
        let server = self.server()?;
        let ior = server.ior(type_id, self.inner.key(id), &self.inner.components)?;
        Ok(ObjectRef::new(self.orb.clone(), ior))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn keys_parse_back() {
        let boot = [1, 2, 3, 4, 5, 6, 7, 8];
        assert_eq!(
            parse_key(b"\xffpersistent_user\x00obj"),
            ParsedKey::Poa {
                path: vec!["persistent_user"],
                boot_id: None,
                id: b"obj"
            }
        );
        assert_eq!(
            parse_key(b"\xffa\xffnested\x00"),
            ParsedKey::Poa {
                path: vec!["a", "nested"],
                boot_id: None,
                id: b""
            }
        );
        let mut transient = b"\xffchild\xfe".to_vec();
        transient.extend_from_slice(&boot);
        transient.extend_from_slice(b"id");
        assert_eq!(
            parse_key(&transient),
            ParsedKey::Poa {
                path: vec!["child"],
                boot_id: Some(&boot),
                id: b"id"
            }
        );
        let mut root = vec![0xfe];
        root.extend_from_slice(&boot);
        assert_eq!(
            parse_key(&root),
            ParsedKey::Poa {
                path: vec![],
                boot_id: Some(&boot),
                id: b""
            }
        );
        assert_eq!(parse_key(b"NameService"), ParsedKey::Simple(b"NameService"));
        assert_eq!(parse_key(b""), ParsedKey::Simple(b""));
        assert_eq!(parse_key(b"\xffno-end"), ParsedKey::Invalid);
        assert_eq!(parse_key(b"\xfe\x01\x02"), ParsedKey::Invalid);
        assert_eq!(parse_key(b"\xff\xff\x00x"), ParsedKey::Invalid);
    }

    #[test]
    fn policy_lists() {
        assert_eq!(Policies::from_list(&[]), Ok(Policies::default()));
        let p = Policies::from_list(&[
            Policy::Lifespan(Lifespan::Persistent),
            Policy::IdAssignment(IdAssignment::User),
        ])
        .unwrap();
        assert_eq!(p.lifespan, Lifespan::Persistent);
        assert_eq!(p.id_assignment, IdAssignment::User);
        assert_eq!(
            Policies::from_list(&[
                Policy::Lifespan(Lifespan::Persistent),
                Policy::Lifespan(Lifespan::Transient),
            ]),
            Err(1)
        );
        assert_eq!(
            Policies::from_list(&[
                Policy::IdAssignment(IdAssignment::User),
                Policy::ImplicitActivation(ImplicitActivation::Implicit),
            ]),
            Err(1)
        );
        assert_eq!(
            Policies::from_list(&[Policy::Thread(ThreadPolicy::MainThread)])
                .unwrap()
                .thread,
            ThreadPolicy::MainThread
        );
        // NON_RETAIN needs a default servant or a servant manager.
        assert_eq!(
            Policies::from_list(&[Policy::ServantRetention(ServantRetention::NonRetain)]),
            Err(0)
        );
        assert!(
            Policies::from_list(&[
                Policy::ServantRetention(ServantRetention::NonRetain),
                Policy::RequestProcessing(RequestProcessing::UseServantManager),
            ])
            .is_ok()
        );
        // A default servant serves many ObjectIds.
        assert_eq!(
            Policies::from_list(&[Policy::RequestProcessing(
                RequestProcessing::UseDefaultServant
            )]),
            Err(0)
        );
        assert!(
            Policies::from_list(&[
                Policy::RequestProcessing(RequestProcessing::UseDefaultServant),
                Policy::IdUniqueness(IdUniqueness::Multiple),
            ])
            .is_ok()
        );
        assert_eq!(
            Policies::from_list(&[
                Policy::ImplicitActivation(ImplicitActivation::Implicit),
                Policy::ServantRetention(ServantRetention::NonRetain),
                Policy::RequestProcessing(RequestProcessing::UseServantManager),
            ]),
            Err(0)
        );
    }
}