summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Naming/CosNaming_i.cpp
blob: c940481c422a5c238883c1ed590f70eca9d9c628 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    cos
//
// = FILENAME
//   CosNaming_i.cpp
//
// = AUTHOR
//    Marina Spivak <marina@cs.wustl.edu> &
//    Sergio Flores-Gaitan <sergio@cs.wustl.edu>
//
// ============================================================================

#include "CosNaming_i.h"

NS_NamingContext::NS_NamingContext (void)
{
  // Deal with faults.
  if (context_.open (NS_MAP_SIZE) == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "NS_NamingContext"));
}

NS_NamingContext::~NS_NamingContext (void)
{
}

CosNaming::NamingContext_ptr
NS_NamingContext::get_context (const CosNaming::Name &name)
{
  // Create compound name to be resolved (<name> - last component).
  CORBA::Environment _env;
  CORBA::ULong len = name.length ();
  CosNaming::Name comp_name (name);
  comp_name.length (len - 1);

  // resolve
  CORBA::Object_ptr cont_ref = resolve (comp_name, _env);

  // Deal with exceptions in resolve: basicly, add the last component
  // of the name to <rest_of_name> and rethrow.
  if (_env.exception () != 0)
    {
      _env.print_exception ("NS_NamingContext::get_context");
      return 0;
    }

  // Reference to a context from <resolve> cannot be nil because
  // cannot <bind> to a nil object.

  // Try narrowing object reference to a context type.
  CosNaming::NamingContext_ptr c;
  c = CosNaming::NamingContext::_narrow (cont_ref, _env);
  if (_env.exception () != 0)
    {
      _env.print_exception ("NS_NamingContext::get_context - _narrow");
      return 0;
    }

  CosNaming::Name rest;
  rest.length (2);
  rest[0] = name[len - 2];
  rest[1] = name[len - 1];

  if (CORBA::is_nil (c))
    {
      CosNaming::Name rest;
      rest.length (2);
      rest[0] = name[len - 2];
      rest[1] = name[len - 1];
    }
  return c;
}

void
NS_NamingContext::bind (const CosNaming::Name& n,
			CORBA::Object_ptr obj,
			CORBA::Environment &_env)
{
  int result = 0;
  _env.clear ();

  // get the length of the name
  CORBA::ULong len = n.length ();

  // Check for invalid name.
  if (len == 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::InvalidName);
      return;
    }

  // If we received compound name, resolve it to get the context in
  // which the binding should take place, then perform the binding on
  // target context.
  if (len > 1)
    {
      CosNaming::NamingContext_var cont = get_context (n);
      CosNaming::Name simple_name;
      simple_name.length (1);
      simple_name[0] = n[len - 1];
      cont->bind (simple_name, obj, _env);
    }

  // If we received a simple name, we need to bind it in this context.
 else
    {
      NS_IntId entry (obj);
      NS_ExtId name (n[0].id, n[0].kind);

      // Try binding the name.
      result = context_.bind (name, entry);
      if (result == 1)
	{
	  _env.clear ();
	  _env.exception (new CosNaming::NamingContext::AlreadyBound);
	  return;
	}
      // Something went wrong with the internal structure
      else if (result == -1)
	{
	  _env.clear ();
	  _env.exception (new CORBA::UNKNOWN (CORBA::COMPLETED_NO));
	  return;
	}

      ACE_DEBUG ((LM_DEBUG,
                  "bound: <%s,%s>\n",
		  n[0].id.in ()==0? "nil" : n[0].id.in (),
		  n[0].kind.in ()==0? "nil" : n[0].kind.in ()));
    }
}

void
NS_NamingContext::rebind (const CosNaming::Name& n,
			  CORBA::Object_ptr obj,
			  CORBA::Environment &_env)
{
  int result = 0;

  // Get the length of the name.
  CORBA::ULong len = n.length ();

  // Check for invalid name.
  if (len == 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::InvalidName);
      return;
    }

  // If we received compound name, resolve it to get the context in
  // which the rebinding should take place, then perform the rebinding
  // on target context.
  if (len > 1)
    {
      CosNaming::NamingContext_var cont = get_context (n);
      CosNaming::Name simple_name;
      simple_name.length (1);
      simple_name[0] = n[len - 1];
      cont->rebind (simple_name, obj, _env);
    }

  else
    {
      // If we received a simple name, we need to rebind it in this
      // context.

      NS_IntId entry (obj);
      NS_ExtId name (n[0].id, n[0].kind);
      NS_IntId oldentry;
      NS_ExtId oldname;

      // Try rebinding the name.
      result = context_.rebind (name, entry, oldname, oldentry);

      // Something went wrong with the internal structure
      if (result == -1)
	{
	  _env.clear ();
	  _env.exception (new CORBA::UNKNOWN (CORBA::COMPLETED_NO));
	  return;
	}
    }
}

void
NS_NamingContext::bind_context (const CosNaming::Name &n,
				CosNaming::NamingContext_ptr nc,
				CORBA::Environment &_env)
{
  int result = 0;

  // Get the length of the name.
  CORBA::ULong len = n.length ();

  // Check for invalid name.
  if (len == 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::InvalidName);
      return;
    }

  // If we received compound name, resolve it to get the context in
  // which the binding should take place, then perform the binding on
  // target context.
  if (len > 1)
    {
      CosNaming::NamingContext_var cont = get_context (n);
      CosNaming::Name simple_name;
      simple_name.length (1);
      simple_name[0] = n[len - 1];
      cont->bind_context (simple_name, nc, _env);
    }

  // If we received a simple name, we need to bind it in this context.
  else
    {
      CosNaming::NameComponent comp = n[0];
      NS_IntId entry (nc, CosNaming::ncontext);
      NS_ExtId name (n[0].id, n[0].kind);

      // Try binding the name.
      result = context_.bind (name, entry);
      if ( result == 1)
	{
	  _env.clear ();
	  _env.exception (new CosNaming::NamingContext::AlreadyBound);
	  return;
	}
      // Something went wrong with the internal structure
      else if (result == -1)
	{
	  _env.clear ();
	  _env.exception (new CORBA::UNKNOWN (CORBA::COMPLETED_NO));
	  return;
	}	
    }
}

void
NS_NamingContext::rebind_context (const CosNaming::Name &n,
				  CosNaming::NamingContext_ptr nc,
				  CORBA::Environment &_env)
{
  // Get the length of the name.
  CORBA::ULong len = n.length ();

  // Check for invalid name.
  if (len == 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::InvalidName);
      return;
    }

  // If we received compound name, resolve it to get the context in
  // which the rebinding should take place, then perform the rebinding
  // on target context.
  if (len > 1)
    {
      CosNaming::NamingContext_var cont = get_context (n);
      CosNaming::Name simple_name;
      simple_name.length (1);
      simple_name[0] = n[len - 1];
      cont->rebind_context (simple_name, nc, _env);
    }

  // if we received a simple name, we need to rebind it in this context.
  else
    {
      CosNaming::NameComponent comp = n[0];
      NS_IntId entry (nc, CosNaming::ncontext);
      NS_ExtId name (n[0].id, n[0].kind);
      NS_IntId oldentry;
      NS_ExtId oldname;

      // try rebinding the name.
      if (context_.rebind (name, entry, oldname, oldentry) < 0);
	// deal with consequences
    }
}

CORBA::Object_ptr
NS_NamingContext::resolve (const CosNaming::Name& n,
			   CORBA::Environment &_env)
{
  // get the length of the name
  CORBA::ULong len = n.length ();

  // check for invalid name.
  if (len == 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::InvalidName);
      return CORBA::Object::_nil ();
    }

  ACE_DEBUG ((LM_DEBUG,
              "Trying to resolve <%s,%s>\n",
	      n[0].id.in (),
              n[0].kind.in ()));

  // Resolve the first component of the name.
  NS_ExtId name (n[0].id, n[0].kind);
  NS_IntId entry;
  if (context_.find (name, entry) == -1)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::NotFound (CosNaming::NamingContext::not_object, n));
      return CORBA::Object::_nil ();
    }

  CORBA::Object_ptr item = entry.ref_;

  // if the name we have to resolve is a compound name
  // we need to recursively resolve it.
  if (len > 1)
    {
      CosNaming::NamingContext_var cont;
      if (entry.type_ == CosNaming::ncontext)
	{
	  cont = CosNaming::NamingContext::_narrow (item, _env);
	  if (_env.exception () != 0)
	    {
	      _env.print_exception ("NS_NamingContext::resolve");
	      return CORBA::Object::_nil ();
	    }
	}
      else
	{
	  _env.clear ();
	  _env.exception (new CosNaming::NamingContext::NotFound (CosNaming::NamingContext::not_context, n));
	  return CORBA::Object::_nil ();
	}

      CosNaming::Name rest_of_name (len - 1);
      rest_of_name.length (len - 1);
      for (CORBA::ULong i = 1; i < len; i++)
	rest_of_name[i-1] = n[i];

      return (cont->resolve (rest_of_name, _env));
    }

  ACE_DEBUG ((LM_DEBUG,
              "Resolved <%s,%s> to %08.8x\n",
	      n[0].id.in (),
              n[0].kind.in (),
	      item));

  // If the name we had to resolve was simple, we just need to return
  // the result.
  return CORBA::Object::_duplicate (item);
}

void
NS_NamingContext::unbind (const CosNaming::Name& n,
			  CORBA::Environment &_env)
{
  // if (do_operation (n, CORBA::_nil (), NS_NamingContext::unbind) == 0)

  // get the length of the name
  CORBA::ULong len = n.length ();

  // check for invalid name.
  if (len == 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::InvalidName);
      return;
    }

  // If we received compound name, resolve it to get the context in
  // which the unbinding should take place, then perform the unbinding
  // on target context.
  if (len > 1)
    {
      CosNaming::NamingContext_var cont = get_context (n);
      CosNaming::Name simple_name;
      simple_name.length (1);
      simple_name[0] = n[len - 1];
      cont->unbind (simple_name, _env);
    }
  else
    {
      // If we received a simple name, we need to unbind it in this
      // context.

      NS_ExtId name (n[0].id, n[0].kind);
      // try unbinding the name.
      if (context_.unbind (name) == -1)
	{
	  _env.clear ();
	  _env.exception (new CosNaming::NamingContext::NotFound (CosNaming::NamingContext::not_object, n));
	  return;
	}
    }
}
    
CosNaming::NamingContext_ptr 
NS_NamingContext::new_context (CORBA::Environment &_env) 
{
  NS_NamingContext *c = new NS_NamingContext;

  return c->_this (_env);
}
    
CosNaming::NamingContext_ptr 
NS_NamingContext::bind_new_context (const CosNaming::Name& n, 
				    CORBA::Environment &_env) 
{
  NS_NamingContext *c = new NS_NamingContext;

  bind_context (n, c->_this (_env), _env);
  
  // Release object if exception occurs.   
  if (_env.exception () != 0)
    {
      delete c;
      return CosNaming::NamingContext::_nil ();
    }
  
  return c->_this (_env);
}

void
NS_NamingContext::destroy (CORBA::Environment &_env)
{
  if (context_.current_size () != 0)
    {
      _env.clear ();
      _env.exception (new CosNaming::NamingContext::NotEmpty);
      return;
    }

  // Destroy context.
  CORBA::release (tie_ref_);
}
    
void 
NS_NamingContext::list (CORBA::ULong how_many, 
			CosNaming::BindingList_out bl, 
			CosNaming::BindingIterator_out bi, 
			CORBA::Environment &) 
{
  // Dynamically allocate hash map iterator.
  NS_NamingContext::HASH_MAP::ITERATOR *hash_iter =
    new NS_NamingContext::HASH_MAP::ITERATOR (context_);

  // Number of bindings that will go into the BindingList.
  CORBA::ULong n;

  // Number of bindings in the context is > <how_many> so need to
  // return a BindingIterator.
  if (context_.current_size () > how_many)
     {
       NS_BindingIterator *bind_iter;
       
       ACE_NEW (bind_iter, NS_BindingIterator (hash_iter));

       // @@ ????

       ACE_UNUSED_ARG (bind_iter);

       //  bind_iter->initialize (bi);
       CosNaming::BindingIterator::_duplicate (bi);

       n = how_many;
    }
  else
    {
      // Number of bindings in the context is <= <how_many>,
      // so do not need to return a BindingIterator.
      bi = CosNaming::BindingIterator::_nil ();
      n = context_.current_size ();
    }

  // Use hash iterator to populate a BindingList with bindings.
  CosNaming::BindingList bindings;
  bindings.length (n);
  NS_NamingContext::HASH_MAP::ENTRY *hash_entry;

  for (CORBA::ULong i = 0; i < n; i++)
    {
      hash_iter->next (hash_entry);
      hash_iter->advance ();

      bindings[i].binding_type = hash_entry->int_id_.type_;

      bindings[i].binding_name.length (1);
      bindings[i].binding_name[0].id =
	CORBA::string_dup (hash_entry->ext_id_.id_.fast_rep ());
      bindings[i].binding_name[0].kind =
	CORBA::string_dup (hash_entry->ext_id_.kind_.fast_rep ());
    }

  // @@ Marina, please add check for memory failure.
  ACE_NEW (bl, CosNaming::BindingList (bindings));

  // If did not allocate BindingIterator, deallocate hash map
  // iterator.
  if (context_.current_size () <= how_many)
    delete hash_iter;
}

NS_BindingIterator::NS_BindingIterator (NS_NamingContext::HASH_MAP::ITERATOR *hash_iter)
{
  hash_iter_ = hash_iter;
}

NS_BindingIterator::~NS_BindingIterator (void)
{
  delete hash_iter_;
}

CORBA::Boolean
NS_BindingIterator::next_one (CosNaming::Binding_out b,
			      CORBA::Environment &_env)
{
  // Macro to avoid "warning: unused parameter" type warning.
  ACE_UNUSED_ARG (_env);

  if (hash_iter_->done ()) {
    b = new CosNaming::Binding;
    return 0;
  }
  else
    {
      b = new CosNaming::Binding;

      NS_NamingContext::HASH_MAP::ENTRY *hash_entry;
      hash_iter_->next (hash_entry);
      hash_iter_->advance ();

      b->binding_type = hash_entry->int_id_.type_;

      b->binding_name.length (1);
      b->binding_name[0].id =
	CORBA::string_dup (hash_entry->ext_id_.id_.fast_rep ());
      b->binding_name[0].kind =
	CORBA::string_dup (hash_entry->ext_id_.kind_.fast_rep ());

      return 1;
    }
}

CORBA::Boolean
NS_BindingIterator::next_n (CORBA::ULong how_many,
			    CosNaming::BindingList_out bl,
			    CORBA::Environment &_env)
{
  // Macro to avoid "warning: unused parameter" type warning.
  ACE_UNUSED_ARG (_env);

  if (hash_iter_->done ()) {
    bl = new CosNaming::BindingList;
    return 0;
  }
  else
    {
      // Statically allocate a BindingList.
      CosNaming::BindingList bindings;

      // Initially assume that iterator has the requested number of
      // bindings.
      bindings.length (how_many);

      // Iterate and populate the BindingList.
      NS_NamingContext::HASH_MAP::ENTRY *hash_entry;
      for (CORBA::ULong i = 0; i < how_many; i++)
	{
	  hash_iter_->next (hash_entry);

	  bindings[i].binding_type = hash_entry->int_id_.type_;

	  bindings[i].binding_name.length (1);
	  bindings[i].binding_name[0].id =
	    CORBA::string_dup (hash_entry->ext_id_.id_.fast_rep ());
	  bindings[i].binding_name[0].kind =
	    CORBA::string_dup (hash_entry->ext_id_.kind_.fast_rep ());

	  if (hash_iter_->advance () == 0)
	    {
	      // If no more bindings left, reset length to the actual
	      // number of bindings populated and get out of the loop.
	      bindings.length (i + 1);
	      break;
	    }
	}

      // Marina, please check for failed memory allocation.
      ACE_NEW_RETURN (bl, CosNaming::BindingList (bindings), 0);
      return 1;
    }
}

void
NS_BindingIterator::destroy (CORBA::Environment &_env)
{
  // Macro to avoid "warning: unused parameter" type warning.
  ACE_UNUSED_ARG (_env);

  //  CORBA::release (tie_ref_);
}

#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
// These templates will specialized in libACE.* if the platforms does
// not define ACE_MT_SAFE.

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Manager<NS_ExtId, NS_IntId, ACE_Null_Mutex>;
template class  ACE_Hash_Map_Entry<NS_ExtId, NS_IntId>;
template class  ACE_Hash_Map_Iterator_Base<NS_ExtId, NS_IntId, ACE_Null_Mutex>;
template class  ACE_Hash_Map_Iterator<NS_ExtId, NS_IntId, ACE_Null_Mutex>;
template class  ACE_Hash_Map_Reverse_Iterator<NS_ExtId, NS_IntId, ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Hash_Map_Manager<NS_ExtId, NS_IntId, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Entry<NS_ExtId, NS_IntId>
#pragma instantiate ACE_Hash_Map_Iterator_Base<NS_ExtId, NS_IntId, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator<NS_ExtId, NS_IntId, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Reverse_Iterator<NS_ExtId, NS_IntId, ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#endif /* ACE_MT_SAFE */