summaryrefslogtreecommitdiff
path: root/CIAO/DAnCE/Plan_Launcher/Plan_Launcher_Base_Impl.cpp
blob: 10fcf4083b62963a58d3db9d181d6bb9b4af8a89 (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
// $Id$

#include "Plan_Launcher_Base_Impl.h"
#include "DAnCE/Logger/Log_Macros.h"
#include "ace/Env_Value_T.h"
#include "ace/OS_NS_stdio.h"
#include "ace/Get_Opt.h"

Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl(CORBA::ORB_ptr orb, int argc,
    ACE_TCHAR *argv[]) :
      orb_(CORBA::ORB::_duplicate (orb)),
      em_(Deployment::ExecutionManager::_nil()), em_ior_("file://em.ior") //default
      , mode_(0x0)
{
  DANCE_TRACE ("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl");
  if (CORBA::is_nil (this->orb_))
    {
      DANCE_DEBUG (8, (LM_INFO, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - ")
                    ACE_TEXT("Creating internal ORB.\n")));
      this->orb_ = CORBA::ORB_init (argc, argv);
    }

  this->parse_args(argc, argv);

  DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_i::init - em_ior = \"%C\"\n")
                , this->em_ior_.c_str()));

  if (this->mode_ & (MODE_START_PLAN | MODE_STOP_PLAN))
    {
      CORBA::Object_var obj;
      if (this->em_ior_.length() > 0)
        {
          obj = this->orb_->string_to_object(this->em_ior_.c_str());
          if (CORBA::is_nil(obj))
            {
              DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - ")
                            ACE_TEXT("Failed to retrieve EM object from \"%C\"\n"), this->em_ior_.c_str()));
              throw Deployment_Failure ("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - "
                                        "failed to retrieve EM object");
            }
        }
      try
       {
         this->em_ = ::Deployment::ExecutionManager::_narrow (obj.in());
       }
      catch(const CORBA::Exception&)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - ")
                        ACE_TEXT("Failed to retrieve EM object from \"%C\"\n"), this->em_ior_.c_str()));
          throw Deployment_Failure ("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - "
                                    "failed to retrieve EM object");
        }

      if (CORBA::is_nil (this->em_.in()))
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - ")
                        ACE_TEXT("failed to get an execution manager.\n")));
          throw Deployment_Failure ("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - "
                                    "failed to get an execution manager");
        }
    }
  DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::Plan_Launcher_Base_Impl - ")
                ACE_TEXT("Obtained Execution Manager ref\n")));
}

Plan_Launcher_Base_Impl::~Plan_Launcher_Base_Impl()
{
}

void Plan_Launcher_Base_Impl::execute()
{
  DANCE_TRACE ("Plan_Launcher_Base_Impl::execute - ");

  if (this->mode_ & MODE_START_PLAN)
    {
      size_t sz = this->cdr_plan_urls_.size();
      for (size_t i= 0; i < sz; ++i)
        {
          DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::execute - ")
                        ACE_TEXT("launching plan \"<%s>\"...\n"), this->cdr_plan_urls_[i].c_str()));
          ::Deployment::DeploymentPlan_var plan = this->read_cdr_plan_file(this->cdr_plan_urls_[i].c_str());
          try
          {
            CORBA::String_var uuid = this->launch_plan (plan.in());
            if (0 == uuid.in ())
              {
                throw Deployment_Failure ("Plan_Launcher_Base_Impl::execute - "
                                          "Error launching plan\n");
              }
            DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::execute - ")
                          ACE_TEXT("returned plan UUID is \"%C\"\n"), uuid.in ()));
          }
          catch (...)
          {
            this->teardown_plan (plan->UUID.in());
            throw;
          }
        }
    }
  else if (this->mode_ & MODE_STOP_PLAN)
    {
      this->stop_plan();
    }
}

const char *
Plan_Launcher_Base_Impl::launch_plan (const ::Deployment::DeploymentPlan &plan)
{
  DANCE_TRACE ("Plan_Launcher_Base_Impl::launch_plan");

  try
    {
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO
                    ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - Starting...\n")));
      if (CORBA::is_nil (this->em_.in ()))
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("Plan_Launcher_Base_Impl::launch_plan - ")
                  ACE_TEXT ("launch_plan called witn an uninitialized EM.\n")));
          return 0;
        }

      DANCE_DEBUG (6, (LM_DEBUG, DLINFO
              ACE_TEXT ("Plan_Launcher_Base_Impl::launch_plan - ")
              ACE_TEXT ("about to call this->em_->preparePlan\n")));

      ::Deployment::DomainApplicationManager_var dam;
      try
        {
          dam = this->em_->preparePlan (plan, 0);
        }
      catch(...)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("An exception was thrown during EM->preparePlan.\n")));
          throw;
        }

      DANCE_DEBUG (6, (LM_DEBUG, DLINFO
              ACE_TEXT ("Plan_Launcher_Base_Impl::launch_plan - ")
              ACE_TEXT ("after to call this->em_->preparePlan\n")));

      if (CORBA::is_nil (dam.in ()))
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("CIAO_PlanLauncher:preparePlan call failed: ")
                        ACE_TEXT("nil DomainApplicationManager reference\n")));
          return 0;
        }
      else
        {
          DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("DAM was received from preparePlan.\n")));
        }

      this->write_dam_ior (dam.in());

      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                    ACE_TEXT("Obtained DAM ref\n")));

      ::Deployment::Properties_var properties;
      ACE_NEW_RETURN (properties,
          Deployment::Properties,
          0);

      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                    ACE_TEXT("before startLaunch...\n")));

      ::Deployment::Connections_var conns;
      Deployment::Application_var da;
      try
        {
          da = dam->startLaunch (properties.in (), conns.out());
        }
      catch(...)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("An exception was thrown during DAM->startLaunch.\n")));
          throw;
        }

      if (CORBA::is_nil (da.in()))
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("CIAO_PlanLauncher:startLaunch call failed: ")
                  ACE_TEXT("nil DomainApplication reference\n")));
          return 0;
        }
      else
        {
          DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("DomainApplication was received from startLaunch\n")));
        }

      this->create_external_connections (plan, conns.inout());

      // Call finish Launch to complete the connections
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                    ACE_TEXT("before finishLaunch\n")));
      try
        {
          da->finishLaunch (conns.in(), false); // is_ReDAC by default is <false>
        }
      catch(...)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("An exception was thrown during DA->finishLaunch.\n")));
          throw;
        }

      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                    ACE_TEXT("after finishLaunch\n")));

      // Call start to activate components
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                    ACE_TEXT("before start activating components...\n")));

      try
        {
          da->start ();
        }
      catch(...)
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - ")
                        ACE_TEXT("An exception was thrown during DA->start.\n")));
          throw;
        }

      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::launch_plan - [success]\n")));
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO
              ACE_TEXT ("Plan_Launcher_Base_Impl::launch_plan - ")
              ACE_TEXT ("Application Deployed successfully\n")));

    }
  catch (const Deployment::ResourceNotAvailable& ex)
    {
      char buf[1024];
      ACE_OS::sprintf (buf, "Plan_Launcher_Base_Impl::launch_plan - EXCEPTION: ResourceNotAvaiable exception caught: <%s>,\n"
          "Type: <%s>\n"
          "Property: <%s>\n"
          "Element: <%s>\n"
          "Resource: <%s>\n",
          ex.name.in (),
          ex.resourceType.in (),
          ex.propertyName.in (),
          ex.elementName.in (),
          ex.resourceName.in ());
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("%C"), buf));
      throw Deployment_Failure (buf);
    }
  catch (const Deployment::StartError& ex)
    {
      char buf[1024];
      ACE_OS::sprintf (buf, "Plan_Launcher_Base_Impl::launch_plan - EXCEPTION: StartError exception caught: <%s>, <%s>\n",
          ex.name.in (),
          ex.reason.in ());
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("%C"), buf));
      throw Deployment_Failure (buf);
    }
  catch (const Deployment::InvalidProperty& ex)
    {
      char buf[1024];
      ACE_OS::sprintf (buf, "Plan_Launcher_Base_Impl::launch_plan - EXCEPTION: InvalidProperty exception caught: <%s>, <%s>\n",
          ex.name.in (),
          ex.reason.in ());
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("%C"), buf));
      throw Deployment_Failure (buf);
    }
  catch (const Deployment::InvalidConnection& ex)
    {
      char buf[1024];
      ACE_OS::sprintf (buf, "Plan_Launcher_Base_Impl::launch_plan - EXCEPTION: InvalidConnection exception caught: <%s>, <%s>\n",
          ex.name.in (),
          ex.reason.in ());
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("%C"), buf));
      throw Deployment_Failure (buf);
    }
  catch (const CORBA::Exception& ex)
    {
      char buf[1024];
      ACE_OS::sprintf (buf, "Plan_Launcher_Base_Impl::launch_plan - CORBA EXCEPTION: <%s>\n",
          ex._info().fast_rep());
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("%C"), buf));
      throw Deployment_Failure (buf);
    }
  catch (...)
    {
      char buf[1024];
      ACE_OS::sprintf (buf, "Plan_Launcher_Base_Impl::launch_plan - EXCEPTION: non-CORBA exception\n");
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("%C"), buf));
      throw Deployment_Failure (buf);
    }

  return CORBA::string_dup (plan.UUID.in());
}

bool Plan_Launcher_Base_Impl::teardown_plan(const char *uuid)
{
  DANCE_TRACE ("Plan_Launcher_Base_Impl::teardown_plan");

  try
    {
      DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::teardown_plan - looking for uuid=\"%C\"\n"), uuid));
      ::Deployment::DomainApplicationManagers_var mgrs = this->em_->getManagers();
      ::Deployment::DomainApplicationManager_var dapp_mgr;
      for (size_t i = 0; i < mgrs->length(); ++i)
        {
          ACE_CString s = (*mgrs) [i]->getPlan()->UUID.in();
          DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::teardown_plan - ")
                        ACE_TEXT("comparing with existing uuid=\"%C\"\n"), s.c_str()));
          if (s == uuid)
            {
              dapp_mgr = ::Deployment::DomainApplicationManager::_duplicate ( (*mgrs) [i].in());
              break;
            }
        }

      if (!::CORBA::is_nil (dapp_mgr.in()))
        {
          this->teardown_plan (dapp_mgr.in());
        }
      else
        {
          throw Deployment_Failure ("Failed to find DAM by UUID");
        }
    }
  catch (const CORBA::Exception&)
    {
      // @todo the destroy_dam_by_plan could give a stoperror exception
      // we should handle
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::teardown_plan - ")
                    ACE_TEXT("Unable to find DomainApplicationManager ")
                    ACE_TEXT("for plan with uuid: %C\n"), uuid));
      throw;
    }

  return true;
}

void
Plan_Launcher_Base_Impl::teardown_plan(::Deployment::DomainApplicationManager_ptr dam)
{
  DANCE_DEBUG (6, (LM_DEBUG, DLINFO
              ACE_TEXT("Plan_Launcher_Base_Impl::teardown_plan - destroy the application.....\n")));
  ::Deployment::Applications_var apps = dam->getApplications();
  if (0 == apps->length())
    {
      DANCE_DEBUG (6, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::teardown_plan - ")
                   ACE_TEXT("DAM has no application.\n")));
    }
  for (CORBA::ULong i = 0; i < apps->length(); ++i)
    {
      dam->destroyApplication(apps[i]);
    }
  this->destroy_dam(dam);

  DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::teardown_plan - [success]\n")));
}

void
Plan_Launcher_Base_Impl::destroy_dam(
    ::Deployment::DomainApplicationManager_ptr dam)
{
  DANCE_DEBUG (6, (LM_DEBUG, DLINFO
              ACE_TEXT("Plan_Launcher_Base_Impl::destroy_dam - destroy the manager.....\n")));
  this->em_->destroyManager(dam);

  DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::destroy_dam - [success]\n")));
}

void
Plan_Launcher_Base_Impl::usage(const ACE_TCHAR*)
{
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Plan Launcher Options :\n")
          ACE_TEXT ("-k|--em-ior <EXECUTION_MANAGER_IOR>")
          ACE_TEXT (" : Default file://em.ior\n")
          ACE_TEXT ("-d|--read-cdr-plan <CDR_DEPLOYMENT_PLAN_URL>\n")
          ACE_TEXT ("-w|--write-cdr-plan <CDR_DEPLOYMENT_PLAN_URL>\n")
          ACE_TEXT ("-t|--plan-uuid <PLAN_UUID>\n")
                 //          ACE_TEXT ("-a|dam-ior <DOMAIN_APPLICATION_MANAGER_IOR>\n")
          ACE_TEXT ("-q|--stop-plan : Stop the plan\n")
          ACE_TEXT ("-h|--help : Show this usage information\n")
          ACE_TEXT ("-x|--read-plan <XML_DEPLOYMENT_PLAN_URL>\n\n")));
}

void
Plan_Launcher_Base_Impl::parse_args(int argc, ACE_TCHAR *argv[])
{
  DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("PL options : \"")));

  for (int i = 0; i < argc; ++i)
    {
      DANCE_DEBUG (9, (LM_TRACE, "\t%s\n", argv[i]));
    }

  ACE_Get_Opt get_opt(argc, argv,
  ACE_TEXT ("k:d:w:t:a:qx:h"), 0);
  get_opt.long_option(ACE_TEXT("em-ior"), 'k', ACE_Get_Opt::ARG_REQUIRED);
  get_opt.long_option(ACE_TEXT("read-cdr-plan"), 'd', ACE_Get_Opt::ARG_REQUIRED);
  get_opt.long_option(ACE_TEXT("write-cdr-plan"), 'w', ACE_Get_Opt::ARG_REQUIRED);
  get_opt.long_option(ACE_TEXT("plan-uuid"), 't', ACE_Get_Opt::ARG_REQUIRED);
  get_opt.long_option(ACE_TEXT("dam-ior"), 'a', ACE_Get_Opt::ARG_REQUIRED);
  get_opt.long_option(ACE_TEXT("stop-plan"), 'q', ACE_Get_Opt::NO_ARG);
  get_opt.long_option(ACE_TEXT("read-plan"), 'x', ACE_Get_Opt::ARG_REQUIRED);
  get_opt.long_option(ACE_TEXT("help"), 'h', ACE_Get_Opt::NO_ARG);

  int c;
  ACE_CString s;
  while ( (c = get_opt ()) != EOF)
    {
      switch (c)
        {
          case 'k':
            this->em_ior_ = expand_env_vars (get_opt.opt_arg());
            break;
          case 'd':
            s = expand_env_vars (get_opt.opt_arg());
            // trim leading file://
            if (0 < s.length())
              {
                ssize_t const pos = s.find("file://");
                if (0 == pos)
                  {
                    s = s.substring(7);
                  }
                this->cdr_plan_urls_.push_back(s);
              }
            break;
          case 'w':
            if (0 < this->cdr_dest_url_.length())
              {
                DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Ambiguity: more then one --write-cdr-plan option.\n")));
                throw Deployment_Failure("Ambiguity: more then one --write-cdr-plan option.");
              }
            this->cdr_dest_url_ = expand_env_vars (get_opt.opt_arg());
            this->mode_ |= MODE_WRITE_CDR;
            // trim leading file://
            if (0 < this->cdr_dest_url_.length())
              {
                ssize_t pos = this->cdr_dest_url_.find("file://");
                if (0 == pos)
                  {
                    this->cdr_dest_url_= this->cdr_dest_url_.substring(7);
                  }
              }
            break;
          case 't':
            this->plan_uuid_ = ACE_TEXT_ALWAYS_CHAR (get_opt.opt_arg());
            break;
          case 'a':
            this->dam_ior_ = expand_env_vars (get_opt.opt_arg());
            DANCE_DEBUG (9, (LM_TRACE, "Parsed DAM IOR : %s\n", this->dam_ior_.c_str()));
            break;
          case 'q':
            this->mode_ |= MODE_STOP_PLAN;
            break;
          case 'x':
            this->xml_plan_urls_.push_back(expand_env_vars (get_opt.opt_arg()));
            break;
          case 'h':
            usage (argv[0]);
            throw Help_Issued ();
            break;
          default:
            usage (argv[0]);
            throw Deployment_Failure ("parse_args : invalid arguments");
        }
    }
  if (0 == (this->mode_ & (MODE_STOP_PLAN | MODE_WRITE_CDR)))
    {
      this->mode_ |= MODE_START_PLAN;
    }
  // Post-check
  this->check_mode_consistentness();
}

void
Plan_Launcher_Base_Impl::write_dam_ior(
    ::Deployment::DomainApplicationManager_ptr dam)
{
  if (this->dam_ior_.length () == 0)
    return;

  CORBA::String_var ior = this->orb_->object_to_string(dam);

  FILE* ior_output_file = ACE_OS::fopen (this->dam_ior_.c_str(), "w");

  if (ior_output_file)
    {
      ACE_OS::fprintf (ior_output_file, "%s", ior.in());
      ACE_OS::fclose (ior_output_file);
    }
  else
    {
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("PlanLauncher.cpp::write_dap_ior - Error in opening file <%s> to write DAM IOR: %m"),
              this->dam_ior_.c_str()));
      throw Deployment_Failure (ACE_CString ("write_dap_ior - Error in opening file <%s> to write DAM IOR: %m") +
          this->dam_ior_);
    }
}

void
Plan_Launcher_Base_Impl::stop_plan(void)
{
  bool stopped = false;
  if (!is_empty (this->plan_uuid_))
    {
      stopped = true;
      DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                   ACE_TEXT("Stopping plan \"%C\"\n"), this->plan_uuid_.c_str ()));

      if (!this->teardown_plan(this->plan_uuid_.c_str ()))
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                        ACE_TEXT("tear down assembly failed: unknown plan uuid.\n")));
        }
    }

  if (!is_empty (this->dam_ior_))
    {
      stopped = true;
      DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                   ACE_TEXT("Stopping plan by DAM IOR.\n")));
      CORBA::Object_var
        obj = this->orb_->string_to_object(this->dam_ior_.c_str());
      if (CORBA::is_nil (obj.in ()))
        {
          DANCE_ERROR (1, (LM_ERROR, DLINFO
                        ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                        ACE_TEXT("tear down assembly failed: DAM IOR is invalid.\n")));
        }
      else
        {
          Deployment::DomainApplicationManager_var
            dam = Deployment::DomainApplicationManager::_narrow (obj.in());
          this->teardown_plan(dam.in());
        }
    }

  if (0 < this->cdr_plan_urls_.size())
    {
      stopped = true;
      size_t const sz = this->cdr_plan_urls_.size();
      for (size_t i = 0; i < sz; ++i)
        {
          DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                       ACE_TEXT("Stopping plan by plan file : <%s>\n"), this->cdr_plan_urls_[i].c_str()));
          ::Deployment::DeploymentPlan_var plan = this->read_cdr_plan_file(this->cdr_plan_urls_[i].c_str());
          if (!this->teardown_plan(plan->UUID.in()))
            {
              DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                            ACE_TEXT("tear down assembly failed: unknown plan uuid.\n")));
            }
        }
    }

  if (!stopped && 0 == this->xml_plan_urls_.size())
    {
      DANCE_DEBUG (9, (LM_TRACE, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::stop_plan - ")
                   ACE_TEXT("Stopping plan all running plans.\n")));
      ::Deployment::DomainApplicationManagers_var
          mgrs = this->em_->getManagers();
      ::Deployment::DomainApplicationManager_var dapp_mgr;
      for (size_t i = 0; i < mgrs->length(); ++i)
        {
          this->teardown_plan((*mgrs) [i]);
        }
    }
}

void
Plan_Launcher_Base_Impl::create_external_connections(
    const ::Deployment::DeploymentPlan &plan, Deployment::Connections& conn)
{
  DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT("create_external_connections - start\n")));
  for (CORBA::ULong i = 0; i < plan.connection.length(); i++)
    {
      if (plan.connection[i].externalReference.length() > 0
          && plan.connection[i].externalReference[0].provider)
        {
          DANCE_DEBUG (6, (LM_DEBUG,
                  "Plan_Launcher_i::create_external_connections - create connection %C from IOR %C\n",
                  plan.connection[i].name.in(),
                  plan.connection[i].externalReference[0].location.in()));
          CORBA::ULong const indx = conn.length();
          conn.length(indx + 1);
          conn[indx].name= CORBA::string_dup (plan.connection[i].name.in());
          conn[indx].endpoint.length(1L);
          CORBA::Object_ptr
              obj = this->orb_->string_to_object(plan.connection[i].externalReference[0].location.in());
          if (!CORBA::is_nil (obj))
            {
              //Components::CCMObject_ptr ccmobj = Components::CCMObject::_narrow (obj);
              conn[indx].endpoint[0] = obj;
            }
          else
            {
              DANCE_DEBUG (6, (LM_DEBUG,
                      ACE_TEXT("Plan_Launcher_i::create_external_connections - can't create object for IOR %C\n"),
                      plan.connection[i].externalReference[0].location.in()));
              throw 0;
            }
        }
    }
}

Deployment::DeploymentPlan*
Plan_Launcher_Base_Impl::read_cdr_plan_file(const char *cdr_plan_uri)
{
  Deployment::DeploymentPlan* res = 0;
  try
    {
      FILE* file = ACE_OS::fopen (cdr_plan_uri, "r");
      if (0 == file)
        {
          ACE_CString s = "read_cdr_plan_file : failed to open file \"";
          s += cdr_plan_uri;
          s += "\"";
          throw Deployment_Failure (s.c_str());
        }

      size_t buf_size = 0;
      ACE_OS::fread (&buf_size, sizeof (buf_size), 1, file);

      char * buf = new char[buf_size];
      ACE_OS::fread (buf, 1, buf_size, file);
        {
          TAO_InputCDR cdr (buf, buf_size);
          ACE_NEW_THROW_EX (res,
              Deployment::DeploymentPlan (),
              CORBA::NO_MEMORY ());
          cdr >> *res;
        }
      delete [] buf;
      ACE_OS::fclose (file);
    }
  catch(...)
    {
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Failed to read CDR plan file.\n")));
      throw Deployment_Failure("Failed to read CDR plan file.");
    }
  return res;
}

void
Plan_Launcher_Base_Impl::write_cdr_plan_file(const char * filename,
    const Deployment::DeploymentPlan & plan)
{
  try
    {
      FILE * file = ACE_OS::fopen (filename, "w");
      if (0 == file)
        {
          throw Deployment_Failure ("write_cdr_plan_file : failed to open file.");
        }
      TAO_OutputCDR cdr;
      if (cdr << plan)
        {
          size_t buf_size = cdr.total_length();
          ACE_OS::fwrite (&buf_size, sizeof (buf_size), 1, file);
          for (const ACE_Message_Block *i = cdr.begin ();
              i != 0;
              i = i->cont ())
            {
              ACE_OS::fwrite (i->rd_ptr (), 1, i->length (), file);
            }
          ACE_OS::fclose (file);
        }
      else
        {
          throw Deployment_Failure ("write_cdr_plan_file : failed marshal plan.");
        }
    }
  catch(...)
    {
      DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT("Plan_Launcher_Base_Impl::write_cdr_plan_file - ")
                    ACE_TEXT("Failed to write CDR plan file.\n")));
      throw Deployment_Failure("Failed to write CDR plan file.");
    }
}

void
Plan_Launcher_Base_Impl::check_mode_consistentness()
{
  if (this->mode_ & MODE_START_PLAN)
    {
      if (0 == this->cdr_plan_urls_.size() && 0 == this->xml_plan_urls_.size())
        {
          throw Deployment_Failure ("check_mode_consistentness : no plan to start");
          }
      }
    else if (this->mode_ & MODE_STOP_PLAN)
      {
        //          if (is_empty (this->cdr_plan_url_)
        //              && is_empty (this->xml_plan_url_)
        //              && is_empty (this->plan_uuid_)
        //              && is_empty (this->dam_ior_))
        //            {
        //              throw Deployment_Failure ("check_mode_consistentness : no plan to stop");
        //            }
      }
    else if (this->mode_ & MODE_WRITE_CDR)
      {
        if (is_empty (this->cdr_dest_url_))
          {
            throw Deployment_Failure ("check_mode_consistentness : no cdr url for writing plan.");
          }
        if (1 != this->xml_plan_urls_.size())
          {
            throw Deployment_Failure ("check_mode_consistentness : not one source url for writing plan.");
          }
      }
    else
      {
        throw Deployment_Failure ("check_mode_consistentness : no action specified");
      }
  }

ACE_CString
Plan_Launcher_Base_Impl::expand_env_vars (const ACE_TCHAR * s)
  {
    ACE_TString src(s);
    ACE_TString res;
    size_t pos_done = 0;
    while (pos_done < (size_t) src.length())
      {
        size_t pos_start = src.find ('$', pos_done);
        if (ACE_TString::npos == pos_start)
          {
            res += src.substring (pos_done);
            pos_done = src.length();
          }
        else // take the substring before '$' and append value
          {
            if (pos_start > pos_done)
              {
                res += src.substring (pos_done, pos_start - pos_done);
                pos_done = pos_start;
              }

            size_t pos_end = src.length();

            size_t p;

            p = src.find (ACE_TEXT(' '), pos_start + 1);
            if (ACE_TString::npos != p && pos_end > p) pos_end = p;

            p = src.find (ACE_TEXT('/'), pos_start + 1);
            if (ACE_TString::npos != p && pos_end > p) pos_end = p;

            p = src.find (ACE_TEXT('\\'), pos_start + 1);
            if (ACE_TString::npos != p && pos_end > p) pos_end = p;

            p = src.find (ACE_TEXT('$'), pos_start + 1);
            if (ACE_TString::npos != p && pos_end > p) pos_end = p;

            if (pos_end - pos_start > 1)
              {
                ACE_Env_Value<const ACE_TCHAR*> val (src.substring (pos_start + 1, pos_end - pos_start - 1).c_str(), 0);
                res += val;
                pos_done = pos_end;
              }
            else
              {
                DANCE_DEBUG (6, (LM_WARNING, DLINFO
                              ACE_TEXT("Plan_Launcher_Base_Impl::expand_env_vars - ")
                              ACE_TEXT("Envvar can not be parsed out at %i in \"<%s>\""),
                              pos_start,
                              src.c_str()));
              }
          }
      }
    return res;
  }