Activator_Options.cpp

Go to the documentation of this file.
00001 //=============================================================================
00002 /**
00003  *  @file    Activator_Options.cpp
00004  *
00005  *  $Id: Activator_Options.cpp 78894 2007-07-14 14:56:10Z sowayaa $
00006  *
00007  *  @author Darrell Brunsch <brunsch@cs.wustl.edu>
00008  */
00009 //=============================================================================
00010 #include "Activator_Options.h"
00011 
00012 #include "ace/Arg_Shifter.h"
00013 #include "ace/OS_NS_strings.h"
00014 #include "ace/Log_Msg.h"
00015 
00016 ACE_RCSID (ImplRepo_Service,
00017            Activator_Options,
00018            "$Id: Activator_Options.cpp 78894 2007-07-14 14:56:10Z sowayaa $")
00019 
00020 #if defined (ACE_WIN32)
00021 static const HKEY SERVICE_REG_ROOT = HKEY_LOCAL_MACHINE;
00022 // This string must agree with the one used in Activator_NT_Service.h
00023 static const ACE_TCHAR *SERVICE_REG_PATH =
00024   ACE_TEXT ("SYSTEM\\CurrentControlSet\\Services\\TAOImRActivator\\Parameters");
00025 #endif /* ACE_WIN32 */
00026 
00027 Activator_Options::Activator_Options ()
00028 : debug_ (1)
00029 , service_ (false)
00030 , notify_imr_ (false)
00031 , service_command_(SC_NONE)
00032 , env_buf_len_ (Activator_Options::ENVIRONMENT_BUFFER)
00033 {
00034 }
00035 
00036 int
00037 Activator_Options::parse_args (int &argc, char *argv[])
00038 {
00039   ACE_Arg_Shifter shifter (argc, argv);
00040 
00041   while (shifter.is_anything_left ())
00042     {
00043       if (ACE_OS::strcasecmp (shifter.get_current (),
00044                               ACE_TEXT ("-c")) == 0)
00045         {
00046           shifter.consume_arg ();
00047 
00048           if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
00049             {
00050               ACE_ERROR ((LM_ERROR, "Error: -c option needs a command\n"));
00051               this->print_usage ();
00052               return -1;
00053             }
00054 
00055           if (ACE_OS::strcasecmp (shifter.get_current (),
00056                                    ACE_TEXT ("install")) == 0)
00057             {
00058               this->service_command_ = SC_INSTALL;
00059             }
00060           else if (ACE_OS::strcasecmp (shifter.get_current (),
00061                                    ACE_TEXT ("remove")) == 0)
00062             {
00063               this->service_command_ = SC_REMOVE;
00064             }
00065           else if (ACE_OS::strcasecmp (shifter.get_current (),
00066                                    ACE_TEXT ("install_no_imr")) == 0)
00067             {
00068               this->service_command_ = SC_INSTALL_NO_LOCATOR;
00069             }
00070           else
00071             {
00072               ACE_ERROR((LM_ERROR, "Error: Unknown service command : %s\n", shifter.get_current()));
00073               this->print_usage ();
00074               return -1;
00075             }
00076         }
00077       else if (ACE_OS::strcasecmp (shifter.get_current (),
00078                                    ACE_TEXT ("-d")) == 0)
00079         {
00080           shifter.consume_arg ();
00081 
00082           if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
00083             {
00084               ACE_ERROR ((LM_ERROR, "Error: -d option needs a debuglevel\n"));
00085               this->print_usage ();
00086               return -1;
00087             }
00088 
00089           this->debug_ = ACE_OS::atoi (shifter.get_current ());
00090         }
00091       else if (ACE_OS::strcasecmp (shifter.get_current (),
00092                                    ACE_TEXT ("-e")) == 0)
00093         {
00094           shifter.consume_arg ();
00095 
00096           if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
00097             {
00098               ACE_ERROR ((LM_ERROR, "Error: -e option needs "
00099                                     "an environment buffer length\n"));
00100               this->print_usage ();
00101               return -1;
00102             }
00103 
00104           this->env_buf_len_ = ACE_OS::atoi (shifter.get_current ());
00105         }
00106       else if (ACE_OS::strcasecmp (shifter.get_current (),
00107                                    ACE_TEXT ("-o")) == 0)
00108         {
00109           shifter.consume_arg ();
00110 
00111           if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
00112             {
00113               ACE_ERROR ((LM_ERROR, "Error: -o option needs a filename\n"));
00114               this->print_usage ();
00115               return -1;
00116             }
00117           this->ior_output_file_ = shifter.get_current ();
00118         }
00119       else if (ACE_OS::strcasecmp (shifter.get_current (),
00120                                    ACE_TEXT ("-s")) == 0)
00121         {
00122           this->service_ = true;
00123         }
00124       else if ((ACE_OS::strcasecmp (shifter.get_current (),
00125                                     ACE_TEXT ("-?")) == 0)
00126                || (ACE_OS::strcasecmp (shifter.get_current (),
00127                                        ACE_TEXT ("-h")) == 0))
00128         {
00129           this->print_usage ();
00130           return 1;
00131         }
00132       else if (ACE_OS::strcasecmp (shifter.get_current (),
00133                                    ACE_TEXT ("-n")) == 0)
00134         {
00135           shifter.consume_arg ();
00136 
00137           if (!shifter.is_anything_left () || shifter.get_current ()[0] == '-')
00138             {
00139               ACE_ERROR ((LM_ERROR, "Error: -n option needs a name\n"));
00140               this->print_usage ();
00141               return -1;
00142             }
00143           this->name_ = shifter.get_current ();
00144         }
00145       else if (ACE_OS::strcasecmp (shifter.get_current (),
00146                                    ACE_TEXT ("-l")) == 0)
00147         {
00148           this->notify_imr_ = true;
00149         }
00150       else
00151         {
00152           shifter.ignore_arg ();
00153           continue;
00154         }
00155 
00156       shifter.consume_arg ();
00157     }
00158   return 0;
00159 }
00160 
00161 int
00162 Activator_Options::init (int argc, char *argv[])
00163 {
00164   // Make an initial pass through and grab the arguments that we recognize.
00165   // This may also run the commands to install or remove the nt service.
00166   int result = this->parse_args (argc, argv);
00167   if (result != 0)
00168     {
00169       return result;
00170     }
00171 
00172   for (int i = 0; i < argc; ++i)
00173     {
00174       this->cmdline_ += ACE_CString (argv[i]) + ACE_CString (" ");
00175     }
00176 
00177   return 0;
00178 }
00179 
00180 int
00181 Activator_Options::init_from_registry (void)
00182 {
00183   this->load_registry_options();
00184   return 0;
00185 }
00186 
00187 void
00188 Activator_Options::print_usage (void) const
00189 {
00190   ACE_ERROR ((LM_ERROR,
00191               "Usage:\n"
00192               "\n"
00193               "ImR_Activator [-c cmd] [-d 0|1|2] [-e buflen] [-o file] [-l] [-n name]\n"
00194               "\n"
00195               "  -c command  Runs service commands \n"
00196               "              ('install' or 'remove' or 'install_no_imr')\n"
00197               "  -d level    Sets the debug level\n"
00198               "  -e buflen   Set the environment buffer length in bytes for activated servants\n"
00199               "  -o file     Outputs the ImR's IOR to a file\n"
00200               "  -l          Notify the ImR when a process exits\n"
00201               "  -n name     Specify a name for the Activator\n")
00202              );
00203 }
00204 
00205 int
00206 Activator_Options::save_registry_options()
00207 {
00208 #if defined (ACE_WIN32)
00209   HKEY key = 0;
00210   // Create or open the parameters key
00211   LONG err = ACE_TEXT_RegCreateKeyEx (SERVICE_REG_ROOT,
00212                              SERVICE_REG_PATH,
00213                              0,
00214                              "", // class
00215                              REG_OPTION_NON_VOLATILE,
00216                              KEY_ALL_ACCESS,
00217                              0,
00218                              &key,
00219                              0
00220                              );
00221   if (err != ERROR_SUCCESS)
00222     {
00223       return -1;
00224     }
00225   err = ACE_TEXT_RegSetValueEx (key, "ORBInitOptions", 0, REG_SZ,
00226     (LPBYTE) this->cmdline_.c_str (), this->cmdline_.length () + 1);
00227   ACE_ASSERT (err == ERROR_SUCCESS);
00228 
00229   err = ACE_TEXT_RegSetValueEx (key, "IORFile", 0, REG_SZ,
00230     (LPBYTE) this->ior_output_file_.c_str (), this->ior_output_file_.length () + 1);
00231   ACE_ASSERT (err == ERROR_SUCCESS);
00232 
00233   err = ACE_TEXT_RegSetValueEx (key, "DebugLevel", 0, REG_DWORD,
00234     (LPBYTE) &this->debug_ , sizeof (this->debug_));
00235   ACE_ASSERT (err == ERROR_SUCCESS);
00236 
00237   err = ACE_TEXT_RegSetValueEx( key, "Name", 0, REG_SZ,
00238     (LPBYTE) this->name_.c_str (), this->name_.length () + 1);
00239   ACE_ASSERT (err == ERROR_SUCCESS);
00240 
00241   DWORD tmpint = this->notify_imr_;
00242   err = ACE_TEXT_RegSetValueEx (key, "NotifyImR", 0, REG_DWORD,
00243     (LPBYTE) &tmpint , sizeof (tmpint));
00244   ACE_ASSERT (err == ERROR_SUCCESS);
00245 
00246   err = ::RegCloseKey (key);
00247   ACE_ASSERT (err == ERROR_SUCCESS);
00248 #endif
00249   return 0;
00250 }
00251 
00252 int
00253 Activator_Options::load_registry_options ()
00254 {
00255 #if defined (ACE_WIN32)
00256   HKEY key = 0;
00257   // Create or open the parameters key
00258   LONG err = ACE_TEXT_RegOpenKeyEx (SERVICE_REG_ROOT,
00259                              SERVICE_REG_PATH,
00260                              0,
00261                              KEY_READ,
00262                              &key
00263                              );
00264   if (err != ERROR_SUCCESS)
00265     {
00266       // If there aren't any saved parameters, then that's ok.
00267       return 0;
00268     }
00269   char tmpstr[4096];
00270   DWORD sz = sizeof (tmpstr);
00271   DWORD type = 0;
00272   err = ACE_TEXT_RegQueryValueEx (key, "ORBInitOptions", 0, &type,
00273     (LPBYTE) tmpstr, &sz);
00274   if (err == ERROR_SUCCESS)
00275     {
00276       ACE_ASSERT (type == REG_SZ);
00277       tmpstr[sz - 1] = '\0';
00278       this->cmdline_ = tmpstr;
00279     }
00280 
00281   sz = sizeof(tmpstr);
00282   err = ACE_TEXT_RegQueryValueEx (key, "IORFile", 0, &type,
00283     (LPBYTE) tmpstr, &sz);
00284   if (err == ERROR_SUCCESS)
00285     {
00286       ACE_ASSERT (type == REG_SZ);
00287       tmpstr[sz - 1] = '\0';
00288       this->ior_output_file_ = tmpstr;
00289     }
00290 
00291   sz = sizeof(debug_);
00292   err = ACE_TEXT_RegQueryValueEx (key, "DebugLevel", 0, &type,
00293     (LPBYTE) &this->debug_ , &sz);
00294   if (err == ERROR_SUCCESS)
00295     {
00296       ACE_ASSERT (type == REG_DWORD);
00297     }
00298 
00299   sz = sizeof(tmpstr);
00300   err = ACE_TEXT_RegQueryValueEx (key, "Name", 0, &type,
00301     (LPBYTE) tmpstr, &sz);
00302   if (err == ERROR_SUCCESS)
00303     {
00304       ACE_ASSERT (type == REG_SZ);
00305       tmpstr[sz - 1] = '\0';
00306       this->name_ = tmpstr;
00307     }
00308 
00309   DWORD tmpint = 0;
00310   sz = sizeof(tmpint);
00311   err = ACE_TEXT_RegQueryValueEx (key, "NotifyImR", 0, &type,
00312     (LPBYTE) &tmpint , &sz);
00313   if (err == ERROR_SUCCESS)
00314     {
00315       ACE_ASSERT (type == REG_DWORD);
00316     }
00317   this->notify_imr_ = tmpint != 0;
00318 
00319   err = ::RegCloseKey (key);
00320   ACE_ASSERT(err == ERROR_SUCCESS);
00321 #endif /* ACE_WIN32 */
00322   return 0;
00323 }
00324 
00325 bool
00326 Activator_Options::service (void) const
00327 {
00328   return this->service_;
00329 }
00330 
00331 bool
00332 Activator_Options::notify_imr (void) const
00333 {
00334   return this->notify_imr_;
00335 }
00336 
00337 unsigned int
00338 Activator_Options::debug (void) const
00339 {
00340   return this->debug_;
00341 }
00342 
00343 const ACE_CString&
00344 Activator_Options::ior_filename (void) const
00345 {
00346   return this->ior_output_file_;
00347 }
00348 
00349 Activator_Options::SERVICE_COMMAND
00350 Activator_Options::service_command (void) const
00351 {
00352   return this->service_command_;
00353 }
00354 
00355 const char*
00356 Activator_Options::cmdline (void) const
00357 {
00358   return this->cmdline_.c_str ();
00359 }
00360 
00361 const ACE_CString&
00362 Activator_Options::name (void) const
00363 {
00364   return this->name_;
00365 }
00366 
00367 int
00368 Activator_Options::env_buf_len (void) const
00369 {
00370   return this->env_buf_len_;
00371 }

Generated on Sun Jan 27 15:54:50 2008 for TAO_Implementation_Repository by doxygen 1.3.6