summaryrefslogtreecommitdiff
path: root/DAnCE/dance/LocalityManager/Handler/LocalityActivator_Impl.cpp
blob: 655932128809b8b30db634ecc11eda9c6ed58869 (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
#include "LocalityActivator_Impl.h"
#include "ace/Log_Msg.h"
#include "ace/UUID.h"
#include "dance/Logger/Log_Macros.h"
#include "dance/DAnCE_PropertiesC.h"
#include "tao/ORB_Core.h"
#include "dance/LocalityManager/Scheduler/Plugin_Manager.h"

namespace DAnCE
{
    DAnCE_LocalityActivator_i::DAnCE_LocalityActivator_i (CORBA::ULong def_spawn_delay,
                                                          const char * default_cs_path,
                                                          const char * cs_args,
                                                          bool multithreaded,
                                                          CORBA::ORB_ptr orb,
                                                          PortableServer::POA_ptr poa)
      : spawn_delay_ (def_spawn_delay),
        multithreaded_ (multithreaded),
        orb_ (CORBA::ORB::_duplicate (orb)),
        poa_ (PortableServer::POA::_duplicate (poa)),
        cs_path_ (default_cs_path),
        cs_args_ (cs_args),
        mutex_ (),
        condition_ (mutex_)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::DAnCE_LocalityActivator_i");
      ACE_Utils::UUID_GENERATOR::instance ()->init ();

      // initialize the process manager with the ORBs reactor
      // so the exit handlers get triggered when needed
      this->process_manager_.open (ACE_Process_Manager::DEFAULT_SIZE,
                                   this->orb_->orb_core ()->reactor ());
    }

    DAnCE_LocalityActivator_i::~DAnCE_LocalityActivator_i(void)
    {
    }

    void
    DAnCE_LocalityActivator_i::locality_manager_callback (
      ::DAnCE::LocalityManager_ptr serverref,
      const char * server_UUID,
      ::Deployment::Properties_out config)
    {
      DANCE_TRACE("DAnCE_LocalityActivator_i::locality_manager_callback");

      DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                   (LM_TRACE, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                    ACE_TEXT ("Received callback from LocalityManager %C\n"),
                    server_UUID));

      Server_Info *info = 0;

      {
        ACE_GUARD_THROW_EX ( TAO_SYNCH_MUTEX,
                             guard,
                             this->container_mutex_,
                             CORBA::NO_RESOURCES ());
        for (SERVER_INFOS::iterator i  (this->server_infos_.begin ());
             i != this->server_infos_.end (); ++i)
          {
            DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE,
                         (LM_TRACE, DLINFO
                          ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                          ACE_TEXT ("Comparing %C with %C\n"),
                          (*i)->uuid_.c_str (), server_UUID));
            if ((*i)->uuid_ == server_UUID)
              {
                info = (*i).get ();
              }
          }
      }

      if (!info)
        {
          DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                       (LM_WARNING, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                        ACE_TEXT ("Received callback from LocalityManager %C, which doesn't belong to me.\n"),
                        server_UUID));
          throw ::CORBA::BAD_PARAM ();
        }

      if (info->status_ == Server_Info::ACTIVE)
        {
          DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                        ACE_TEXT ("Received callback from LocalityManager %C, ")
                        ACE_TEXT ("which has already been configured.\n"),
                        server_UUID));
          throw ::CORBA::BAD_INV_ORDER ();
        }

      if (!CORBA::is_nil (info->ref_))
        {
          DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                        ACE_TEXT ("Received callback from LocalityManager %C, ")
                        ACE_TEXT ("which has already called back.\n"),
                        server_UUID));
          throw ::CORBA::BAD_INV_ORDER ();
        }

      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_TRACE, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                    ACE_TEXT ("Received callback from LocalityManager %C\n"),
                    server_UUID));

      info->ref_ = ::DAnCE::LocalityManager::_duplicate (serverref);

      this->create_properties (*info, config);

      // @@TODO: May want to print out configvalues here.
      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::locality_manager_callback - ")
                    ACE_TEXT ("Generated %u Properties for LocalityManager %C\n"),
                    config->length (), server_UUID));
    }

    void
    DAnCE_LocalityActivator_i::configuration_complete (const char *server_UUID)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::configuration_complete");

      DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                   (LM_TRACE, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::configuration_complete - ")
                    ACE_TEXT ("Received configuration_complete from LocalityManager %C\n"),
                    server_UUID));

      try
        {
          Server_Info *info = 0;

          {
            ACE_GUARD_THROW_EX ( TAO_SYNCH_MUTEX,
                                 guard,
                                 this->container_mutex_,
                                 CORBA::NO_RESOURCES ());
            for (SERVER_INFOS::iterator j (this->server_infos_.begin ());
                 j != this->server_infos_.end (); ++j)
              {
                if ((*j)->uuid_ == server_UUID)
                  {
                    info = (*j).get ();
                  }
              }
          }

          if (!info)
            {
              DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                           (LM_WARNING, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::configuration_complete - ")
                            ACE_TEXT ("Received configuration_complete from LocalityManager %C, which doesn't belong to me.\n"),
                            server_UUID));
              throw ::CORBA::BAD_PARAM ();
            }

          if (info->status_ == Server_Info::ACTIVE)
            {
              DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::configuration_complete - ")
                            ACE_TEXT ("Received configuration_complete from LocalityManager %C, ")
                            ACE_TEXT ("which has already been completed.\n"),
                           server_UUID));
              throw ::CORBA::BAD_INV_ORDER ();
            }

          if (CORBA::is_nil (info->ref_.in ()))
            {
              DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::configuration_complete - ")
                            ACE_TEXT ("Received configuration_complete from LocalityManager %C, ")
                            ACE_TEXT ("which has not called back.\n"),
                            server_UUID));
              throw ::CORBA::BAD_INV_ORDER ();
            }

          ACE_GUARD_THROW_EX (TAO_SYNCH_MUTEX,
                              guard,
                              info->mutex_,
                              CORBA::NO_RESOURCES ());
          info->status_ = Server_Info::ACTIVE;
          info->condition_.signal ();
        }
      catch (...)
        {
          DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::configuration_complete - ")
                        ACE_TEXT ("Caught unknown exception while processing ")
                        ACE_TEXT ("configuration_complete\n")));
          throw;
        }
    }

    ::DAnCE::LocalityManager_ptr
    DAnCE_LocalityActivator_i::create_locality_manager (const ::Deployment::DeploymentPlan &plan,
                                                        CORBA::ULong instanceRef,
                                                        const ::Deployment::Properties & config)
    {
      DANCE_TRACE("DAnCE_LocalityActivator_i::create_locality_manager");

      Safe_Server_Info server (new Server_Info (plan,
                                                instanceRef,
                                                config.length () + 1));

      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::create_locality_manager - ")
                    ACE_TEXT ("Received %u config properties\n"),
                    config.length ()));

      DAnCE::Utility::build_property_map (*server->cmap_, config);

      ACE_CString cmd_options = this->construct_command_line (*server);

      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::create_locality_manager - ")
                    ACE_TEXT ("LocalityManager arguments: %C\n"),
                    cmd_options.c_str ()));

      {
        ACE_GUARD_THROW_EX ( TAO_SYNCH_MUTEX,
                             guard,
                             this->container_mutex_,
                             CORBA::NO_RESOURCES ());
        server_infos_.insert (server);
      }

      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_TRACE, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::create_locality_manager - ")
                    ACE_TEXT ("Attempting to spawn LocalityManager with UUID %C\n"),
                    server->uuid_.c_str ()));

      // Now we need to get a copy of the one that was inserted...
      pid_t const pid = this->spawn_locality_manager (new Server_Child_Handler (server),
                                                      cmd_options);

      ACE_Time_Value timeout (this->spawn_delay_);

      CORBA::Any val;

      if (server->cmap_->find (DAnCE::LOCALITY_TIMEOUT, val) == 0)
        {
          CORBA::ULong t;
          if (val >>= t)
            {
              DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                           (LM_DEBUG, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::create_locality_manager - ")
                            ACE_TEXT ("Using provided non-default server timeout of %u\n"), t));
              timeout = ACE_Time_Value (t);
            }
          else
            {
              DANCE_ERROR (DANCE_LOG_MAJOR_DEBUG_INFO,
                           (LM_WARNING, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::create_locality_manager - ")
                            ACE_TEXT ("Failed to extract provided non-default server timeout ")
                            ACE_TEXT ("from property '%C', ")
                            ACE_TEXT ("falling back to default timeout of %u\n"),
                            this->spawn_delay_));
            }
        }

      if (this->multithreaded_)
        this->multi_threaded_wait_for_callback (*server, timeout);
      else
        this->single_threaded_wait_for_callback (*server, timeout);

      server->pid_ = pid; // register pid of successfully started lm processkate

      DANCE_DEBUG (DANCE_LOG_MAJOR_EVENT,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::create_locality_manager - ")
                    ACE_TEXT ("LocalityManager %C successfully spawned and configured!\n"),
                    server->uuid_.c_str ()));

      return ::DAnCE::LocalityManager::_duplicate (server->ref_.in ());
    }

    ACE_CString
    DAnCE_LocalityActivator_i::construct_command_line (Server_Info &server)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::construct_command_line");

      // Build our command line to launch the component server
      ACE_CString cmd_options (this->cs_args_);

      CORBA::Any val;

      if (server.cmap_->find (DAnCE::LOCALITY_UUID, val) == 0)
        {
          // Nodeapplication has requested a custom uuid
          DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE,
                       (LM_TRACE, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::construct_command_line - ")
                        ACE_TEXT ("Using provided UUID\n")));
          const char *uuid = 0;
          val >>= uuid;
          server.uuid_ = uuid;
        }
      else
        {
          DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE,
                       (LM_TRACE, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::construct_command_line - ")
                        ACE_TEXT ("Using generated UUID\n")));
          ACE_Utils::UUID uuid;
          ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID (uuid);
          server.uuid_ = *uuid.to_string ();
        }

      if (server.cmap_->find (DAnCE::LOCALITY_ARGUMENTS, val) == 0)
        {
          const char *args = 0;
          val >>= args;

          DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE, (LM_TRACE, DLINFO
                           ACE_TEXT ("DAnCE_LocalityActivator_i::construct_command_line - ")
                           ACE_TEXT ("Adding provided server arguments %C\n"), args));

          cmd_options += ' ';
          cmd_options += args;
          cmd_options += ' ';
        }

      if (server.cmap_->find (DAnCE::DANCE_LM_CONFIGFILE, val) == 0)
        {
          const char *args = 0;
          val >>= args;

          DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE, (LM_TRACE, DLINFO
                           ACE_TEXT ("DAnCE_LocalityActivator_i::construct_command_line - ")
                           ACE_TEXT ("Adding provided locality configuration file %C\n"), args));

          cmd_options += " --plugin-config ";
          cmd_options += args;
          cmd_options += ' ';
        }

      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::construct_command_line - ")
                    ACE_TEXT ("Creating component server")
                    ACE_TEXT (" with UUID %C\n"), server.uuid_.c_str ()));

      cmd_options += " -u ";
      cmd_options += server.uuid_;

      return cmd_options;
    }

    pid_t
    DAnCE_LocalityActivator_i::spawn_locality_manager (Server_Child_Handler* exit_handler,
                                                       const ACE_CString &cmd_line)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::spawn_locality_manager");

      const Server_Info &si = exit_handler->server_info ();

      // Get my object reference
      CORBA::Object_var obj = this->poa_->servant_to_reference (this);
      CORBA::String_var ior = this->orb_->object_to_string (obj.in ());
      CORBA::Any val;

      const char *path = this->cs_path_.c_str ();

      if (si.cmap_->find (DAnCE::LOCALITY_EXECUTABLE, val) == 0)
        {
          val >>= path;
          DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                        ACE_TEXT ("Using provided component server executable: %C\n"),
                        path));
        }
      else
        {
          DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                        ACE_TEXT ("Using default component server executable\n")));
        }

      ACE_Process_Options options (true,
        ACE_OS::strlen (path) + ACE_OS::strlen (cmd_line.c_str()) + ACE_OS::strlen (ior.in ()) + 15);
      if (options.command_line ("%s %s -c %s",
                                path,
                                cmd_line.c_str (),
                                ior.in ()) != 0)
        {
          DANCE_ERROR (DANCE_LOG_ERROR, (LM_ERROR, DLINFO
                           ACE_TEXT ("Failed to create commandline\n")));
          throw Deployment::StartError ("locality_manager",
                                        "Failed to create command line for server");
        }

      options.avoid_zombies (0);

      // check for customized PATH addition
      if (si.cmap_->find (DANCE_LM_PATH, val) == 0)
        {
          path = 0;
          val >>= path;
          if (path)
            {
              ACE_CString newpath (ACE_OS::getenv ("PATH"));
              newpath += ACE_TEXT_ALWAYS_CHAR (ACE_LD_SEARCH_PATH_SEPARATOR_STR);
              newpath += path;
              options.setenv (ACE_TEXT("PATH"), ACE_TEXT_CHAR_TO_TCHAR (newpath.c_str ()));

              DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                           (LM_TRACE, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                            ACE_TEXT ("configured customized PATH environment: %C\n"),
                            newpath.c_str ()));
            }
        }

      // check for customized LD search path addition
      if (si.cmap_->find (DANCE_LM_LIBPATH, val) == 0)
        {
          path = 0;
          val >>= path;
          if (path)
            {
              ACE_CString newpath (ACE_OS::getenv (ACE_TEXT_ALWAYS_CHAR (ACE_LD_SEARCH_PATH)));
              newpath += ACE_TEXT_ALWAYS_CHAR (ACE_LD_SEARCH_PATH_SEPARATOR_STR);
              newpath += path;
              options.setenv (ACE_LD_SEARCH_PATH, ACE_TEXT_CHAR_TO_TCHAR (newpath.c_str ()));

              DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                           (LM_TRACE, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                            ACE_TEXT ("configured customized %s environment: %C\n"),
                            ACE_LD_SEARCH_PATH,
                            newpath.c_str ()));
            }
        }

      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_TRACE, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                    ACE_TEXT ("Spawning process, command line is %s\n"),
                    options.command_line_buf ()));

      pid_t const pid = this->process_manager_.spawn (options,
                                                      exit_handler);

      if (pid == ACE_INVALID_PID)
        {
          // clean up as in this case the handler did not get registered
          delete exit_handler;

          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                        ACE_TEXT ("Failed to spawn a LocalityManager process\n")));
          throw Deployment::StartError ("locality_manager",
                                        "Failed to spawn process");
        }

      DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                   (LM_TRACE, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::spawn_locality_manager - ")
                    ACE_TEXT ("Process successfully spawned with pid %u\n"),
                    pid));
      return pid;
    }

    void
    DAnCE_LocalityActivator_i::
    single_threaded_wait_for_callback (const Server_Info &si,
                                       ACE_Time_Value &timeout)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::single_threaded_wait_for_callback");

      // Below code is broken for thread-per-connection concurrency model,
      // since the main thread is running ORB event loop and will spawn
      // a different thread to handle the call <register_node_application>,
      // the <perform_work> operation will not be invoked and finally
      // a timeout will occur. For a similar reason, it won't work
      // for thread-pool concurrency model.
      while (true)
        {
          if (si.status_ != Server_Info::ACTIVE && si.status_ != Server_Info::TERMINATED)
            {
              ACE_GUARD_THROW_EX ( TAO_SYNCH_MUTEX,
                                   guard,
                                   this->mutex_,
                                   CORBA::NO_RESOURCES ());
              // The next guy to acquire the mutex may have already
              // been activated by the previous leader's perform_work,
              // so let's check to make sure that only non-activated
              // folks are hanging on perform_work.
              if (si.status_ != Server_Info::ACTIVE && si.status_ != Server_Info::TERMINATED)
                {
                  this->orb_->perform_work (timeout);
                }
            }

          if (si.status_ == Server_Info::TERMINATED)
            {
              DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::single_threaded_wait_for_callback - ")
                            ACE_TEXT ("Startup failed for LocalityManager %C; process exited before activation.\n"),
                            si.uuid_.c_str ()));
              throw ::Deployment::StartError ("locality_manager",
                                              "Failed to startup LocalityManager");
            }

          if (si.status_ == Server_Info::ACTIVE)
            {
              break;
            }

          if (timeout == ACE_Time_Value::zero)
            {
              DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::single_threaded_wait_for_callback - ")
                            ACE_TEXT ("Timed out while waiting for LocalityManager %C to call back.\n"),
                            si.uuid_.c_str ()));
              throw ::Deployment::StartError ("locality_manager",
                                              "Timed out waiting for LocalityManager");
            }
        }
    }

    void
    DAnCE_LocalityActivator_i::
      multi_threaded_wait_for_callback (Server_Info &si,
                                        ACE_Time_Value &timeout)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::multi_threaded_wait_for_callback");

      // Wait for a conditional variable
      ACE_GUARD_THROW_EX ( TAO_SYNCH_MUTEX,
                           guard,
                           si.mutex_,
                           CORBA::NO_RESOURCES ());

      while (si.status_ != Server_Info::ACTIVE)
        {
          if (si.condition_.wait (&timeout) == -1)
            {
              DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::multi_threaded_wait_for_callback - ")
                            ACE_TEXT ("Timed out while waiting for LocalityManager %C to call back.\n"),
                            si.uuid_.c_str ()));
              throw Deployment::StartError ("locality_manager",
                                            "timed out waiting for callback");
            }

          if (si.status_ == Server_Info::TERMINATED)
            {
              DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_LocalityActivator_i::multi_threaded_wait_for_callback - ")
                            ACE_TEXT ("Startup failed for LocalityManager %C; process exited before activation.\n"),
                            si.uuid_.c_str ()));
              throw ::Deployment::StartError ("locality_manager",
                                              "Failed to startup LocalityManager");
            }
        }
    }

    void
    DAnCE_LocalityActivator_i::remove_locality_manager (::DAnCE::LocalityManager_ptr server)
    {
      DANCE_TRACE ("DAnCE_LocalityActivator_i::remove_locality_manager");

      Safe_Server_Info info;

      {
        ACE_GUARD_THROW_EX ( TAO_SYNCH_MUTEX,
                             guard,
                             this->container_mutex_,
                             CORBA::NO_RESOURCES ());

        SERVER_INFOS::iterator i;
        for (i = this->server_infos_.begin ();
             i != this->server_infos_.end ();
             ++i)
          {
            if ((!i->null ()) &&
                (!CORBA::is_nil ((*i)->ref_)) &&
                (*i)->ref_->_is_equivalent (server))
              {
                info = *i;
                this->server_infos_.erase (i);
                break;
              }
          }
      }

      if (!info)
        {
          DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::remove_locality_manager - ")
                        ACE_TEXT ("Failed to find equivalent LocalityManager under my management.\n")));
          throw ::Deployment::StopError ("locality manager",
                                         "unable to stop locality manager");
        }

      if (info->status_ == Server_Info::TERMINATED)
        {
          DANCE_DEBUG (DANCE_LOG_WARNING,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::remove_locality_manager - ")
                        ACE_TEXT ("Locality Manager with UUID <%C> already terminated\n"),
                        info->uuid_.c_str ()));
          return;
        }

      try
        {
          DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                       (LM_TRACE, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::remove_locality_manager - ")
                        ACE_TEXT ("Calling shutdown on LocalityManager %C\n"),
                        info->uuid_.c_str ()));
          info->status_ = Server_Info::TERMINATE_REQUESTED;
          server->shutdown ();
        }
      catch (::Deployment::StopError &)
        {
          DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                       (LM_WARNING, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::remove_locality_manager - ")
                        ACE_TEXT ("Received RemoveFailure exception from LocalityManager %C\n"),
                        info->uuid_.c_str ()));
        }

      DANCE_DEBUG (DANCE_LOG_MAJOR_EVENT,
                   (LM_INFO, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::remove_locality_manager - ")
                    ACE_TEXT ("LocalityManager %C successfully shut down.\n"),
                    info->uuid_.c_str ()));
    }

    void
    DAnCE_LocalityActivator_i::create_properties (
      const Server_Info &info,
      Deployment::Properties_out config)
    {
      DANCE_DEBUG (DANCE_LOG_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::create_properties - ")
                    ACE_TEXT ("Creating property sequence for LocalityManager <%C> with ")
                    ACE_TEXT ("length %u\n"),
                    info.uuid_.c_str (),
                    info.cmap_->current_size ()));

      if (info.cmap_->current_size () == 0) return;

      ACE_NEW_THROW_EX (config,
                        Deployment::Properties (info.cmap_->current_size ()),
                        CORBA::NO_MEMORY ());

      config->length (info.cmap_->current_size ());

      CORBA::ULong pos = 0;
      Utility::PROPERTY_MAP::iterator i = info.cmap_->begin ();

      do {
        config[pos].name = i->ext_id_.c_str ();
        config[pos].value = i->int_id_;
        ++pos;
      } while (i.advance () != 0);
    }

    DAnCE_LocalityActivator_i::Server_Child_Handler::Server_Child_Handler (
        const Safe_Server_Info&  si)
      : server_info_ (si)
      {}

    DAnCE_LocalityActivator_i::Server_Child_Handler::~Server_Child_Handler ()
    {
    }

    int DAnCE_LocalityActivator_i::Server_Child_Handler::handle_close (
        ACE_HANDLE, ACE_Reactor_Mask)
    {
      DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::Server_Child_Handler::handle_close\n")));

      delete this;  // clean us up
      return 0;
    }

    int DAnCE_LocalityActivator_i::Server_Child_Handler::handle_exit (
        ACE_Process *proc)
    {
      DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                   (LM_INFO, DLINFO
                    ACE_TEXT ("DAnCE_LocalityActivator_i::Server_Child_Handler::handle_exit")
                    ACE_TEXT (" - Locality Manager UUID %C, pid=%d: %d\n"),
                    this->server_info_->uuid_.c_str (),
                    int (proc->getpid ()),
                    int (proc->exit_code ()) ));

      // this method is guaranteed to be called synchronously
      // so we can safely call anything we like

      // Check if the termination was requested, log an error if not.
      if (this->server_info_->status_  != Server_Info::TERMINATE_REQUESTED)
        {
          DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_LocalityActivator_i::Server_Child_Handler::handle_exit - ")
                        ACE_TEXT ("Error: Unexpected locality shutdown.\n")));

          const DAnCE::Plugin_Manager::INTERCEPTORS &interceptors =
            PLUGIN_MANAGER::instance ()->fetch_interceptors ();

        for (Plugin_Manager::INTERCEPTORS::const_iterator i = interceptors.begin ();
             i != interceptors.end ();
             ++i)
          {
            try
              {
                ::CORBA::Any tmp;
                (*i)->unexpected_event (this->server_info_->plan_,
                                        this->server_info_->instanceRef_,
                                        tmp,
                                        "Unexpected component server exit.");
              }
            catch (...)
              {
                // swallow
              }
          }
        }

      // flag this process as exited
      this->server_info_->status_ = Server_Info::TERMINATED;

      // signal possibly waiting startup thread
      this->server_info_->condition_.signal ();

      return 0;
    }

}