summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/tao_imr_i.cpp
blob: c589f6dd3ce3e880e82d513102d677deb1364b1d (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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
// $Id$

#include "tao_imr_i.h"

#include "tao/PortableServer/PortableServer.h"
#include "tao/PortableServer/ForwardRequestC.h"

#include "tao/Stub.h"
#include "tao/Profile.h"

#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"
#include "ace/OS_NS_strings.h"
#include "ace/OS.h"
#include "ace/Argv_Type_Converter.h"

TAO_IMR_i::TAO_IMR_i (void)
  : imr_ (ImplementationRepository::Administration::_nil ())
{
  // Nothing
}

TAO_IMR_i::~TAO_IMR_i (void)
{
}

int
TAO_IMR_i::run ()
{
  if (this->op_.get () == 0)
    {
      ACE_ERROR ((LM_ERROR, "Unknown operation"));
      return TAO_IMR_Op::UNKNOWN;
    }

  return this->op_->run ();
}

int
TAO_IMR_i::init (int argc, ACE_TCHAR **argv)
{
  const char *exception_message = "Null Message";

  ACE_Argv_Type_Converter convert (argc, argv);

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Retrieve the ORB.
      this->orb_ = CORBA::ORB_init (convert.get_argc(), convert.get_ASCII_argv(),
                                    "tao_imr_i" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Parse command line and verify parameters.
      if (this->parse_args (convert.get_argc(), convert.get_TCHAR_argv()) == -1)
        return -1;

      // Get the ImplRepo object
      CORBA::Object_var obj =
        orb_->resolve_initial_references ("ImplRepoService" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (obj.in ()))
        {
          ACE_ERROR ((LM_ERROR, "Unable to resolve the ImR.\n"));
          return -1;
        }

      exception_message = "While narrowing ImR";

      this->imr_ =
        ImplementationRepository::Administration::_narrow (obj.in () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (imr_.in ()))
        {
          ACE_ERROR ((LM_ERROR, "Unable to narrow the ImR.\n"));
          return -1;
        }

      this->op_->set_imr (this->imr_.in ());
    }
  ACE_CATCHANY
    {
      ACE_ERROR ((LM_ERROR, "TAO_IMR_i::init - %s\n", exception_message));
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Exception");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}


// Go through and figure out which operation we should do.

int
TAO_IMR_i::parse_args (int argc, ACE_TCHAR **argv)
{
  // Make sure one command was given
  if (argc < 2)
    {
      ACE_ERROR((LM_ERROR, "Error: No operation specified.\n"));
      this->print_usage ();
      return -1;
    }

  this->op_.reset(TAO_IMR_Op::make_op (argv[1]));

  // Check for unrecognized operation

  if (this->op_.get() == 0)
    {
      ACE_ERROR((LM_ERROR, "Error: Unknown operation '%s'.\n", argv[1]));
      this->print_usage ();
      return -1;
    }

  // Adjust argc and argv so only the command specific args are passed
  return this->op_->parse (argc - 1, argv + 1);
}


// Print out information about all operations.

void
TAO_IMR_i::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Usage: tao_imr [options] command [command-arguments]\n"
              "  where [options] are ORB options\n"
              "  where command is one of the following:\n"
              "    start         Start a server through the ImR\n"
              "    add           Add an entry to the ImR\n"
              "    autostart Activates all AUTO_START servers\n"
              "    ior       Creates a simplified IOR\n"
              "    list          List the entries in the ImR\n"
              "    remove        Remove an entry from the ImR\n"
              "    shutdown      Shut down a server through the ImR\n"
              "    shutdown-repo Shut down the ImR\n"
              "    update        Update an entry in the ImR\n"
              "  where [command-arguments] depend on the command\n"));
}


// Factory for operations

TAO_IMR_Op *
TAO_IMR_Op::make_op (const ACE_TCHAR *op_name)
{
  if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("activate")) == 0)
    {
      ACE_ERROR((LM_ERROR, "Warning: The activate option has been renamed to start.\n"));
      return new TAO_IMR_Op_Activate ();
    }
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("start")) == 0)
    return new TAO_IMR_Op_Activate ();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("add")) == 0)
    return new TAO_IMR_Op_Register (true);
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("autostart")) == 0)
    return new TAO_IMR_Op_Autostart();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("ior")) == 0)
    return new TAO_IMR_Op_IOR();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("list")) == 0)
    return new TAO_IMR_Op_List ();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("remove")) == 0)
    return new TAO_IMR_Op_Remove ();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("shutdown")) == 0)
    return new TAO_IMR_Op_Shutdown ();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("shutdown-repo")) == 0)
    return new TAO_IMR_Op_ShutdownRepo ();
  else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("update")) == 0)
    return new TAO_IMR_Op_Register (false);

  return 0;
}


TAO_IMR_Op::~TAO_IMR_Op ()
{
  // Nothing
}

void
TAO_IMR_Op::set_imr (ImplementationRepository::Administration_ptr imr)
{
  this->imr_ = imr;
}

void
TAO_IMR_Op::display_server_information (const ImplementationRepository::ServerInformation &info)
{
  // Figure out what the activation string is.
  const char *act = "UNKNOWN STARTUP";
  if (info.startup.activation == ImplementationRepository::NORMAL)
    act = "NORMAL";
  else if (info.startup.activation == ImplementationRepository::MANUAL)
    act = "MANUAL";
  else if (info.startup.activation == ImplementationRepository::PER_CLIENT)
    act = "PER_CLIENT";
  else if (info.startup.activation == ImplementationRepository::AUTO_START)
    act = "AUTO_START";

  // Print out information
  ACE_DEBUG ((LM_DEBUG, "Server <%s>\n", info.server.in ()));

  const char * locked_out = "";

  int limit = info.startup.start_limit;
  if (info.startup.start_limit < 0)
    {
      limit = -limit;
      locked_out = "  Locked Out\n";
    }

  ACE_DEBUG ((LM_DEBUG,
              "  Activator: %s\n"
              "  Command Line: %s\n"
              "  Working Directory: %s\n"
              "  Activation Mode: %s\n"
              "  Number of retries: %d\n"
              "%s",
              info.startup.activator.in (),
              info.startup.command_line.in (),
              info.startup.working_directory.in (),
              act,
              limit - 1,
              locked_out));
  for (CORBA::ULong i = 0; i < info.startup.environment.length (); ++i)
    ACE_DEBUG ((LM_DEBUG, "Environment Variable: %s=%s \n",
                info.startup.environment[i].name.in (),
                info.startup.environment[i].value.in ()));

  if (info.startup.activation == ImplementationRepository::PER_CLIENT)
    ACE_DEBUG ((LM_DEBUG,
                "  No running info available for PER_CLIENT mode\n"));
  else if (ACE_OS::strlen (info.partial_ior.in ()) > 0)
    ACE_DEBUG ((LM_DEBUG,
                "  Running at endpoint: %s\n",
                info.partial_ior.in ()));
  else   // I am assuming that a blank partial_ior means currently not running.
    ACE_DEBUG ((LM_DEBUG,
                "  Not currently running\n"));

  ACE_DEBUG ((LM_DEBUG, "\n"));
}

TAO_IMR_Op_List::TAO_IMR_Op_List (void)
  : verbose_server_information_ (0)
{
  // Nothing
}

TAO_IMR_Op_Register::TAO_IMR_Op_Register (bool is_add)
  : is_add_ (is_add)
  , set_command_line_ (false)
  , set_environment_vars_(false)
  , set_working_dir_ (false)
  , set_activation_ (false)
  , activation_(ImplementationRepository::NORMAL)
  , set_retry_count_(false)
  , retry_count_ (0)
  , set_activator_ (false)
{
  // Nothing
}

void
TAO_IMR_Op_Activate::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Starts a server using its registered Activator.\n"
              "\n"
              "Usage: tao_imr [options] start <name>\n"
              "  where [options] are ORB options\n"
              "  where <name> is the name of a registered POA.\n"
              "  -h Displays this\n"));
}

int
TAO_IMR_Op_Activate::parse (int argc, ACE_TCHAR **argv)
{
  // Check for enough arguments (we need at least one for the server name)
  if (argc < 2)
    {
      this->print_usage ();
      return -1;
    }

  // Skip both the program name and the "activate" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("h"));

  this->server_name_.set (ACE_TEXT_TO_CHAR_IN (argv[1]));
  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'h':
          this->print_usage ();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }
  return 0;
}

void
TAO_IMR_Op_Autostart::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Usage: tao_imr [options] autostart\n"
              "  where [options] are ORB options\n"
              "  -h Displays this\n"));
}

int
TAO_IMR_Op_Autostart::parse (int argc, ACE_TCHAR **argv)
{
  // Skip the "autostart" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("h"));

  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'h':  // display help
          this->print_usage ();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }
  return 0;
}

void
TAO_IMR_Op_IOR::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Creates an IOR for a server that is registered with the IMR and uses\n"
              "the InterOperable Naming Service.  Please see the documentation for\n"
              "more information on which server configurations work with this command.\n"
              "\n"
              "Usage: tao_imr [options] ior <object_key> [command-arguments]\n"
              "  where [options] are ORB options\n"
              "  where <object_key> matches the simple key bound in the server IORTable.\n"
              "  where [command-arguments] can be\n"
              "    -f filename   filename to output the IOR to\n"
              "    -h            Displays this\n"));
}

int
TAO_IMR_Op_IOR::parse (int argc, ACE_TCHAR **argv)
{
  // Check for enough arguments (we need at least one for the server name)
  if (argc < 2)
    {
      this->print_usage ();
      return -1;
    }

  // Skip both the program name and the "ior" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("hf:"));

  this->server_name_.set (ACE_TEXT_TO_CHAR_IN (argv[1]));
  if (this->server_name_.length() == 0 || this->server_name_[0] == '-')
    {
      ACE_ERROR((LM_ERROR, "ERROR : name is required.\n"));
      this->print_usage ();
      return -1;
    }

  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'f':  // File name
          this->filename_.set (ACE_TEXT_TO_CHAR_IN (get_opts.opt_arg ()));
          break;
        case 'h':  // display help
          this->print_usage();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }

  return 0;
}

void
TAO_IMR_Op_List::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Lists all or one of the servers in the Implementation Repository\n"
              "\n"
              "Usage: tao_imr [options] list [name] [command-arguments]\n"
              "  where [options] are ORB options\n"
              "  where [name] is the optional server name to search for\n"
              "  where [command-arguments] can be\n"
              "    -v            Verbose: Displays more info for each server when\n"
              "                  displaying more than one server\n"
              "    -h            Displays this\n"));
}

int
TAO_IMR_Op_List::parse (int argc, ACE_TCHAR **argv)
{
  int server_flag = 0;

  if (argc > 1 && argv[1][0] != '-')
    {
      this->server_name_.set (ACE_TEXT_TO_CHAR_IN (argv[1]));
      server_flag = 2;
    }

  // Skip both the program name and the "list" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("vh"), server_flag);

  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'v': // verbose server display
          this->verbose_server_information_ = 1;
          break;
        case 'h':  // display help
          this->print_usage ();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }
  return 0;
}

void
TAO_IMR_Op_Remove::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Removes a server entry\n"
              "\n"
              "Usage: tao_imr [options] remove <name>\n"
              "  where [options] are ORB options\n"
              "  where <name> is the POA name used by the server object\n"
              "  -h Displays this\n"));
}

int
TAO_IMR_Op_Remove::parse (int argc, ACE_TCHAR **argv)
{
  // Check for enough arguments (we need at least one for the server name)
  if (argc < 2)
    {
      this->print_usage ();
      return -1;
    }

  // Skip both the program name and the "remove" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("h"));

  this->server_name_.set (ACE_TEXT_TO_CHAR_IN (argv[1]));
  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'h':
          this->print_usage ();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }
  return 0;
}

void
TAO_IMR_Op_Shutdown::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Shuts down a server\n"
              "\n"
              "Usage: tao_imr [options] shutdown <name>\n"
              "  where [options] are ORB options\n"
              "  where <name> is the name of the server object\n"
              "  -h Displays this\n"));
}

int
TAO_IMR_Op_Shutdown::parse (int argc, ACE_TCHAR **argv)
{
  // Check for enough arguments (we need at least one for the server name)
  if (argc < 2)
    {
      this->print_usage ();
      return -1;
    }

  // Skip both the program name and the "shutdown" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("h"));

  this->server_name_.set (ACE_TEXT_TO_CHAR_IN (argv[1]));
  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'h':
          this->print_usage ();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }
  return 0;
}

TAO_IMR_Op_ShutdownRepo::TAO_IMR_Op_ShutdownRepo()
  : activators_(false)
{
}

void
TAO_IMR_Op_ShutdownRepo::print_usage (void)
{
  ACE_ERROR ((LM_ERROR, "Shuts down the ImR\n"
              "\n"
              "Usage: tao_imr [options] shutdown-repo [-a]\n"
              "  where [options] are ORB options\n"
              "  Specify -a to also shutdown any registered ImR Activators.\n"
              "  -h Displays this\n"));
}

int
TAO_IMR_Op_ShutdownRepo::parse (int argc, ACE_TCHAR **argv)
{
  // Check for enough arguments (we need at least one for the server name)
  if (argc < 1)
    {
      this->print_usage ();
      return -1;
    }

  // Skip both the program name and the "shutdown-repo" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("ha"));

  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'h':
          this->print_usage ();
          return -1;
        case 'a':
          activators_ = true;
          break;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }
    }

  return 0;
}

void
TAO_IMR_Op_Register::addenv (ACE_TCHAR *opt)
{
  CORBA::ULong length = this->environment_vars_.length ();
  // Increase the length of the sequence
  this->environment_vars_.length (length + 1);
  ACE_TString tokens (opt);
  int index = tokens.find (ACE_TEXT("="));
  // Insert at position length since that is our new element
  this->environment_vars_ [length].name =
    CORBA::string_dup (tokens.substr (0, index).c_str ());
  this->environment_vars_ [length].value =
    CORBA::string_dup (tokens.substr (index + 1).c_str ());
}

void
TAO_IMR_Op_Register::print_usage (void)
{
  ACE_ERROR ((LM_ERROR,
              "Adds/Updates a server entry\n"
              "\n"
              "Usage: tao_imr [options] <add|update> <name> [command-arguments]\n"
              "  where [options] are ORB options\n"
              "  where <name> is the POA name used by the server object\n"
              "  where [command-arguments] can be\n"
              "    -h            Displays this\n"
              "    -l            Activator name.\n"
              "    -c command    Startup command\n"
              "    -w dir        Working directory\n"
              "    -e name=value Set environment variables\n"
              "    -a mode       Set activate mode (NORMAL|MANUAL|PER_CLIENT|AUTO_START)\n"
              "    -r count      Set the startup/ping retry count to count\n"));
}

int
TAO_IMR_Op_Register::parse (int argc, ACE_TCHAR **argv)
{
  // Check for enough arguments (we need at least one for the server name)
  if (argc < 2)
    {
      ACE_ERROR((LM_ERROR, "Error: Must supply at least a server name.\n"));
      this->print_usage ();
      return -1;
    }

  // Skip both the program name and the "update" command
  ACE_Get_Arg_Opt<ACE_TCHAR> get_opts (argc, argv, ACE_TEXT("hc:w:a:e:r:R:l:"));

  this->server_name_.set (ACE_TEXT_TO_CHAR_IN (argv[1]));
  int c;

  while ((c = get_opts ()) != -1)
    {
      switch (c)
        {
        case 'c':  // Command line arguments
          this->set_command_line_ = true;
          this->command_line_.set (ACE_TEXT_TO_CHAR_IN (get_opts.opt_arg ()));
          break;
        case 'e':  // set environment variables
          this->set_environment_vars_ = true;
          this->addenv( get_opts.opt_arg () );
          break;
        case 'w':  // Working Directory
          this->set_working_dir_ = true;
          this->working_dir_.set (ACE_TEXT_TO_CHAR_IN (get_opts.opt_arg ()));
          break;
        case 'a':  // Activation Mode
          this->set_activation_ = true;
          if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("NORMAL")) == 0)
            this->activation_ = ImplementationRepository::NORMAL;
          else if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("MANUAL")) == 0)
            this->activation_ = ImplementationRepository::MANUAL;
          else if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("PER_CLIENT")) == 0)
            this->activation_ = ImplementationRepository::PER_CLIENT;
          else if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("AUTO_START")) == 0)
            this->activation_ = ImplementationRepository::AUTO_START;
          else
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Unknown Activation Mode <%s>.\n",
                               get_opts.opt_arg ()),
                              -1);
          break;
        case 'r':
        case 'R':   // startup/ping Retry Count
          {
            this->set_retry_count_ = true;
            int rc = ACE_OS::atoi(get_opts.optarg);
            if (rc > 0)
              this->retry_count_ = rc;
          }
          break;
        case 'l': /// hostname of the activator
          this->activator_.set (ACE_TEXT_TO_CHAR_IN (get_opts.optarg));
          this->set_activator_ = true;
          break;
        case 'h':  // display help
          this->print_usage ();
          return -1;
        default:
          ACE_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
          this->print_usage ();
          return -1;
        }

    }
  return 0;
}


// ============================================================================
// = Run methods


int
TAO_IMR_Op_Activate::run (void)
{
  ACE_ASSERT(! CORBA::is_nil(imr_));
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->imr_->activate_server (this->server_name_.c_str () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
      ACE_DEBUG ((LM_DEBUG,
                  "Successfully Activated server <%s>\n",
                  this->server_name_.c_str ()));
    }
  ACE_CATCH (ImplementationRepository::CannotActivate, ex)
    {
      ACE_ERROR ((LM_ERROR, "Cannot activate server <%s>, reason: <%s>\n",
                  this->server_name_.c_str (),
                  ex.reason.in ()));
      return TAO_IMR_Op::CANNOT_ACTIVATE;
    }
  ACE_CATCH (ImplementationRepository::NotFound, ex)
    {
      ACE_ERROR ((LM_ERROR, "Could not find server <%s>.\n", this->server_name_.c_str ()));
      return TAO_IMR_Op::NOT_FOUND;
    }
  ACE_CATCH (PortableServer::ForwardRequest, ex)
    {
      ACE_RE_THROW;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Activating Server");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_Autostart::run (void)
{
  ACE_ASSERT(! CORBA::is_nil (imr_));

  ImplementationRepository::ServerInformationList_var server_list;
  ImplementationRepository::ServerInformationIterator_var server_iter;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->imr_->list (0,
                        server_list,
                        server_iter
                        ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_ASSERT(CORBA::is_nil (server_iter.in ()));

      CORBA::ULong len = server_list->length ();
      for (CORBA::ULong i = 0; i < len; ++i)
        {
          ACE_TRY_EX (inside)
            {
              this->imr_->activate_server (server_list[i].server.in () ACE_ENV_ARG_PARAMETER);
              ACE_TRY_CHECK_EX (inside);
            }
          ACE_CATCHANY
            {
              ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, server_list[i].server.in ());
              // Ignore exception
            }
          ACE_ENDTRY;
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "autostart");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_IOR::run (void)
{
  ACE_ASSERT (! CORBA::is_nil(imr_));

  // Create a corbaloc string
  // Todo : Most of this logic duplicates that in the POA.cpp
  ACE_TRY_NEW_ENV
    {
      if (CORBA::is_nil (this->imr_)
          || !this->imr_->_stubobj ()
          || !this->imr_->_stubobj ()->profile_in_use ())
        {
          ACE_ERROR_RETURN ((
                             LM_ERROR,
                             ACE_TEXT ("Invalid ImR IOR.\n")
                             ), -1);
        }

      CORBA::String_var imr_str =
        this->imr_->_stubobj ()->
        profile_in_use ()->to_string (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Search for "corbaloc:" alone, without the protocol.  This code
      // should be protocol neutral.
      const char corbaloc[] = "corbaloc:";
      char *pos = ACE_OS::strstr (imr_str.inout (), corbaloc);

      if (pos == 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR, "Could not parse IMR IOR.\n"), -1);
        }
      else
        {
          pos = ACE_OS::strchr (pos + sizeof (corbaloc), ':');
          pos = ACE_OS::strchr (pos + 1,
                                this->imr_->_stubobj ()->profile_in_use ()->object_key_delimiter ());

          if (pos)
            {
              *(pos + 1) = 0;  // Crop the string
            }
          else
            {
              ACE_ERROR_RETURN ((LM_ERROR, "Could not parse IMR IOR.\n"), -1);
            }
        }
      ACE_CString ior (imr_str.in ());

      // Add the key
      ior += this->server_name_;

      ACE_DEBUG ((LM_DEBUG, "%s\n", ior.c_str ()));

      if (this->filename_.length () > 0)
        {
          FILE *file = ACE_OS::fopen (this->filename_.c_str (), ACE_TEXT("w"));

          if (file == 0)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "Error: Unable to open %s for writing: %p\n",
                                 this->filename_.c_str ()),
                                -1);
            }

          ACE_OS::fprintf (file, "%s", ior.c_str ());
          ACE_OS::fclose (file);
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "IOR");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_List::run (void)
{
  ACE_ASSERT (! CORBA::is_nil(imr_));

  ImplementationRepository::ServerInformationList_var server_list;
  ImplementationRepository::ServerInformationIterator_var server_iter;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // If there is a server name, list only that server.  Otherwise, look
      // at all of them.
      if (this->server_name_.length () == 0)
        {
          this->imr_->list (0,
                            server_list.out(),
                            server_iter.out()
                            ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          if (server_list->length() == 0)
            {
              ACE_DEBUG((LM_DEBUG, "No servers found.\n"));
              return TAO_IMR_Op::NORMAL;
            }

          for (CORBA::ULong i = 0; i < server_list->length (); i++)
            this->display_server_information (server_list[i]);

          ACE_ASSERT (CORBA::is_nil (server_iter.in ()));
        }
      else
        {
          ImplementationRepository::ServerInformation_var si;

          this->imr_->find (this->server_name_.c_str (), si ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          this->verbose_server_information_ = 1;

          this->display_server_information (si.in ());
        }
    }
  ACE_CATCH (ImplementationRepository::NotFound, ex)
    {
      ACE_ERROR ((LM_ERROR, "Could not find server <%s>.\n", this->server_name_.c_str ()));
      return TAO_IMR_Op::NOT_FOUND;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "List");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_Remove::run (void)
{
  ACE_ASSERT (! CORBA::is_nil(imr_));

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->imr_->remove_server (this->server_name_.c_str () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG, "Successfully removed server <%s>\n",
                  this->server_name_.c_str ()));
    }
  ACE_CATCH (ImplementationRepository::NotFound, ex)
    {
      ACE_ERROR ((LM_ERROR, "Could not find server <%s>.\n",
                  this->server_name_.c_str ()));
      return TAO_IMR_Op::NOT_FOUND;
    }
  ACE_CATCH (CORBA::NO_PERMISSION, ex)
    {
      ACE_ERROR ((LM_ERROR, "No Permission: ImplRepo is in Locked mode\n"));
      return TAO_IMR_Op::NO_PERMISSION;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Removing Server");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_Shutdown::run (void)
{
  ACE_ASSERT (! CORBA::is_nil(imr_));

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->imr_->shutdown_server (this->server_name_.c_str () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG, "Successfully shut down server <%s>\n",
                  this->server_name_.c_str ()));
    }
  ACE_CATCH (ImplementationRepository::NotFound, ex)
    {
      ACE_ERROR ((LM_ERROR, "Server <%s> already shut down.\n", this->server_name_.c_str ()));
      return TAO_IMR_Op::NOT_FOUND;
    }
  ACE_CATCH(CORBA::TIMEOUT, ex)
    {
      ACE_DEBUG ((LM_DEBUG, "Timeout waiting for <%s> to shutdown.\n",
                  this->server_name_.c_str ()));
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Shutting Down Server");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_ShutdownRepo::run (void)
{
  ACE_ASSERT(! CORBA::is_nil(imr_));

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      bool servers = false; // not implemented yet, if ever
      this->imr_->shutdown (activators_, servers ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG, "ImR shutdown initiated.\n"));
    }
  ACE_CATCH(CORBA::TIMEOUT, ex)
    {
      ACE_DEBUG ((LM_DEBUG, "Timeout waiting for ImR shutdown.\n"));
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Shutting Down ImR");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

int
TAO_IMR_Op_Register::run (void)
{
  ACE_ASSERT (! CORBA::is_nil(imr_));

  ImplementationRepository::ServerInformation_var server_information;
  ImplementationRepository::StartupOptions  local;
  ImplementationRepository::StartupOptions* options = NULL;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->imr_->find(this->server_name_.c_str (),
                       server_information.out() ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (server_name_ == server_information->server.in())
        {
          if (is_add_)
            {
              ACE_DEBUG((LM_DEBUG, "Server <%s> already registered.\n", this->server_name_.c_str ()));
              return ALREADY_REGISTERED;
            }
          options = &server_information->startup;
        }
      else // not found
        {
          if (!is_add_)
            {
              ACE_DEBUG((LM_DEBUG, "Adding Server <%s> on update command.\n", this->server_name_.c_str ()));
              is_add_ = true;
            }
          options = &local;
        }

      if (this->set_command_line_)
        options->command_line = CORBA::string_dup (this->command_line_.c_str ());

      if (this->set_environment_vars_)
        options->environment = this->environment_vars_;

      if (this->set_working_dir_)
        options->working_directory = CORBA::string_dup (this->working_dir_.c_str ());

      if (this->set_activation_ || is_add_)
        options->activation = this->activation_;

      if (this->set_retry_count_ || is_add_)
        options->start_limit = this->retry_count_ + 1;

      if (this->set_activator_)
        options->activator = CORBA::string_dup(this->activator_.c_str ());
      // If the command line is set, we must have an activator
      else if (this->set_command_line_ &&
               (options->activator.in () == 0 || *options->activator.in () == 0))
        {
          char host_name[MAXHOSTNAMELEN + 1];
          ACE_OS::hostname (host_name, MAXHOSTNAMELEN);
          options->activator = CORBA::string_dup (host_name);
          ACE_DEBUG ((LM_DEBUG, "Updating Server <%s> with default activator of <%s>.\n",
                      this->server_name_.c_str (), options->activator.in ()));
        }

      this->imr_->add_or_update_server (this->server_name_.c_str (), *options ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_DEBUG((LM_DEBUG, "Successfully registered <%s>.\n", this->server_name_.c_str ()));
    }
  ACE_CATCH (CORBA::NO_PERMISSION, ex)
    {
      ACE_ERROR ((LM_ERROR, "No Permission: ImplRepo is in Locked mode\n"));
      return TAO_IMR_Op::NO_PERMISSION;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Updating server");
      return TAO_IMR_Op::UNKNOWN;
    }
  ACE_ENDTRY;

  return TAO_IMR_Op::NORMAL;
}

// ============================================================================
// = Display Server Information methods

void
TAO_IMR_Op_List::display_server_information (const ImplementationRepository::ServerInformation &info)
{
  if (this->verbose_server_information_)
    TAO_IMR_Op::display_server_information (info);
  else
    ACE_DEBUG ((LM_DEBUG, "<%s>\n", info.server.in ()));
}