summaryrefslogtreecommitdiff
path: root/TAO/tao/POA.cpp
blob: 36a000fdfd56e820d7f06268d8655c7dbe15b16d (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
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
// @(#) $Id$

// CORBA
#include "tao/corba.h"

// POA
#include "tao/POA.h"

// Exception macros
#include "tao/poa_macros.h"

// auto_ptr class
#include "ace/Auto_Ptr.h"

// This is the maximum space require to convert the ulong into a
// string.
const int TAO_POA::max_space_required_for_ulong = 24;

// This is the maximum space require to convert the ulong into a
// string.
const int TAO_Creation_Time::max_space_required_for_two_ulong_to_hex = 8 * 2;

TAO_Thread_Policy::TAO_Thread_Policy (PortableServer::ThreadPolicyValue value,
                                      PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Thread_Policy::TAO_Thread_Policy (const TAO_Thread_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::ThreadPolicyValue
TAO_Thread_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Thread_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Thread_Policy> new_policy (new TAO_Thread_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);

  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Thread_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Thread_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_Lifespan_Policy::TAO_Lifespan_Policy (PortableServer::LifespanPolicyValue value,
                                          PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Lifespan_Policy::TAO_Lifespan_Policy (const TAO_Lifespan_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::LifespanPolicyValue
TAO_Lifespan_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Lifespan_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Lifespan_Policy> new_policy (new TAO_Lifespan_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Lifespan_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Lifespan_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_Id_Uniqueness_Policy::TAO_Id_Uniqueness_Policy (PortableServer::IdUniquenessPolicyValue value,
                                                    PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Id_Uniqueness_Policy::TAO_Id_Uniqueness_Policy (const TAO_Id_Uniqueness_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::IdUniquenessPolicyValue
TAO_Id_Uniqueness_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Id_Uniqueness_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Id_Uniqueness_Policy> new_policy (new TAO_Id_Uniqueness_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Id_Uniqueness_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Id_Uniqueness_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_Id_Assignment_Policy::TAO_Id_Assignment_Policy (PortableServer::IdAssignmentPolicyValue value,
                                                    PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Id_Assignment_Policy::TAO_Id_Assignment_Policy (const TAO_Id_Assignment_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::IdAssignmentPolicyValue
TAO_Id_Assignment_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Id_Assignment_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Id_Assignment_Policy> new_policy (new TAO_Id_Assignment_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Id_Assignment_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Id_Assignment_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_Implicit_Activation_Policy::TAO_Implicit_Activation_Policy (PortableServer::ImplicitActivationPolicyValue value,
                                                                PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Implicit_Activation_Policy::TAO_Implicit_Activation_Policy (const TAO_Implicit_Activation_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::ImplicitActivationPolicyValue
TAO_Implicit_Activation_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Implicit_Activation_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Implicit_Activation_Policy> new_policy (new TAO_Implicit_Activation_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Implicit_Activation_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Implicit_Activation_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_Servant_Retention_Policy::TAO_Servant_Retention_Policy (PortableServer::ServantRetentionPolicyValue value,
                                                            PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Servant_Retention_Policy::TAO_Servant_Retention_Policy (const TAO_Servant_Retention_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::ServantRetentionPolicyValue
TAO_Servant_Retention_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Servant_Retention_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Servant_Retention_Policy> new_policy (new TAO_Servant_Retention_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Servant_Retention_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Servant_Retention_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_Request_Processing_Policy::TAO_Request_Processing_Policy (PortableServer::RequestProcessingPolicyValue value,
                                                              PortableServer::POA_ptr poa)
  : value_ (value),
    poa_ (PortableServer::POA::_duplicate (poa))
{
}

TAO_Request_Processing_Policy::TAO_Request_Processing_Policy (const TAO_Request_Processing_Policy &rhs)
  : value_ (rhs.value_),
    poa_ (PortableServer::POA::_duplicate (rhs.poa_.in ()))
{
}

PortableServer::RequestProcessingPolicyValue
TAO_Request_Processing_Policy::value (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);
  return this->value_;
}

PortableServer::Policy_ptr
TAO_Request_Processing_Policy::copy (CORBA::Environment &env)
{
  auto_ptr<TAO_Request_Processing_Policy> new_policy (new TAO_Request_Processing_Policy (*this));

  PortableServer::Policy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::Policy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation object
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_Request_Processing_Policy::destroy (CORBA::Environment &env)
{
  // Remove self from POA
  //
  // Note that there is no real error checking here as we can't do
  // much about errors here anyway
  //
  PortableServer::POA_var poa = this->_default_POA (env);
  if (env.exception () == 0)
    {
      PortableServer::ObjectId_var id = poa->servant_to_id (this, env);
      if (env.exception () == 0)
        poa->deactivate_object (id.in (), env);
    }

  // Commit suicide: must have been dynamically allocated
  delete this;
}

PortableServer::POA_ptr
TAO_Request_Processing_Policy::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

TAO_POA_Policies::TAO_POA_Policies (void)
  :  thread_ (PortableServer::ORB_CTRL_MODEL),
     lifespan_ (PortableServer::TRANSIENT),
     id_uniqueness_ (PortableServer::UNIQUE_ID),
     id_assignment_ (PortableServer::SYSTEM_ID),
     implicit_activation_ (PortableServer::NO_IMPLICIT_ACTIVATION),
     servant_retention_ (PortableServer::RETAIN),
     request_processing_ (PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY)
{
}

void
TAO_POA_Policies::parse_policies (const PortableServer::PolicyList &policies,
                                  CORBA::Environment &env)
{
  for (CORBA::ULong i = 0;
       i < policies.length () && env.exception () == 0;
       i++)
    {
      this->parse_policy (policies[i], env);
    }

  if (this->validity_check () == -1)
    {
      CORBA::Exception *exception = new PortableServer::POA::InvalidPolicy;
      env.exception (exception);
      return;
    }
}

int
TAO_POA_Policies::validity_check (void)
{
  // The NON_RETAIN policy requires either the USE_DEFAULT_SERVANT or
  // USE_SERVANT_MANAGER policies.
  if (this->servant_retention_ == PortableServer::NON_RETAIN)
    if (this->request_processing_ != PortableServer::USE_SERVANT_MANAGER &&
        this->request_processing_ != PortableServer::USE_DEFAULT_SERVANT)
      return -1;

  // USE_ACTIVE_OBJECT_MAP_ONLY requires the RETAIN policy.
  if (this->request_processing_ == PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY)
    if (this->servant_retention_ != PortableServer::RETAIN)
      return -1;

  // USE_DEFAULT_SERVANT requires the MULTIPLE_ID policy.
  if (this->request_processing_ == PortableServer::USE_DEFAULT_SERVANT)
    if (this->id_uniqueness_ != PortableServer::MULTIPLE_ID)
      return -1;

  // IMPLICIT_ACTIVATION requires the SYSTEM_ID and RETAIN policies.
  if (this->implicit_activation_ == PortableServer::IMPLICIT_ACTIVATION)
    if (this->servant_retention_ != PortableServer::RETAIN ||
        this->id_assignment_ != PortableServer::SYSTEM_ID)
      return -1;

  return 0;
}

void
TAO_POA_Policies::parse_policy (const PortableServer::Policy_ptr policy,
                                CORBA::Environment &env)
{
  PortableServer::ThreadPolicy_var thread
    = PortableServer::ThreadPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (thread.in ()))
    {
      this->thread_ = thread->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  PortableServer::LifespanPolicy_var lifespan
    = PortableServer::LifespanPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (lifespan.in ()))
    {
      this->lifespan_ = lifespan->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  PortableServer::IdUniquenessPolicy_var id_uniqueness
    = PortableServer::IdUniquenessPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (id_uniqueness.in ()))
    {
      this->id_uniqueness_ = id_uniqueness->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  PortableServer::IdAssignmentPolicy_var id_assignment
    = PortableServer::IdAssignmentPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (id_assignment.in ()))
    {
      this->id_assignment_ = id_assignment->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  PortableServer::ImplicitActivationPolicy_var implicit_activation
    = PortableServer::ImplicitActivationPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (implicit_activation.in ()))
    {
      this->implicit_activation_ = implicit_activation->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  PortableServer::ServantRetentionPolicy_var servant_retention
    = PortableServer::ServantRetentionPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (servant_retention.in ()))
    {
      this->servant_retention_ = servant_retention->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  PortableServer::RequestProcessingPolicy_var request_processing
    = PortableServer::RequestProcessingPolicy::_narrow (policy, env);
  if (!CORBA::is_nil (request_processing.in ()))
    {
      this->request_processing_ = request_processing->value (env);
      return;
    }
  else
    {
      env.clear ();
    }

  CORBA::Exception *exception = new PortableServer::POA::InvalidPolicy;
  env.exception (exception);
  return;
}

PortableServer::ThreadPolicyValue
TAO_POA_Policies::thread (void) const
{
  return this->thread_;
}

void
TAO_POA_Policies::thread (PortableServer::ThreadPolicyValue value)
{
  this->thread_ = value;
}

PortableServer::LifespanPolicyValue
TAO_POA_Policies::lifespan (void) const
{
  return this->lifespan_;
}

void
TAO_POA_Policies::lifespan (PortableServer::LifespanPolicyValue value)
{
  this->lifespan_ = value;
}

PortableServer::IdUniquenessPolicyValue
TAO_POA_Policies::id_uniqueness (void) const
{
  return this->id_uniqueness_;
}

void
TAO_POA_Policies::id_uniqueness (PortableServer::IdUniquenessPolicyValue value)
{
  this->id_uniqueness_ = value;
}

PortableServer::IdAssignmentPolicyValue
TAO_POA_Policies::id_assignment (void) const
{
  return this->id_assignment_;
}

void
TAO_POA_Policies::id_assignment (PortableServer::IdAssignmentPolicyValue value)
{
  this->id_assignment_ = value;
}

PortableServer::ImplicitActivationPolicyValue
TAO_POA_Policies::implicit_activation (void) const
{
  return this->implicit_activation_;
}

void
TAO_POA_Policies::implicit_activation (PortableServer::ImplicitActivationPolicyValue value)
{
  this->implicit_activation_ = value;
}

PortableServer::ServantRetentionPolicyValue
TAO_POA_Policies::servant_retention (void) const
{
  return this->servant_retention_;
}

void
TAO_POA_Policies::servant_retention (PortableServer::ServantRetentionPolicyValue value)
{
  this->servant_retention_ = value;
}

PortableServer::RequestProcessingPolicyValue
TAO_POA_Policies::request_processing (void) const
{
  return this->request_processing_;
}

void
TAO_POA_Policies::request_processing (PortableServer::RequestProcessingPolicyValue value)
{
  this->request_processing_ = value;
}

TAO_Creation_Time::TAO_Creation_Time (const ACE_Time_Value &creation_time)
{
  // Convert seconds and micro seconds into string
  ACE_OS::sprintf (this->time_stamp_,
                   "%08.8x%08.8x",
                   creation_time.sec (),
                   creation_time.usec ());
}

TAO_Creation_Time::TAO_Creation_Time (void)
{
  ACE_OS::memset (this->time_stamp_,
                  0,
                  TAO_Creation_Time::creation_time_length ());
}

void
TAO_Creation_Time::creation_time (const void *creation_time)
{
  ACE_OS::memcpy (this->time_stamp_,
                  creation_time,
                  TAO_Creation_Time::creation_time_length ());
}

const void *
TAO_Creation_Time::creation_time (void) const
{
  return &this->time_stamp_[0];
}

int
TAO_Creation_Time::creation_time_length (void)
{
  return TAO_Creation_Time::max_space_required_for_two_ulong_to_hex;
}

int
TAO_Creation_Time::operator== (const TAO_Creation_Time &rhs) const
{
  return ACE_OS::memcmp (this->time_stamp_,
                         rhs.time_stamp_,
                         TAO_Creation_Time::creation_time_length ()) == 0;
}

int
TAO_Creation_Time::operator!= (const TAO_Creation_Time &rhs) const
{
  return ACE_OS::memcmp (this->time_stamp_,
                         rhs.time_stamp_,
                         TAO_Creation_Time::creation_time_length ()) != 0;
}

int
TAO_Creation_Time::operator== (const TAO_Temporary_Creation_Time &rhs) const
{
  return rhs == *this;
}

int
TAO_Creation_Time::operator!= (const TAO_Temporary_Creation_Time &rhs) const
{
  return rhs != *this;
}

TAO_Temporary_Creation_Time::TAO_Temporary_Creation_Time (void)
  : time_stamp_ (0)
{
}

void
TAO_Temporary_Creation_Time::creation_time (const void *creation_time)
{
  this->time_stamp_ = (void *) creation_time;
}

int
TAO_Temporary_Creation_Time::operator== (const TAO_Creation_Time &rhs) const
{
  return ACE_OS::memcmp (this->time_stamp_,
                         rhs.creation_time (),
                         TAO_Creation_Time::creation_time_length ()) == 0;
}

int
TAO_Temporary_Creation_Time::operator!= (const TAO_Creation_Time &rhs) const
{
  return ACE_OS::memcmp (this->time_stamp_,
                         rhs.creation_time (),
                         TAO_Creation_Time::creation_time_length ()) != 0;
}

TAO_POA::TAO_POA (const TAO_POA::String &adapter_name,
                  TAO_POA_Manager &poa_manager,
                  const TAO_POA_Policies &policies,
                  TAO_POA *parent,
                  CORBA::Environment &env)
  : name_ (adapter_name),
    poa_manager_ (poa_manager),
    policies_ (policies),
    parent_ (parent),
    active_object_map_ (new TAO_Object_Table),
    delete_active_object_map_ (1),
    adapter_activator_ (),
    servant_activator_ (),
    servant_locator_ (),
    default_servant_ (0),
    children_ (),
    lock_ (),
    closing_down_ (0),
    counter_ (0),
    creation_time_ (ACE_OS::gettimeofday ())
{
  this->set_complete_name ();

  // Register self with manager
  this->poa_manager_.register_poa (this, env);
}

TAO_POA::TAO_POA (const TAO_POA::String &adapter_name,
                  TAO_POA_Manager &poa_manager,
                  const TAO_POA_Policies &policies,
                  TAO_POA *parent,
                  TAO_Object_Table &active_object_map,
                  CORBA::Environment &env)
  : name_ (adapter_name),
    poa_manager_ (poa_manager),
    policies_ (policies),
    parent_ (parent),
    active_object_map_ (&active_object_map),
    delete_active_object_map_ (0),
    adapter_activator_ (),
    servant_activator_ (),
    servant_locator_ (),
    default_servant_ (0),
    children_ (),
    lock_ (),
    closing_down_ (0),
    counter_ (0),
    creation_time_ (ACE_OS::gettimeofday ())
{
  this->set_complete_name ();

  // Register self with manager
  this->poa_manager_.register_poa (this, env);
}

TAO_POA *
TAO_POA::clone (const TAO_POA::String &adapter_name,
                TAO_POA_Manager &poa_manager,
                const TAO_POA_Policies &policies,
                TAO_POA *parent,
                CORBA::Environment &env)
{
  return new TAO_POA (adapter_name,
                      poa_manager,
                      policies,
                      parent,
                      env);
}

TAO_POA *
TAO_POA::clone (const TAO_POA::String &adapter_name,
                TAO_POA_Manager &poa_manager,
                const TAO_POA_Policies &policies,
                TAO_POA *parent,
                TAO_Object_Table &active_object_map,
                CORBA::Environment &env)
{
  return new TAO_POA (adapter_name,
                      poa_manager,
                      policies,
                      parent,
                      active_object_map,
                      env);
}

TAO_POA::~TAO_POA (void)
{
  if (this->delete_active_object_map_)
    delete active_object_map_;

  // Remove POA from the POAManager
  //
  // Note: Errors are ignored here since there is nothing we can do
  // about them
  //
  CORBA::Environment env;
  this->poa_manager_.remove_poa (this, env);
}

ACE_Lock &
TAO_POA::lock (void)
{
  return this->lock_;
}

PortableServer::POA_ptr
TAO_POA::create_POA (const char *adapter_name,
                     PortableServer::POAManager_ptr poa_manager,
                     const PortableServer::PolicyList &policies,
                     CORBA::Environment &env)
{
  // If any of the policy objects specified are not valid for the ORB
  // implementation, if conflicting policy objects are specified, or
  // if any of the specified policy objects require prior
  // administrative action that has not been performed, an
  // InvalidPolicy exception is raised containing the index in the
  // policies parameter value of the first offending policy object.
  TAO_POA_Policies tao_policies;
  tao_policies.parse_policies (policies, env);
  if (env.exception () != 0)
    return PortableServer::POA::_nil ();

  // If the poa_manager parameter is null, a new POAManager object is
  // created and associated with the new POA. Otherwise, the specified
  // POAManager object is associated with the new POA. The POAManager
  // object can be obtained using the attribute name the_POAManager.
  TAO_POA_Manager *poa_manager_impl = 0;
  if (CORBA::is_nil (poa_manager))
    {
      poa_manager_impl = this->poa_manager_.clone ();
    }
  else
    {
      PortableServer::Servant servant = poa_manager->_servant ();
      if (servant == 0)
        {
          CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
          env.exception (exception);
          return PortableServer::POA::_nil ();
        }

      void *ptr = servant->_downcast (servant->_interface_repository_id ());
      POA_PortableServer::POAManager *mgr = (POA_PortableServer::POAManager *) ptr;
      poa_manager_impl = ACE_dynamic_cast (TAO_POA_Manager *, mgr);
    }

  TAO_POA *result = this->create_POA (adapter_name,
                                      *poa_manager_impl,
                                      tao_policies,
                                      env);
  if (env.exception () != 0)
    return PortableServer::POA::_nil ();

  return result->_this (env);
}

TAO_POA *
TAO_POA::create_POA (const TAO_POA::String &adapter_name,
                     TAO_POA_Manager &poa_manager,
                     const TAO_POA_Policies &policies,
                     CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->create_POA_i (adapter_name,
                             poa_manager,
                             policies,
                             env);
}

TAO_POA *
TAO_POA::create_POA_i (const TAO_POA::String &adapter_name,
                       TAO_POA_Manager &poa_manager,
                       const TAO_POA_Policies &policies,
                       CORBA::Environment &env)
{
  // If this is the leaf POA name
  if (this->leaf_poa_name (adapter_name, env))
    {
      // This operaton creates a new POA as a child of the target POA. The
      // specified name identifies the new POA with respect to other POAs
      // with the same parent POA. If the target POA already has a child
      // POA with the specified name, the AdapterAlreadyExists exception
      // is raised.
      int result = this->children_.find (adapter_name);

      // Child was found
      if (result != -1)
        {
          CORBA::Exception *exception = new PortableServer::POA::AdapterAlreadyExists;
          env.exception (exception);
          return 0;
        }

      //
      // Child was not found
      //

      // The specified policy objects are associated with the POA and used
      // to control its behavior. The policy objects are effectively
      // copied before this operation returns, so the application is free
      // to destroy them while the POA is in use. Policies are not
      // inherited from the parent POA.
      auto_ptr<TAO_POA> new_poa (this->clone (adapter_name,
                                              poa_manager,
                                              policies,
                                              this,
                                              env));

      if (env.exception () != 0)
        return 0;

      // Add to children map
      if (this->children_.bind (adapter_name, new_poa.get ()) != 0)
        {
          CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
          env.exception (exception);
          return 0;
        }

      // Note: Creating a POA using a POA manager that is in the active
      // state can lead to race conditions if the POA supports preexisting
      // objects, because the new POA may receive a request before its
      // adapter activator, servant manager, or default servant have been
      // initialized. These problems do not occur if the POA is created by
      // an adapter activator registered with a parent of the new POA,
      // because requests are queued until the adapter activator
      // returns. To avoid these problems when a POA must be explicitly
      // initialized, the application can initialize the POA by invoking
      // find_POA with a TRUE activate parameter.

      // Everything is fine
      // Don't let the auto_ptr delete the implementation
      return new_poa.release ();
    }
  // Not the leaf POA name
  else
    {
      // Find the topmost name
      TAO_POA::String topmost_poa_name;
      TAO_POA::String tail_poa_name;
      this->parse_poa_name (adapter_name,
                            topmost_poa_name,
                            tail_poa_name,
                            env);

      if (env.exception () != 0)
        return 0;

      // If we are the topmost poa, let's create the tail
      if (topmost_poa_name == this->name_)
        {
          return this->create_POA_i (tail_poa_name,
                                     poa_manager,
                                     policies,
                                     env);
        }

      //
      // We are not the topmost POA
      //

      // Try to find the topmost child
      TAO_POA *child_poa = 0;
      int result = this->children_.find (topmost_poa_name, child_poa);

      // Child was not found or the topmost is us
      if (result != 0)
        {
          child_poa = this->create_POA_i (topmost_poa_name,
                                          poa_manager,
                                          policies,
                                          env);
          if (env.exception () != 0)
            return 0;
        }

      // At this point, the intermediate child POA was either found or
      // created
      return child_poa->create_POA (tail_poa_name,
                                    poa_manager,
                                    policies,
                                    env);
    }
}

PortableServer::POA_ptr
TAO_POA::find_POA (const char *adapter_name,
                   CORBA::Boolean activate_it,
                   CORBA::Environment &env)
{
  TAO_POA::String name (adapter_name);

  TAO_POA *result = this->find_POA (name,
                                    activate_it,
                                    env);

  if (env.exception () != 0)
    return PortableServer::POA::_nil ();

  return result->_this (env);
}

TAO_POA *
TAO_POA::find_POA (const TAO_POA::String &adapter_name,
                   CORBA::Boolean activate_it,
                   CORBA::Environment &env)
{
  // Check if we are the POA the user is looking for
  if (adapter_name == this->name_)
    return this;

  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->find_POA_i_optimized (adapter_name,
                                     activate_it,
                                     env);

}

TAO_POA *
TAO_POA::find_POA_i (const TAO_POA::String &adapter_name,
                     CORBA::Boolean activate_it,
                     CORBA::Environment &env)
{
  // Check if we are the POA the user is looking for
  if (adapter_name == this->name_)
    return this;

  return this->find_POA_i_optimized (adapter_name,
                                     activate_it,
                                     env);
}

TAO_POA *
TAO_POA::find_POA_i_optimized (const TAO_POA::String &adapter_name,
                               CORBA::Boolean activate_it,
                               CORBA::Environment &env)
{
  // If this is the leaf POA name
  if (this->leaf_poa_name (adapter_name, env))
    {
      // If the target POA is the parent of a child POA with the
      // specified name (relative to the target POA), that child POA
      // is returned.
      TAO_POA *child_poa = 0;
      int result = this->children_.find (adapter_name, child_poa);

      // Child was found
      if (result != -1)
        return child_poa;

      //
      // Child was not found
      //

      // If a child POA with the specified name does not exist and the
      // value of the activate_it parameter is TRUE, the target POA's
      // AdapterActivator, if one exists, is invoked, and, if it
      // successfully activates the child POA, that child POA is
      // returned.
      if (activate_it && !CORBA::is_nil (this->adapter_activator_.in ()))
        {
          PortableServer::POA_var self = this->_this (env);
          // Check for exceptions
          if (env.exception () != 0)
            return 0;

          CORBA::Boolean success =
            this->adapter_activator_->unknown_adapter (self.in (),
                                                       adapter_name.c_str (),
                                                       env);
          // Check for exceptions
          if (env.exception () != 0)
            return 0;

          // On success
          if (success)
            {
              // Search the children table again
              result = this->children_.find (adapter_name, child_poa);

              // Child was found
              if (result != -1)
                return child_poa;
            }
        }

      // Otherwise, the AdapterNonExistent exception is raised.
      CORBA::Exception *exception = new PortableServer::POA::AdapterNonExistent;
      env.exception (exception);
      return 0;
    }
  // Not the leaf POA name
  else
    {
      // Find the topmost name
      // Find the topmost name
      TAO_POA::String topmost_poa_name;
      TAO_POA::String tail_poa_name;
      this->parse_poa_name (adapter_name,
                            topmost_poa_name,
                            tail_poa_name,
                            env);

      if (env.exception () != 0)
        return 0;

      // If we are the topmost poa, let's create the tail
      if (topmost_poa_name == this->name_)
        {
          return this->find_POA_i (tail_poa_name,
                                   activate_it,
                                   env);
        }

      //
      // We are not the topmost POA
      //

      // Try to find the topmost child
      TAO_POA *child_poa;
      int result = this->children_.find (topmost_poa_name, child_poa);

      // Child was not found
      if (result != 0)
        {
          child_poa = this->find_POA_i_optimized (topmost_poa_name,
                                                  activate_it,
                                                  env);
          if (env.exception () != 0)
            return 0;
        }

      // At this point, the intermediate child POA was either found or
      // created
      return child_poa->find_POA (tail_poa_name,
                                  activate_it,
                                  env);
    }
}

void
TAO_POA::destroy (CORBA::Boolean etherealize_objects,
                  CORBA::Boolean wait_for_completion,
                  CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->destroy_i (etherealize_objects,
                   wait_for_completion,
                   env);
}

void
TAO_POA::destroy_i (CORBA::Boolean etherealize_objects,
                    CORBA::Boolean wait_for_completion,
                    CORBA::Environment &env)
{
  this->closing_down_ = 1;

  // This operation destroys the POA and all descendant POAs. The POA
  // so destroyed (that is, the POA with its name) may be re-created
  // later in the same process. (This differs from the
  // POAManager::deactivate operation that does not allow a
  // re-creation of its associated POA in the same process.)

  // Remove POA from the parent
  if (this->parent_ != 0)
    this->parent_->delete_child (this->name_, env);

  if (env.exception () != 0)
    return;

  // Remove all children POAs
  for (CHILDREN::iterator iterator = this->children_.begin ();
       iterator != this->children_.end () && env.exception () == 0;
       iterator++)
    {
      TAO_POA *child_poa = (*iterator).int_id_;
      child_poa->destroy (etherealize_objects,
                          wait_for_completion,
                          env);
    }

  if (env.exception () != 0)
    return;

  // When a POA is destroyed, any requests that have started execution
  // continue to completion. Any requests that have not started
  // execution are processed as if they were newly arrived, that is,
  // the POA will attempt to cause recreation of the POA by invoking
  // one or more adapter activators as described in Section 3.3.3.

  // If the etherealize_objects parameter is TRUE, the POA has the
  // RETAIN policy, and a servant manager is registered with the POA,
  // the etherealize operation on the servant manager will be called
  // for each active object in the Active Object Map. The apparent
  // destruction of the POA occurs before any calls to etherealize are
  // made.  Thus, for example, an etherealize method that attempts to
  // invoke operations on the POA will receive the OBJECT_NOT_EXIST
  // exception.

  // Remove the registered objects
  if (etherealize_objects)
    {
      if (this->policies ().servant_retention () == PortableServer::RETAIN &&
          this->policies ().request_processing () == PortableServer::USE_SERVANT_MANAGER &&
          !CORBA::is_nil (this->servant_activator_.in ()))
        {
          PortableServer::POA_var self = this->_this (env);
          if (env.exception () != 0)
            return;

          while (1)
            {
              TAO_Object_Table::iterator iterator = this->active_object_map ().begin ();
              if (iterator == this->active_object_map ().end () || env.exception () != 0)
                break;

              PortableServer::Servant servant = 0;
              PortableServer::ObjectId id ((*iterator).ext_id_);

              int result = this->active_object_map ().unbind (id, servant);
              if (result != 0)
                {
                  CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
                  env.exception (exception);
                  return;
                }

              CORBA::Boolean remaining_activations = CORBA::B_FALSE;

              if (this->policies ().id_uniqueness () == PortableServer::MULTIPLE_ID &&
                  this->active_object_map ().find (servant) != -1)
                remaining_activations = CORBA::B_TRUE;

              this->servant_activator_->etherealize (id,
                                                     self.in (),
                                                     servant,
                                                     CORBA::B_TRUE,
                                                     remaining_activations,
                                                     env);
            }
        }
    }

  // If the wait_for_completion parameter is TRUE, the destroy
  // operation will return only after all requests in process have
  // completed and all invocations of etherealize have
  // completed. Otherwise, the destroy operation returns after
  // destroying the POAs.

  ACE_UNUSED_ARG (wait_for_completion);

  // Commit suicide
  delete this;
}

void
TAO_POA::delete_child (const TAO_POA::String &child,
                       CORBA::Environment &env)
{
  // If we are not closing down, we must remove this child from our
  // collection.
  if (!this->closing_down_)
    {
      // Lock access to the POA for the duration of this transaction
      TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

      this->delete_child_i (child,
                            env);
    }

  // If we are closing down, we are currently iterating over our
  // children and there is not need to remove this child from our
  // collection.
}

void
TAO_POA::delete_child_i (const TAO_POA::String &child,
                         CORBA::Environment &env)
{
  if (this->children_.unbind (child) != 0)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return;
    }
}

TAO_POA_Policies &
TAO_POA::policies (void)
{
  return this->policies_;
}

TAO_Object_Table &
TAO_POA::active_object_map (void)
{
  return *this->active_object_map_;
}

PortableServer::ServantManager_ptr
TAO_POA::get_servant_manager (CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), PortableServer::ServantManager::_nil (), env);

  return this->get_servant_manager_i (env);
}

PortableServer::ServantManager_ptr
TAO_POA::get_servant_manager_i (CORBA::Environment &env)
{
  // This operation requires the USE_SERVANT_MANAGER policy; if not
  // present, the WrongPolicy exception is raised.
  if (this->policies ().request_processing () != PortableServer::USE_SERVANT_MANAGER)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return PortableServer::ServantManager::_nil ();
    }

  // This operation returns the servant manager associated with the
  // POA.  If no servant manager has been associated with the POA, it
  // returns a null reference.
  if (this->policies ().servant_retention () == PortableServer::RETAIN)
    return PortableServer::ServantManager::_duplicate (this->servant_activator_.in ());
  else
    return PortableServer::ServantManager::_duplicate (this->servant_locator_.in ());
}

void
TAO_POA::set_servant_manager (PortableServer::ServantManager_ptr imgr,
                              CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->set_servant_manager_i (imgr,
                               env);
}

void
TAO_POA::set_servant_manager_i (PortableServer::ServantManager_ptr imgr,
                                CORBA::Environment &env)
{
  // This operation requires the USE_SERVANT_MANAGER policy; if not
  // present, the WrongPolicy exception is raised.
  if (this->policies ().request_processing () != PortableServer::USE_SERVANT_MANAGER)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return;
    }

  // This operation sets the default servant manager associated with
  // the POA.
  if (this->policies ().servant_retention () == PortableServer::RETAIN)
    {
      this->servant_activator_ = PortableServer::ServantActivator::_narrow (imgr, env);
      if (CORBA::is_nil (this->servant_activator_.in ()))
        {
          CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
          env.exception (exception);
          return;
        }
    }
  else
    {
      this->servant_locator_ = PortableServer::ServantLocator::_narrow (imgr, env);
      if (CORBA::is_nil (this->servant_locator_.in ()))
        {
          CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
          env.exception (exception);
          return;
        }
    }
}

PortableServer::Servant
TAO_POA::get_servant (CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->get_servant_i (env);
}

PortableServer::Servant
TAO_POA::get_servant_i (CORBA::Environment &env)
{
  // This operation requires the USE_DEFAULT_SERVANT policy; if not
  // present, the WrongPolicy exception is raised.
  if (this->policies ().request_processing () != PortableServer::USE_DEFAULT_SERVANT)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return 0;
    }

  // This operation returns the default servant associated with the
  // POA.
  if (this->default_servant_ != 0)
    {
      return this->default_servant_;
    }
  else
    // If no servant has been associated with the POA, the NoServant
    // exception is raised.
    {
      CORBA::Exception *exception = new PortableServer::POA::NoServant;
      env.exception (exception);
      return 0;
    }
}

void
TAO_POA::set_servant (PortableServer::Servant servant,
                      CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->set_servant_i (servant,
                       env);
}

void
TAO_POA::set_servant_i (PortableServer::Servant servant,
                        CORBA::Environment &env)
{
  // This operation requires the USE_DEFAULT_SERVANT policy; if not
  // present, the WrongPolicy exception is raised.
  if (this->policies ().request_processing () != PortableServer::USE_DEFAULT_SERVANT)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return;
    }

  // This operation registers the specified servant with the POA as
  // the default servant. This servant will be used for all requests
  // for which no servant is found in the Active Object Map.
  this->default_servant_ = servant;
}

PortableServer::ObjectId *
TAO_POA::activate_object (PortableServer::Servant servant,
                          CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->activate_object_i (servant,
                                  env);
}

PortableServer::ObjectId *
TAO_POA::activate_object_i (PortableServer::Servant servant,
                            CORBA::Environment &env)
{
  // This operation requires the SYSTEM_ID and RETAIN policy; if not
  // present, the WrongPolicy exception is raised.
  if (!(this->policies ().id_assignment () == PortableServer::SYSTEM_ID &&
        this->policies ().servant_retention () == PortableServer::RETAIN))
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return 0;
    }

  // If the POA has the UNIQUE_ID policy and the specified servant is
  // already in the Active Object Map, the ServantAlreadyActive
  // exception is raised.
  if (this->policies ().id_uniqueness () == PortableServer::UNIQUE_ID &&
      this->active_object_map ().find (servant) != -1)
    {
      CORBA::Exception *exception = new PortableServer::POA::ServantAlreadyActive;
      env.exception (exception);
      return 0;
    }

  // Otherwise, the activate_object operation generates an Object Id
  // and enters the Object Id and the specified servant in the Active
  // Object Map. The Object Id is returned.
  PortableServer::ObjectId_var new_id = this->create_object_id ();

  if (this->active_object_map ().bind (new_id.in (), servant) == -1)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return 0;
    }

  // Everything is finally ok
  return new_id._retn ();
}

void
TAO_POA::activate_object_with_id (const PortableServer::ObjectId &id,
                                  PortableServer::Servant servant,
                                  CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->activate_object_with_id_i (id,
                                   servant,
                                   env);
}

void
TAO_POA::activate_object_with_id_i (const PortableServer::ObjectId &id,
                                    PortableServer::Servant servant,
                                    CORBA::Environment &env)
{
  // This operation requires the RETAIN policy; if not present, the
  // WrongPolicy exception is raised.
  if (this->policies ().servant_retention () != PortableServer::RETAIN)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return;
    }

  // If the POA has the SYSTEM_ID policy and it detects that the
  // Object Id value was not generated by the system or for this POA,
  // the activate_object_with_id operation may raise the BAD_PARAM
  // system exception.  An ORB is not required to detect all such
  // invalid Object Id values, but a portable application must not
  // invoke activate_object_with_id on a POA that has the SYSTEM_ID
  // policy with an Object Id value that was not previously generated
  // by the system for that POA, or, if the POA also has the
  // PERSISTENT policy, for a previous instantiation of the same POA.
  if (this->policies ().id_assignment () == PortableServer::SYSTEM_ID &&
      !this->is_poa_generated_id (id))
    {
      CORBA::Exception *exception = new CORBA::BAD_PARAM (CORBA::COMPLETED_NO);
      env.exception (exception);
      return;
    }

  // If the CORBA object denoted by the Object Id value is already
  // active in this POA (there is a servant bound to it in the Active
  // Object Map), the ObjectAlreadyActive exception is raised.
  if (this->active_object_map ().find (id) != -1)
    {
      CORBA::Exception *exception = new PortableServer::POA::ObjectAlreadyActive;
      env.exception (exception);
      return;
    }

  // If the POA has the UNIQUE_ID policy and the servant is already in
  // the Active Object Map, the ServantAlreadyActive exception is
  // raised.
  if (this->policies ().id_uniqueness () == PortableServer::UNIQUE_ID &&
      this->active_object_map ().find (servant) != -1)
    {
      CORBA::Exception *exception = new PortableServer::POA::ServantAlreadyActive;
      env.exception (exception);
      return;
    }

  // Otherwise, the activate_object_with_id operation enters an
  // association between the specified Object Id and the specified
  // servant in the Active Object Map.
  if (this->active_object_map ().bind (id, servant) == -1)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return;
    }

  // Everything is finally ok
}

void
TAO_POA::deactivate_object (const PortableServer::ObjectId &oid,
                            CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->deactivate_object_i (oid,
                             env);
}

void
TAO_POA::deactivate_object_i (const PortableServer::ObjectId &oid,
                              CORBA::Environment &env)
{
  // This operation requires the RETAIN policy; if not present, the
  // WrongPolicy exception is raised.
  if (this->policies ().servant_retention () != PortableServer::RETAIN)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return;
    }

  // This operation causes the association of the Object Id specified
  // by the oid parameter and its servant to be removed from the
  // Active Object Map.
  PortableServer::Servant servant = 0;
  int result = this->active_object_map ().unbind (oid, servant);

  // If there is no active object associated with the specified Object
  // Id, the operation raises an ObjectNotActive exception.
  if (result != 0)
    {
      CORBA::Exception *exception = new PortableServer::POA::ObjectNotActive;
      env.exception (exception);
      return;
    }

  // If a servant manager is associated with the POA,
  // ServantLocator::etherealize will be invoked with the oid and the
  // servant. (The deactivate_object operation does not wait for the
  // etherealize operation to complete before deactivate_object
  // returns.)
  //
  // Note: If the servant associated with the oid is serving multiple
  // Object Ids, ServantLocator::etherealize may be invoked multiple
  // times with the same servant when the other objects are
  // deactivated. It is the responsibility of the object
  // implementation to refrain from destroying the servant while it is
  // active with any Id.
  if (!CORBA::is_nil (this->servant_activator_.in ()))
    {
      PortableServer::POA_var self = this->_this (env);
      if (env.exception () != 0)
        return;

      CORBA::Boolean remaining_activations = CORBA::B_FALSE;

      if (this->policies ().id_uniqueness () == PortableServer::MULTIPLE_ID &&
          this->active_object_map ().find (servant) != -1)
        remaining_activations = CORBA::B_TRUE;

      this->servant_activator_->etherealize (oid,
                                             self.in (),
                                             servant,
                                             CORBA::B_FALSE,
                                             remaining_activations,
                                             env);
    }
}

CORBA::Object_ptr
TAO_POA::create_reference (const char *intf,
                           CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD_RETURN (ACE_Lock, monitor, this->lock (), CORBA::Object::_nil (), env);

  return this->create_reference_i (intf,
                                   env);
}

CORBA::Object_ptr
TAO_POA::create_reference_i (const char *intf,
                             CORBA::Environment &env)
{
  // This operation requires the SYSTEM_ID policy; if not present, the
  // WrongPolicy exception is raised.
  if (this->policies ().id_assignment () != PortableServer::SYSTEM_ID)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return CORBA::Object::_nil ();
    }

  // This operation creates an object reference that encapsulates a
  // POA-generated Object Id value and the specified interface
  // repository id. This operation does not cause an activation to
  // take place. The resulting reference may be passed to clients, so
  // that subsequent requests on those references will cause the
  // appropriate servant manager to be invoked, if one is
  // available. The generated Object Id value may be obtained by
  // invoking POA::reference_to_id with the created reference.
  PortableServer::ObjectId_var new_id = this->create_object_id ();
  TAO_ObjectKey_var new_key = this->create_object_key (new_id.in ());

  // Ask the ORB to create you a reference
  return TAO_ORB_Core_instance ()->orb ()->key_to_object (new_key.in (), intf, env);
}

CORBA::Object_ptr
TAO_POA::create_reference_with_id (const PortableServer::ObjectId &oid,
                                   const char *intf,
                                   CORBA::Environment &env)
{
  // If the POA has the SYSTEM_ID policy and it detects that the
  // Object Id value was not generated by the system or for this POA,
  // the create_reference_with_id operation may raise the BAD_PARAM
  // system exception. An ORB is not required to detect all such
  // invalid Object Id values, but a portable application must not
  // invoke this operation on a POA that has the SYSTEM_ID policy with
  // an Object Id value that was not previously generated by the
  // system for that POA, or, if the POA also has the PERSISTENT
  // policy, for a previous instantiation of the same POA.
  if (this->policies ().id_assignment () == PortableServer::SYSTEM_ID &&
      !this->is_poa_generated_id (oid))
    {
      CORBA::Exception *exception = new CORBA::BAD_PARAM (CORBA::COMPLETED_NO);
      env.exception (exception);
      return CORBA::Object::_nil ();
    }

  // This operation creates an object reference that encapsulates the
  // specified Object Id and interface repository Id values. This
  // operation does not cause an activation to take place.  The
  // resulting reference may be passed to clients, so that subsequent
  // requests on those references will cause the object to be
  // activated if necessary, or the default servant used, depending on
  // the applicable policies.
  TAO_ObjectKey_var new_key = this->create_object_key (oid);

  // Ask the ORB to create you a reference
  return TAO_ORB_Core_instance ()->orb ()->key_to_object (new_key.in (), intf, env);
}

PortableServer::ObjectId *
TAO_POA::servant_to_id (PortableServer::Servant servant,
                        CORBA::Environment &env)
{
  // If we had upgradeable locks, this would initially be a read lock
  //
  // Lock access to the POA for the duration of this transaction
  TAO_POA_WRITE_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->servant_to_id_i (servant,
                                env);
}

PortableServer::ObjectId *
TAO_POA::servant_to_id_i (PortableServer::Servant servant,
                          CORBA::Environment &env)
{
  // This operation requires the RETAIN and either the UNIQUE_ID or
  // IMPLICIT_ACTIVATION policies; if not present, the WrongPolicy
  // exception is raised.
  if (!(this->policies ().servant_retention () == PortableServer::RETAIN &&
        (this->policies ().id_uniqueness () == PortableServer::UNIQUE_ID ||
         this->policies ().implicit_activation () == PortableServer::IMPLICIT_ACTIVATION)))
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return 0;
    }

  // This operation has three possible behaviors.

  // If the POA has the UNIQUE_ID policy and the specified servant is
  // active, the Object Id associated with that servant is returned.
  PortableServer::ObjectId_var id;
  PortableServer::ObjectId_out id_out (id);
  if (this->policies ().id_uniqueness () == PortableServer::UNIQUE_ID &&
      this->active_object_map ().find (servant, id_out) != -1)
    {
      return id._retn ();
    }

  // If the POA has the IMPLICIT_ACTIVATION policy and either the POA
  // has the MULTIPLE_ID policy or the specified servant is not
  // active, the servant is activated using a POA-generated Object Id
  // and the Interface Id associated with the servant, and that Object
  // Id is returned.
  if (this->policies ().implicit_activation () == PortableServer::IMPLICIT_ACTIVATION)
    {
      if (this->policies ().id_uniqueness () == PortableServer::MULTIPLE_ID ||
          this->active_object_map ().find (servant) != 0)
        {
          PortableServer::ObjectId_var new_id = this->create_object_id ();

          if (this->active_object_map ().bind (new_id.in (), servant) == -1)
            {
              CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
              env.exception (exception);
              return 0;
            }

          // Everything is finally ok
          return new_id._retn ();
        }
    }

  // Otherwise, the ServantNotActive exception is raised.
  CORBA::Exception *exception = new PortableServer::POA::ServantNotActive;
  env.exception (exception);
  return 0;
}

CORBA::Object_ptr
TAO_POA::servant_to_reference (PortableServer::Servant servant,
                               CORBA::Environment &env)
{
  // Note: The allocation of an Object Id value and installation in
  // the Active Object Map caused by implicit activation may actually
  // be deferred until an attempt is made to externalize the
  // reference. The real requirement here is that a reference is
  // produced that will behave appropriately (that is, yield a
  // consistent Object Id value when asked politely).
  PortableServer::ObjectId_var oid = this->servant_to_id (servant,
                                                          env);

  if (env.exception () != 0)
    return CORBA::Object::_nil ();
  else
    return this->create_reference_with_id (oid.in (),
                                           servant->_interface_repository_id (),
                                           env);
}

PortableServer::Servant
TAO_POA::reference_to_servant (CORBA::Object_ptr reference,
                               CORBA::Environment &env)
{
  // This operation requires the RETAIN policy or the
  // USE_DEFAULT_SERVANT policy. If neither policy is present, the
  // WrongPolicy exception is raised.
  if (!(this->policies ().servant_retention () == PortableServer::RETAIN ||
        this->policies ().request_processing () == PortableServer::USE_DEFAULT_SERVANT))
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return 0;
    }

  // If the POA has the RETAIN policy and the specified object is
  // present in the Active Object Map, this operation returns the
  // servant associated with that object in the Active Object Map.
  if (this->policies ().servant_retention () == PortableServer::RETAIN)
    {
      TAO_ObjectKey_var key = reference->_key (env);

      // If the object reference was not created by this POA, the
      // WrongAdapter exception is raised.
      PortableServer::ObjectId_var id;
      PortableServer::ObjectId_out id_out (id);
      TAO_POA::String poa_name;
      CORBA::Boolean persistent = CORBA::B_FALSE;
      TAO_Temporary_Creation_Time poa_creation_time;

      int result = this->parse_key_temporary_id (key.in (),
                                                 poa_name,
                                                 id_out,
                                                 persistent,
                                                 poa_creation_time);
      if (result != 0 ||
          poa_name != this->complete_name () ||
          persistent != this->persistent () ||
          !this->persistent () && poa_creation_time != this->creation_time_)
        {
          CORBA::Exception *exception = new PortableServer::POA::WrongAdapter;
          env.exception (exception);
          return 0;
        }

      return this->id_to_servant (id.in (), env);
    }

  // Otherwise, if the POA has the USE_DEFAULT_SERVANT policy and a
  // default servant has been registered with the POA, this operation
  // returns the default servant.
  if (this->policies ().request_processing () == PortableServer::USE_DEFAULT_SERVANT)
    {
      // Lock access to the POA for the duration of this transaction
      TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

      if (this->default_servant_ != 0)
        {
          return this->default_servant_;
        }
      else
        // Otherwise, the ObjectNotActive exception is raised.
        {
          CORBA::Exception *exception = new PortableServer::POA::ObjectNotActive;
          env.exception (exception);
          return 0;
        }
    }

  // Not reached
  return 0;
}

PortableServer::ObjectId *
TAO_POA::reference_to_id (CORBA::Object_ptr reference,
                          CORBA::Environment &env)
{
  // The WrongPolicy exception is declared to allow future extensions.

  // This operation returns the Object Id value encapsulated by the
  // specified reference.

  // This operation is valid only if the reference was created by the
  // POA on which the operation is being performed.  If the object
  // reference was not created by this POA, the WrongAdapter exception
  // is raised.
  TAO_ObjectKey_var key = reference->_key (env);
  PortableServer::ObjectId_var id;
  PortableServer::ObjectId_out id_out (id);
  TAO_POA::String poa_name;
  CORBA::Boolean persistent = CORBA::B_FALSE;
  TAO_Temporary_Creation_Time poa_creation_time;

  int result = this->parse_key_permanent_id (key.in (),
                                             poa_name,
                                             id_out,
                                             persistent,
                                             poa_creation_time);
  if (result != 0 ||
      poa_name != this->complete_name () ||
      persistent != this->persistent () ||
      !this->persistent () && poa_creation_time != this->creation_time_)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongAdapter;
      env.exception (exception);
      return 0;
    }

  // The object denoted by the reference does not have to be active
  // for this operation to succeed.
  return id._retn ();
}

PortableServer::Servant
TAO_POA::id_to_servant (const PortableServer::ObjectId &oid,
                        CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->id_to_servant_i (oid,
                                env);
}

PortableServer::Servant
TAO_POA::id_to_servant_i (const PortableServer::ObjectId &oid,
                          CORBA::Environment &env)
{
  // This operation requires the RETAIN policy; if not present, the
  // WrongPolicy exception is raised.
  if (this->policies ().servant_retention () != PortableServer::RETAIN)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return 0;
    }

  // This operation returns the active servant associated with the
  // specified Object Id value.  If the Object Id value is not active
  // in the POA, an ObjectNotActive exception is raised.
  PortableServer::Servant servant = 0;
  if (this->active_object_map ().find (oid, servant) != -1)
    {
      return servant;
    }
  else
    // Otherwise, the ObjectNotActive exception is raised.
    {
      CORBA::Exception *exception = new PortableServer::POA::ObjectNotActive;
      env.exception (exception);
      return 0;
    }
}

CORBA::Object_ptr
TAO_POA::id_to_reference (const PortableServer::ObjectId &oid,
                          CORBA::Environment &env)
{
  // Lock access to the POA for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  return this->id_to_reference_i (oid, env);
}

CORBA::Object_ptr
TAO_POA::id_to_reference_i (const PortableServer::ObjectId &oid,
                            CORBA::Environment &env)
{
  // This operation requires the RETAIN policy; if not present, the
  // WrongPolicy exception is raised.
  if (this->policies ().servant_retention () != PortableServer::RETAIN)
    {
      CORBA::Exception *exception = new PortableServer::POA::WrongPolicy;
      env.exception (exception);
      return 0;
    }

  // If an object with the specified Object Id value is currently
  // active, a reference encapsulating the information used to
  // activate the object is returned.
  PortableServer::Servant servant = 0;
  if (this->active_object_map ().find (oid, servant) != -1)
    return this->create_reference_with_id (oid,
                                           servant->_interface_repository_id (),
                                           env);
  else
    // If the Object Id value is not active in the POA, an
    // ObjectNotActive exception is raised.
    {
      CORBA::Exception *exception = new PortableServer::POA::ObjectNotActive;
      env.exception (exception);
      return 0;
    }
}

PortableServer::POA_ptr
TAO_POA::the_parent (CORBA::Environment &env)
{
  if (this->parent_ != 0)
    return this->parent_->_this (env);
  else
    return PortableServer::POA::_nil ();
}

PortableServer::POAManager_ptr
TAO_POA::the_POAManager (CORBA::Environment &env)
{
  return this->poa_manager_._this (env);
}

PortableServer::AdapterActivator_ptr
TAO_POA::the_activator (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);

  return PortableServer::AdapterActivator::_duplicate (this->adapter_activator_.in ());
}

void
TAO_POA::the_activator (PortableServer::AdapterActivator_ptr adapter_activator,
                        CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);

  this->adapter_activator_ = PortableServer::AdapterActivator::_duplicate (adapter_activator);
}

TAO_POA *
TAO_POA::locate_poa_i (const TAO_ObjectKey &key,
                       PortableServer::ObjectId_out id,
                       CORBA::Environment &env)
{
  TAO_POA::String poa_name;
  CORBA::Boolean persistent = CORBA::B_FALSE;
  TAO_Temporary_Creation_Time poa_creation_time;

  int result = this->parse_key_temporary_id (key,
                                             poa_name,
                                             id,
                                             persistent,
                                             poa_creation_time);
  if (result != 0)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return 0;
    }

  TAO_POA *poa = this->find_POA_i (poa_name,
                                   persistent,
                                   env);
  if (env.exception () != 0)
    return 0;

  // Make sure it is not an "old" objectId
  if (!poa->persistent () &&
      poa->creation_time () != poa_creation_time)
    {
      CORBA::Exception *exception = new CORBA::OBJECT_NOT_EXIST (CORBA::COMPLETED_NO);
      env.exception (exception);
      return 0;
    }

  return poa;
}

int
TAO_POA::locate_servant (const TAO_ObjectKey &key,
                         CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), -1, env);

  PortableServer::Servant servant = 0;
  TAO_POA::LOCATION_RESULT result = this->locate_servant_i (key, servant, env);

  switch (result)
    {
    case TAO_POA::FOUND:
    // Optimistic attitude
    case TAO_POA::DEFAULT_SERVANT:
    case TAO_POA::SERVANT_MANAGER:
      return 0;

    case TAO_POA::NOT_FOUND:
      return -1;
    }

  return -1;
}

PortableServer::Servant
TAO_POA::find_servant (const TAO_ObjectKey &key,
                       CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), 0, env);

  PortableServer::Servant servant = 0;
  TAO_POA::LOCATION_RESULT result = this->locate_servant_i (key, servant, env);

  switch (result)
    {
    case TAO_POA::FOUND:
      return servant;

    case TAO_POA::DEFAULT_SERVANT:
    case TAO_POA::SERVANT_MANAGER:
    case TAO_POA::NOT_FOUND:
      return 0;
    }

  return 0;
}

TAO_POA::LOCATION_RESULT
TAO_POA::locate_servant_i (const TAO_ObjectKey &key,
                           PortableServer::Servant &servant,
                           CORBA::Environment &env)
{
  PortableServer::ObjectId_var id;
  PortableServer::ObjectId_out id_out (id);

  TAO_POA *poa = this->locate_poa_i (key,
                                     id_out,
                                     env);
  if (env.exception () != 0)
    return TAO_POA::NOT_FOUND;

  // If the POA has the RETAIN policy, the POA looks in the Active
  // Object Map to find if there is a servant associated with the
  // Object Id value from the request. If such a servant exists,
  // return TAO_POA::FOUND.
  if (poa->policies ().servant_retention () == PortableServer::RETAIN &&
      poa->active_object_map ().find (id.in (), servant) != -1)
    // Success
    return TAO_POA::FOUND;

  // If the POA has the NON_RETAIN policy or has the RETAIN policy but
  // didn't find a servant in the Active Object Map, the POA takes the
  // following actions:

  // If the USE_ACTIVE_OBJECT_MAP_ONLY policy is in effect, the POA raises
  // the OBJECT_NOT_EXIST system exception.
  if (poa->policies ().request_processing () == PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY)
    return TAO_POA::NOT_FOUND;

  // If the POA has the USE_DEFAULT_SERVANT policy, a default servant
  // has been associated with the POA, return
  // TAO_POA::DEFAULT_SERVANT. If no servant has been associated with
  // the POA, return TAO_POA::NOT_FOUND.
  if (poa->policies ().request_processing () == PortableServer::USE_DEFAULT_SERVANT)
    {
      if (poa->default_servant_ == 0)
        return TAO_POA::NOT_FOUND;
      else
        // Success
        return TAO_POA::DEFAULT_SERVANT;
    }

  // If the POA has the USE_SERVANT_MANAGER policy, a servant manager
  // has been associated with the POA, return
  // TAO_POA::SERVANT_MANAGER. If no servant manager has been
  // associated with the POA, return TAO_POA::NOT_FOUND.
  if (poa->policies ().request_processing () == PortableServer::USE_SERVANT_MANAGER)
    {
      if (CORBA::is_nil (poa->servant_activator_.in ()) &&
          CORBA::is_nil (poa->servant_locator_.in ()))
        return TAO_POA::NOT_FOUND;
      else
        // Success
        return TAO_POA::SERVANT_MANAGER;
    }

  // Failure
  return TAO_POA::NOT_FOUND;
}

PortableServer::Servant
TAO_POA::locate_poa_and_servant_i (const TAO_ObjectKey &key,
                                   const char *operation,
                                   PortableServer::ObjectId_out id,
                                   TAO_POA *&poa_impl,
                                   CORBA::Environment &env)
{
  poa_impl = this->locate_poa_i (key,
                                 id,
                                 env);
  if (env.exception () != 0)
    return 0;

  // If the POA has the RETAIN policy, the POA looks in the Active
  // Object Map to find if there is a servant associated with the
  // Object Id value from the request. If such a servant exists, the
  // POA invokes the appropriate method on the servant.
  if (poa_impl->policies ().servant_retention () == PortableServer::RETAIN)
    {
      PortableServer::Servant servant = 0;
      if (poa_impl->active_object_map ().find (*id.ptr (), servant) != -1)
        // Success
        return servant;
    }

  // If the POA has the NON_RETAIN policy or has the RETAIN policy but
  // didn't find a servant in the Active Object Map, the POA takes the
  // following actions:

  // If the USE_ACTIVE_OBJECT_MAP_ONLY policy is in effect, the POA raises
  // the OBJECT_NOT_EXIST system exception.
  if (poa_impl->policies ().request_processing () == PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY)
    {
      CORBA::Exception *exception = new CORBA::OBJECT_NOT_EXIST (CORBA::COMPLETED_NO);
      env.exception (exception);
      return 0;
    }

  // If the POA has the USE_DEFAULT_SERVANT policy, a default servant
  // has been associated with the POA so the POA will invoke the
  // appropriate method on that servant. If no servant has been
  // associated with the POA, the POA raises the OBJ_ADAPTER system
  // exception.
  if (poa_impl->policies ().request_processing () == PortableServer::USE_DEFAULT_SERVANT)
    {
      if (poa_impl->default_servant_ == 0)
        {
          CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
          env.exception (exception);
          return 0;
        }
      else
        {
          // Success
          return poa_impl->default_servant_;
        }
    }

  // If the POA has the USE_SERVANT_MANAGER policy, a servant manager
  // has been associated with the POA so the POA will invoke incarnate
  // or preinvoke on it to find a servant that may handle the
  // request. (The choice of method depends on the NON_RETAIN or
  // RETAIN policy of the POA.) If no servant manager has been
  // associated with the POA, the POA raises the OBJ_ADAPTER system
  // exception.
  //
  // If a servant manager is located and invoked, but the servant
  // manager is not directly capable of incarnating the object, it
  // (the servant manager) may deal with the circumstance in a variety
  // of ways, all of which are the application's responsibility.  Any
  // system exception raised by the servant manager will be returned
  // to the client in the reply. In addition to standard CORBA
  // exceptions, a servant manager is capable of raising a
  // ForwardRequest exception. This exception includes an object
  // reference.
  //
  if (poa_impl->policies ().request_processing () == PortableServer::USE_SERVANT_MANAGER)
    {
      if (CORBA::is_nil (poa_impl->servant_activator_.in ()) &&
          CORBA::is_nil (poa_impl->servant_locator_.in ()))
        {
          CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
          env.exception (exception);
          return 0;
        }

      PortableServer::POA_var poa = poa_impl->_this (env);
      if (env.exception () != 0)
        return 0;

      if (poa_impl->policies ().servant_retention () == PortableServer::RETAIN)
        {
          // @@
          // Invocations of incarnate on the servant manager are serialized.
          // Invocations of etherealize on the servant manager are serialized.
          // Invocations of incarnate and etherealize on the servant manager are mutually exclusive.
          PortableServer::Servant servant = poa_impl->servant_activator_->incarnate (*id.ptr (),
                                                                                     poa.in (),
                                                                                     env);
          if (env.exception () != 0 || servant == 0)
            return 0;

          // If the incarnate operation returns a servant that is
          // already active for a different Object Id and if the POA
          // also has the UNIQUE_ID policy, the incarnate has violated
          // the POA policy and is considered to be in error. The POA
          // will raise an OBJ_ADAPTER system exception for the
          // request.
          if (poa_impl->policies ().id_uniqueness () == PortableServer::UNIQUE_ID &&
              poa_impl->active_object_map ().find (servant) != -1)
            {
              CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
              env.exception (exception);
              return 0;
            }

          // The POA enters the returned Servant value into the Active
          // Object Map so that subsequent requests with the same
          // ObjectId value will be delivered directly to that servant
          // without invoking the servant manager.
          if (poa_impl->active_object_map ().bind (*id.ptr (), servant) == -1)
            {
              CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
              env.exception (exception);
              return 0;
            }

          // Success
          return servant;
        }
      else
        //
        // Don't retain servant
        //
        {
          // No serialization of invocations of preinvoke or
          // postinvoke may be assumed; there may be multiple
          // concurrent invocations of preinvoke for the same
          // ObjectId.
          //
          // The same thread will be used to preinvoke the object,
          // process the request, and postinvoke the object.
          //
          PortableServer::ServantLocator::Cookie cookie;
          PortableServer::Servant servant = poa_impl->servant_locator_->preinvoke (*id.ptr (),
                                                                                   poa.in (),
                                                                                   operation,
                                                                                   cookie,
                                                                                   env);
          if (env.exception () != 0 || servant == 0)
            return 0;

          // Remember the cookie
          TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
          TAO_POA_Current *poa_current = orb_core->poa_current ();
          poa_current->locator_cookie (cookie);

          // Success
          return servant;
        }
    }

  // Failure
  CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
  env.exception (exception);
  return 0;
}

void
TAO_POA::dispatch_servant (const TAO_ObjectKey &key,
                           CORBA::ServerRequest &req,
                           void *context,
                           CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_READ_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->dispatch_servant_i (key, req, context, env);
}

void
TAO_POA::dispatch_servant_i (const TAO_ObjectKey &key,
                             CORBA::ServerRequest &req,
                             void *context,
                             CORBA::Environment &env)
{
  PortableServer::ObjectId_var id;
  PortableServer::ObjectId_out id_out (id);
  TAO_POA *poa = 0;
  // const char *operation = req.operation ();
  const char *operation = req.op_name ();

  // Setup for POA Current
  TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
  TAO_POA_Current current_context;
  // Set the current context and remember the old one
  TAO_POA_Current *previous_context = orb_core->poa_current (&current_context);

  PortableServer::Servant servant = this->locate_poa_and_servant_i (key,
                                                                    operation,
                                                                    id_out,
                                                                    poa,
                                                                    env);
  if (env.exception () != 0 || servant == 0)
    return;

  // Setup for upcall
  poa->pre_invoke (key,
                   id.in (),
		   servant,
                   &current_context,
                   env);
  
  // Upcall
  servant->_dispatch (req,
                      context,
                      env);

  // Cleanup from upcall
  poa->post_invoke (servant,
                    operation,
                    &current_context,
                    env);
  
  // Reset old context
  orb_core->poa_current (previous_context);

}

void
TAO_POA::pre_invoke (const TAO_ObjectKey &key,
                     const PortableServer::ObjectId &id,
		     PortableServer::Servant servant,
                     TAO_POA_Current *poa_current,
                     CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);

  poa_current->POA_impl (this);
  poa_current->object_key (key);
  poa_current->object_id (id);
  poa_current->servant (servant);
}

void
TAO_POA::post_invoke (PortableServer::Servant servant,
                      const char *operation,
                      TAO_POA_Current *poa_current,
                      CORBA::Environment &env)
{
  PortableServer::ServantLocator::Cookie cookie = poa_current->locator_cookie ();

  if (cookie != 0)
    {
      PortableServer::POA_var poa = poa_current->get_POA (env);

      this->servant_locator_->postinvoke (poa_current->object_id (),
                                          poa.in (),
                                          operation,
                                          cookie,
                                          servant,
                                          env);
    }

  // poa_current->clear ();

}

const TAO_Creation_Time &
TAO_POA::creation_time (void)
{
  return this->creation_time_;
}

int
TAO_POA::parse_key_permanent_id (const TAO_ObjectKey &key,
                                 TAO_POA::String &poa_name,
                                 PortableServer::ObjectId_out id,
                                 CORBA::Boolean &persistent,
                                 TAO_Temporary_Creation_Time &poa_creation_time)
{
  return this->parse_key (key,
                          poa_name,
                          id,
                          persistent,
                          poa_creation_time,
                          0);
}

int
TAO_POA::parse_key_temporary_id (const TAO_ObjectKey &key,
                                 TAO_POA::String &poa_name,
                                 PortableServer::ObjectId_out id,
                                 CORBA::Boolean &persistent,
                                 TAO_Temporary_Creation_Time &poa_creation_time)
{
  return this->parse_key (key,
                          poa_name,
                          id,
                          persistent,
                          poa_creation_time,
                          1);
}

int
TAO_POA::rfind (const TAO_ObjectKey &key,
                char c,
                int pos) const
{
  if (pos == ACE_CString::npos)
    pos = key.length ();

  for (int i = pos - 1; i >= 0; i--)
    if (key[i] == c)
      return i;

  return TAO_POA::String::npos;
}


int
TAO_POA::parse_key (const TAO_ObjectKey &key,
                    TAO_POA::String &poa_name,
                    PortableServer::ObjectId_out id,
                    CORBA::Boolean &persistent,
                    TAO_Temporary_Creation_Time &poa_creation_time,
                    int temporary_id)
{
  // Try to find the last separator
  int last_token_position = this->rfind (key, TAO_POA::name_separator ());

  // If not found, the name is not correct
  if (last_token_position == TAO_POA::String::npos)
    return -1;

  // Check the first byte (persistence indicator)
  char object_key_type = key[0];
  if (object_key_type == this->persistent_key_type ())
    persistent = 1;
  else if (object_key_type == this->transient_key_type ())
    persistent = 0;
  else
    // Incorrect key
    return -1;

  // Starting at object_key_type_length, take the next
  // creation_time_length byte for the timestamp
  int starting_at = TAO_POA::object_key_type_length ();
  poa_creation_time.creation_time (&key[starting_at]);

  // Take the substring from creation_time_length +
  // object_key_type_length to last_token_position for the POA name
  starting_at = TAO_POA::object_key_type_length () + TAO_Creation_Time::creation_time_length ();
  int how_many = last_token_position - starting_at;
  poa_name.set ((const char *) &key[starting_at],
                how_many);

  // Take the substring from (last_token_position + separator_length)
  // to length for the objectId
  starting_at = last_token_position + TAO_POA::name_separator_length ();
  how_many = key.length () - starting_at;

  // Only for the life time of the upcall
  if (temporary_id)
    id = new PortableServer::ObjectId (how_many,
                                       how_many,
                                       (CORBA::Octet *) &key[starting_at],
                                       CORBA::B_FALSE);
  else
    // You can use the id as long as you like
    id = TAO_POA::string_to_ObjectId ((const char *) &key[starting_at],
                                      how_many);

  // Success
  return 0;
}

CORBA::Boolean
TAO_POA::persistent (void)
{
  return this->policies ().lifespan () == PortableServer::PERSISTENT;
}

PortableServer::ObjectId *
TAO_POA::create_object_id (void)
{
  // Note:  This method assumes two things:
  // 1. Locks are held when this method is called
  // 2. Size of octet == size of string element

  // Buffer for counter
  char counter[TAO_POA::max_space_required_for_ulong];

  // Convert counter into string
  ACE_OS::sprintf (counter,
                   "%ld",
                   this->counter_);

#if defined (POA_NAME_IN_POA_GENERATED_ID)

  // Calculate the required buffer size.
  // Note: 1 is for the null terminator
  TAO_POA::String id =
    this->name_ +
    TAO_POA::id_separator () +
    counter;

  char *result = id.c_str ();

#else /* POA_NAME_IN_POA_GENERATED_ID */

  char *result = counter;

#endif /* POA_NAME_IN_POA_GENERATED_ID */

  // Increment counter
  this->counter_++;

  // Create the sequence
  return TAO_POA::string_to_ObjectId (result);
}

TAO_ObjectKey *
TAO_POA::create_object_key (const PortableServer::ObjectId &id)
{
  // Calculate the space required for the key
  int buffer_size =
    this->object_key_type_length () +
    TAO_Creation_Time::creation_time_length () +
    this->complete_name_.length () +
    TAO_POA::name_separator_length () +
    id.length ();

  // Create the buffer for the key
  CORBA::Octet *buffer = TAO_ObjectKey::allocbuf (buffer_size);

  // Keeps track of where the next infomation goes; start at 0 byte
  int starting_at = 0;

  // Copy the persistence bit
  buffer[starting_at] = (CORBA::Octet) this->object_key_type ();

  // Then copy the timestamp
  starting_at += this->object_key_type_length ();
  ACE_OS::memcpy (&buffer[starting_at],
                  this->creation_time_.creation_time (),
                  TAO_Creation_Time::creation_time_length ());

  // Put the POA name into the buffer
  starting_at += TAO_Creation_Time::creation_time_length ();
  ACE_OS::memcpy (&buffer[starting_at],
                  this->complete_name_.c_str (),
                  this->complete_name_.length ());

  // Add the name separator
  starting_at += this->complete_name_.length ();
  buffer[starting_at] = (CORBA::Octet) TAO_POA::name_separator ();

  // Then copy the ID into the key
  starting_at += TAO_POA::name_separator_length ();
  ACE_OS::memcpy (&buffer[starting_at],
                  id.get_buffer (),
                  id.length ());

  // Create the key, giving the ownership of the buffer to the
  // sequence.
  return new TAO_ObjectKey (buffer_size,
                            buffer_size,
                            buffer,
                            CORBA::B_TRUE);
}

int
TAO_POA::is_poa_generated_id (const PortableServer::ObjectId &id)
{

#if defined (POA_NAME_IN_POA_GENERATED_ID)

  // Grab the buffer
  const char *id_buffer = (const char *) id.get_buffer ();

  // Check to see if the POA name is the first part of the id
  return
    this->name_.length () < id.length () &&
    ACE_OS::strncmp (id_buffer,
                     this->name_.c_str (),
                     this->name_.length ()) == 0;

#else /* POA_NAME_IN_POA_GENERATED_ID */

  return 1;

#endif /* POA_NAME_IN_POA_GENERATED_ID */
}

int
TAO_POA::is_poa_generated_key (const TAO_ObjectKey &key)
{
  // Grab the buffer
  const char *key_buffer = (const char *) key.get_buffer ();

  // Check to see if the complete POA name is the first part of the
  // key
  return
    this->complete_name_.length () < key.length () &&
    ACE_OS::strncmp (key_buffer,
                     this->complete_name_.c_str (),
                     this->complete_name_.length ()) == 0;
}

int
TAO_POA::leaf_poa_name (const TAO_POA::String &adapter_name,
                        CORBA::Environment &env)
{
  // This method does not throw any exceptions
  ACE_UNUSED_ARG (env);

  // Try to find the name separator
  if (adapter_name.find (TAO_POA::name_separator ()) == TAO_POA::String::npos)
    // If not found, the name was a leaf
    return 1;
  else
    // If found, the name was not a leaf
    return 0;
}

void
TAO_POA::parse_poa_name (const TAO_POA::String &adapter_name,
                         TAO_POA::String &topmost_poa_name,
                         TAO_POA::String &tail_poa_name,
                         CORBA::Environment &env)
{
  // Try to find the name separator
  int token_position = adapter_name.find (TAO_POA::name_separator ());

  // If not found, the name was a leaf, throw exception
  if (token_position == TAO_POA::String::npos)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return;
    }
  else
    {
      // If found, take the substring from 0 to token_position
      int starting_at = 0;
      int how_many = token_position - starting_at;
      topmost_poa_name = adapter_name.substr (starting_at,
                                              how_many);

      // Take the substring from (token_position + separator_length)
      // to length
      starting_at = token_position + TAO_POA::name_separator_length ();
      how_many = adapter_name.length () - starting_at;
      tail_poa_name = adapter_name.substr (starting_at,
                                           how_many);
    }
}

CORBA::String
TAO_POA::the_name (CORBA::Environment &env)
{
  ACE_UNUSED_ARG (env);

  return CORBA::string_dup (this->name_.c_str ());
}

TAO_POA::String
TAO_POA::complete_name (void)
{
  return this->complete_name_;
}

void
TAO_POA::set_complete_name (void)
{
  if (this->parent_ != 0)
    {
      this->complete_name_ += this->parent_->complete_name ();
      this->complete_name_ += TAO_POA::name_separator ();
    }
  this->complete_name_ += this->name_;
}

PortableServer::ObjectId *
TAO_POA::string_to_ObjectId (const char *string)
{
  // Size of string
  //
  // We DO NOT include the zero terminator, as this is simply an
  // artifact of the way strings are stored in C.
  //
  CORBA::ULong buffer_size = ACE_OS::strlen (string);

  // Create the buffer for the Id
  CORBA::Octet *buffer = PortableServer::ObjectId::allocbuf (buffer_size);

  // Copy the contents
  ACE_OS::memcpy (buffer, string, buffer_size);

  // Create and return a new ID
  return new PortableServer::ObjectId (buffer_size,
                                       buffer_size,
                                       buffer,
                                       CORBA::B_TRUE);
}

PortableServer::ObjectId *
TAO_POA::string_to_ObjectId (const char *string,
                             int size)
{
  // Create the buffer for the Id
  CORBA::Octet *buffer = PortableServer::ObjectId::allocbuf (size);

  // Copy the contents
  ACE_OS::memcpy (buffer, string, size);

  // Create and return a new ID
  return new PortableServer::ObjectId (size,
                                       size,
                                       buffer,
                                       CORBA::B_TRUE);
}

PortableServer::ObjectId *
TAO_POA::wstring_to_ObjectId (const CORBA::WChar *string)
{
  // Size of Id
  //
  // We DO NOT include the zero terminator, as this is simply an
  // artifact of the way strings are stored in C.
  //
  CORBA::ULong string_length = ACE_WString::wstrlen (string);

  size_t buffer_size = string_length * sizeof (CORBA::WChar);

  // Create the buffer for the Id
  CORBA::Octet *buffer = PortableServer::ObjectId::allocbuf (buffer_size);

  // Copy contents
  ACE_OS::memcpy (buffer, string, buffer_size);

  // Create a new ID
  return new PortableServer::ObjectId (buffer_size,
                                       buffer_size,
                                       buffer,
                                       CORBA::B_TRUE);
}

CORBA::String
TAO_POA::ObjectId_to_string (const PortableServer::ObjectId &id)
{
  // Create space
  CORBA::String string = CORBA::string_alloc (id.length ());

  // Copy the data
  ACE_OS::memcpy (string, id.get_buffer (), id.length ());

  // Null terminate the string
  string[id.length ()] = '\0';

  // Return string
  return string;
}

CORBA::WChar *
TAO_POA::ObjectId_to_wstring (const PortableServer::ObjectId &id)
{
  // Create space
  CORBA::WString string = CORBA::wstring_alloc (id.length ());

  // Copy the data
  ACE_OS::memcpy (string, id.get_buffer (), id.length () * sizeof (CORBA::WChar));

  // Null terminate the string
  string[id.length ()] = '\0';

  // Return string
  return string;
}

char
TAO_POA::name_separator (void)
{
  return '/';
}

char
TAO_POA::id_separator (void)
{
  return ':';
}

CORBA::ULong
TAO_POA::name_separator_length (void)
{
  return sizeof (char);
}

CORBA::ULong
TAO_POA::id_separator_length (void)
{
  return sizeof (char);
}

char
TAO_POA::object_key_type (void)
{
  if (this->persistent ())
    return TAO_POA::persistent_key_type ();
  else
    return TAO_POA::transient_key_type ();
}

char
TAO_POA::persistent_key_type (void)
{
  return 'P';
}

char
TAO_POA::transient_key_type (void)
{
  return 'T';
}

CORBA::ULong
TAO_POA::object_key_type_length (void)
{
  return sizeof (char);
}

PortableServer::ThreadPolicy_ptr
TAO_POA::create_thread_policy (PortableServer::ThreadPolicyValue value,
                               CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::ThreadPolicy::_nil ();

  auto_ptr<TAO_Thread_Policy> new_policy (new TAO_Thread_Policy (value, self.in ()));
  PortableServer::ThreadPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::ThreadPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

PortableServer::LifespanPolicy_ptr
TAO_POA::create_lifespan_policy (PortableServer::LifespanPolicyValue value,
                                 CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::LifespanPolicy::_nil ();

  auto_ptr<TAO_Lifespan_Policy> new_policy (new TAO_Lifespan_Policy (value, self.in ()));
  PortableServer::LifespanPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::LifespanPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

PortableServer::IdUniquenessPolicy_ptr
TAO_POA::create_id_uniqueness_policy (PortableServer::IdUniquenessPolicyValue value,
                                      CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::IdUniquenessPolicy::_nil ();

  auto_ptr<TAO_Id_Uniqueness_Policy> new_policy (new TAO_Id_Uniqueness_Policy (value, self.in ()));
  PortableServer::IdUniquenessPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::IdUniquenessPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

PortableServer::IdAssignmentPolicy_ptr
TAO_POA::create_id_assignment_policy (PortableServer::IdAssignmentPolicyValue value,
                                      CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::IdAssignmentPolicy::_nil ();

  auto_ptr<TAO_Id_Assignment_Policy> new_policy (new TAO_Id_Assignment_Policy (value, self.in ()));
  PortableServer::IdAssignmentPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::IdAssignmentPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

PortableServer::ImplicitActivationPolicy_ptr
TAO_POA::create_implicit_activation_policy (PortableServer::ImplicitActivationPolicyValue value,
                                            CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::ImplicitActivationPolicy::_nil ();

  auto_ptr<TAO_Implicit_Activation_Policy> new_policy (new TAO_Implicit_Activation_Policy (value, self.in ()));
  PortableServer::ImplicitActivationPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::ImplicitActivationPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

PortableServer::ServantRetentionPolicy_ptr
TAO_POA::create_servant_retention_policy (PortableServer::ServantRetentionPolicyValue value,
                                          CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::ServantRetentionPolicy::_nil ();

  auto_ptr<TAO_Servant_Retention_Policy> new_policy (new TAO_Servant_Retention_Policy (value, self.in ()));
  PortableServer::ServantRetentionPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::ServantRetentionPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

PortableServer::RequestProcessingPolicy_ptr
TAO_POA::create_request_processing_policy (PortableServer::RequestProcessingPolicyValue value,
                                           CORBA::Environment &env)
{
  PortableServer::POA_var self = this->_this (env);
  // Check for exceptions
  if (env.exception () != 0)
    return PortableServer::RequestProcessingPolicy::_nil ();

  auto_ptr<TAO_Request_Processing_Policy> new_policy (new TAO_Request_Processing_Policy (value, self.in ()));
  PortableServer::RequestProcessingPolicy_var result = new_policy->_this (env);
  if (env.exception () != 0)
    return PortableServer::RequestProcessingPolicy::_nil ();
  else
    {
      // Make sure that the auto_ptr does not delete the
      // implementation
      new_policy.release ();
      return result._retn ();
    }
}

void
TAO_POA::encode_sequence_to_string (CORBA::String &str,
                                    const TAO_Unbounded_Sequence<CORBA::Octet> &seq)
{
  // We must allocate a buffer which is (gag) 3 times the length
  // of the sequence, which is the length required in the worst-case
  // scenario of all non-printable characters.
  //
  // There are two strategies here...we could allocate all that space here,
  // fill it up, then copy-allocate new space of just the right length.
  // OR, we could just return this space.  The classic time-space tradeoff,
  // and for now we'll let time win out, which means that we only do the
  // allocation once.
  u_int len = 3 * seq.length() + 1 /* for zero termination */;
  str = CORBA::string_alloc (len);

  char *cp = str;

  for (u_int i = 0;
       cp < (cp+len) && i < seq.length();
       i++)
    {
      u_char byte = seq[i];
      if (isascii (byte) && isprint (byte) && byte != '\\')
        {
          *cp++ = (char) byte;
          continue;
        }

      *cp++ = '\\';
      *cp++ = ACE::nibble2hex (byte & 0x0f);
      *cp++ = ACE::nibble2hex ((byte >> 4) & 0x0f);
    }
  // Zero terminate
  *cp = '\0';
}

void
TAO_POA::decode_string_to_sequence (TAO_Unbounded_Sequence<CORBA::Octet> &seq,
                                    CORBA::String str)
{
  if (str == 0)
    {
      seq.length (0);
      return;
    }

  u_int length = ACE_OS::strlen (str);
  char *eos = str + length;
  char *cp = str;

  // Set the length of the sequence to be as long as
  // we'll possibly need...we'll reset it to the actual
  // length later.
  seq.length (length);

  u_int i = 0;
  for (;
       cp < eos && i < seq.length ();
       i++)
    {
      if (*cp == '\\')
        {
          // This is an escaped non-printable,
          // so we decode the hex values into
          // the sequence's octet
          seq[i] = (u_char) (ACE::hex2byte (cp[1]) << 4);
          seq[i] |= (u_char) ACE::hex2byte (cp[2]);
          cp += 3;
        }
      else
        {
          // Copy it in
          seq[i] = *cp++;
        }
    }

  // Set the length appropriately
  seq.length (i);
}

CORBA::Boolean
TAO_Adapter_Activator::unknown_adapter (PortableServer::POA_ptr parent,
                                        const char *name,
                                        CORBA::Environment &env)
{
  // Default policies
  PortableServer::PolicyList default_policies;

  // This assumes that the lock on the parent is recursive
  PortableServer::POA_var child = parent->create_POA (name,
                                                      PortableServer::POAManager::_nil (),
                                                      default_policies,
                                                      env);

  if (env.exception () != 0)
    return CORBA::B_FALSE;
  else
    {
      PortableServer::AdapterActivator_var activator = this->_this (env);
      if (env.exception () != 0)
        {
          child->destroy (CORBA::B_FALSE,
                          CORBA::B_FALSE,
                          env);
          return CORBA::B_FALSE;
        }

      child->the_activator (activator.in (), env);

      if (env.exception () != 0)
        {
          child->destroy (CORBA::B_FALSE,
                          CORBA::B_FALSE,
                          env);
          return CORBA::B_FALSE;
        }

      // Finally everything is fine
      return CORBA::B_TRUE;
    }
}

TAO_POA_Manager::TAO_POA_Manager (void)
  : state_ (HOLDING),
    closing_down_ (0),
    lock_ (),
    poa_collection_ ()
{
}

TAO_POA_Manager::~TAO_POA_Manager (void)
{
}

void
TAO_POA_Manager::activate (CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  // This operation changes the state of the POA manager to active. If
  // issued while the POA manager is in the inactive state, the
  // AdapterInactive exception is raised.  Entering the active state
  // enables the associated POAs to process requests.

  if (this->state_ == INACTIVE)
    {
      CORBA::Exception *exception = new PortableServer::POAManager::AdapterInactive;
      env.exception (exception);
      return;
    }
  else
    this->state_ = ACTIVE;
}

void
TAO_POA_Manager::hold_requests (CORBA::Boolean wait_for_completion,
                                CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  // This operation changes the state of the POA manager to
  // holding. If issued while the POA manager is in the inactive
  // state, the AdapterInactive exception is raised.  Entering the
  // holding state causes the associated POAs to queue incoming
  // requests.  Any requests that have been queued but have not
  // started executing will continue to be queued while in the holding
  // state.

  if (this->state_ == INACTIVE)
    {
      CORBA::Exception *exception = new PortableServer::POAManager::AdapterInactive;
      env.exception (exception);
      return;
    }
  else
    this->state_ = HOLDING;

  // If the wait_for_completion parameter is FALSE, this operation
  // returns immediately after changing the state. If the parameter is
  // TRUE, this operation does not return until either there are no
  // actively executing requests in any of the POAs associated with
  // this POA manager (that is, all requests that were started prior
  // to the state change have completed) or the state of the POA
  // manager is changed to a state other than holding.

  ACE_UNUSED_ARG (wait_for_completion);
}

void
TAO_POA_Manager::discard_requests (CORBA::Boolean wait_for_completion,
                                   CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  // This operation changes the state of the POA manager to
  // discarding. If issued while the POA manager is in the inactive
  // state, the AdapterInactive exception is raised.  Entering the
  // discarding state causes the associated POAs to discard incoming
  // requests.  In addition, any requests that have been queued but
  // have not started executing are discarded. When a request is
  // discarded, a TRANSIENT system exception is returned to the
  // client.

  if (this->state_ == INACTIVE)
    {
      CORBA::Exception *exception = new PortableServer::POAManager::AdapterInactive;
      env.exception (exception);
      return;
    }
  else
    this->state_ = DISCARDING;

  // If the wait_for_completion parameter is FALSE, this operation
  // returns immediately after changing the state. If the parameter is
  // TRUE, this operation does not return until either there are no
  // actively executing requests in any of the POAs associated with
  // this POA manager (that is, all requests that were started prior
  // to the state change have completed) or the state of the POA
  // manager is changed to a state other than discarding.

  ACE_UNUSED_ARG (wait_for_completion);
}

void
TAO_POA_Manager::deactivate (CORBA::Boolean etherealize_objects,
                             CORBA::Boolean wait_for_completion,
                             CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->closing_down_ = 1;

  // This operation changes the state of the POA manager to
  // inactive. If issued while the POA manager is in the inactive
  // state, the AdapterInactive exception is raised.  Entering the
  // inactive state causes the associated POAs to reject requests that
  // have not begun to be executed as well as any new requests.

  if (this->state_ == INACTIVE)
    {
      CORBA::Exception *exception = new PortableServer::POAManager::AdapterInactive;
      env.exception (exception);
      return;
    }
  else
    this->state_ = INACTIVE;

  // After changing the state, if the etherealize_objects parameter is:
  //
  // a) TRUE - the POA manager will cause all associated POAs that
  // have the RETAIN and USE_SERVANT_MANAGER policies to perform the
  // etherealize operation on the associated servant manager for all
  // active objects.
  //
  // b) FALSE - the etherealize operation is not called. The purpose
  // is to provide developers with a means to shut down POAs in a
  // crisis (for example, unrecoverable error) situation.

  for (POA_COLLECTION::iterator iterator = this->poa_collection_.begin ();
       iterator != this->poa_collection_.end () && env.exception () == 0;
       iterator++)
    {
      TAO_POA *poa = *iterator;
      poa->destroy (etherealize_objects,
                    wait_for_completion,
                    env);
    }

  // If the wait_for_completion parameter is FALSE, this operation
  // will return immediately after changing the state. If the
  // parameter is TRUE, this operation does not return until there are
  // no actively executing requests in any of the POAs associated with
  // this POA manager (that is, all requests that were started prior
  // to the state change have completed) and, in the case of a TRUE
  // etherealize_objects, all invocations of etherealize have
  // completed for POAs having the RETAIN and USE_SERVANT_MANAGER
  // policies.

  // If the ORB::shutdown operation is called, it makes a call on
  // deactivate with a TRUE etherealize_objects parameter for each POA
  // manager known in the process; the wait_for_completion parameter
  // to deactivate will be the same as the similarly named parameter
  // of ORB::shutdown.
}

TAO_POA_Manager::Processing_State
TAO_POA_Manager::state (CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_READ_GUARD_RETURN (ACE_Lock, monitor, this->lock (), UNKNOWN, env);

  return this->state_;
}

void
TAO_POA_Manager::remove_poa (TAO_POA *poa,
                             CORBA::Environment &env)
{
  // If we are not closing down, we must remove this poa from our
  // collection.
  if (!this->closing_down_)
    {
      // Lock access to the POAManager for the duration of this transaction
      TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

      this->remove_poa_i (poa,
                          env);
    }

  // If we are closing down, we are currently iterating over our poa
  // collection and there is not need to remove this poa from our
  // collection.
}

void
TAO_POA_Manager::remove_poa_i (TAO_POA *poa,
                               CORBA::Environment &env)
{
  int result = this->poa_collection_.remove (poa);

  if (result != 0)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return;
    }
}

void
TAO_POA_Manager::register_poa (TAO_POA *poa,
                               CORBA::Environment &env)
{
  // Lock access to the POAManager for the duration of this transaction
  TAO_POA_WRITE_GUARD (ACE_Lock, monitor, this->lock (), env);

  this->register_poa_i (poa,
                        env);
}

void
TAO_POA_Manager::register_poa_i (TAO_POA *poa,
                                 CORBA::Environment &env)
{
  int result = this->poa_collection_.insert (poa);

  if (result != 0)
    {
      CORBA::Exception *exception = new CORBA::OBJ_ADAPTER (CORBA::COMPLETED_NO);
      env.exception (exception);
      return;
    }
}

ACE_Lock &
TAO_POA_Manager::lock (void)
{
  return this->lock_;
}

TAO_POA_Manager *
TAO_POA_Manager::clone (void)
{
  return new TAO_POA_Manager;
}

TAO_Strategy_POA::TAO_Strategy_POA (const TAO_POA::String &adapter_name,
                                    TAO_POA_Manager &poa_manager,
                                    const TAO_POA_Policies &policies,
                                    TAO_POA *parent,
                                    CORBA::Environment &env)
  : TAO_POA (adapter_name,
             poa_manager,
             policies,
             parent,
             env),
    lock_ (0)
{
  this->lock_ = TAO_ORB_Core_instance ()->server_factory ()->create_poa_lock ();
}

TAO_Strategy_POA::TAO_Strategy_POA (const TAO_POA::String &adapter_name,
                                    TAO_POA_Manager &poa_manager,
                                    const TAO_POA_Policies &policies,
                                    TAO_POA *parent,
                                    TAO_Object_Table &active_object_map,
                                    CORBA::Environment &env)
  : TAO_POA (adapter_name,
             poa_manager,
             policies,
             parent,
             active_object_map,
             env),
    lock_ (0)
{
  this->lock_ = TAO_ORB_Core_instance ()->server_factory ()->create_poa_lock ();
}

TAO_Strategy_POA::~TAO_Strategy_POA (void)
{
  delete this->lock_;
}

TAO_POA *
TAO_Strategy_POA::clone (const TAO_POA::String &adapter_name,
                         TAO_POA_Manager &poa_manager,
                         const TAO_POA_Policies &policies,
                         TAO_POA *parent,
                         CORBA::Environment &env)
{
  return new SELF (adapter_name,
                   poa_manager,
                   policies,
                   parent,
                   env);
}

TAO_POA *
TAO_Strategy_POA::clone (const TAO_POA::String &adapter_name,
                         TAO_POA_Manager &poa_manager,
                         const TAO_POA_Policies &policies,
                         TAO_POA *parent,
                         TAO_Object_Table &active_object_map,
                         CORBA::Environment &env)
{
  return new SELF (adapter_name,
                   poa_manager,
                   policies,
                   parent,
                   active_object_map,
                   env);
}

ACE_Lock &
TAO_Strategy_POA::lock (void)
{
  return *this->lock_;
}

TAO_Strategy_POA_Manager::TAO_Strategy_POA_Manager (void)
  : TAO_POA_Manager (),
    lock_ (0)
{
  this->lock_ = TAO_ORB_Core_instance ()->server_factory ()->create_poa_mgr_lock ();
}

TAO_Strategy_POA_Manager::~TAO_Strategy_POA_Manager (void)
{
  delete this->lock_;
}

TAO_POA_Manager *
TAO_Strategy_POA_Manager::clone (void)
{
  return new SELF;
}

ACE_Lock &
TAO_Strategy_POA_Manager::lock (void)
{
  return *this->lock_;
}

TAO_POA_Current::TAO_POA_Current (void)
  : poa_impl_ (0),
    object_id_ (0),
    object_key_ (0),
    cookie_ (0),
    servant_ (0)
{
}

TAO_POA_Current::TAO_POA_Current (TAO_POA *impl,
                                  const TAO_ObjectKey &key,
                                  const PortableServer::ObjectId &id,
                                  PortableServer::Servant servant)
  : poa_impl_ (impl),
    object_id_ (&id),
    object_key_ (&key),
    cookie_ (0),
    servant_ (servant)
{}

TAO_POA_Current::~TAO_POA_Current (void)
{
}

PortableServer::POA_ptr
TAO_POA_Current::get_POA (CORBA::Environment &env)
{
  if (!this->context_is_valid ())
    {
      CORBA::Exception *exception = new PortableServer::Current::NoContext;
      env.exception (exception);
      return PortableServer::POA::_nil ();
    }

  PortableServer::POA_var result = this->poa_impl_->_this (env);
  if (env.exception () != 0)
    return PortableServer::POA::_nil ();
  else
    return result._retn ();
}

PortableServer::ObjectId *
TAO_POA_Current::get_object_id (CORBA::Environment &env)
{
  if (!this->context_is_valid ())
    {
      CORBA::Exception *exception = new PortableServer::Current::NoContext;
      env.exception (exception);
      return 0;
    }

  // Create a new one and pass it back
  return new PortableServer::ObjectId (*this->object_id_);
}

void
TAO_POA_Current::clear (void)
{
  this->poa_impl_ = 0;
  this->object_id_ = 0;
  this->servant_ = 0;
  this->object_key_ = 0;
  this->cookie_ = 0;
}

int
TAO_POA_Current::context_is_valid (void)
{
  return
    this->poa_impl_ != 0 &&
    this->object_id_ != 0 &&
    this->servant_ != 0 &&
    this->object_key_ != 0;
}

void
TAO_POA_Current::POA_impl (TAO_POA *impl)
{
  this->poa_impl_ = impl;
}

TAO_POA *
TAO_POA_Current::POA_impl (void) const
{
  return this->poa_impl_;
}

void
TAO_POA_Current::object_id (const PortableServer::ObjectId &id)
{
  this->object_id_ = &id;
}

const PortableServer::ObjectId &
TAO_POA_Current::object_id (void) const
{
  return *this->object_id_;
}

void
TAO_POA_Current::object_key (const TAO_ObjectKey &key)
{
  this->object_key_ = &key;
}

const TAO_ObjectKey &
TAO_POA_Current::object_key (void) const
{
  return *this->object_key_;
}

void
TAO_POA_Current::servant (PortableServer::Servant servant)
{
  this->servant_ = servant;
}

PortableServer::Servant
TAO_POA_Current::servant (void) const
{
  return this->servant_;
}

int
TAO_POA_Current::in_upcall (void) const
{
  return (this->servant_ != 0);
}

PortableServer::ServantLocator::Cookie
TAO_POA_Current::locator_cookie (void) const
{
  return this->cookie_;
}

void
TAO_POA_Current::locator_cookie (PortableServer::ServantLocator::Cookie cookie)
{
  this->cookie_ = cookie;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Auto_Basic_Ptr<TAO_Id_Assignment_Policy>;
template class ACE_Auto_Basic_Ptr<TAO_Id_Uniqueness_Policy>;
template class ACE_Auto_Basic_Ptr<TAO_Implicit_Activation_Policy>;
template class ACE_Auto_Basic_Ptr<TAO_Lifespan_Policy>;
template class ACE_Auto_Basic_Ptr<TAO_Object_Table_Iterator_Impl>;
template class ACE_Auto_Basic_Ptr<TAO_POA>;
template class ACE_Auto_Basic_Ptr<TAO_Request_Processing_Policy>;
template class ACE_Auto_Basic_Ptr<TAO_Servant_Retention_Policy>;
template class ACE_Auto_Basic_Ptr<TAO_Thread_Policy>;
template class ACE_Lock_Adapter<ACE_Null_Mutex>;
#if defined (ACE_HAS_THREADS)
template class ACE_Lock_Adapter<ACE_Recursive_Thread_Mutex>;
template class ACE_Lock_Adapter<ACE_Thread_Mutex>;
#endif /* ACE_HAS_THREADS */
template class ACE_Map_Entry<TAO_Unbounded_Sequence<unsigned char>, TAO_ServantBase *>;
template class ACE_Map_Iterator<ACE_CString, TAO_POA *, ACE_Null_Mutex>;
template class ACE_Map_Iterator_Base<ACE_CString, TAO_POA *, ACE_Null_Mutex>;
template class ACE_Map_Manager<ACE_CString, TAO_POA *, ACE_Null_Mutex>;
template class ACE_Read_Guard<ACE_Lock>;
template class ACE_Unbounded_Set<TAO_POA *>;
template class ACE_Unbounded_Set_Iterator<TAO_POA *>;
template class ACE_Write_Guard<ACE_Lock>;
template class TAO_Object_Manager<PortableServer::Policy>;
template class TAO_Unbounded_Object_Sequence<PortableServer::Policy>;
template class auto_ptr<TAO_Id_Assignment_Policy>;
template class auto_ptr<TAO_Id_Uniqueness_Policy>;
template class auto_ptr<TAO_Implicit_Activation_Policy>;
template class auto_ptr<TAO_Lifespan_Policy>;
template class auto_ptr<TAO_Object_Table_Iterator_Impl>;
template class auto_ptr<TAO_POA>;
template class auto_ptr<TAO_Request_Processing_Policy>;
template class auto_ptr<TAO_Servant_Retention_Policy>;
template class auto_ptr<TAO_Thread_Policy>;
template class ACE_Map_Entry<ACE_CString, TAO_POA *>;
template class ACE_Map_Reverse_Iterator<ACE_CString, TAO_POA *, ACE_Null_Mutex>;
template class ACE_Node<TAO_POA *>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Id_Assignment_Policy>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Id_Uniqueness_Policy>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Implicit_Activation_Policy>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Lifespan_Policy>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Object_Table_Iterator_Impl>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_POA>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Request_Processing_Policy>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Servant_Retention_Policy>
#pragma instantiate ACE_Auto_Basic_Ptr<TAO_Thread_Policy>
#pragma instantiate ACE_Lock_Adapter<ACE_Null_Mutex>
#if defined (ACE_HAS_THREADS)
#pragma instantiate ACE_Lock_Adapter<ACE_Recursive_Thread_Mutex>
#pragma instantiate ACE_Lock_Adapter<ACE_Thread_Mutex>
#endif /* ACE_HAS_THREADS */
#pragma instantiate ACE_Map_Entry<TAO_Unbounded_Sequence<unsigned char>, TAO_ServantBase *>
#pragma instantiate ACE_Map_Iterator<ACE_CString, TAO_POA *, ACE_Null_Mutex>
#pragma instantiate ACE_Map_Iterator_Base<ACE_CString, TAO_POA *, ACE_Null_Mutex>
#pragma instantiate ACE_Map_Manager<ACE_CString, TAO_POA *, ACE_Null_Mutex>
#pragma instantiate ACE_Read_Guard<ACE_Lock>
#pragma instantiate ACE_Unbounded_Set<TAO_POA *>
#pragma instantiate ACE_Unbounded_Set_Iterator<TAO_POA *>
#pragma instantiate ACE_Write_Guard<ACE_Lock>
#pragma instantiate TAO_Object_Manager<PortableServer::Policy>
#pragma instantiate TAO_Unbounded_Object_Sequence<PortableServer::Policy>
#pragma instantiate auto_ptr<TAO_Id_Assignment_Policy>
#pragma instantiate auto_ptr<TAO_Id_Uniqueness_Policy>
#pragma instantiate auto_ptr<TAO_Implicit_Activation_Policy>
#pragma instantiate auto_ptr<TAO_Lifespan_Policy>
#pragma instantiate auto_ptr<TAO_Object_Table_Iterator_Impl>
#pragma instantiate auto_ptr<TAO_POA>
#pragma instantiate auto_ptr<TAO_Request_Processing_Policy>
#pragma instantiate auto_ptr<TAO_Servant_Retention_Policy>
#pragma instantiate auto_ptr<TAO_Thread_Policy>
#pragma instantiate ACE_Map_Entry<ACE_CString, TAO_POA *>
#pragma instantiate ACE_Map_Reverse_Iterator<ACE_CString, TAO_POA *, ACE_Null_Mutex>
#pragma instantiate ACE_Node<TAO_POA *>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */