Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / ImplRepo_Service / tao_imr_i.cpp
blob2a55ef99a9a1d579b9ff849624bd76a2d9441849
1 #include "orbsvcs/Log_Macros.h"
2 #include "tao_imr_i.h"
4 #include "tao/PortableServer/PortableServer.h"
5 #include "tao/PortableServer/ForwardRequestC.h"
7 #include "tao/Stub.h"
8 #include "tao/Profile.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/Read_Buffer.h"
11 #include "ace/OS_NS_strings.h"
12 #include "ace/os_include/os_netdb.h"
13 #include "ace/OS_NS_unistd.h"
15 TAO_IMR_i::TAO_IMR_i ()
16 : argc_ (0),
17 argv_ (0),
18 orb_ (),
19 imr_ (ImplementationRepository::Administration::_nil ()),
20 op_ ()
22 // Nothing
25 TAO_IMR_i::~TAO_IMR_i ()
29 int
30 TAO_IMR_i::run ()
32 if (this->op_.get () == 0)
34 ORBSVCS_ERROR ((LM_ERROR, "Unknown operation"));
35 return TAO_IMR_Op::UNKNOWN;
38 return this->op_->run ();
41 int
42 TAO_IMR_i::init (int argc, ACE_TCHAR **argv)
44 this->argc_ = argc;
45 this->argv_ = argv;
47 const char *exception_message = "Null Message";
49 try
51 // Retrieve the ORB.
52 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_, "tao_imr_i");
54 // Parse command line and verify parameters.
55 if (this->parse_args () == -1)
56 return -1;
58 // Get the ImplRepo object
59 CORBA::Object_var obj =
60 orb_->resolve_initial_references ("ImplRepoService");
62 if (CORBA::is_nil (obj.in ()))
64 ORBSVCS_ERROR ((LM_ERROR, "Unable to resolve the ImR.\n"));
65 return -1;
68 exception_message = "While narrowing ImR";
70 this->imr_ =
71 ImplementationRepository::Administration::_narrow (obj.in ());
73 if (CORBA::is_nil (imr_.in ()))
75 ORBSVCS_ERROR ((LM_ERROR, "Unable to narrow the ImR.\n"));
76 return -1;
79 this->op_->set_imr (this->imr_.in ());
81 catch (const CORBA::Exception& ex)
83 ORBSVCS_ERROR ((LM_ERROR, "TAO_IMR_i::init - %C\n", exception_message));
84 ex._tao_print_exception ("Exception");
85 return -1;
88 return 0;
92 // Go through and figure out which operation we should do.
94 int
95 TAO_IMR_i::parse_args ()
97 // Make sure one command was given
98 if (this->argc_ < 2)
100 ORBSVCS_ERROR((LM_ERROR, "Error: No operation specified.\n"));
101 this->print_usage ();
102 return -1;
105 this->op_.reset (TAO_IMR_Op::make_op (this->argv_[1]));
107 // Check for unrecognized operation
109 if (this->op_.get () == 0)
111 ORBSVCS_ERROR((LM_ERROR, "Error: Unknown operation '%s'.\n", this->argv_[1]));
112 this->print_usage ();
113 return -1;
116 // Adjust argc and argv so only the command specific args are passed
117 return this->op_->parse (this->argc_ - 1, this->argv_ + 1);
121 // Print out information about all operations.
123 void
124 TAO_IMR_i::print_usage ()
126 ORBSVCS_ERROR ((LM_ERROR, "Usage: tao_imr [options] command [command-arguments]\n"
127 " where [options] are ORB options\n"
128 " where command is one of the following:\n"
129 " start Start a server through the ImR\n"
130 " add Add an entry to the ImR\n"
131 " autostart Activates all AUTO_START servers\n"
132 " ior Creates a simplified IOR\n"
133 " list List the entries in the ImR\n"
134 " remove Remove an entry from the ImR\n"
135 " shutdown Shut down a server through the ImR\n"
136 " shutdown-repo Shut down the ImR\n"
137 " update Update an entry in the ImR\n"
138 " where [command-arguments] depend on the command\n"));
142 // Factory for operations
144 TAO_IMR_Op *
145 TAO_IMR_Op::make_op (const ACE_TCHAR *op_name)
147 if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("activate")) == 0)
149 ORBSVCS_ERROR((LM_ERROR, "Warning: The activate option has been renamed to start.\n"));
150 return new TAO_IMR_Op_Activate ();
152 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("start")) == 0)
153 return new TAO_IMR_Op_Activate ();
154 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("add")) == 0)
155 return new TAO_IMR_Op_Register (true);
156 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("autostart")) == 0)
157 return new TAO_IMR_Op_Autostart();
158 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("ior")) == 0)
159 return new TAO_IMR_Op_IOR();
160 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("kill")) == 0)
161 return new TAO_IMR_Op_Kill ();
162 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("link")) == 0)
163 return new TAO_IMR_Op_Link ();
164 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("list")) == 0)
165 return new TAO_IMR_Op_List ();
166 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("remove")) == 0)
167 return new TAO_IMR_Op_Remove ();
168 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("shutdown")) == 0)
169 return new TAO_IMR_Op_Shutdown ();
170 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("shutdown-repo")) == 0)
171 return new TAO_IMR_Op_ShutdownRepo ();
172 else if (ACE_OS::strcasecmp (op_name, ACE_TEXT ("update")) == 0)
173 return new TAO_IMR_Op_Register (false);
175 return 0;
179 TAO_IMR_Op::~TAO_IMR_Op ()
181 // Nothing
184 void
185 TAO_IMR_Op::set_imr (ImplementationRepository::Administration_ptr imr)
187 this->imr_ = imr;
190 void
191 TAO_IMR_Op::display_server_information (const ImplementationRepository::ServerInformation &info)
193 // Figure out what the activation string is.
194 const char *act = "UNKNOWN STARTUP";
195 if (info.startup.activation == ImplementationRepository::NORMAL)
196 act = "NORMAL";
197 else if (info.startup.activation == ImplementationRepository::MANUAL)
198 act = "MANUAL";
199 else if (info.startup.activation == ImplementationRepository::PER_CLIENT)
200 act = "PER_CLIENT";
201 else if (info.startup.activation == ImplementationRepository::AUTO_START)
202 act = "AUTO_START";
204 // Print out information
205 ORBSVCS_DEBUG ((LM_DEBUG, "Server <%C>\n", info.server.in ()));
207 const char * locked_out = "";
209 int limit = info.startup.start_limit;
210 if (info.startup.start_limit < 0)
212 limit = -limit;
213 locked_out = " Locked Out\n";
216 ORBSVCS_DEBUG ((LM_DEBUG, " Activator: %C\n", info.startup.activator.in ()));
218 ORBSVCS_DEBUG ((LM_DEBUG, " Command Line: "));
219 if (ACE_OS::strlen(info.startup.command_line.in ()) <= ACE_MAXLOGMSGLEN)
221 ORBSVCS_DEBUG ((LM_DEBUG, "%C", info.startup.command_line.in ()));
223 else
225 char *cl = const_cast<char *>(info.startup.command_line.in ());
226 while (*cl)
228 char tmp = 0;
229 size_t len = ACE_OS::strlen(cl);
230 if (len > ACE_MAXLOGMSGLEN)
232 len = ACE_MAXLOGMSGLEN;
233 tmp = cl[len+1];
234 cl[len+1] = 0;
236 ORBSVCS_DEBUG ((LM_DEBUG, "%C", cl));
237 cl[len+1] = tmp;
238 cl += len;
241 ORBSVCS_DEBUG ((LM_DEBUG, "\n"));
242 ORBSVCS_DEBUG ((LM_DEBUG, " Working Directory: %s\n", info.startup.working_directory.in ()));
243 ORBSVCS_DEBUG ((LM_DEBUG, " Activation Mode: %C\n", act));
244 ORBSVCS_DEBUG ((LM_DEBUG, " Number of retries: %d\n%C", limit - 1, locked_out));
247 for (CORBA::ULong i = 0; i < info.startup.environment.length (); ++i)
248 ORBSVCS_DEBUG ((LM_DEBUG, "Environment Variable: %C=%C\n",
249 info.startup.environment[i].name.in (),
250 info.startup.environment[i].value.in ()));
252 if (info.startup.activation == ImplementationRepository::PER_CLIENT)
253 ORBSVCS_DEBUG ((LM_DEBUG,
254 " No running info available for PER_CLIENT mode\n"));
255 else if (ACE_OS::strlen (info.partial_ior.in ()) > 0)
256 ORBSVCS_DEBUG ((LM_DEBUG,
257 " Running at endpoint: %C\n",
258 info.partial_ior.in ()));
259 else // I am assuming that a blank partial_ior means currently not running.
260 ORBSVCS_DEBUG ((LM_DEBUG,
261 " Not currently running\n"));
263 ORBSVCS_DEBUG ((LM_DEBUG, "\n"));
266 TAO_IMR_Op_List::TAO_IMR_Op_List ()
267 : verbose_server_information_ (0)
268 , list_only_active_servers_ (0)
269 , how_many_ (0)
270 , terse_ (0)
272 // Nothing
275 TAO_IMR_Op_Register::TAO_IMR_Op_Register (bool is_add)
276 : is_add_ (is_add)
277 , set_command_line_ (false)
278 , set_environment_vars_(false)
279 , set_working_dir_ (false)
280 , set_activation_ (false)
281 , activation_(ImplementationRepository::NORMAL)
282 , set_retry_count_(false)
283 , retry_count_ (0)
284 , set_activator_ (false)
286 // Nothing
289 void
290 TAO_IMR_Op_Activate::print_usage ()
292 ORBSVCS_ERROR ((LM_ERROR,
293 "Starts a server using its registered Activator.\n\n"
294 "Usage: tao_imr [options] start <name>\n"
295 " where [options] are ORB options\n"
296 " where <name> is the name of a registered POA.\n"
297 " -q run quietly\n"
298 " -h Displays this\n"));
302 TAO_IMR_Op_Activate::parse (int argc, ACE_TCHAR **argv)
304 // Check for enough arguments (we need at least one for the server name)
305 if (argc < 2)
307 this->print_usage ();
308 return -1;
311 // Skip both the program name and the "activate" command
312 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("hq"));
314 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
315 int c;
317 while ((c = get_opts ()) != -1)
319 switch (c)
321 case 'q' :
322 this->quiet_ = true;
323 break;
324 case 'h':
325 this->print_usage ();
326 return -1;
327 default:
328 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
329 this->print_usage ();
330 return -1;
333 return 0;
336 void
337 TAO_IMR_Op_Autostart::print_usage ()
339 ORBSVCS_ERROR ((LM_ERROR, "Usage: tao_imr [options] autostart\n"
340 " where [options] are ORB options\n"
341 " -h Displays this\n"));
345 TAO_IMR_Op_Autostart::parse (int argc, ACE_TCHAR **argv)
347 // Skip the "autostart" command
348 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("h"));
350 int c;
352 while ((c = get_opts ()) != -1)
354 switch (c)
356 case 'h': // display help
357 this->print_usage ();
358 return -1;
359 default:
360 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
361 this->print_usage ();
362 return -1;
365 return 0;
368 void
369 TAO_IMR_Op_IOR::print_usage ()
371 ORBSVCS_ERROR ((LM_ERROR, "Creates an IOR for a server that is registered with the IMR and uses\n"
372 "the InterOperable Naming Service. Please see the documentation for\n"
373 "more information on which server configurations work with this command.\n"
374 "\n"
375 "Usage: tao_imr [options] ior <object_key> [command-arguments]\n"
376 " where [options] are ORB options\n"
377 " where <object_key> matches the simple key bound in the server IORTable.\n"
378 " where [command-arguments] can be\n"
379 " -f filename filename to output the IOR to\n"
380 " -h Displays this\n"));
384 TAO_IMR_Op_IOR::parse (int argc, ACE_TCHAR **argv)
386 // Check for enough arguments (we need at least one for the server name)
387 if (argc < 2)
389 this->print_usage ();
390 return -1;
393 // Skip both the program name and the "ior" command
394 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("hf:"));
396 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
397 if (this->server_name_.length() == 0 || this->server_name_[0] == '-')
399 ORBSVCS_ERROR((LM_ERROR, "ERROR : name is required.\n"));
400 this->print_usage ();
401 return -1;
404 int c;
406 while ((c = get_opts ()) != -1)
408 switch (c)
410 case 'f': // File name
411 this->filename_ = get_opts.opt_arg ();
412 break;
413 case 'h': // display help
414 this->print_usage ();
415 return -1;
416 default:
417 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
418 this->print_usage ();
419 return -1;
422 return 0;
425 void
426 TAO_IMR_Op_Kill::print_usage ()
428 ORBSVCS_ERROR ((LM_ERROR,
429 "Sends a signal to the designated process\n\n"
430 "The process must have been started by the ImR\n"
431 "and may not use per-client activation\n"
432 "Usage: tao_imr [options] kill [name] [command-arguments]\n"
433 " where [options] are ORB options\n"
434 " where [name] is the registered POA name the peers link to\n"
435 " where [command-arguments] can be\n"
436 " -q run quietly\n"
437 " -s signum default 9, the signal to be sent to the\n"
438 " process for named server\n"));
442 TAO_IMR_Op_Kill::parse (int argc, ACE_TCHAR **argv)
444 int server_flag = 0;
446 if (argc > 1 && argv[1][0] != '-')
448 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
449 server_flag = 2;
451 this->signum_ = 9;
453 // Skip both the program name and the "list" command
454 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("s:hq"), server_flag);
456 int c;
458 while ((c = get_opts ()) != -1)
460 switch (c)
462 case 's': //signum
463 this->signum_ = ACE_OS::strtol (get_opts.opt_arg (), 0, 10);
464 break;
465 case 'q' :
466 this->quiet_ = true;
467 break;
468 case 'h': // display help
469 this->print_usage ();
470 return -1;
471 default:
472 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
473 this->print_usage ();
474 return -1;
477 return 0;
480 void
481 TAO_IMR_Op_Link::print_usage ()
483 ORBSVCS_ERROR ((LM_ERROR, "Links multiple POAs to a single executable\n"
484 "\n"
485 "Usage: tao_imr [options] link [name] [command-arguments]\n"
486 " where [options] are ORB options\n"
487 " where [name] is the registered POA name the peers link to\n"
488 " where [command-arguments] can be\n"
489 " -p peers provides a comma separated list of additional POAs that are\n"
490 " part of the same executable. -p may be repeated\n"));
494 TAO_IMR_Op_Link::parse (int argc, ACE_TCHAR **argv)
496 int server_flag = 0;
498 if (argc > 1 && argv[1][0] != '-')
500 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
501 server_flag = 2;
504 // Skip both the program name and the "list" command
505 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:h"), server_flag);
507 int c;
509 while ((c = get_opts ()) != -1)
511 switch (c)
513 case 'p': //list of peers
515 char *arg = ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ());
516 CORBA::ULong last = this->peers_.length ();
517 CORBA::ULong num = 0;
518 char *c = arg;
519 while (c != 0)
521 num++;
522 c = ACE_OS::strchr (c, ',');
523 if (c != 0)
524 c++;
526 num += last;
527 this->peers_.length (num);
528 while (last < num)
530 if ((c = ACE_OS::strchr (arg, ',')) != 0)
531 *c = 0;
532 this->peers_[last++] = CORBA::string_dup (arg);
533 if (c != 0)
534 arg = c+1;
536 break;
538 case 'h': // display help
539 this->print_usage ();
540 return -1;
541 default:
542 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
543 this->print_usage ();
544 return -1;
547 return 0;
550 void
551 TAO_IMR_Op_List::print_usage ()
553 ORBSVCS_ERROR ((LM_ERROR, "Lists all or one of the servers in the Implementation Repository\n"
554 "\n"
555 "Usage: tao_imr [options] list [name] [command-arguments]\n"
556 " where [options] are ORB options\n"
557 " where [name] is the optional server name to search for\n"
558 " where [command-arguments] can be\n"
559 " -v Verbose: Displays more info for each server when\n"
560 " displaying more than one server\n"
561 " -a List only servers that are determined to be active\n"
562 " -n <count> Request no more than <count> entries and use an iterator\n"
563 " -h Displays this\n"));
567 TAO_IMR_Op_List::parse (int argc, ACE_TCHAR **argv)
569 int server_flag = 0;
571 if (argc > 1 && argv[1][0] != '-')
573 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
574 server_flag = 2;
577 // Skip both the program name and the "list" command
578 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("ahn:tv"), server_flag);
580 int c;
582 while ((c = get_opts ()) != -1)
584 switch (c)
586 case 'v': // verbose server display
587 this->verbose_server_information_ = 1;
588 break;
589 case 't':
590 this->terse_ = 1;
591 break;
592 case 'a':
593 this->list_only_active_servers_ = 1;
594 break;
595 case 'n':
596 this->how_many_ = ACE_OS::atoi (get_opts.opt_arg ());
597 break;
598 case 'h': // display help
599 this->print_usage ();
600 return -1;
601 default:
602 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
603 this->print_usage ();
604 return -1;
607 return 0;
610 void
611 TAO_IMR_Op_Remove::print_usage ()
613 ORBSVCS_ERROR ((LM_ERROR, "Removes a server entry\n"
614 "\n"
615 "Usage: tao_imr [options] remove <name> [-f [-s <signum>]]\n"
616 " where [options] are ORB options\n"
617 " -f forces shutdown or kill of a running server"
618 " -s specifies a signal for killing the server, if it is 0, a shutdown will be used"
619 " where <name> is the POA name used by the server object\n"
620 " -h Displays this\n"));
624 TAO_IMR_Op_Remove::parse (int argc, ACE_TCHAR **argv)
626 // Check for enough arguments (we need at least one for the server name)
627 if (argc < 2)
629 this->print_usage ();
630 return -1;
632 this->force_ = false;
633 this->signum_ = 0;
635 // Skip both the program name and the "remove" command
636 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("hfs:"));
638 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
639 int c;
641 while ((c = get_opts ()) != -1)
643 switch (c)
645 case 'h':
646 this->print_usage ();
647 return -1;
648 case 'f':
649 this->force_ = true;
650 break;
651 case 's':
652 this->signum_ = ACE_OS::strtol (get_opts.opt_arg (), 0, 10);
653 break;
654 default:
655 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
656 this->print_usage ();
657 return -1;
661 return 0;
664 void
665 TAO_IMR_Op_Shutdown::print_usage ()
667 ORBSVCS_ERROR ((LM_ERROR,
668 "Shuts down a server\n\n"
669 "Usage: tao_imr [options] shutdown <name>\n"
670 " where [options] are ORB options\n"
671 " where <name> is the name of the server object\n"
672 " -q run quietly\n"
673 " -h Displays this\n"));
677 TAO_IMR_Op_Shutdown::parse (int argc, ACE_TCHAR **argv)
679 // Check for enough arguments (we need at least one for the server name)
680 if (argc < 2)
682 this->print_usage ();
683 return -1;
686 // Skip both the program name and the "shutdown" command
687 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("hq"));
689 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
690 int c;
692 while ((c = get_opts ()) != -1)
694 switch (c)
696 case 'q' :
697 this->quiet_ = true;
698 break;
699 case 'h':
700 this->print_usage ();
701 return -1;
702 default:
703 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
704 this->print_usage ();
705 return -1;
708 return 0;
711 TAO_IMR_Op_ShutdownRepo::TAO_IMR_Op_ShutdownRepo()
712 : activators_(false)
716 void
717 TAO_IMR_Op_ShutdownRepo::print_usage ()
719 ORBSVCS_ERROR ((LM_ERROR, "Shuts down the ImR\n"
720 "\n"
721 "Usage: tao_imr [options] shutdown-repo [-a]\n"
722 " where [options] are ORB options\n"
723 " Specify -a to also shutdown any registered ImR Activators.\n"
724 " -h Displays this\n"));
728 TAO_IMR_Op_ShutdownRepo::parse (int argc, ACE_TCHAR **argv)
730 // Check for enough arguments (we need at least one for the server name)
731 if (argc < 1)
733 this->print_usage ();
734 return -1;
737 // Skip both the program name and the "shutdown-repo" command
738 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("ha"));
740 int c;
742 while ((c = get_opts ()) != -1)
744 switch (c)
746 case 'h':
747 this->print_usage ();
748 return -1;
749 case 'a':
750 activators_ = true;
751 break;
752 default:
753 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
754 this->print_usage ();
755 return -1;
759 return 0;
762 void
763 TAO_IMR_Op_Register::addenv (ACE_TCHAR *opt)
765 CORBA::ULong length = this->environment_vars_.length ();
766 // Increase the length of the sequence
767 this->environment_vars_.length (length + 1);
768 ACE_TString tokens (opt);
769 size_t index = tokens.find (ACE_TEXT("="));
770 // Insert at position length since that is our new element
771 this->environment_vars_ [length].name =
772 CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(tokens.substr (0, index).c_str ()));
773 this->environment_vars_ [length].value =
774 CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(tokens.substr (index + 1).c_str ()));
777 void
778 TAO_IMR_Op_Register::print_usage ()
780 ORBSVCS_ERROR ((LM_ERROR,
781 "Adds/Updates a server entry\n\n"
782 "Usage: tao_imr [options] <add|update> <name> [command-arguments]\n"
783 " where [options] are ORB options\n"
784 " where <name> is the POA name used by the server object\n"
785 " where [command-arguments] can be\n"
786 " -h Displays this\n"
787 " -q run quietly\n"
788 " -l Activator name.\n"
789 " -c command Startup command\n"
790 " -w dir Working directory\n"
791 " -e name=value Set environment variables\n"
792 " -a mode Set activate mode (NORMAL|MANUAL|PER_CLIENT|AUTO_START)\n"
793 " -r count Set the startup/ping retry count to count\n"));
797 TAO_IMR_Op_Register::parse (int argc, ACE_TCHAR **argv)
799 // Check for enough arguments (we need at least one for the server name)
800 if (argc < 2)
802 ORBSVCS_ERROR((LM_ERROR, "Error: Must supply at least a server name.\n"));
803 this->print_usage ();
804 return -1;
807 // Skip both the program name and the "update" command
808 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("hqc:w:a:e:r:R:l:"));
810 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(argv[1]);
811 int c;
813 while ((c = get_opts ()) != -1)
815 switch (c)
817 case 'c': // Command line arguments
818 this->set_command_line_ = true;
819 this->command_line_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
820 break;
821 case 'e': // set environment variables
822 this->set_environment_vars_ = true;
823 this->addenv( get_opts.opt_arg () );
824 break;
825 case 'w': // Working Directory
826 this->set_working_dir_ = true;
827 this->working_dir_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
828 break;
829 case 'a': // Activation Mode
830 this->set_activation_ = true;
831 if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("NORMAL")) == 0)
832 this->activation_ = ImplementationRepository::NORMAL;
833 else if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("MANUAL")) == 0)
834 this->activation_ = ImplementationRepository::MANUAL;
835 else if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("PER_CLIENT")) == 0)
836 this->activation_ = ImplementationRepository::PER_CLIENT;
837 else if (ACE_OS::strcasecmp (get_opts.opt_arg (), ACE_TEXT("AUTO_START")) == 0)
838 this->activation_ = ImplementationRepository::AUTO_START;
839 else
840 ORBSVCS_ERROR_RETURN ((LM_ERROR,
841 "Unknown Activation Mode <%s>.\n",
842 get_opts.opt_arg ()),
843 -1);
844 break;
845 case 'r':
846 case 'R': // startup/ping Retry Count
848 this->set_retry_count_ = true;
849 int rc = ACE_OS::atoi(get_opts.optarg);
850 if (rc > 0)
851 this->retry_count_ = rc;
853 break;
854 case 'l': /// hostname of the activator
855 this->activator_ = ACE_TEXT_ALWAYS_CHAR(get_opts.optarg);
856 this->set_activator_ = true;
857 break;
858 case 'q' :
859 this->quiet_ = true;
860 break;
861 case 'h': // display help
862 this->print_usage ();
863 return -1;
864 default:
865 ORBSVCS_ERROR((LM_ERROR, "ERROR : Unknown option '%c'\n", (char) c));
866 this->print_usage ();
867 return -1;
870 return 0;
874 // ============================================================================
875 // = Run methods
879 TAO_IMR_Op_Activate::run ()
881 ACE_ASSERT(! CORBA::is_nil(imr_));
884 this->imr_->activate_server (this->server_name_.c_str ());
885 if (!this->quiet_)
887 ORBSVCS_DEBUG ((LM_DEBUG,
888 "Successfully Activated server <%C>\n",
889 this->server_name_.c_str ()));
892 catch (const ImplementationRepository::CannotActivate& ex)
894 if (!this->quiet_)
896 ORBSVCS_ERROR ((LM_ERROR,
897 "Cannot activate server <%C> reason <%C>\n",
898 this->server_name_.c_str (),
899 ex.reason.in ()));
901 return TAO_IMR_Op::CANNOT_ACTIVATE;
903 catch (const ImplementationRepository::NotFound&)
905 if (!this->quiet_)
907 ORBSVCS_ERROR ((LM_ERROR,
908 "Could not find server <%C>.\n",
909 this->server_name_.c_str ()));
911 return TAO_IMR_Op::NOT_FOUND;
913 catch (const PortableServer::ForwardRequest&)
915 throw;
917 catch (const CORBA::Exception& ex)
919 if (!this->quiet_)
921 ex._tao_print_exception ("Activating Server");
923 return TAO_IMR_Op::UNKNOWN;
926 return TAO_IMR_Op::NORMAL;
930 TAO_IMR_Op_Autostart::run ()
932 ACE_ASSERT(! CORBA::is_nil (imr_));
934 ImplementationRepository::ServerInformationList_var server_list;
935 ImplementationRepository::ServerInformationIterator_var server_iter;
939 this->imr_->list (0,
940 false,
941 server_list,
942 server_iter);
944 ACE_ASSERT(CORBA::is_nil (server_iter.in ()));
946 CORBA::ULong len = server_list->length ();
947 for (CORBA::ULong i = 0; i < len; ++i)
951 this->imr_->activate_server (server_list[i].server.in ());
953 catch (const CORBA::Exception& ex)
955 ex._tao_print_exception (server_list[i].server.in ());
956 // Ignore exception
960 catch (const CORBA::Exception& ex)
962 ex._tao_print_exception ("autostart");
963 return TAO_IMR_Op::UNKNOWN;
966 return TAO_IMR_Op::NORMAL;
970 TAO_IMR_Op_IOR::run ()
972 ACE_ASSERT (! CORBA::is_nil(imr_));
974 // Create a corbaloc string
975 // Todo : Most of this logic duplicates that in the POA.cpp
978 if (CORBA::is_nil (this->imr_)
979 || !this->imr_->_stubobj ()
980 || !this->imr_->_stubobj ()->profile_in_use ())
982 ORBSVCS_ERROR_RETURN ((
983 LM_ERROR,
984 ACE_TEXT ("Invalid ImR IOR.\n")
985 ), -1);
988 CORBA::String_var imr_str =
989 this->imr_->_stubobj ()->
990 profile_in_use ()->to_string ();
992 // Search for "corbaloc:" alone, without the protocol. This code
993 // should be protocol neutral.
994 const char corbaloc[] = "corbaloc:";
995 char *pos = ACE_OS::strstr (imr_str.inout (), corbaloc);
997 if (pos == 0)
999 ORBSVCS_ERROR_RETURN ((LM_ERROR, "Could not parse IMR IOR.\n"), -1);
1001 else
1003 pos = ACE_OS::strchr (pos + sizeof (corbaloc), ':');
1004 pos = ACE_OS::strchr (pos + 1,
1005 this->imr_->_stubobj ()->profile_in_use ()->object_key_delimiter ());
1007 if (pos)
1009 *(pos + 1) = 0; // Crop the string
1011 else
1013 ORBSVCS_ERROR_RETURN ((LM_ERROR, "Could not parse IMR IOR.\n"), -1);
1016 ACE_CString ior (imr_str.in ());
1018 // Add the key
1019 const char jacorb[] = "JACORB:";
1020 const char *posjacorb = ACE_OS::strstr (server_name_.c_str (), jacorb);
1021 if (posjacorb)
1023 ior += posjacorb + sizeof(jacorb) - 1;
1025 else
1027 ior += this->server_name_;
1030 ORBSVCS_DEBUG ((LM_DEBUG, ACE_TEXT ("%C\n"), ior.c_str ()));
1032 if (this->filename_.length () > 0)
1034 FILE *file = ACE_OS::fopen (this->filename_.c_str (), "w");
1036 if (file == 0)
1038 ORBSVCS_ERROR_RETURN ((LM_ERROR,
1039 ACE_TEXT ("Error: Unable to open %s for writing: %p\n"),
1040 this->filename_.c_str ()),
1041 -1);
1044 ACE_OS::fprintf (file, "%s", ior.c_str ());
1045 ACE_OS::fclose (file);
1048 catch (const CORBA::Exception& ex)
1050 ex._tao_print_exception ("IOR");
1051 return TAO_IMR_Op::UNKNOWN;
1054 return TAO_IMR_Op::NORMAL;
1058 TAO_IMR_Op_Kill::run ()
1060 ACE_ASSERT (! CORBA::is_nil(imr_));
1062 ImplementationRepository::AdministrationExt_var imrext =
1063 ImplementationRepository::AdministrationExt::_narrow (imr_);
1064 if (CORBA::is_nil (imrext.in()))
1066 ORBSVCS_ERROR_RETURN
1067 ((LM_ERROR,
1068 ACE_TEXT ("Error: requested IMR does not support the kill operation\n")),
1069 -1);
1074 imrext->kill_server (this->server_name_.c_str(), this->signum_);
1076 catch (const ImplementationRepository::NotFound &)
1078 if (!this->quiet_)
1080 ORBSVCS_ERROR ((LM_ERROR,
1081 "Could not find server <%C>.\n",
1082 this->server_name_.c_str ()));
1084 return TAO_IMR_Op::NOT_FOUND;
1086 catch (const ImplementationRepository::CannotComplete& ex)
1088 if (!this->quiet_)
1090 ORBSVCS_ERROR ((LM_ERROR,
1091 "Cannot complete kill of <%C> reason <%C>\n",
1092 this->server_name_.c_str (),
1093 ex.reason.in ()));
1095 return TAO_IMR_Op::CANNOT_COMPLETE;
1097 catch (const CORBA::Exception& ex)
1099 ex._tao_print_exception ("Kill");
1100 return TAO_IMR_Op::UNKNOWN;
1103 return TAO_IMR_Op::NORMAL;
1107 TAO_IMR_Op_Link::run ()
1109 ACE_ASSERT (! CORBA::is_nil(imr_));
1111 ImplementationRepository::AdministrationExt_var imrext =
1112 ImplementationRepository::AdministrationExt::_narrow (imr_);
1113 if (CORBA::is_nil (imrext.in()))
1115 ORBSVCS_ERROR_RETURN
1116 ((LM_ERROR,
1117 ACE_TEXT ("Error: requested IMR does not support the kill operation\n")),
1118 -1);
1123 imrext->link_servers (this->server_name_.c_str(), this->peers_);
1125 catch (const ImplementationRepository::NotFound &)
1127 ORBSVCS_ERROR ((LM_ERROR,
1128 "Could not find server <%C>.\n",
1129 this->server_name_.c_str ()));
1130 return TAO_IMR_Op::NOT_FOUND;
1132 catch (const ImplementationRepository::CannotComplete& ex)
1134 ORBSVCS_ERROR ((LM_ERROR,
1135 "Cannot complete kill of <%C> reason <%C>\n",
1136 this->server_name_.c_str (),
1137 ex.reason.in ()));
1138 return TAO_IMR_Op::CANNOT_COMPLETE;
1140 catch (const CORBA::Exception& ex)
1142 ex._tao_print_exception ("Kill");
1143 return TAO_IMR_Op::UNKNOWN;
1146 return TAO_IMR_Op::NORMAL;
1151 TAO_IMR_Op_List::run ()
1153 ACE_ASSERT (! CORBA::is_nil(imr_));
1155 ImplementationRepository::ServerInformationList_var server_list;
1156 ImplementationRepository::ServerInformationIterator_var server_iter;
1160 // If there is a server name, list only that server. Otherwise, look
1161 // at all of them.
1162 if (this->server_name_.length () == 0)
1164 this->imr_->list (this->how_many_,
1165 this->list_only_active_servers_,
1166 server_list.out(),
1167 server_iter.out());
1169 if (server_list->length() == 0)
1171 ORBSVCS_DEBUG((LM_DEBUG, "No servers found.\n"));
1172 return TAO_IMR_Op::NORMAL;
1175 for (CORBA::ULong i = 0; i < server_list->length (); i++)
1176 this->display_server_information (server_list[i]);
1178 if (!CORBA::is_nil (server_iter.in ()))
1180 ORBSVCS_DEBUG ((LM_DEBUG, "An iterator was returned.\n"));
1181 for (bool done = false; !done; )
1183 done = server_iter->next_n (this->how_many_, server_list.out());
1184 for (CORBA::ULong i = 0; i < server_list->length (); i++)
1185 this->display_server_information (server_list[i]);
1187 server_iter->destroy ();
1190 else
1192 ImplementationRepository::ServerInformation_var si;
1194 this->imr_->find (this->server_name_.c_str (), si);
1195 if (ACE_OS::strlen (si->server.in()) > 0)
1197 this->verbose_server_information_ = 1;
1198 this->display_server_information (si.in ());
1200 else
1202 throw ImplementationRepository::NotFound ();
1206 catch (const ImplementationRepository::NotFound&)
1208 ORBSVCS_ERROR ((LM_ERROR,
1209 "Could not find server <%C>.\n",
1210 this->server_name_.c_str ()));
1211 return TAO_IMR_Op::NOT_FOUND;
1213 catch (const CORBA::Exception& ex)
1215 ex._tao_print_exception ("List");
1216 return TAO_IMR_Op::UNKNOWN;
1219 return TAO_IMR_Op::NORMAL;
1223 TAO_IMR_Op_Remove::run ()
1225 ACE_ASSERT (! CORBA::is_nil(imr_));
1229 if (this->force_)
1231 ImplementationRepository::AdministrationExt_var ext =
1232 ImplementationRepository::AdministrationExt::_narrow (imr_);
1233 ACE_ASSERT (! CORBA::is_nil(ext));
1234 ext->force_remove_server (this->server_name_.c_str (),
1235 this->signum_);
1237 else
1239 this->imr_->remove_server (this->server_name_.c_str ());
1241 ORBSVCS_DEBUG ((LM_DEBUG, "Successfully removed server <%C>\n",
1242 this->server_name_.c_str ()));
1244 catch (const ImplementationRepository::NotFound&)
1246 ORBSVCS_ERROR ((LM_ERROR, "Could not find server <%C>.\n",
1247 this->server_name_.c_str ()));
1248 return TAO_IMR_Op::NOT_FOUND;
1250 catch (const ImplementationRepository::CannotComplete& cc)
1252 ORBSVCS_ERROR ((LM_ERROR, "Could not complete forced removal of server <%C>. reason: %C\n",
1253 this->server_name_.c_str (), cc.reason.in() ));
1254 return TAO_IMR_Op::CANNOT_COMPLETE;
1256 catch (const CORBA::NO_PERMISSION& np)
1258 if ((np.minor () & 0x7FU) == 0xFU) //TAO_EBUSY_MINOR_CODE)
1260 ORBSVCS_ERROR ((LM_ERROR, "Server <%C> still busy.\n",
1261 this->server_name_.c_str ()));
1262 return TAO_IMR_Op::CANNOT_COMPLETE;
1264 ORBSVCS_ERROR ((LM_ERROR, "No Permission: ImplRepo is in Locked mode\n"));
1265 return TAO_IMR_Op::NO_PERMISSION;
1267 catch (const CORBA::Exception& ex)
1269 ex._tao_print_exception ("Removing Server");
1270 return TAO_IMR_Op::UNKNOWN;
1273 return TAO_IMR_Op::NORMAL;
1277 TAO_IMR_Op_Shutdown::run ()
1279 ACE_ASSERT (! CORBA::is_nil(imr_));
1283 this->imr_->shutdown_server (this->server_name_.c_str ());
1285 if (!this->quiet_)
1287 ORBSVCS_DEBUG ((LM_DEBUG,
1288 "Successfully shut down server <%C>\n",
1289 this->server_name_.c_str ()));
1292 catch (const ImplementationRepository::NotFound&)
1294 if (!this->quiet_)
1296 ORBSVCS_ERROR ((LM_ERROR,
1297 "Server <%C> already shut down.\n",
1298 this->server_name_.c_str ()));
1300 return TAO_IMR_Op::NOT_FOUND;
1302 catch (const CORBA::TIMEOUT&)
1304 if (!this->quiet_)
1306 ORBSVCS_DEBUG ((LM_DEBUG,
1307 "Timeout waiting for <%C> to shutdown.\n",
1308 this->server_name_.c_str ()));
1311 catch (const CORBA::Exception& ex)
1313 ex._tao_print_exception ("Shutting Down Server");
1314 return TAO_IMR_Op::UNKNOWN;
1317 return TAO_IMR_Op::NORMAL;
1321 TAO_IMR_Op_ShutdownRepo::run ()
1323 ACE_ASSERT(! CORBA::is_nil(imr_));
1327 bool servers = false; // not implemented yet, if ever
1328 this->imr_->shutdown (activators_, servers);
1330 ORBSVCS_DEBUG ((LM_DEBUG, "ImR shutdown initiated.\n"));
1332 catch (const CORBA::TIMEOUT&)
1334 ORBSVCS_DEBUG ((LM_DEBUG, "Timeout waiting for ImR shutdown.\n"));
1336 catch (const CORBA::Exception& ex)
1338 ex._tao_print_exception ("Shutting Down ImR");
1339 return TAO_IMR_Op::UNKNOWN;
1342 return TAO_IMR_Op::NORMAL;
1346 TAO_IMR_Op_Register::run ()
1348 ACE_ASSERT (! CORBA::is_nil(imr_));
1350 ImplementationRepository::ServerInformation_var server_information;
1351 ImplementationRepository::StartupOptions local;
1352 ImplementationRepository::StartupOptions* options = 0;
1356 this->imr_->find (this->server_name_.c_str (),
1357 server_information.out ());
1359 if (server_name_ == server_information->server.in())
1361 if (is_add_)
1363 if (!this->quiet_)
1365 ORBSVCS_DEBUG((LM_DEBUG,
1366 "(%P|%t) ImR: Server <%C> already registered.\n",
1367 this->server_name_.c_str ()));
1369 return ALREADY_REGISTERED;
1371 options = &server_information->startup;
1373 else // not found
1375 if (!is_add_)
1377 if (!this->quiet_)
1379 ORBSVCS_DEBUG((LM_DEBUG,
1380 "(%P|%t) Adding Server <%C> on update command.\n",
1381 this->server_name_.c_str ()));
1383 is_add_ = true;
1385 local.activation= ImplementationRepository::NORMAL;
1386 options = &local;
1389 if (this->set_command_line_)
1390 options->command_line = CORBA::string_dup (this->command_line_.c_str ());
1392 if (this->set_environment_vars_)
1393 options->environment = this->environment_vars_;
1395 if (this->set_working_dir_)
1396 options->working_directory = CORBA::string_dup (this->working_dir_.c_str ());
1398 if (this->set_activation_ || is_add_)
1399 options->activation = this->activation_;
1401 if (this->set_retry_count_ || is_add_)
1402 options->start_limit = this->retry_count_ + 1;
1404 if (this->set_activator_)
1405 options->activator = CORBA::string_dup(this->activator_.c_str ());
1406 // If the command line is set, we must have an activator
1407 else if (this->set_command_line_ &&
1408 (options->activator.in () == 0 || *options->activator.in () == 0))
1410 char host_name[MAXHOSTNAMELEN + 1];
1411 ACE_OS::hostname (host_name, MAXHOSTNAMELEN);
1412 options->activator = CORBA::string_dup (host_name);
1413 if (!this->quiet_)
1415 ORBSVCS_DEBUG ((LM_DEBUG,
1416 "(%P|%t) ImR: Updating Server <%C> with default "
1417 "activator of <%C>\n",
1418 this->server_name_.c_str (),
1419 options->activator.in ()));
1422 this->imr_->add_or_update_server (this->server_name_.c_str (), *options);
1424 if (!this->quiet_)
1426 ORBSVCS_DEBUG((LM_DEBUG,
1427 "(%P|%t) ImR: Successfully registered <%C>\n",
1428 this->server_name_.c_str ()));
1431 catch (const CORBA::NO_PERMISSION&)
1433 ORBSVCS_ERROR ((LM_ERROR, "(%P|%t) ImR: No Permission: ImplRepo is in Locked mode\n"));
1434 return TAO_IMR_Op::NO_PERMISSION;
1436 catch (const CORBA::Exception& ex)
1438 ex._tao_print_exception ("Updating server");
1439 return TAO_IMR_Op::UNKNOWN;
1442 return TAO_IMR_Op::NORMAL;
1445 // ============================================================================
1446 // = Display Server Information methods
1448 void
1449 TAO_IMR_Op_List::display_server_information (const ImplementationRepository::ServerInformation &info)
1451 const char *maybe = "";
1452 if (this->list_only_active_servers_)
1454 switch (info.activeStatus)
1456 case ImplementationRepository::ACTIVE_NO:
1457 return;
1458 case ImplementationRepository::ACTIVE_MAYBE:
1459 maybe = "(maybe)";
1460 break;
1461 default:
1462 break;
1466 if (this->verbose_server_information_)
1467 TAO_IMR_Op::display_server_information (info);
1468 else
1470 if (this->terse_)
1471 ORBSVCS_DEBUG ((LM_DEBUG, "%C\n", info.server.in ()));
1472 else
1473 ORBSVCS_DEBUG ((LM_DEBUG, "<%C> %C\n", info.server.in (), maybe));