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