summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Trader_Utils.cpp
blob: 4de7d0c86385565cdc724a698ed9997243d0c541 (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
// $Id$

#include "orbsvcs/Trader/Trader_Utils.h"
#include "ace/OS_NS_string.h"

ACE_RCSID(Trader, Trader_Utils, "$Id$")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

bool
operator== (CORBA::String_var const & lhs,
            CORBA::String_var const & rhs)
{
  return (ACE_OS::strcmp (lhs.in (), rhs.in ()) == 0);
}

TAO_Policy_Creator::TAO_Policy_Creator (int num_policies)
  : policies_ (num_policies),
    num_policies_ (0)
{
  for (int i = 0; i < TAO_Policies::REQUEST_ID + 1; i++)
    this->poltable_[i] = -1;
}

void
TAO_Policy_Creator::search_card (CORBA::ULong scard)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::SEARCH_CARD);
  policy.value <<= scard;
}

void
TAO_Policy_Creator::match_card (CORBA::ULong mcard)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::MATCH_CARD);
  policy.value <<= mcard;
}

void
TAO_Policy_Creator::return_card (CORBA::ULong rcard)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::RETURN_CARD);
  policy.value <<= rcard;
}

void
TAO_Policy_Creator::use_modifiable_properties (CORBA::Boolean mod_props)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::USE_MODIFIABLE_PROPERTIES);
  policy.value <<= CORBA::Any::from_boolean (mod_props);
}

void
TAO_Policy_Creator::use_dynamic_properties (CORBA::Boolean dyn_props)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::USE_DYNAMIC_PROPERTIES);
  policy.value <<= CORBA::Any::from_boolean (dyn_props);
}

void
TAO_Policy_Creator::use_proxy_offers (CORBA::Boolean prox_offs)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::USE_PROXY_OFFERS);
  policy.value <<= CORBA::Any::from_boolean (prox_offs);
}

void
TAO_Policy_Creator::starting_trader (const CosTrading::TraderName& name)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::STARTING_TRADER);
  policy.value <<= name;
}

void
TAO_Policy_Creator::starting_trader (CosTrading::TraderName* name)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::STARTING_TRADER);
  policy.value <<= name;
}

void
TAO_Policy_Creator::
link_follow_rule (CosTrading::FollowOption follow_option)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::LINK_FOLLOW_RULE);
  policy.value <<= follow_option;
}

void
TAO_Policy_Creator::hop_count (CORBA::ULong hop_count)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::HOP_COUNT);
  policy.value <<= hop_count;
}

void
TAO_Policy_Creator::exact_type_match (CORBA::Boolean exact_type)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::EXACT_TYPE_MATCH);
  policy.value <<= CORBA::Any::from_boolean (exact_type);
}

void
TAO_Policy_Creator::request_id (const CosTrading::Admin::OctetSeq& request_id)
{
  CosTrading::Policy& policy =
    this->fetch_next_policy (TAO_Policies::REQUEST_ID);
  policy.value <<= request_id;
}

TAO_Policy_Creator::operator const CosTrading::PolicySeq& (void) const
{
  return this->policies_;
}

const CosTrading::PolicySeq&
TAO_Policy_Creator::policy_seq (void) const
{
  return this->policies_;
}

CosTrading::Policy&
TAO_Policy_Creator::fetch_next_policy (TAO_Policies::POLICY_TYPE pol_type)
{
  CORBA::ULong index = 0;

  if (this->poltable_[pol_type] == -1)
    {
      // Expand the policy sequence, and copy in the policy name into
      // the new element.
      CORBA::ULong length = this->policies_.length ();
      this->num_policies_++;

      if (length < this->num_policies_)
        this->policies_.length (this->num_policies_);

      index = this->num_policies_ - 1;

      // Ensure the starting trader policy gets the first slot.
      if (pol_type != TAO_Policies::STARTING_TRADER
          || index == 0)
        {
          this->policies_[index].name = TAO_Policies::POLICY_NAMES[pol_type];
          this->poltable_[pol_type] = index;
        }
      else
        {
          // Copy the element in the first slot to the newly
          // allocated slot.
          TAO_Policies::POLICY_TYPE occupying_policy =
            TAO_Policies::STARTING_TRADER;
          for (CORBA::ULong i = 0; i < this->num_policies_ - 1; i++)
            {
              if (this->poltable_[i] == 0)
                {
                  occupying_policy =
                    static_cast<TAO_Policies::POLICY_TYPE> (i);
                  break;
                }
            }

          this->poltable_[occupying_policy] = index;
          this->poltable_[TAO_Policies::STARTING_TRADER] = 0;
          this->policies_[index].name =
            TAO_Policies::POLICY_NAMES[occupying_policy];
          this->policies_[index].value = this->policies_[0].value;
          this->policies_[0].name =
            TAO_Policies::POLICY_NAMES[TAO_Policies::STARTING_TRADER];

          index = 0;
        }
    }
  else
    index = this->poltable_[pol_type];

  return this->policies_[index];
}

// Constructor

TAO_Property_Evaluator::
TAO_Property_Evaluator(const CosTrading::PropertySeq& props,
                       CORBA::Boolean supports_dp)
  : props_ (props),
    supports_dp_ (supports_dp),
    dp_cache_ (new CORBA::Any*[props.length ()])
{
  if (this->dp_cache_ != 0)
    {
      for (CORBA::ULong i = 0; i < this->props_.length (); i++)
        this->dp_cache_[i] = 0;
    }
}


TAO_Property_Evaluator::
TAO_Property_Evaluator(CosTrading::Offer& offer,
                       CORBA::Boolean supports_dp)
  : props_ (offer.properties),
    supports_dp_ (supports_dp),
    dp_cache_ (new CORBA::Any*[offer.properties.length ()])
{
  if (this->dp_cache_ != 0)
    for (CORBA::ULong i = 0; i < this->props_.length (); i++)
      this->dp_cache_[i] = 0;
}

TAO_Property_Evaluator::~TAO_Property_Evaluator (void)
{
  // Clean up the results of any dynamic properties.
  for (CORBA::ULong i = 0; i < this->props_.length (); i++)
    if (this->dp_cache_[i] != 0)
      delete this->dp_cache_[i];

  delete [] this->dp_cache_;
}

int
TAO_Property_Evaluator::is_dynamic_property (int index)
{
  int return_value = 0;
  int num_properties = this->props_.length();

  // Ensure index is in bounds.
  if (index >= 0 && index < num_properties)
    {
      // Obtain the value of the property at index <index>.
      const CORBA::Any& value = this->props_[index].value;
      CORBA::TypeCode_var type = value.type ();

      // @@ Seth, this will not work on platforms using environment variable.
      ACE_DECLARE_NEW_CORBA_ENV;
      CORBA::Boolean equal = type->equal (CosTradingDynamic::_tc_DynamicProp
                                          ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);

      if (equal)
        return_value = 1;
    }

  return return_value;
}

CORBA::Any*
TAO_Property_Evaluator::property_value (int index
                                        ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  CORBA::Any* prop_val = 0;
  CORBA::Boolean in_cache =
    this->dp_cache_ != 0 && this->dp_cache_[index] != 0;

  int dynamic = this->is_dynamic_property (index);

  if (!dynamic)
    prop_val = (CORBA::Any *) &(this->props_[index].value);
  else if (this->supports_dp_ && in_cache)
    prop_val = this->dp_cache_[index];
  else if (this->supports_dp_)
    {
      // Property is defined at this point.
      CosTradingDynamic::DynamicProp* dp_struct;
      const CORBA::String_var name = this->props_[index].name.in ();
      const CORBA::Any& value = this->props_[index].value;

      // Extract the DP_Struct.
      value >>= dp_struct;

      CosTradingDynamic::DynamicPropEval_var dp_eval =
        CosTradingDynamic::DynamicPropEval::_duplicate (dp_struct->eval_if.in ());

      if (CORBA::is_nil (dp_eval.in ()))
        {
          ACE_THROW_RETURN (CosTradingDynamic::
                            DPEvalFailure (name,
                                           CORBA::TypeCode::_nil (),
                                           CORBA::Any ()),
                            prop_val);
        }
      else
        {
          CORBA::TypeCode* type = dp_struct->returned_type.in ();
          CORBA::Any& info = dp_struct->extra_info;

          ACE_TRY
            {
              // Retrieve the value of the dynamic property.
              prop_val = dp_eval->evalDP(name, type, info ACE_ENV_ARG_PARAMETER);
              ACE_TRY_CHECK;

              if (this->dp_cache_ != 0)
                this->dp_cache_[index] = prop_val;
            }
          ACE_CATCH (CORBA::SystemException, excp)
            {
              ACE_TRY_THROW
                (CosTradingDynamic::DPEvalFailure (name, type, info));
            }
          ACE_ENDTRY;
          ACE_CHECK_RETURN (prop_val);
        }
    }

  return prop_val;
}

CORBA::TypeCode_ptr
TAO_Property_Evaluator::property_type (int index)
{
  CORBA::TypeCode_ptr prop_type = CORBA::TypeCode::_nil();

  // Determine if property is both defined and dynamic.
  if (this->is_dynamic_property (index))
    {
      // Extract type information from the DP_Struct.
      const CORBA::Any& value = this->props_[index].value;
      CosTradingDynamic::DynamicProp* dp_struct = 0;
      value >>= dp_struct;

      // Grab a pointer to the returned_type description
      prop_type = CORBA::TypeCode::_duplicate (dp_struct->returned_type.in ());
    }
  else
    // TypeCode is self-evident at this point.
    prop_type = this->props_[index].value.type ();

  return prop_type;
}

TAO_Property_Evaluator_By_Name::
TAO_Property_Evaluator_By_Name (const CosTrading::PropertySeq& properties
                                ACE_ENV_ARG_DECL,
                                CORBA::Boolean supports_dp)
  ACE_THROW_SPEC ((CosTrading::DuplicatePropertyName,
                   CosTrading::IllegalPropertyName))
    : TAO_Property_Evaluator (properties, supports_dp)
{
  int length = this->props_.length();

  for (int i = 0; i < length; i++)
    {
      const CosTrading::Property& prop = this->props_[i];

      if (! TAO_Trader_Base::is_valid_property_name (prop.name))
        ACE_THROW (CosTrading::IllegalPropertyName (prop.name));

      CORBA::String_var prop_name = prop.name.in ();
      if (this->table_.bind (prop_name, i))
        ACE_THROW (CosTrading::DuplicatePropertyName (prop.name));
    }
}

TAO_Property_Evaluator_By_Name::
TAO_Property_Evaluator_By_Name(CosTrading::Offer& offer,
                               CORBA::Boolean supports_dp)
  : TAO_Property_Evaluator(offer, supports_dp)
{
  int length = this->props_.length();

  for (int i = 0; i < length; i++)
    {
      CORBA::String_var prop_name = (const char*) this->props_[i].name;
      this->table_.bind (prop_name, i);
    }
}

int
TAO_Property_Evaluator_By_Name::
is_dynamic_property(const char* property_name)
{
  int predicate = 0;
  int index = 0;
  CORBA::String_var prop_name (property_name);

  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (this->table_.find (prop_name, index) == 0)
    predicate = this->TAO_Property_Evaluator::is_dynamic_property(index);

  return predicate;
}

CORBA::Any*
TAO_Property_Evaluator_By_Name::property_value (const char* property_name
                                                ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  int index = 0;
  CORBA::Any* prop_value = 0;
  CORBA::String_var prop_name (property_name);

  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (this->table_.find (prop_name, index) == 0)
    {
      prop_value =
        this->TAO_Property_Evaluator::property_value (index
                                                      ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);
    }

  return prop_value;
}

CORBA::TypeCode_ptr
TAO_Property_Evaluator_By_Name::property_type (const char* property_name)
{
  int index = 0;
  CORBA::String_var prop_name (property_name);
  CORBA::TypeCode_ptr prop_type = CORBA::TypeCode::_nil();

  // If the property name is in the map, delegate evaluation to our
  // superclass. Otherwise, throw an exception.
  if (this->table_.find (prop_name, index) == 0)
    prop_type = this->TAO_Property_Evaluator::property_type (index);

  return prop_type;
}

const CosTrading::Property*
TAO_Property_Evaluator_By_Name::get_property (const char* property_name)
{
  int index = 0;
  CosTrading::Property* property = 0;
  CORBA::String_var prop_name (property_name);

  if (this->table_.find (prop_name, index) == 0)
    property = (CosTrading::Property *) &this->props_[index];

  return property;
}

TAO_Dynamic_Property::~TAO_Dynamic_Property (void)
{
}

CosTradingDynamic::DynamicProp*
TAO_Dynamic_Property::
construct_dynamic_prop (const char* name,
                        CORBA::TypeCode_ptr returned_type,
                        const CORBA::Any& extra_info)
{
  ACE_UNUSED_ARG (name);

  CosTradingDynamic::DynamicProp* dp_struct = 0;

  ACE_NEW_RETURN (dp_struct,
                  CosTradingDynamic::DynamicProp,
                  0);

  if (this->prop_.in () == CosTradingDynamic::DynamicPropEval::_nil ())
    {
      // Seth, we need a way to either propagate exceptions out.
      ACE_DECLARE_NEW_CORBA_ENV;

      this->prop_ = this->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);

      this->_remove_ref (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (0);
    }

  dp_struct->eval_if =
    CosTradingDynamic::DynamicPropEval::_duplicate (this->prop_.in ());

  dp_struct->returned_type =
    CORBA::TypeCode::_duplicate (returned_type);
  dp_struct->extra_info = extra_info;

  return dp_struct;
}

void
TAO_Dynamic_Property::destroy (void)
{
  if (this->prop_.in () != CosTradingDynamic::DynamicPropEval::_nil ())
    {
      // @@ Seth, we need a way to propagate exceptions out.
      ACE_DECLARE_NEW_CORBA_ENV;
      PortableServer::POA_var poa = this->_default_POA (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      PortableServer::ObjectId_var id =
        poa->servant_to_id (this
                            ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      poa->deactivate_object (id.in ()
                              ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }
}

const char* TAO_Policies::POLICY_NAMES[] =
{
  "starting_trader",
  "exact_type_match",
  "hop_count",
  "link_follow_rule",
  "match_card",
  "return_card",
  "search_card",
  "use_dynamic_properties",
  "use_modifiable_properties",
  "use_proxy_offers",
  "request_id"
};

TAO_Policies::TAO_Policies (TAO_Trader_Base& trader,
                            const CosTrading::PolicySeq& policies
                            ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CosTrading::Lookup::IllegalPolicyName,
                  CosTrading::DuplicatePolicyName))
  : trader_ (trader)
{
  for (int i = 0; i < TAO_NUM_POLICIES; i++)
    this->policies_[i] = 0;

  for (CORBA::ULong j = 0; j < policies.length (); j++)
    {
      const char *pol_name = (const char *) policies[j].name;
      size_t length = (pol_name == 0) ? 0 : ACE_OS::strlen (pol_name);
      int index = -1;

      if (length < ACE_OS::strlen (POLICY_NAMES[HOP_COUNT]))
        ACE_THROW (CosTrading::Lookup::IllegalPolicyName (pol_name));

      switch (pol_name[0])
        {
        case 'e':
          index = EXACT_TYPE_MATCH;
          break;
        case 'h':
          index = HOP_COUNT;
          break;
        case 'l':
          index = LINK_FOLLOW_RULE;
          break;
        case 'm':
          index = MATCH_CARD;
          break;
        case 'r':
          if (pol_name[2] == 't')
            index = RETURN_CARD;
          else if (pol_name[2] == 'q')
            index = REQUEST_ID;
          break;
        case 's':
          if (pol_name[1] == 't')
            index = STARTING_TRADER;
          else if (pol_name[1] == 'e')
            index = SEARCH_CARD;
          break;
        case 'u':
          if (pol_name[4] == 'd')
            index = USE_DYNAMIC_PROPERTIES;
          if (pol_name[4] == 'm')
            index = USE_MODIFIABLE_PROPERTIES;
          if (pol_name[4] == 'p')
            index = USE_PROXY_OFFERS;
        }

      // Match the name of the policy, and insert its value into the
      // vector.
      if (index == -1 || ACE_OS::strcmp (POLICY_NAMES[index], pol_name) != 0)
        ACE_THROW (CosTrading::Lookup::IllegalPolicyName (pol_name));
      else if (this->policies_[index] != 0)
        ACE_THROW (CosTrading::DuplicatePolicyName (pol_name));
      else
        this->policies_[index] = (CosTrading::Policy *) &(policies[j]);
    }
}

TAO_Policies::~TAO_Policies (void)
{
}

CORBA::ULong
TAO_Policies::ulong_prop (POLICY_TYPE pol
                          ACE_ENV_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  CORBA::ULong return_value = 0, max_value = 0;
  const TAO_Import_Attributes_i& import_attrs =
    this->trader_.import_attributes ();

  // Discover the default values for each of the possible cardinality
  // policies.
  switch (pol)
    {
    case SEARCH_CARD:
      return_value = import_attrs.def_search_card ();
      max_value = import_attrs.max_search_card ();
      break;
    case MATCH_CARD:
      return_value = import_attrs.def_match_card ();
      max_value = import_attrs.max_match_card ();
      break;
    case RETURN_CARD:
      return_value = import_attrs.def_return_card ();
      max_value = import_attrs.max_return_card ();
      break;
    case HOP_COUNT:
      return_value = import_attrs.def_hop_count ();
      max_value = import_attrs.max_hop_count ();
      break;
    default:
      break;
    }

  if (this->policies_[pol] != 0)
    {
      // Extract the desired policy value.
      const CosTrading::Policy* policy = this->policies_[pol];
      const CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode_var type = value.type ();

      CORBA::Boolean equal_ulong = type->equal (CORBA::_tc_ulong ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (return_value);

      if (!equal_ulong)
        ACE_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
                          return_value);
      else
        value >>= return_value;

      if (max_value < return_value)
        return_value = max_value;
    }

  return return_value;
}

CORBA::ULong
TAO_Policies::search_card (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->ulong_prop (SEARCH_CARD ACE_ENV_ARG_PARAMETER);
}

CORBA::ULong
TAO_Policies::match_card (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->ulong_prop (MATCH_CARD ACE_ENV_ARG_PARAMETER);
}

CORBA::ULong
TAO_Policies::return_card (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->ulong_prop (RETURN_CARD ACE_ENV_ARG_PARAMETER);
}

CORBA::Boolean
TAO_Policies::boolean_prop (POLICY_TYPE pol
                            ACE_ENV_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  CORBA::Boolean def_value = 1,
    return_value = 1;
  const TAO_Support_Attributes_i& support_attrs =
    this->trader_.support_attributes ();

  switch (pol)
    {
    case USE_MODIFIABLE_PROPERTIES:
      def_value = support_attrs.supports_modifiable_properties ();
      break;
    case USE_DYNAMIC_PROPERTIES:
      def_value = support_attrs.supports_dynamic_properties ();
      break;
    case USE_PROXY_OFFERS:
      def_value = support_attrs.supports_proxy_offers ();
      break;
    case EXACT_TYPE_MATCH:
      def_value = 0;
      break;
    default:
      break;
    }

  if (this->policies_[pol] != 0)
    {
      const CosTrading::Policy* policy = this->policies_[pol];
      const CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode_var type = value.type ();

      CORBA::Boolean equal_boolean =
        type->equal (CORBA::_tc_boolean ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (return_value);

      if (!equal_boolean)
        ACE_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
                          return_value);
      else
        value >>= CORBA::Any::to_boolean (return_value);

      if (def_value == 0 &&
          pol != EXACT_TYPE_MATCH)
        return_value = 0;
    }
  else
    return_value = def_value;

  return return_value;
}


CORBA::Boolean
TAO_Policies::use_modifiable_properties (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->boolean_prop (USE_MODIFIABLE_PROPERTIES ACE_ENV_ARG_PARAMETER);
}

CORBA::Boolean
TAO_Policies::use_dynamic_properties (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->boolean_prop (USE_DYNAMIC_PROPERTIES ACE_ENV_ARG_PARAMETER);
}

CORBA::Boolean
TAO_Policies::use_proxy_offers (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->boolean_prop (USE_PROXY_OFFERS ACE_ENV_ARG_PARAMETER);
}

CORBA::Boolean
TAO_Policies::exact_type_match (ACE_ENV_SINGLE_ARG_DECL) const
    ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->boolean_prop (EXACT_TYPE_MATCH ACE_ENV_ARG_PARAMETER);
}


CosTrading::TraderName*
TAO_Policies::starting_trader (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch,
                   CosTrading::Lookup::InvalidPolicyValue))
{
  CosTrading::TraderName* trader_name = 0;

  if (this->policies_[STARTING_TRADER] != 0)
    {
      CosTrading::Policy* policy = this->policies_[STARTING_TRADER];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode_var type = value.type ();

      CORBA::Boolean equal_tradername =
        type->equal (CosTrading::_tc_TraderName ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (trader_name);

      CORBA::Boolean equal_linknameseq =
        type->equal (CosTrading::_tc_LinkNameSeq ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (trader_name);

      if (!equal_tradername ||
          !equal_linknameseq)
        ACE_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
                          trader_name);
      else
        value >>= trader_name;
    }

  return trader_name;
}

CosTrading::FollowOption
TAO_Policies::link_follow_rule (ACE_ENV_SINGLE_ARG_DECL) const
    ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  CosTrading::FollowOption return_value =
    this->trader_.import_attributes ().def_follow_policy ();

  if (this->policies_[LINK_FOLLOW_RULE] != 0)
    {
      CosTrading::FollowOption max_follow_policy =
        this->trader_.import_attributes ().max_follow_policy ();

      CosTrading::Policy* policy = this->policies_[LINK_FOLLOW_RULE];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode_var type = value.type ();

      // Extract the link follow rule
      CORBA::Boolean type_equal =
        type->equal (CosTrading::_tc_FollowOption ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (return_value);

      if (!type_equal)
        ACE_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
                          return_value);
      else
        value >>= return_value;

      if (return_value > max_follow_policy)
        return_value = max_follow_policy;
    }

  return return_value;
}

CosTrading::FollowOption
TAO_Policies::link_follow_rule (const CosTrading::Link::LinkInfo& link_info
                                ACE_ENV_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch,
                   CosTrading::Lookup::InvalidPolicyValue,
                   CosTrading::Link::IllegalLinkName,
                   CosTrading::Link::UnknownLinkName))
{
  CosTrading::FollowOption return_value = CosTrading::local_only;
  CosTrading::FollowOption trader_max_follow_policy =
    this->trader_.import_attributes ().max_follow_policy ();
  CosTrading::FollowOption link_limiting_follow_rule =
    link_info.limiting_follow_rule;

  // If not defined defaults to trader.def_link_follow_rule
  CosTrading::FollowOption query_link_follow_rule =
    this->link_follow_rule (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (return_value);

  return_value = (query_link_follow_rule < trader_max_follow_policy)
    ? query_link_follow_rule : trader_max_follow_policy;
  return_value = (return_value < link_limiting_follow_rule)
    ? return_value : link_limiting_follow_rule;

  return return_value;
}

CORBA::ULong
TAO_Policies::hop_count (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  return this->ulong_prop (HOP_COUNT ACE_ENV_ARG_PARAMETER);
}

CosTrading::Admin::OctetSeq*
TAO_Policies::request_id (ACE_ENV_SINGLE_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch))
{
  CosTrading::Admin::OctetSeq* request_id = 0;

  if (this->policies_[REQUEST_ID] != 0)
    {
      CosTrading::Policy* policy = this->policies_[REQUEST_ID];
      CosTrading::PolicyValue& value = policy->value;
      CORBA::TypeCode_var type = value.type ();

      CORBA::Boolean equal_octetseq =
        type->equal (CosTrading::Admin::_tc_OctetSeq ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (request_id);

      if (!equal_octetseq)
        ACE_THROW_RETURN (CosTrading::Lookup::PolicyTypeMismatch (*policy),
                          request_id);
      else
        value >>= request_id;
    }

  return request_id;
}

void
TAO_Policies::
copy_in_follow_option (CosTrading::PolicySeq& policy_seq,
                       const CosTrading::Link::LinkInfo& link_info
                       ACE_ENV_ARG_DECL) const
  ACE_THROW_SPEC ((CosTrading::Lookup::PolicyTypeMismatch,
                   CosTrading::Lookup::InvalidPolicyValue))
{
  CosTrading::FollowOption follow_option = CosTrading::local_only;
  CosTrading::FollowOption trader_max_follow_policy =
    this->trader_.import_attributes ().max_follow_policy ();

  if (this->policies_[LINK_FOLLOW_RULE] != 0)
    {
      CosTrading::FollowOption query_link_follow_rule =
        this->link_follow_rule (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;

      follow_option = (CosTrading::FollowOption)
        (link_info.limiting_follow_rule < trader_max_follow_policy
         ? (link_info.limiting_follow_rule < query_link_follow_rule
            ? link_info.limiting_follow_rule
            : query_link_follow_rule)
         : (trader_max_follow_policy < query_link_follow_rule
            ? trader_max_follow_policy
            : query_link_follow_rule));
    }
  else
    follow_option = (CosTrading::FollowOption)
      (link_info.def_pass_on_follow_rule < trader_max_follow_policy
       ? link_info.def_pass_on_follow_rule
       : trader_max_follow_policy);

  CORBA::ULong i = 0;
  for (i = 0; i < policy_seq.length (); i++)
    if (ACE_OS::strcmp (policy_seq[i].name,
                        POLICY_NAMES[LINK_FOLLOW_RULE]) == 0)
      {
        policy_seq[i].value <<= follow_option;
        break;
      }

  if (i == policy_seq.length ())
    {
      policy_seq.length (i + 1);
      policy_seq[i].name = POLICY_NAMES[LINK_FOLLOW_RULE];
      policy_seq[i].value <<= follow_option;
    }
}

void
TAO_Policies::
copy_to_pass (CosTrading::PolicySeq& policy_seq,
              const CosTrading::Admin::OctetSeq& request_id
              ACE_ENV_ARG_DECL) const
{
  CORBA::ULong counter = 0;
  CosTrading::Policy* policy_buffer =
    CosTrading::PolicySeq::allocbuf (REQUEST_ID + 1);

  if (policy_buffer == 0)
    return;

  for (int i = 0; i <= REQUEST_ID; i++)
    {
      CosTrading::Policy& new_policy = policy_buffer[counter];

       if (i == REQUEST_ID)
        {
          // Set the new request id.
          new_policy.name = POLICY_NAMES[REQUEST_ID];
          new_policy.value <<= request_id;
          counter++;
        }
       else if (this->policies_[i] != 0)
        {
          // Copy in the existing policies.
          new_policy.name = POLICY_NAMES[i];
          new_policy.value = this->policies_[i]->value;
          counter++;
        }

      // We always require a hop count.
      if (i == HOP_COUNT)
        {
          CORBA::ULong count = this->hop_count (ACE_ENV_SINGLE_ARG_PARAMETER);
          ACE_CHECK;

          new_policy.name = POLICY_NAMES[HOP_COUNT];
          new_policy.value <<= count - 1;

          // Don't count hop count twice.
          if (this->policies_[i] == 0)
            counter++;
        }
    }

  policy_seq.replace (REQUEST_ID + 1,
                      counter,
                      policy_buffer,
                      1);
}

void
TAO_Policies::copy_to_forward (CosTrading::PolicySeq& policy_seq,
                               const CosTrading::TraderName& trader_name) const
{
  // Create a new policy sequence, shortening the starting trader
  // policy by one link.

  CORBA::ULong counter = 0;
  CosTrading::Policy* policy_buffer =
    CosTrading::PolicySeq::allocbuf (REQUEST_ID + 1);

  if (policy_buffer == 0)
    return;

  for (int i = 0; i <= REQUEST_ID; i++)
    {
      CosTrading::Policy& new_policy = policy_buffer[counter];

      if (this->policies_[i] != 0)
        {
          // Copy in the existing policies.
          if (i == STARTING_TRADER && trader_name.length () > 1)
            {
              // Eliminate the first link of the trader name.
              // Only pass on the property if the sequence
              // contains more links after us.

              // The any will sieze control of this memory.
              // Allocating here avoids copying in the policy
              // any.
              CORBA::ULong length = trader_name.length ();
              CosTrading::LinkName* buf =
                CosTrading::TraderName::allocbuf (length - 1);

              if (buf != 0)
                {
                  for (CORBA::ULong j = 1; j < length; j++)
                    buf[j - 1] = CORBA::string_dup (trader_name[j]);

                  new_policy.name = this->policies_[i]->name;
                  CosTrading::TraderName new_name (length - 1,
                                                   length - 1,
                                                   buf,
                                                   1);

                  new_policy.value <<= new_name;
                  counter++;
                }
            }
          else if (i != STARTING_TRADER)
            {
              new_policy.name = this->policies_[i]->name;
              new_policy.value = this->policies_[i]->value;
              counter++;
            }
        }
    }

  // Create the new sequence
  policy_seq.replace (REQUEST_ID + 1,
                      counter,
                      policy_buffer, 1);
}

TAO_Offer_Modifier::
TAO_Offer_Modifier (const char* type_name,
                    const CosTradingRepos::ServiceTypeRepository::TypeStruct& type_struct,
                    CosTrading::Offer* offer)
  : type_ (type_name),
    offer_ (offer)
{
  const CosTradingRepos::ServiceTypeRepository::PropStructSeq&
    pstructs = type_struct.props;
  CosTrading::PropertySeq& prop_seq = this->offer_->properties;
  CORBA::ULong pstructs_length = pstructs.length (),
    props_length = prop_seq.length (),
    i = 0;

  // Create a mapping of property names to their types.
  for (i = 0; i < pstructs_length; i++)
    {
      CORBA::String_var prop_name = pstructs[i].name.in ();
      CORBA::TypeCode_ptr type_code =
        CORBA::TypeCode::_duplicate (pstructs[i].value_type.in ());
      this->prop_types_.bind (prop_name, type_code);
    }

  // Separate the type defined properties into mandatory and readonly
  for (i = 0; i < pstructs_length; i++)
    {
      const char* pname = pstructs[i].name;

      if (pstructs[i].mode ==
          CosTradingRepos::ServiceTypeRepository::PROP_MANDATORY)
        {
          CORBA::String_var prop_name (pname);
          this->mandatory_.insert (prop_name);
        }
      else if (pstructs[i].mode ==
               CosTradingRepos::ServiceTypeRepository::PROP_READONLY)
        {
          CORBA::String_var prop_name (pname);
          this->readonly_.insert (prop_name);
        }
    }

  // Insert the indices of the offer properties into a map.
  for (i = 0; i < props_length; i++)
    {
      CORBA::String_var prop_name =
        static_cast<const char*> (prop_seq[i].name);
      this->props_.bind (prop_name, &prop_seq[i]);
    }
}

TAO_Offer_Modifier::~TAO_Offer_Modifier (void)
{
  for (TAO_Typecode_Table::iterator type_iter (this->prop_types_);
       ! type_iter.done ();
       type_iter++)
    {
      CORBA::TypeCode_ptr corba_type = (*type_iter).int_id_;
      CORBA::release (corba_type);
    }
}

void
TAO_Offer_Modifier::
delete_properties (const CosTrading::PropertyNameSeq& deletes
                   ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CosTrading::Register::UnknownPropertyName,
                  CosTrading::Register::MandatoryProperty,
                  CosTrading::IllegalPropertyName,
                  CosTrading::DuplicatePropertyName))
{
  // Validate that the listed property names can be deleted
  CORBA::ULong i = 0,
    length = deletes.length ();
  TAO_String_Set delete_me;

  for (i = 0; i < length; i++)
    {
      const char* dname = static_cast<const char*> (deletes[i]);
      if (! TAO_Trader_Base::is_valid_property_name (dname))
        ACE_THROW (CosTrading::IllegalPropertyName (dname));
      else
        {
          CORBA::String_var prop_name (dname);
          if (this->mandatory_.find (prop_name) == 0)
            ACE_THROW (CosTrading::Register::MandatoryProperty (this->type_, dname));
          else if (delete_me.insert (prop_name) == 1)
            ACE_THROW (CosTrading::DuplicatePropertyName (dname));
          else if (this->props_.find (prop_name) == -1)
            ACE_THROW (CosTrading::Register::UnknownPropertyName (dname));
        }
    }

  // Delete those properties from the offer.
  for (i = 0; i < length; i++)
    {
      CORBA::String_var prop_name =
        static_cast<const char *> (deletes[i]);
      this->props_.unbind (prop_name);
    }
}

void
TAO_Offer_Modifier::
merge_properties (const CosTrading::PropertySeq& modifies
                  ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CosTrading::IllegalPropertyName,
                   CosTrading::DuplicatePropertyName,
                   CosTrading::PropertyTypeMismatch,
                   CosTrading::ReadonlyDynamicProperty,
                   CosTrading::Register::ReadonlyProperty))
{
  int i = 0, length = 0;
  TAO_String_Set modify_me;

  // Ensure that the proposed changes aren't to readonly properties or
  // otherwise invalid.
  TAO_Property_Evaluator prop_eval (modifies);
  for (i = 0, length = modifies.length (); i < length; i++)
    {
      const char* mname = modifies[i].name;
      if (TAO_Trader_Base::is_valid_property_name (mname))
        {
          CORBA::String_var prop_name (mname);
          if (this->readonly_.find (prop_name) == 0)
            {
              // Can't assign a dynamic property to a property with
              // readonly mode, and can't reassign a readonly property.
              if (prop_eval.is_dynamic_property (i))
                ACE_THROW (CosTrading::ReadonlyDynamicProperty (this->type_, mname));
              else if (this->props_.find (prop_name) == 0)
                ACE_THROW (CosTrading::Register::ReadonlyProperty (this->type_, mname));
            }

          // Validate the property type if the property is defined in
          // the service type description.
          CORBA::TypeCode_ptr type_def = 0;
          if (this->prop_types_.find (prop_name, type_def) == 0)
            {
              CORBA::TypeCode_var prop_type = prop_eval.property_type (i);

              // @@ Frank: This code used to have this comment and line
              //    of code before I fixed the "ACE_TRY" fuzz warning here.
              // @@ Seth, are we trying to ignore the exception here?
              // CORBA::Environment ACE_TRY_ENV;
              // @@ Frank: It seems clear that this is not going to work as
              // expected.  Is the purpose to ignore any exceptions from
              // equal ()?  For now, exceptions are returned since this
              // seemed "safest".

              CORBA::Boolean td_equal =
                type_def->equal (prop_type.in () ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;

              if (!td_equal)
                ACE_THROW (CosTrading::PropertyTypeMismatch (mname, modifies[i]));
            }

          if (modify_me.insert (prop_name) == 1)
            ACE_THROW (CosTrading::DuplicatePropertyName (mname));
        }
      else
        ACE_THROW (CosTrading::IllegalPropertyName (mname));
    }
}

void
TAO_Offer_Modifier::affect_change (const CosTrading::PropertySeq& modifies)
{
  // Create a new property list reflecting the deletes, modifies, and
  // add operations performed, and place this property list in the
  // offer.

  // Merge these properties with the original set.
  CORBA::ULong i = 0,
    merge_length = modifies.length ();

  for (i = 0; i < merge_length; i++)
    {
      Property_Table::ENTRY* entry = 0;
      CORBA::String_var prop_name = modifies[i].name.in ();

      CosTrading::Property* prop =
        const_cast<CosTrading::Property*> (&modifies[i]);
      if (this->props_.bind (prop_name, prop, entry) == 1)
        // We need to rebind here.
        entry->int_id_ = prop;
    }

  CORBA::ULong num_modified = 0,
    original_length = this->offer_->properties.length (),
    total_length = static_cast<CORBA::ULong> (this->props_.current_size ());

  // Scrap the existing property sequence and begin a new one
  CosTrading::PropertySeq prop_seq (total_length);
  //  this->offer_->properties.length (total_length);

  // Copy in the unaffected and modified props into the offer,
  // excluding those that were deleted. Let's try and retain their
  // relative ordering.
  for (i = 0; i < original_length; i++)
    {
      CosTrading::Property* prop_value = 0;
      const char* name = this->offer_->properties[i].name;
      CORBA::String_var prop_name (name);
      if (this->props_.unbind (prop_name, prop_value) == 0)
        prop_seq[num_modified++] = *prop_value;
    }

  for (i = 0; i < merge_length; i++)
    {
      CosTrading::Property* prop_value = 0;
      const char* name = modifies[i].name;
      CORBA::String_var prop_name (name);
      if (this->props_.unbind (prop_name, prop_value) == 0)
        prop_seq[num_modified++] = *prop_value;
    }

  this->offer_->properties.length (total_length);
  for (i = 0; i < total_length; i++)
    this->offer_->properties[i] = prop_seq[i];
  // Free the old, orphaned sequence.
  //  CosTrading::PropertySeq::freebuf (prop_buf);
}

TAO_Offer_Filter::TAO_Offer_Filter (TAO_Policies& policies
                                    ACE_ENV_ARG_DECL)
{
  search_card_ = policies.search_card (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  match_card_ = policies.match_card (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  return_card_ = policies.return_card (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  dp_ = policies.use_dynamic_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  mod_ = policies.use_modifiable_properties (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::Boolean exact_type_match =
    policies.exact_type_match (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (exact_type_match == 1)
    {
      CORBA::String_var exact_match
        (TAO_Policies::POLICY_NAMES[TAO_Policies::EXACT_TYPE_MATCH]);
      this->limits_.insert (exact_match);
    }
}

void
TAO_Offer_Filter::
configure_type (CosTradingRepos::ServiceTypeRepository::TypeStruct* type_struct)
{
  CosTradingRepos::ServiceTypeRepository::PropStructSeq&
    prop_seq = type_struct->props;

  // Take note of non-modifiable properties in the type_struct
  this->not_mod_props_.reset ();
  for (int i = prop_seq.length () - 1; i >= 0; i--)
    {
      CosTradingRepos::ServiceTypeRepository::PropertyMode mode = prop_seq[i].mode;
      if (mode == CosTradingRepos::ServiceTypeRepository::PROP_MANDATORY_READONLY ||
          mode == CosTradingRepos::ServiceTypeRepository::PROP_READONLY)
        {
          CORBA::String_var prop_name ((const char*) prop_seq[i].name);
          this->not_mod_props_.insert (prop_name);
        }
    }
}

CORBA::Boolean
TAO_Offer_Filter::ok_to_consider (CosTrading::Offer* offer)
{
  CORBA::String_var use_mods =
    TAO_Policies::POLICY_NAMES[TAO_Policies::USE_MODIFIABLE_PROPERTIES];
  CORBA::String_var use_dyns =
    TAO_Policies::POLICY_NAMES[TAO_Policies::USE_DYNAMIC_PROPERTIES];
  CORBA::Boolean return_value = 1;
  TAO_Property_Evaluator prop_eval (*offer);

  // If we should screen offers, determine if this offer is unworthy
  // for consideration.
  if (! (this->mod_ && this->dp_))
    {
      for (int i = offer->properties.length () - 1;
           i >= 0 && return_value;
           i--)
        {
          // Winnow away the unwanted offers with modifiable or
          // dynamic properties.
          if (! this->mod_)
            {
              // Determine if this property name is found in the set
              // of modifiable properties for the type being considered.
              CORBA::String_var prop_name ((const char*) offer->properties[i].name);
              if (this->not_mod_props_.find (prop_name) == -1)
                {
                  this->limits_.insert (use_mods);
                  return_value = 0;
                }
            }

          if (! this->dp_ && return_value)
            {
              // Determine if this property is dynamic.
              if (prop_eval.is_dynamic_property (i))
                {
                  this->limits_.insert (use_dyns);
                  return_value = 0;
                }
            }

          if (return_value == 0)
            break;
        }
    }

  // If we're good to go, consider this offer considered and decrement
  // the search cardinality counter.
  if (return_value)
    {
      this->search_card_--;
      if (this->search_card_ == 0)
        {
          CORBA::String_var search_card =
            TAO_Policies::POLICY_NAMES[TAO_Policies::SEARCH_CARD];
          this->limits_.insert (search_card);
        }
    }

  return return_value;
}

CORBA::Boolean
TAO_Offer_Filter::ok_to_consider_more (void)
{
  return this->search_card_ > 0 && this->match_card_ > 0;
}

void
TAO_Offer_Filter::matched_offer (void)
{
  this->match_card_--;
  this->return_card_--;

  if (this->match_card_ == 0)
    {
      CORBA::String_var match_card =
        TAO_Policies::POLICY_NAMES[TAO_Policies::MATCH_CARD];
      this->limits_.insert (match_card);
    }

  if (this->return_card_ == 0)
    {
      CORBA::String_var return_card =
        TAO_Policies::POLICY_NAMES[TAO_Policies::MATCH_CARD];
      this->limits_.insert (return_card);
    }
}

CORBA::ULong
TAO_Offer_Filter::search_card_remaining (void) const
{
  return this->search_card_;
}

CORBA::ULong
TAO_Offer_Filter::match_card_remaining (void) const
{
  return this->match_card_;
}

CosTrading::PolicyNameSeq*
TAO_Offer_Filter::limits_applied (void)
{
  int i = 0;
  CORBA::ULong size = static_cast<CORBA::ULong> (this->limits_.size ());
  CosTrading::PolicyName* temp =
    CosTrading::PolicyNameSeq::allocbuf (size);

  for (TAO_String_Set::iterator p_iter (this->limits_.begin());
       ! p_iter.done ();
       p_iter.advance ())
    {
      CORBA::String_var* policy_name_ptr = 0;
      p_iter.next (policy_name_ptr);
      temp[i++] = CORBA::string_dup (policy_name_ptr->in ());
    }

  return new CosTrading::PolicyNameSeq (size, size, temp, 1);
}

TAO_Property_Filter::
TAO_Property_Filter (const SPECIFIED_PROPS& desired_props
                     ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CosTrading::IllegalPropertyName,
                   CosTrading::DuplicatePropertyName))
  : policy_  (desired_props._d ())
{
  if (this->policy_ == CosTrading::Lookup::some)
    {
      const CosTrading::PropertyNameSeq&
        prop_seq = desired_props.prop_names ();
      int length = prop_seq.length ();

      for (int i = 0; i < length; i++)
        {
          const char* pname = prop_seq[i];

          // Check for errors or duplicates
          if (TAO_Trader_Base::is_valid_property_name (pname))
            {
              CORBA::String_var prop_name (pname);
              if (this->props_.insert (prop_name) == 1)
                ACE_THROW (CosTrading::DuplicatePropertyName (pname));
            }
          else
            ACE_THROW (CosTrading::IllegalPropertyName (pname));
        }
    }
}

TAO_Property_Filter::
TAO_Property_Filter (const TAO_Property_Filter& prop_filter)
  : props_ (prop_filter.props_),
    policy_ (prop_filter.policy_)
{
}

TAO_Property_Filter&
TAO_Property_Filter::operator= (const TAO_Property_Filter& other)
{
  this->props_ = other.props_;
  this->policy_ = other.policy_;

  return *this;
}

void
TAO_Property_Filter::filter_offer (CosTrading::Offer* source,
                                   CosTrading::Offer& destination)
{
  Prop_Queue prop_queue;
  CosTrading::PropertySeq& s_props = source->properties;
  CosTrading::PropertySeq& d_props = destination.properties;
  CORBA::ULong length = static_cast<CORBA::ULong> (s_props.length ()),
               elem = 0;

  destination.reference = CORBA::Object::_duplicate (source->reference.in ());
  if (this->policy_ == CosTrading::Lookup::some)
    {
      for (CORBA::ULong i = 0; i < length; i++)
        {
          if (this->policy_ == CosTrading::Lookup::all)
            prop_queue.enqueue_tail (&s_props[i]);
          else
            {
              const char* p_name = s_props[i].name;
              CORBA::String_var prop_name (p_name);

              // Save those property that match.
              if (this->props_.find (prop_name) == 0)
                prop_queue.enqueue_tail (&s_props[i]);
            }
        }

      // Shove the matched properties into the destination property
      // sequence.
      length = static_cast<CORBA::ULong> (prop_queue.size ());
      d_props.length (length);
      for (Prop_Queue::ITERATOR prop_iter (prop_queue);
           ! prop_iter.done ();
           prop_iter.advance (), elem++)
        {
          CosTrading::Property** prop_ptr = 0;

          prop_iter.next (prop_ptr);
          d_props[elem] = **prop_ptr;
        }
    }
  else if (this->policy_ == CosTrading::Lookup::all)
    //      CosTrading::Property* props = s_props.get_buffer (0);
    //      d_props.replace (length, length, props, 0);
    d_props = s_props;
}

TAO_END_VERSIONED_NAMESPACE_DECL