summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Service_Type_Repository.cpp
blob: 1e808f6362ea792b945bc3dc7d7fc5b57c1483b2 (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
/* -*- C++ -*- */

// ========================================================================
// $Id$
// 
// = LIBRARY
//    orbsvcs
// 
// = FILENAME
//   Service_Type_Repository.cpp
//
// = AUTHOR
//    Marina Spivak <marina@cs.wustl.edu>
//    Seth Widoff <sbw1@cs.wustl.edu>
// 
// ========================================================================

#if !defined (TAO_SERVICE_TYPE_REPOSITORY_C)
#define TAO_SERVICE_TYPE_REPOSITORY_C

#include "Locking.h"
#include "Service_Type_Repository.h"

template <class MAP_LOCK_TYPE>
const char* TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
NAME = "Type_Repository";

template <class MAP_LOCK_TYPE>
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
TAO_Service_Type_Repository (void)
{
  incarnation_.low = 0;
  incarnation_.high = 0;
}

template <class MAP_LOCK_TYPE>
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
~TAO_Service_Type_Repository (void)
{
}

template <class MAP_LOCK_TYPE>
SERVICE_TYPE_REPOS::IncarnationNumber 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
incarnation (CORBA::Environment& _env)
{
  SERVICE_TYPE_REPOS::IncarnationNumber inc_num;
  TAO_READ_GUARD_RETURN (MAP_LOCK_TYPE, 
			 ace_mon, 
			 this->type_map_.lock (),
			 inc_num);
  return incarnation_;
}

template <class MAP_LOCK_TYPE>
SERVICE_TYPE_REPOS::IncarnationNumber 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
add_type (const char * name, 
	  const char * if_name, 
	  const SERVICE_TYPE_REPOS::PropStructSeq& props, 
	  const SERVICE_TYPE_REPOS::ServiceTypeNameSeq& super_types,
	  CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException,
		  CosTrading::IllegalServiceType, 
		  SERVICE_TYPE_REPOS::ServiceTypeExists, 
		  SERVICE_TYPE_REPOS::InterfaceTypeMismatch, 
		  CosTrading::IllegalPropertyName, 
		  CosTrading::DuplicatePropertyName, 
		  SERVICE_TYPE_REPOS::ValueTypeRedefinition, 
		  CosTrading::UnknownServiceType, 
		  SERVICE_TYPE_REPOS::DuplicateServiceTypeName))
{
  PROP_MAP prop_map;
  SUPER_TYPE_MAP super_map;
  SERVICE_TYPE_REPOS::TypeStruct info;
  SERVICE_TYPE_MAP::iterator type_iterator;
  SERVICE_TYPE_REPOS::IncarnationNumber inc_num;
  
  TAO_WRITE_GUARD_RETURN (MAP_LOCK_TYPE, 
			  ace_mon, 
			  this->type_map_.lock (),
			  inc_num);

  // make sure Type name is valid
  if (! TAO_Trader_Base::is_valid_identifier_name (name))
    TAO_THROW_RETURN (CosTrading::IllegalServiceType (name),
		      this->incarnation_);

  // check if the service type already exists.
  type_iterator = this->type_map_.find (name);
  if (type_iterator != this->type_map_.end ())
    TAO_THROW_RETURN (SERVICE_TYPE_REPOS::ServiceTypeExists (),
		      this->incarnation_);
  
  // make sure all property names are valid and appear only once.
  this->validate_properties (props, prop_map, _env);
  TAO_CHECK_ENV_RETURN (_env, this->incarnation_);
  
  // check that all super_types exist, and none are duplicated.
  this->validate_supertypes (super_types, super_map, _env);
  TAO_CHECK_ENV_RETURN (_env, this->incarnation_);

  // NOTE: I don't really know a way to do this without an Interface
  // Repository, since the Interface Repository IDs don't contain
  // information about supertypes.
  // 
  // make sure interface name is legal.
  //  this->validate_interface (if_name, super_types, _env);
  //  TAO_CHECK_ENV_RETURN(_env, this->incarnation);
  //
  // Instead, we do this:
  // 
  if (if_name == 0)
    TAO_THROW_RETURN (SERVICE_TYPE_REPOS::InterfaceTypeMismatch (),
		      this->incarnation_);
  
  // collect and make sure that properties of all supertypes and this type
  // are compatible.  We can use prop_map and super_types_map for the job.
  this->validate_inheritance (prop_map, super_map, _env);
  TAO_CHECK_ENV_RETURN (_env, this->incarnation_);
  
  // we can now use prop_map to construct a sequence of all properties the
  // this type.
  info.if_name = if_name;
  info.props = props;
  info.super_types = super_types;
  info.masked = 0;
  info.incarnation = this->incarnation_; 
  this->update_type_map (name, info, prop_map, super_map);

  // increment incarnation number
  this->incarnation_.low++;
  // if we wrapped around in lows...
  if (this->incarnation_.low == 0)
    this->incarnation_.high++;

  return this->type_map_[name].type_info_.incarnation;
}

template <class MAP_LOCK_TYPE>
void 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
remove_type (const char * name,
	     CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException, 
		  CosTrading::IllegalServiceType, 
		  CosTrading::UnknownServiceType, 
		  SERVICE_TYPE_REPOS::HasSubTypes))
{
  if (! TAO_Trader_Base::is_valid_identifier_name (name))
    TAO_THROW (CosTrading::IllegalServiceType (name));

  TAO_WRITE_GUARD (MAP_LOCK_TYPE, 
		   ace_mon, 
		   this->type_map_.lock ());

  // check if the type exists.
  SERVICE_TYPE_MAP::iterator type_iterator = this->type_map_.find (name);
  if (type_iterator == this->type_map_.end ())
    TAO_THROW (CosTrading::UnknownServiceType (name));

  // check if has any subtypes.
  Type_Info& type_info = (*type_iterator).second;
  if (type_info.sub_types_.empty () == 0)
    {
      const char* type_name = type_info.sub_types_.front ().data ();
      TAO_THROW (SERVICE_TYPE_REPOS::HasSubTypes (name, type_name));
    }

  // remove this type from an entry of each of its supertypes.
  SERVICE_TYPE_REPOS::ServiceTypeNameSeq &super =
    type_info.type_info_.super_types;

  for (CORBA::ULong i = 0; i < super.length (); i++)
    {
      // find the super type
      const char *n = super[i];
      SERVICE_TYPE_MAP::iterator super_iterator = this->type_map_.find (n);
      
      // remove this type from its list of subtypes
      (*super_iterator).second.sub_types_.remove (name);
    }

  // remove the type from the map.
  this->type_map_.erase (type_iterator);
}
           
template <class MAP_LOCK_TYPE>
SERVICE_TYPE_REPOS::ServiceTypeNameSeq* 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
list_types (const SERVICE_TYPE_REPOS::SpecifiedServiceTypes& which_types,
	    CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException))
{
  TAO_READ_GUARD_RETURN (MAP_LOCK_TYPE, 
			 ace_mon, 
			 this->type_map_.lock (),
			 (SERVICE_TYPE_REPOS::ServiceTypeNameSeq*) 0);
  
  SERVICE_TYPE_REPOS::ServiceTypeNameSeq_ptr result =
    new SERVICE_TYPE_REPOS::ServiceTypeNameSeq ();
  
  CORBA::ULong i = 0;
  
  if (which_types._d () == SERVICE_TYPE_REPOS::all)
    {
      result->length (this->type_map_.size ());
      for (SERVICE_TYPE_MAP::iterator itr = this->type_map_.begin ();
	   itr != this->type_map_.end ();
	   itr++, i++)
	(*result)[i] = CORBA::string_dup ((*itr).first.c_str ());
    }
  else
    {
      SERVICE_TYPE_REPOS::IncarnationNumber num =
	which_types.incarnation ();

      if (num > incarnation_)
	result->length (0);
      else
	result->length (this->type_map_.size ());
      
      for (SERVICE_TYPE_MAP::iterator itr = this->type_map_.begin ();
	   itr != this->type_map_.end ();
	   itr++, i++)
	{
	  if (num < (*itr).second.type_info_.incarnation)
	    (*result)[i] = CORBA::string_dup ((*itr).first.c_str ());
	}
      
      result->length (i);
    }
  
  return result;
}

           
template <class MAP_LOCK_TYPE>
SERVICE_TYPE_REPOS::TypeStruct*
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
describe_type (const char * name,
	       CORBA::Environment& _env) 
  TAO_THROW_SPEC ((CORBA::SystemException,
		  CosTrading::IllegalServiceType, 
		  CosTrading::UnknownServiceType))
{
  if (! TAO_Trader_Base::is_valid_identifier_name (name))
    TAO_THROW_RETURN (CosTrading::IllegalServiceType (name),
		      (SERVICE_TYPE_REPOS::TypeStruct*) 0);

  TAO_READ_GUARD_RETURN (MAP_LOCK_TYPE, 
			 ace_mon, 
			 this->type_map_.lock (),
			 (SERVICE_TYPE_REPOS::TypeStruct*) 0);

  // make sure the type exists.
  SERVICE_TYPE_MAP::iterator type_iterator = this->type_map_.find (name);
  if (type_iterator == this->type_map_.end ())
    TAO_THROW_RETURN (CosTrading::UnknownServiceType (name),
		      (SERVICE_TYPE_REPOS::TypeStruct*) 0);
  
  // return appropriate information about the type.
  SERVICE_TYPE_REPOS::TypeStruct* descr = new SERVICE_TYPE_REPOS::TypeStruct;
  SERVICE_TYPE_REPOS::TypeStruct & s = (*type_iterator).second.type_info_;

  (*descr) = s;

  return descr;
}
       
template <class MAP_LOCK_TYPE>
SERVICE_TYPE_REPOS::TypeStruct* 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
fully_describe_type (const char * name,
		     CORBA::Environment& _env) 
  TAO_THROW_SPEC ((CORBA::SystemException,
		  CosTrading::IllegalServiceType, 
		  CosTrading::UnknownServiceType))
{
  if (! TAO_Trader_Base::is_valid_identifier_name (name))
    TAO_THROW_RETURN (CosTrading::IllegalServiceType (name),
		      (SERVICE_TYPE_REPOS::TypeStruct*) 0);
  
  TAO_READ_GUARD_RETURN (MAP_LOCK_TYPE, 
			 ace_mon, 
			 this->type_map_.lock (),
			 (SERVICE_TYPE_REPOS::TypeStruct*) 0);

  // make sure the type exists.
  SERVICE_TYPE_MAP::iterator type_iterator = this->type_map_.find (name);
  if (type_iterator == this->type_map_.end ())
    TAO_THROW_RETURN (CosTrading::UnknownServiceType (name),
		      (SERVICE_TYPE_REPOS::TypeStruct*) 0);

  // return appropriate information about the type.
  SERVICE_TYPE_REPOS::TypeStruct* descr =
    new SERVICE_TYPE_REPOS::TypeStruct;
  SERVICE_TYPE_REPOS::TypeStruct & s =
    (*type_iterator).second.type_info_;

  // We do the explicit copy, since otherwise we'd have excessive
  // properties copying.
  descr->if_name = s.if_name;
  descr->props = (*type_iterator).second.all_prop_; 
  descr->super_types = s.super_types; 
  descr->masked = s.masked; 
  descr->incarnation = s.incarnation; 

  return descr;  
}

template <class MAP_LOCK_TYPE>
void 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
mask_type (const char * name,
	   CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException, 
		  CosTrading::IllegalServiceType, 
		  CosTrading::UnknownServiceType, 
		  SERVICE_TYPE_REPOS::AlreadyMasked))
{
  if (! TAO_Trader_Base::is_valid_identifier_name (name))
    TAO_THROW (CosTrading::IllegalServiceType (name));

  TAO_WRITE_GUARD (MAP_LOCK_TYPE, 
		   ace_mon, 
		   this->type_map_.lock ());

  // make sure the type exists.
  SERVICE_TYPE_MAP::iterator type_iterator = this->type_map_.find (name);
  if (type_iterator == this->type_map_.end ())
    TAO_THROW (CosTrading::UnknownServiceType (name));

  // make sure the type is unmasked.
  CORBA::Boolean & mask = (*type_iterator).second.type_info_.masked;
  if (mask == 1)
    TAO_THROW (SERVICE_TYPE_REPOS::AlreadyMasked (name));
  // mask.
  else
    mask = 1;
}
       
template <class MAP_LOCK_TYPE>
void 
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
unmask_type (const char * name,
	     CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException,
		  CosTrading::IllegalServiceType, 
		  CosTrading::UnknownServiceType, 
		  SERVICE_TYPE_REPOS::NotMasked))
{
  if (! TAO_Trader_Base::is_valid_identifier_name (name))
    TAO_THROW (CosTrading::IllegalServiceType (name));

  TAO_WRITE_GUARD (MAP_LOCK_TYPE, 
		   ace_mon, 
		   this->type_map_.lock ());

  // make sure the type exists.
  SERVICE_TYPE_MAP::iterator type_iterator = this->type_map_.find (name);
  if (type_iterator == this->type_map_.end ())
    TAO_THROW (CosTrading::UnknownServiceType (name));

  // make sure the type is masked.
  CORBA::Boolean & mask = (*type_iterator).second.type_info_.masked;
  if (mask == 0)
    TAO_THROW (SERVICE_TYPE_REPOS::NotMasked (name));
  // unmask.
  else
    mask = 0;
}

template <class MAP_LOCK_TYPE> void
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
validate_properties (const SERVICE_TYPE_REPOS::PropStructSeq& props,
		     PROP_MAP& prop_map,
		     CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::IllegalPropertyName,
		   CosTrading::DuplicatePropertyName))
{
  for (CORBA::ULong i = 0; i < props.length (); i++)
    {
      const char* n = props[i].name;
      if (! TAO_Trader_Base::is_valid_identifier_name (n))
	TAO_THROW (CosTrading::IllegalPropertyName (n));
      else
	{
	  string prop_name (n);
	  SERVICE_TYPE_REPOS::PropStruct* prop_struct =
	    (SERVICE_TYPE_REPOS::PropStruct *) &props[i];

	  if (! prop_map.insert
	      (make_pair (prop_name, prop_struct)).second)
	    TAO_THROW (CosTrading::DuplicatePropertyName (n));
	}
    }
}

template <class MAP_LOCK_TYPE> void
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
validate_supertypes (const SERVICE_TYPE_REPOS::ServiceTypeNameSeq& super_types,
		     SUPER_TYPE_MAP& super_map,
		     CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::IllegalServiceType,
		  CosTrading::UnknownServiceType,
		  CosTrading::DuplicatePropertyName))
{
  for (CORBA::ULong i = 0; i < super_types.length (); i++)
    {
      char* type = (char*)((const char*)super_types[i]);
      
      if (! TAO_Trader_Base::is_valid_identifier_name (type))
	TAO_THROW (CosTrading::IllegalServiceType (type));
      else
	{
	  string s_type (type);
	  SERVICE_TYPE_MAP::iterator super_iter =
	    this->type_map_.find (s_type);
	  
	  if (super_iter == this->type_map_.end ())
	    TAO_THROW (CosTrading::UnknownServiceType (type));
	  else
	    if (! super_map.insert (make_pair (s_type, super_iter)).second)
	      TAO_THROW (SERVICE_TYPE_REPOS::DuplicateServiceTypeName (type));
	}
    }
}

template <class MAP_LOCK_TYPE> void
TAO_Service_Type_Repository<MAP_LOCK_TYPE>
::validate_inheritance (PROP_MAP& prop_map,
			SUPER_TYPE_MAP& super_map,
			CORBA::Environment& _env)
  TAO_THROW_SPEC ((SERVICE_TYPE_REPOS::ValueTypeRedefinition))
{
  SERVICE_TYPE_REPOS::PropertyMode mode;
  // for each super_type
  for (SUPER_TYPE_MAP::iterator super_map_iterator = super_map.begin ();
       super_map_iterator != super_map.end ();
       super_map_iterator++)
    {
      SERVICE_TYPE_MAP::iterator super_iter = (*super_map_iterator).second;
      
      // for each property in a super_type
      for (int i = 0; i < (*super_iter).second.all_prop_.length (); i++)
	{
	  SERVICE_TYPE_REPOS::PropStruct& property =
	    (*super_iter).second.all_prop_[i];
	  
	  // insert property into the map.
	  string prop_name = (const char*) property.name;
	  if (prop_map.insert (prop_name, &property).second == 0)
	    {
	      // if already there, check that it is compatible with
	      // properties of other types. Value Types have to be the
	      // same. 
	      SERVICE_TYPE_REPOS::PropStruct& property_in_map =
		*(prop_map[prop_name]);
	      
	      if (! property.value_type->equal (property_in_map.value_type, _env))
		{
		  TAO_THROW (SERVICE_TYPE_REPOS::ValueTypeRedefinition
			     (property.name, property,
			      property_in_map.name, property_in_map));
		}
	      
	      // Mode of the parent type has to be the same or less
	      // restrictive. 
	      if (property.mode >= property_in_map.mode)
		{
		  TAO_THROW (SERVICE_TYPE_REPOS::ValueTypeRedefinition
			     (property.name, property,
			      property_in_map.name, property_in_map));
		}
	    }
	}
    }
}

template <class MAP_LOCK_TYPE> void
TAO_Service_Type_Repository<MAP_LOCK_TYPE>::
update_type_map (const char* name,
		 SERVICE_TYPE_REPOS::TypeStruct& info,
		 PROP_MAP& prop_map,
		 SUPER_TYPE_MAP& super_map)
{
  Type_Info type;
  SERVICE_TYPE_REPOS::PropStructSeq all_prop;

  CORBA::ULong i = 0;
  all_prop.length (prop_map.size ());
  for (PROP_MAP::iterator prop_map_iterator = prop_map.begin ();
       prop_map_iterator != prop_map.end ();
       prop_map_iterator++, i++)
    {
      all_prop[i] = *((*prop_map_iterator).second); 
    }

  // update entries for all supertypes to include this type as a subtype. 
  // we can use the super_types_map we have constructed.

  for (SUPER_TYPE_MAP::iterator super_map_iterator = super_map.begin ();
       super_map_iterator != super_map.end ();
       super_map_iterator++)
    {
      SERVICE_TYPE_MAP::iterator super_iter = (*super_map_iterator).second;
      (*super_iter).second.sub_types_.push_back (name);
    }

  // all parameters are valid, create an entry for this service type
  // in the this->type_map_. 
  type.type_info_ = info;
  type.all_prop_ = all_prop;
  this->type_map_[name] = type;

}

int
operator< (const SERVICE_TYPE_REPOS::IncarnationNumber &l,
	   const SERVICE_TYPE_REPOS::IncarnationNumber &r)
{
  if (l.high < r.high)
    return 1;
  else if (l.high == r.high) 
    return (l.low < r.low);
  else
    return 0;
}


int
operator> (const SERVICE_TYPE_REPOS::IncarnationNumber &l,
	   const SERVICE_TYPE_REPOS::IncarnationNumber &r)
{
  return (r < l);
}

#endif /* TAO_SERVICE_TYPE_REPOSITORY_C */