ImR_Activator.cpp

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

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