ImR_Activator.cpp

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // ImR_Activator.cpp,v 1.8 2006/01/04 16:10:11 giovannd Exp
00003 
00004 #include "Activator_Options.h"
00005 #include "ImR_Activator_i.h"
00006 #include "Activator_NT_Service.h"
00007 
00008 #include "orbsvcs/Shutdown_Utilities.h"
00009 
00010 class ImR_Activator_Shutdown : public Shutdown_Functor
00011 {
00012 public:
00013   ImR_Activator_Shutdown (ImR_Activator_i& act);
00014 
00015   void operator() (int which_signal);
00016 private:
00017   ImR_Activator_i& act_;
00018 };
00019 
00020 ImR_Activator_Shutdown::ImR_Activator_Shutdown (ImR_Activator_i &act)
00021   : act_(act)
00022 {
00023 }
00024 
00025 void
00026 ImR_Activator_Shutdown::operator() (int /*which_signal*/)
00027 {
00028   ACE_DECLARE_NEW_CORBA_ENV;
00029   ACE_TRY
00030   {
00031     this->act_.shutdown (true ACE_ENV_ARG_PARAMETER);
00032     ACE_TRY_CHECK;
00033   }
00034   ACE_CATCHANY
00035   {
00036     ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "ImR Activator: ");
00037   }
00038   ACE_ENDTRY;
00039 }
00040 
00041 int
00042 run_standalone (Activator_Options& opts)
00043 {
00044   ImR_Activator_i server;
00045 
00046   ImR_Activator_Shutdown killer (server);
00047   Service_Shutdown kill_contractor (killer);
00048 
00049   ACE_DECLARE_NEW_CORBA_ENV;
00050   ACE_TRY
00051     {
00052       int status = server.init (opts ACE_ENV_ARG_PARAMETER);
00053       ACE_TRY_CHECK;
00054 
00055       if (status == -1)
00056         {
00057           return 1;
00058         }
00059       else
00060         {
00061           // Run the server if it is initialized correctly.
00062           server.run (ACE_ENV_SINGLE_ARG_PARAMETER);
00063           ACE_TRY_CHECK;
00064 
00065           // End the server after its work is done.
00066           status = server.fini (ACE_ENV_SINGLE_ARG_PARAMETER);
00067           ACE_TRY_CHECK;
00068 
00069           if (status == -1)
00070             return 1;
00071         }
00072       return 0;
00073     }
00074   ACE_CATCH (CORBA::SystemException, sysex)
00075     {
00076       ACE_PRINT_EXCEPTION (sysex, "System Exception");
00077     }
00078   ACE_CATCH (CORBA::UserException, userex)
00079     {
00080       ACE_PRINT_EXCEPTION (userex, "User Exception");
00081     }
00082   ACE_CATCHANY
00083     {
00084       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Unknown Exception");
00085     }
00086   ACE_ENDTRY;
00087 
00088   return 1;
00089 }
00090 
00091 #if defined (ACE_WIN32)
00092 ACE_NT_SERVICE_DEFINE (service, Activator_NT_Service, IMR_ACTIVATOR_SERVICE_NAME);
00093 #endif /* ACE_WIN32 */
00094 
00095 int
00096 run_service (void)
00097 {
00098 #if defined (ACE_WIN32)
00099   SERVICE::instance()->name (IMR_ACTIVATOR_SERVICE_NAME, IMR_ACTIVATOR_DISPLAY_NAME);
00100   ACE_NT_SERVICE_RUN (service, SERVICE::instance (), ret);
00101 
00102   if (ret == 0)
00103     ACE_ERROR ((LM_ERROR, "%p\n", "Couldn't start service"));
00104 
00105   return ret;
00106 #else /* ACE_WIN32 */
00107   return 1;
00108 #endif /* ACE_WIN32 */
00109 }
00110 
00111 /**
00112  * Executes the various commands that are useful for a NT service.  Right
00113  * now these include 'install' and 'remove'.  Others, such as 'start' and
00114  * 'stop' can be added, but the 'net' program in Windows already handles
00115  * these commands.
00116  */
00117 static int
00118 run_service_command (Activator_Options& opts)
00119 {
00120   if (opts.service_command () == Activator_Options::SC_NONE)
00121     return 0;
00122 
00123 #if defined (ACE_WIN32)
00124   SERVICE::instance()->name (IMR_ACTIVATOR_SERVICE_NAME, IMR_ACTIVATOR_DISPLAY_NAME);
00125 
00126   if (opts.service_command () == Activator_Options::SC_INSTALL ||
00127     opts.service_command () == Activator_Options::SC_INSTALL_NO_LOCATOR)
00128       {
00129         const DWORD MAX_PATH_LENGTH = 4096;
00130         char pathname[MAX_PATH_LENGTH];
00131 
00132         DWORD length = ACE_TEXT_GetModuleFileName(NULL, pathname, MAX_PATH_LENGTH);
00133         if (length == 0 || length >= MAX_PATH_LENGTH - sizeof(" -s"))
00134           {
00135             ACE_ERROR ((LM_ERROR, "Error: Could not get module file name\n"));
00136             return -1;
00137           }
00138 
00139         // Append the command used for running the implrepo as a service
00140         ACE_OS::strcat (pathname, ACE_TEXT (" -s"));
00141         int ret = -1;
00142         if (opts.service_command () == Activator_Options::SC_INSTALL)
00143           {
00144             const char* DEPENDS_ON = "TAOImR"; // Must match Locator_NT_Service.h
00145 
00146             ret =  SERVICE::instance ()->insert (SERVICE_DEMAND_START,
00147                                                 SERVICE_ERROR_NORMAL,
00148                                                 pathname,
00149                                                 0, // group
00150                                                 0, // tag
00151                                                 DEPENDS_ON
00152                                                 );
00153           }
00154         else
00155           {
00156             ret =  SERVICE::instance ()->insert (SERVICE_DEMAND_START,
00157                                                 SERVICE_ERROR_NORMAL,
00158                                                 pathname);
00159           }
00160       if (ret != -1)
00161         {
00162           ACE_DEBUG ((LM_DEBUG, "ImR Activator: Service installed.\n"));
00163           opts.save_registry_options ();
00164         }
00165       else
00166         {
00167           ACE_ERROR ((LM_ERROR, "Error: Failed to install service.\n"));
00168         }
00169       if (ret == 0)
00170         return 1;
00171     }
00172   else if (opts.service_command () == Activator_Options::SC_REMOVE)
00173     {
00174       int ret = SERVICE::instance ()->remove ();
00175       ACE_DEBUG ((LM_DEBUG, "ImR Activator: Service removed.\n"));
00176       if (ret == 0)
00177         return 1; // If successfull, then we don't want to continue.
00178     }
00179   else
00180     {
00181       ACE_ERROR ((LM_ERROR, "Error: Unknown service command :%d \n",
00182         opts.service_command ()));
00183       return -1;
00184     }
00185 
00186   return -1;
00187 
00188 #else /* ACE_WIN32 */
00189   ACE_ERROR ((LM_ERROR, "NT Service not supported on this platform"));
00190   return -1;
00191 #endif /* ACE_WIN32 */
00192 }
00193 
00194 int
00195 main (int argc, char *argv[])
00196 {
00197   Activator_Options opts;
00198 
00199   int result = opts.init (argc, argv);
00200   if (result < 0)
00201     return 1;  // Error
00202   else if (result > 0)
00203     return 0;  // No error, but we should exit anyway.
00204 
00205   result = run_service_command (opts);
00206   if (result < 0)
00207     return 1;  // Error
00208   else if (result > 0)
00209     return 0;  // No error, but we should exit anyway.
00210 
00211   if (opts.service ())
00212     return run_service ();
00213 
00214   return run_standalone (opts);
00215 }
00216 

Generated on Thu Nov 9 13:36:19 2006 for TAO_Implementation_Repository by doxygen 1.3.6