summaryrefslogtreecommitdiff
path: root/modules/CIAO/DAnCE/Starter/Starter.cpp
blob: 81ecf1af65070f780e68130a47e634f556308644 (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
// -*- C++ -*-

//=============================================================================
/**
 * @file Starter.cpp
 *
 * $Id$
 *
 * @Brief Dynamically runs libraries
 *
 * @author Vinzenz Tornow <vt@prismtech.com>
 */
//=============================================================================

#include "Starter.h"
#include "ace/Service_Config.h"
#include "ace/Thread_Manager.h"
#include "ace/DLL.h"
#include "ace/Get_Opt.h"
#include "tao/ORB.h"
#include "tao/Object.h"
#include "tao/PortableServer/PortableServer.h"
#include "tao/Object_Loader.h"
#include "tao/IORTable/IORTable.h"
#include "DAnCE/Logger/Log_Macros.h"

using namespace DAnCE;

#ifdef DANCE_BUILD_STARTER_EXE

int ACE_TMAIN (int argc, ACE_TCHAR * argv[])
{
  try
    {
      Starter starter (argc, argv);
      starter.execute();
      return 0;
    }
  catch (ACE_CString & e)
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] dance starter failed with an exception : \"%s\"\n", e.c_str()));
    }
  catch (CORBA::Exception & e)
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] dance starter failed with an CORBA exception : \"%s\"\n", e._info().c_str()));
    }
  catch (...)
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] dance starter failed with an unknown exception.\n"));
    }
  return -1;
}

#endif /* DANCE_BUILD_STARTER_EXE */

namespace DAnCE
{

Starter::Starter(int argc, ACE_TCHAR * argv[]) :
      orb_(CORBA::ORB_init (argc, argv, "")),
      optLogLevel_(5), //default
      argc_(argc),
      argv_(argv),
      optNS_(false),
      optEM_(false),
      optPLB_(false),
      optPL_(false)
{
  DANCE_TRACE ("DAnCE::Starter::Starter ()");

  Logger_Service
      * dlf = ACE_Dynamic_Service<Logger_Service>::instance ("DAnCE_Logger_Backend_Factory");

  if (!dlf)
    dlf = new Logger_Service;

  this->logger_.reset (dlf);
  this->logger_->init (argc, argv);

  DANCE_DEBUG ((LM_TRACE, DLINFO
                ACE_TEXT("Starter::Starter - Creating starter...\n")));

  this->parseArgs(argc, argv);

  this->configure_logging_backend ();

  DANCE_DEBUG ((LM_TRACE, DLINFO
                ACE_TEXT("Starter::Starter - Starter was created successfully.\n")));
}

Starter::~Starter()
{
  DANCE_TRACE ("Starter::~Starter");
/*  TAO_Object_Loader
      * loader = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("ExecutionManager_Loader");
  if (0 != loader)
    {
      DANCE_DEBUG ( (LM_TRACE, "[%M] Starter::~Starter - removing EM ...\n"));
      loader = 0;
      ACE_Service_Config::remove ("ExecutionManager_Loader");
    }
  loader
      = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("NodeManager_Loader");
  if (0 != loader)
    {
      DANCE_DEBUG ( (LM_TRACE, "[%M] Starter::~Starter - removing NM ...\n"));
      loader = 0;
      ACE_Service_Config::remove ("NodeManager_Loader");
    }

  this->orb_._retn()->destroy(); */
}

void Starter::parseArgs(int argc, ACE_TCHAR * argv[])
{
  DANCE_DEBUG ( (LM_TRACE, "[%M] Parsing starter's arguments...\n"));

  ACE_Get_Opt opts(argc, argv, "p::n:e::c::r::il:hg:x:d:qk:w:t:a:", 1, 0,
      ACE_Get_Opt::RETURN_IN_ORDER);
  opts.long_option(ACE_TEXT("process-ns"), 'p', ACE_Get_Opt::ARG_OPTIONAL);
  opts.long_option(ACE_TEXT("process-ns-options"), ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("node-mgr"), 'n', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("exec-mgr"), 'e', ACE_Get_Opt::ARG_OPTIONAL);
  opts.long_option(ACE_TEXT("create-plan-ns"), 'c', ACE_Get_Opt::ARG_OPTIONAL);
  opts.long_option(ACE_TEXT("rebind-plan-ns"), 'r', ACE_Get_Opt::ARG_OPTIONAL);
  opts.long_option(ACE_TEXT("port-indirection"), 'i', ACE_Get_Opt::NO_ARG);
  opts.long_option(ACE_TEXT("log-level"), 'l', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("help"), 'h', ACE_Get_Opt::NO_ARG);
  opts.long_option(ACE_TEXT("gen-object-key"), 'g', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("read-plan"), 'x', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("read-cdr-plan"), 'd', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("stop-plan"), 'q', ACE_Get_Opt::NO_ARG);
  opts.long_option(ACE_TEXT("em-ior"), 'k', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("write-cdr-plan"), 'w', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("plan-uuid"), 't', ACE_Get_Opt::ARG_REQUIRED);
  opts.long_option(ACE_TEXT("dam-ior"), 'a', ACE_Get_Opt::ARG_REQUIRED);

  int j;
  char c;
  ACE_TString s;
  while ( (c = opts ()) != -1)
    {
      DANCE_DEBUG((LM_TRACE, "[%M] Option : \"%s\" with argument \"%s\"\n", opts.last_option(), opts.opt_arg()));
      switch (c)
        {
          case '?':
            DANCE_ERROR ( (LM_ERROR, "[%M] Wrong option \"%s\" or this option is requred attribute!\n", opts.last_option()));
            this->usage();
            throw ACE_CString("Error parsing starter arguments");
            break;
          case 'p':
            this->optNS_ = true;
            DANCE_DEBUG ( (LM_TRACE, "[%M] Naming will be started.\n"));
            this->optNSFile_ = opts.opt_arg();
            break;
          case 'n':
            s = opts.opt_arg();
            if (0 < s.length())
              {
                ACE_CString nodename;
                Node node;
                size_t const pos = s.find(ACE_TEXT ("="));
                if (ACE_CString::npos != pos)
                  {
                    nodename = s.substring(0, pos);
                    node.ior_ = s.substring(pos + 1);
                    /*
                    node.obj = this->orb_->string_to_object(objstr.c_str());
                    if (CORBA::is_nil (node.obj))
                      {
                        DANCE_ERROR ( (LM_ERROR, "[%M] Failed create object for node \"%s\"\n", nodename.c_str()));
                        throw ACE_CString ("Invalid IOR in --node-mgr option");
                      }
                      */
                  }
                else
                  {
                    nodename = s;
                    DANCE_DEBUG ( (LM_TRACE, "[%M] Node \"%s\" will be started.\n", nodename.c_str()));
                    if (opts.optind < opts.argc_&& '-' != (s = opts.argv_[opts.optind])[0])
                      {
                        ++opts.optind;
                        node.iorfile_ = s;
                        DANCE_DEBUG ( (LM_TRACE, "[%M] and its IOR will be written to file \"%C\".\n", node.iorfile_.c_str()));
                      }

                  }
                if (0 == this->nodes_.find(nodename))
                  {
                    DANCE_ERROR((LM_ERROR, "[%M] Duplication of NM name \"%s\"\n", nodename.c_str()));
                    this->usage();
                    throw ACE_CString("Duplication of NM name");
                  }
                this->nodes_.bind(nodename, node);
              }
            else
              {
                DANCE_ERROR ( (LM_ERROR, "[%M] --node-mgr option without arguments.\n"));
                this->usage();
                throw ACE_CString ("--node-mgr option without arguments.");
              }
            break;
          case 'e':
            if (this->optEM_)
              {
                DANCE_DEBUG((LM_WARNING, "[%M] ExecutionManager option is encountered more than once. Second and following ignored.\n"));
                break;
              }
            this->optEM_ = true;
            DANCE_DEBUG ( (LM_TRACE, "[%M] ExecutionManager will be started.\n"));
            this->optEMFile_ = opts.opt_arg();
            break;
          case 'l':
            j = ACE_OS::atoi (opts.opt_arg());
            if (j != 0)
              {
                this->optLogLevel_ = j;
              }
            else
              {
                DANCE_ERROR ( (LM_WARNING, "--log-level without argument. Using default.\n"));
              }
            break;
          case 'h':
            this->usage();
            break;
          case 'x':
            this->optPL_ = true;
            DANCE_DEBUG ( (LM_TRACE, "[%M] PlanLauncher will be started.\n"));
            break;
          case 'd':
          case 'q':
            this->optPLB_ = true;
            DANCE_DEBUG ( (LM_TRACE, "[%M] PlanLauncherBase will be started.\n"));
            break;
          case 'g':
            DANCE_DEBUG ( (LM_TRACE, "[%M] Object key will be generated.\n"));
            this->optGenObjKey_ = opts.opt_arg();
            if (0 == this->optGenObjKey_.length())
              {
                DANCE_ERROR ( (LM_ERROR, "[%M] --gen-object-key without argument. Doing nothing.\n"));
              }
            break;
          case 0: // long options that do not have short
            s = opts.last_option();
            if (s == "process-ns-options")
              {
                this->optNSOptions_ = opts.opt_arg();
                if (0 == this->optNSOptions_.length())
                  {
                    DANCE_ERROR ( (LM_ERROR, "[%M] --process-ns-options without argument\n"));
                  }
              }
            else
              {
                if (!isPossibleOption(s.c_str()))
                  {
                    DANCE_ERROR((LM_ERROR, "[%M] Invalid option : %s\n", s.c_str()));
                  }
              }
            break;
          default:
            if (!isPossibleOption(opts.last_option()))
              {
                DANCE_ERROR((LM_ERROR, "[%M] Invalid option : %s\n", opts.last_option()));
                this->usage();
              }
            break;
        }//switch
    }//while

  DANCE_DEBUG ( (LM_TRACE, "[%M] Parsing starter's arguments completed.\n"));
}

void Starter::execute()
{
  DANCE_DEBUG ( (LM_TRACE, "[%M] Executing starter...\n"));
  bool orb_run = false;

  // Generate object key
  if (0 < this->optGenObjKey_.length())
    {
      this->generateObjectKey(this->optGenObjKey_.c_str());
    }

  // Naming
  if (this->optNS_)
    {
      this->initNaming();
      orb_run = true;
    }

  // NodeManagers
  for (ACE_Map_Manager<ACE_CString, Node, ACE_Null_Mutex>::iterator
      it = this->nodes_.begin(); it != this->nodes_.end(); ++it)
    {
      if (!CORBA::is_nil ((*it).int_id_.obj) || 0 < (*it).int_id_.ior_.length())
        continue;
      orb_run = true;
      (*it).int_id_.obj = this->initNodeManager((*it).ext_id_.c_str());
      if (0 != (*it).int_id_.iorfile_.length())
        {
          this->write_IOR((*it).int_id_.iorfile_.c_str(), this->orb_->object_to_string((*it).int_id_.obj));
        }
    }

  // ExecutionManager
  CORBA::Object_var em;
  if (this->optEM_)
    {
      em = this->initExecutionManager();
      if ( !CORBA::is_nil(em) && 0 < this->optEMFile_.length())
        {
          this->write_IOR(this->optEMFile_.c_str(),
              this->orb_->object_to_string(em.in()));
        }
      orb_run = true;
    }

  if (this->optPLB_ || this->optPL_)
    {
      this->runPlanLauncher();
    }

  if (orb_run)
    {
      DANCE_DEBUG ( (LM_TRACE, "[%M] Running starter's ORB...\n"));
      this->orb_->run();
    }
  else
    {
      DANCE_DEBUG ( (LM_TRACE, "[%M] Skipping starter's ORB->run.\n"));
    }
  DANCE_DEBUG ( (LM_TRACE, "[%M] Executing starter has completed.\n"));
}

void Starter::usage()
{
  DANCE_ERROR ( (LM_EMERGENCY, "Usage : dance <options>\n"
          "Options :\n"
          "\t-l|--log-level <log level> - sets log level (default 5). 1 - most detailed.\n"
          "\t-g|--gen-object-key \"<NODE_NAME> <PLAN_ID> <COMPONENT_ID> [<PORT_NAME>]\" - generates a corbaloc URL\n"
          "\t-h|--help - shows this help\n"
          "\t-p|--process-ns [IOR_FILE_NAME] - Instantiate a local name service within the DAnCE process. Export the root context to IOR_FILE if any.\n"
          "\t--process-ns-options <options> - Specifies quoted string of options to be passed to name service. Depends on --process-ns option.\n"
          "\t-e|--exec-mgr [IOR_FILE_NAME] - This process will have an execution manager and it's IOR will optionally be exported to IOR_FILE_NAME.\n"
          "\t-n|--node-mgr name [IOR_FILE_NAME] - Create a named NodeManager in process and optionally export its IOR to IOR_FILE_NAME\n"
          "\t-n|--node-mgr name=<IOR> - Place the NodeManager instance whose reference is IOR under the control of the ExecutionManager in this process (requires --exec-mgr).\n"
          "In addition to the options above, the Service Configurator options are processed too.\n"));
}

void Starter::generateObjectKey(const char * keyargs)
{
  ACE_CString args = keyargs;
  ssize_t pos_start = 0;
  ACE_CString node;
  ACE_CString plan;
  ACE_CString component;
  ACE_CString port;

  // node
  if (pos_start < (ssize_t) args.length())
    {
      size_t pos_end = args.find(' ', pos_start);
      if (ACE_CString::npos == pos_end)
        {
          node = args.substring(pos_start);
          pos_start = args.length();
        }
      else
        {
          node = args.substring(pos_start, pos_end - pos_start);
          pos_start = pos_end + 1;
        }
    }

  // plan
  if (pos_start < (ssize_t) args.length())
    {
      size_t pos_end = args.find(' ', pos_start);
      if (ACE_CString::npos == pos_end)
        {
          plan = args.substring(pos_start);
          pos_start = args.length();
        }
      else
        {
          plan = args.substring(pos_start, pos_end - pos_start);
          pos_start = pos_end + 1;
        }
    }

  // component
  if (pos_start < (ssize_t) args.length())
    {
      size_t pos_end = args.find(' ', pos_start);
      if (ACE_CString::npos == pos_end)
        {
          component = args.substring(pos_start);
          pos_start = args.length();
        }
      else
        {
          component = args.substring(pos_start, pos_end - pos_start);
          pos_start = pos_end + 1;
        }
    }

  // port
  if (pos_start < (ssize_t) args.length())
    {
      size_t pos_end = args.find(' ', pos_start);
      if (ACE_CString::npos == pos_end)
        {
          port = args.substring(pos_start);
          pos_start = args.length();
        }
      else
        {
          port = args.substring(pos_start, pos_end - pos_start);
          pos_start = pos_end + 1;
        }
    }

  // check
  if (0 == node.length() || 0 == plan.length() || 0 == component.length())
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] Invalid object attributes received : \"s\"\n", args.c_str()));
      this->usage();
      return;
    }

  this->generateObjectKey(node.c_str(), plan.c_str(), component.c_str(), 0
      == port.length() ? 0 : port.c_str());
}

void Starter::generateObjectKey(const char * node, const char * plan,
    const char * component, const char * port)
{
  DANCE_DEBUG ( (LM_TRACE, "[%M] Starter::generateObjectKey starting...\n"));
  CORBA::Boolean prev_format = this->orb_->_use_omg_ior_format();
  this->orb_->_use_omg_ior_format(false);
  // warning : parent POA supposed to be RootPOA
  CORBA::Object_var obj = this->orb_->resolve_initial_references("RootPOA");
  PortableServer::POA_var root_poa = PortableServer::POA::_narrow (obj.in());

  CORBA::PolicyList policies(4);
  policies.length(4);
  policies[0]= root_poa->create_id_assignment_policy(PortableServer::USER_ID);
  policies[1]
      = root_poa->create_request_processing_policy(PortableServer::USE_SERVANT_MANAGER);
  policies[2]
      = root_poa->create_servant_retention_policy(PortableServer::NON_RETAIN);
  policies[3] = root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
  PortableServer::POAManager_var mgr = root_poa->the_POAManager();
  PortableServer::POA_var
      myPOA = root_poa->create_POA(node, mgr.in(), policies);
  for (size_t i = 0; i < policies.length(); ++i)
    {
      policies[i]->destroy();
    }

  const char delim = '/';

  ACE_CString s = plan;
  s += delim;
  s += component;
  if (0 != port)
    {
      s += delim;
      s += port;
    }

  PortableServer::ObjectId_var
      oid = PortableServer::string_to_ObjectId (s.c_str());
  CORBA::Object_var o = myPOA->create_reference_with_id(oid.in(),
      "IDL:omg.org/CORBA/Object:1.0");
  s = this->orb_->object_to_string(o);
  size_t pos = s.find(delim);
  if (ACE_CString::npos != pos)
    {
      s = s.substr(pos + 1);
    }
  else
    {
      DANCE_ERROR ( (LM_WARNING, "Failed to cut off the host specific part of URL.\n"));
    }
  DANCE_DEBUG ( (LM_TRACE, "[%M] Starter::generateObjectKey printing result : %s\n", s.c_str()));
  ACE_OS::printf ("%s\n", s.c_str());

  this->orb_->_use_omg_ior_format(prev_format);
  DANCE_DEBUG ( (LM_TRACE, "[%M] Starter::generateObjectKey completed.\n"));
}

void Starter::write_IOR(const ACE_TCHAR * ior_file_name, const char* ior)
{
  FILE* ior_output_file_ = ACE_OS::fopen (ior_file_name, "w");

  if (ior_output_file_)
    {
      ACE_OS::fprintf (ior_output_file_, "%s", ior);
      ACE_OS::fclose (ior_output_file_);
      DANCE_DEBUG ( (LM_DEBUG, "[%M] ior was written into file \"%s\"\n", ior_file_name));
    }
  else
    {
      DANCE_ERROR ( (LM_ERROR, "[%M] Unable to open IOR output file %s : %m\n",
              ior_file_name));
    }
}

void Starter::initNaming()
{
  DANCE_DEBUG ( (LM_TRACE, "[%M] Starting naming...\n"));
  TAO_Object_Loader
      * loader = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("Naming_Loader");
  if (0 == loader)
    {
      ACE_CString directive =
        "dynamic Naming_Loader Service_Object * TAO_CosNaming_Serv:_make_TAO_Naming_Loader() \"";
      directive += this->optNSOptions_ + "\"";
      ACE_Service_Config::process_directive(directive.c_str());
    }

    DANCE_DEBUG ( (LM_TRACE, "[%M] Putting ior to file if necessary...\n"));
    if (0 < this->optNSFile_.length())
      {
        CORBA::Object_var obj = this->orb_->resolve_initial_references("NameService");
        if (CORBA::is_nil(obj))
          {
            DANCE_ERROR((LM_ERROR, "[%M] Failed to rir \"NameService\" after creation to write it to file.\n"));
          }
        else
          {
            this->write_IOR(this->optNSFile_.c_str(), this->orb_->object_to_string(obj));
          }
      }

/*    CORBA::Object_var table_object = this->orb_->resolve_initial_references ("IORTable");
    IORTable::Table_var table = IORTable::Table::_narrow (table_object.in ());
    if (CORBA::is_nil (table.in()))
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to register Naming in IORTable.Nil IORTable\n"));
        return;
      }
    try
      {
        table->bind ("NameService", this->orb_->object_to_string (this->process_naming_));
      }
    catch (...)
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to register Naming in IORTable with \"NameService\". Exception is caught.\n"));
      }*/
    DANCE_DEBUG ( (LM_TRACE, "[%M] Starting naming completed.\n"));
  }

CORBA::Object_ptr
Starter::initNodeManager (const char * node)
  {
    DANCE_DEBUG ( (LM_TRACE, "[%M] Starting NodeManager \"%s\"...\n", node));
    TAO_Object_Loader * loader = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("NodeManager_Loader");
    if (0 == loader)
      {
        ACE_Service_Config::process_directive (ACE_DYNAMIC_SERVICE_DIRECTIVE ("NodeManager_Loader",
                                                                              "DAnCE_NodeManager",
                                                                              "_make_DAnCE_NodeManager_Module",
                                                                              ""));
        loader = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("NodeManager_Loader");
      }
    if (0 == loader)
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to load node manager \"%s\".\n", node));
        throw ACE_CString ("Failed to load NodeManager.");
      }
    int c = 0;
    ACE_TCHAR ** v = 0;
    this->argCopyForNode (node, c, v);
    CORBA::Object_var res = loader->create_object (this->orb_, c, v);
    this->releaseArgs (c, v);
    if (CORBA::is_nil(res.in()))
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to create node manager \"%s\".\n", node));
        throw ACE_CString ("Failed to create NodeManager.");
      }
    DANCE_DEBUG ( (LM_TRACE, "[%M] Starting NodeManager \"%s\" completed.\n", node));
    return res._retn();
  }

CORBA::Object_ptr
Starter::initExecutionManager()
  {
    DANCE_DEBUG ( (LM_TRACE, "[%M] Starting ExecutionManager...\n"));
    ACE_Service_Config::process_directive (
        ACE_DYNAMIC_SERVICE_DIRECTIVE ("ExecutionManager_Loader"
            , "DAnCE_ExecutionManager"
            , "_make_DAnCE_ExecutionManager_Module"
            , ""));
    TAO_Object_Loader * loader = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("ExecutionManager_Loader");
    if (0 == loader)
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to load execution manager .\n"));
        throw ACE_CString ("Failed to load ExecutionManager.");
      }
    int c = 0;
    ACE_TCHAR ** v = 0;
    this->argCopyForEM (c, v);
    CORBA::Object_var em = loader->create_object (this->orb_, c, v);
    this->releaseArgs (c, v);
    if (CORBA::is_nil(em.in()))
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to create execution manager.\n"));
        throw ACE_CString ("Failed to create ExecutionManager.");
      }
    DANCE_DEBUG ( (LM_TRACE, "[%M] Starting ExecutionManager completed.\n"));
    return em._retn();
  }

void
Starter::runPlanLauncher()
  {
    if (this->optPL_)
      {
        DANCE_DEBUG ( (LM_TRACE, "[%M] Starting PlanLauncher...\n"));
        ACE_Service_Config::process_directive (
            ACE_DYNAMIC_SERVICE_DIRECTIVE ("PlanLauncher_Loader"
                , "DAnCE_Plan_Launcher"
                , "_make_DAnCE_Plan_Launcher_Module"
                , ""));
      }
    else
      {
        DANCE_DEBUG ( (LM_TRACE, "[%M] Starting PlanLauncherBase...\n"));
        ACE_Service_Config::process_directive (
            ACE_DYNAMIC_SERVICE_DIRECTIVE ("PlanLauncher_Loader"
                , "DAnCE_Plan_Launcher_Base"
                , "_make_DAnCE_Plan_Launcher_Base_Module"
                , ""));
      }
    TAO_Object_Loader * loader = ACE_Dynamic_Service<TAO_Object_Loader>::instance ("PlanLauncher_Loader");
    if (0 == loader)
      {
        DANCE_ERROR ( (LM_ERROR, "[%M] Failed to load plan launcher.\n"));
        throw ACE_CString ("Failed to load PlanLauncher.");
      }
    int c = 0;
    ACE_TCHAR ** v = 0;
    this->argCopyForPL (c, v);
    loader->create_object (this->orb_, c, v);
    this->releaseArgs (c, v);
    DANCE_DEBUG ( (LM_TRACE, "[%M] Starting PlanLauncher(Base) completed.\n"));
  }

void
Starter::argCopyForNaming (int & c, char **& v)
  {
    int total_sz = 2;
    for (size_t pos = this->optNSOptions_.find (' ');
        ACE_CString::npos != pos;
        pos = this->optNSOptions_.find (' ', pos + 1))
      {
        ++total_sz;
      }
    v = new char*[total_sz];
    c = 0;
    //take the 0-th argument anyway
    v[c++] = CORBA::string_dup (this->argv_[0]);

    for (int i = 1; i < total_sz; ++i) v[i] = 0;

    ssize_t p0 = 0;

    for (ssize_t p1 = this->optNSOptions_.find (' ');
        (size_t) p0 < this->optNSOptions_.length();
        p1 = this->optNSOptions_.find (' ', p0))
      {
        if (p1 == p0)
          {
            p0 = p1 + 1;
            continue;
          }
        v[c++] = CORBA::string_dup (this->optNSOptions_.substring (p0, p1 - p0).c_str());
        p0 = p1 + 1;
      }
  }

void
Starter::argCopyForNode (const char * node, int & c, char **& v)
  {
    const char * validOptions[] =
      { //"--node-mgr", "-n"
        "--process-ns", "-p"
        , "--create-plan-ns", "-c"
        , "--rebind-plan-ns", "-r"
        , "--port-indirection", "-i"
        , 0
      };

    int total_sz = this->argc_ + 1;
    v = new char*[total_sz];
    for (int i = 0; i < total_sz; ++i) v[i] = 0;

    c = 0;
    v[c++] = CORBA::string_dup ("-n");
    v[c++] = CORBA::string_dup (node);
    Node n;
    if (0 == this->nodes_.find(node, n) && 0 < n.iorfile_.length())
      {
        v[c++] = CORBA::string_dup (n.iorfile_.c_str());
      }

    bool take = false;
    for (int i = 0; i < this->argc_; ++i)
      {
        if ('-' == this->argv_[i][0])
          {
            take = false;
            for (int j = 0; 0 != validOptions[j]; ++j)
              {
                if (this->argv_[i] == ACE_OS::strstr (this->argv_[i], validOptions[j]))
                  {
                    if (i + 1 < this->argc_
                        && (0 == ACE_OS::strcmp (this->argv_[i], ACE_TEXT("--node-mgr")) || 0 == ACE_OS::strcmp (this->argv_[i], ACE_TEXT("-n"))))
                      {
                        ACE_CString s = this->argv_[i+1];
                        if (ACE_CString::npos != s.find ('='))
                          {
                            break;
                          }
                        else if (s == node)
                          {
                            v[c++] = CORBA::string_dup (this->argv_[i]);
                            take = true;
                            break;
                          }
                      }
                    else
                      {
                        v[c++] = CORBA::string_dup (this->argv_[i]);
                        take = true;
                        break;
                      }
                  }
              }
          }
        else if (take)
          {
            v[c++] = CORBA::string_dup (this->argv_[i]);
          }
      }
    v[c] = 0;
  }

void
Starter::argCopyForEM (int & c, char **& v)
  {
    const char * validOptions[] =
      {
        //"--node-mgr", "-n"
        "--exec-mgr", "-e"
        , "--process-ns", "-p"
        , "--create-plan-ns", "-c"
        , "--rebind-plan-ns", "-r"
        , "--port-indirection", "-i"
        , 0
      };

    int total_sz = this->argc_ + 2 * this->nodes_.total_size() + 1;
    v = new char*[total_sz];
    for (int i = 0; i < total_sz; ++i) v[i] = 0;

    bool take = false;
    c = 0;
    for (int i = 0; i < this->argc_; ++i)
      {
        if ('-' == this->argv_[i][0])
          {
            take = false;
            for (int j = 0; 0 != validOptions[j]; ++j)
              {
                if (this->argv_[i] == ACE_OS::strstr (this->argv_[i], validOptions[j]))
                  {
                    v[c++] = CORBA::string_dup (this->argv_[i]);
                    take = true;
                    break;
                  }
              }
          }
        else if (take)
          {
            v[c++] = CORBA::string_dup (this->argv_[i]);
          }
      }

    for (ACE_Map_Manager<ACE_CString, Node, ACE_Null_Mutex>::iterator it = this->nodes_.begin();
        it != this->nodes_.end();
        ++it)
      {
        //v[c++] = CORBA::string_dup("--node-mgr");
        v[c++] = CORBA::string_dup ("-n");
        ACE_CString s = (*it).ext_id_;
        s += "=";
        if ( 0 < (*it).int_id_.ior_.length() )
          {
            s += (*it).int_id_.ior_;
          }
        else if (!CORBA::is_nil((*it).int_id_.obj.in()))
          {
            s += this->orb_->object_to_string ( (*it).int_id_.obj.in());
          }
        else
          {
            DANCE_ERROR((LM_ERROR, "[%M] No IOR for node \"%s\"\n", (*it).ext_id_.c_str()));
            continue;
          }
        v[c++] = CORBA::string_dup (s.c_str());
      }

    v[c] = 0;
  }

void
Starter::argCopyForPL (int & c, char **& v)
  {
    const char * validOptions[] =
      { "--em-ior", "-k"
        , "--read-plan", "-x"
        , "--read-cdr-plan", "-d"
        , "--write-cdr-plan", "-w"
        , "--plan-uuid", "-t"
        , "--dam-ior", "-a"
        , "--stop-plan", "-q"
        , 0
      };

    int total_sz = this->argc_ + 1;
    v = new char*[total_sz];
    for (int i = 0; i < total_sz; ++i) v[i] = 0;

    bool take = false;
    c = 0;
    for (int i = 0; i < this->argc_; ++i)
      {
        if ('-' == this->argv_[i][0])
          {
            take = false;
            for (int j = 0; 0 != validOptions[j]; ++j)
              {
                if (this->argv_[i] == ACE_OS::strstr (this->argv_[i], validOptions[j]))
                  {
                    v[c++] = CORBA::string_dup (this->argv_[i]);
                    take = true;
                    break;
                  }
              }
          }
        else if (take)
          {
            v[c++] = CORBA::string_dup (this->argv_[i]);
          }
      }
    v[c] = 0;
  }

void
Starter::releaseArgs (int c, char ** v)
  {
    for (int i = 0; i < c && 0 != v[i]; ++i)
      {
        CORBA::string_free (v[i]);
        v[i] = 0;
      }
    delete [] v;
  }

bool
Starter::isPossibleOption(const char* opt)
{
  const char * validOptions[] =
        { "node-mgr", "n"
          , "exec-mgr", "e"
          , "process-ns", "p"
          , "create-plan-ns", "c"
          , "rebind-plan-ns", "r"
          , "port-indirection", "i"
          , "gen-object-key", "g"
          , "log-level", "l"
          , "em-ior", "k"
          , "read-plan", "x"
          , "read-cdr-plan", "d"
          , "write-cdr-plan", "w"
          , "plan-uuid", "t"
          , "dam-ior", "a"
          , "stop-plan", "q"
          , 0
        };

  ACE_CString option = opt;
  for (int i = 0; 0 != validOptions[i]; ++i)
    {
      if ( option == validOptions[i]) return true;
    }
  return false;
}

void
Starter::configure_logging_backend (void)
{
  Logger_Service
    *clf = ACE_Dynamic_Service<Logger_Service>::instance ("DAnCE_Logger_Backend_Factory");
  if (clf)
    {
      DANCE_DEBUG ((LM_TRACE, DLINFO "Starter::configure_logging_backend - "
                   "Replacing logger backend\n"));
      ACE_Log_Msg_Backend * backend = clf->get_logger_backend(this->orb_);
      backend->open(0);
      ACE_Log_Msg::msg_backend (backend);
      ACE_Log_Msg * ace = ACE_Log_Msg::instance();
      ace->clr_flags(ace->flags());
      ace->set_flags(ACE_Log_Msg::CUSTOM);
    }
}

} // DAnCE