Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / Notify_Service / NT_Notify_Service.cpp
bloba561076517faca03900aba5188d695f6543fda09
1 /* -*- C++ -*- */
2 #include /**/ "NT_Notify_Service.h"
4 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
6 #include /**/ "Notify_Service.h"
7 #include "tao/ORB_Core.h"
8 #include "ace/ARGV.h"
9 #include "ace/Reactor.h"
10 #include "orbsvcs/Log_Macros.h"
12 #define REGISTRY_KEY_ROOT HKEY_LOCAL_MACHINE
13 #define TAO_REGISTRY_SUBKEY ACE_TEXT ("SOFTWARE\\ACE\\TAO")
14 #define TAO_NOTIFY_SERVICE_OPTS_NAME ACE_TEXT ("TaoNotifyServiceOptions")
15 #define TAO_SERVICE_PARAM_COUNT ACE_TEXT ("TaoServiceParameterCount")
17 TAO_NT_Notify_Service::TAO_NT_Notify_Service (void)
18 : argc_ (0),
19 argc_save_ (0),
20 argv_ (0),
21 argv_save_ (0)
25 TAO_NT_Notify_Service::~TAO_NT_Notify_Service (void)
27 if (argv_save_)
29 for (int i = 0; i < argc_save_; i++)
30 ACE_OS::free (argv_save_[i]);
32 ACE_OS::free (argv_save_);
36 void
37 TAO_NT_Notify_Service::handle_control (DWORD control_code)
39 if (control_code == SERVICE_CONTROL_SHUTDOWN
40 || control_code == SERVICE_CONTROL_STOP)
42 report_status (SERVICE_STOP_PENDING);
43 TAO_ORB_Core_instance ()->reactor ()->end_reactor_event_loop ();
44 TAO_ORB_Core_instance ()->orb ()->shutdown (1);
45 report_status (SERVICE_STOPPED);
47 else
48 ACE_NT_Service::handle_control (control_code);
51 int
52 TAO_NT_Notify_Service::handle_exception (ACE_HANDLE)
54 return 0;
57 void
58 TAO_NT_Notify_Service::report_error (const ACE_TCHAR *format,
59 const ACE_TCHAR *val,
60 LONG result)
62 ACE_TCHAR msg[100];
63 ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
65 result,
66 LANG_SYSTEM_DEFAULT,
67 msg,
68 100,0);
69 ORBSVCS_DEBUG ((LM_DEBUG, format, val, msg));
72 void
73 TAO_NT_Notify_Service::arg_manip (char *args, DWORD arglen, bool query)
75 HKEY hkey = 0;
77 // It looks in the NT Registry under
78 // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
79 // "TaoNotifyServiceOptions" for any Notify Service options such as
80 // "-ORBListenEndpoints".
82 // Get/Set Notify Service options from the NT Registry.
84 LONG result = ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT,
85 TAO_REGISTRY_SUBKEY,
87 query ? KEY_READ : KEY_WRITE,
88 &hkey);
90 DWORD type = REG_EXPAND_SZ;
92 if (query)
94 *args = '\0';
95 if (result == ERROR_SUCCESS)
97 result = ACE_TEXT_RegQueryValueEx (hkey,
98 TAO_NOTIFY_SERVICE_OPTS_NAME,
100 &type,
101 (BYTE *)args,
102 &arglen);
103 if (result != ERROR_SUCCESS)
105 this->report_error (ACE_TEXT ("Could not query %s, %s\n"),
106 TAO_NOTIFY_SERVICE_OPTS_NAME, result);
109 else
111 this->report_error (ACE_TEXT ("No key for %s, %s\n"),
112 TAO_REGISTRY_SUBKEY, result);
115 else
117 if (result != ERROR_SUCCESS)
119 result = ACE_TEXT_RegCreateKeyEx (REGISTRY_KEY_ROOT,
120 TAO_REGISTRY_SUBKEY,
124 KEY_WRITE,
126 &hkey,
129 DWORD bufSize = static_cast<DWORD>(ACE_OS::strlen (args) + 1);
130 if (result == ERROR_SUCCESS)
132 result = ACE_TEXT_RegSetValueEx (hkey,
133 TAO_NOTIFY_SERVICE_OPTS_NAME,
135 type,
136 (BYTE *)args,
137 bufSize);
138 if (result != ERROR_SUCCESS)
140 this->report_error (ACE_TEXT ("Could not set %s, %s\n"),
141 TAO_NOTIFY_SERVICE_OPTS_NAME, result);
144 else
146 this->report_error (ACE_TEXT ("Could not create key %s, %s\n"),
147 TAO_REGISTRY_SUBKEY, result);
151 RegCloseKey (hkey);
156 TAO_NT_Notify_Service::set_args (const ACE_TCHAR *args)
158 char argbuf[ACE_DEFAULT_ARGV_BUFSIZ];
159 if (args == 0)
161 this->arg_manip (argbuf, ACE_DEFAULT_ARGV_BUFSIZ, true);
162 ACE_OS::printf ("%s\n", argbuf);
164 else
166 ACE_OS::strcpy (argbuf, ACE_TEXT_ALWAYS_CHAR (args));
167 this->arg_manip (argbuf, 0, false);
169 return 0;
173 TAO_NT_Notify_Service::init (int argc,
174 ACE_TCHAR *argv[])
176 HKEY hkey = 0;
177 BYTE buf[ACE_DEFAULT_ARGV_BUFSIZ];
179 *buf = '\0';
181 // This solution is very kludgy. It looks in the NT Registry under
182 // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
183 // "TaoNotifyServiceOptions" for any Notify Service options such as
184 // "-ORBEndpoint".
186 // Get Notify Service options from the NT Registry.
188 ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT,
189 TAO_REGISTRY_SUBKEY,
191 KEY_READ,
192 &hkey);
194 DWORD type;
195 DWORD bufSize = sizeof (buf);
197 ACE_TEXT_RegQueryValueEx (hkey,
198 TAO_NOTIFY_SERVICE_OPTS_NAME,
200 &type,
201 buf,
202 &bufSize);
204 RegCloseKey (hkey);
206 // Add options to the args list (if any).
208 if (ACE_OS::strlen ((char *) buf) > 0)
210 ACE_ARGV args ((const char*) buf);
211 // Allocate the internal args list to be one bigger than the
212 // args list passed into the function. We use a 'save' list in
213 // case we use a 'destructive' args list processor - this way we
214 // maintain the correct argv and argc for memory freeing
215 // operations in the destructor.
216 argv_save_ = (ACE_TCHAR **) ACE_OS::malloc (sizeof (ACE_TCHAR *) * (argc + args.argc ()));
218 // Copy the values into the internal args buffer.
219 int i;
220 for (i = 0; i < argc; i++)
221 argv_save_[i] = ACE_OS::strdup (argv[i]);
223 int j = 0;
224 for (i = argc; i < static_cast<int> ((args.argc () + argc)); i++)
225 argv_save_[i] = ACE_OS::strdup (args.argv ()[j++]);
227 // Set the arg counter.
228 argc_save_ = argc + args.argc ();
229 argc_ = argc_save_;
230 argv_ = argv_save_;
232 else
234 argc_ = argc;
235 argv_ = argv;
238 return 0;
242 TAO_NT_Notify_Service::svc (void)
244 TAO_Notify_Service_Driver notify_service;
248 if (notify_service.init (argc_, argv_) == -1)
249 return -1;
251 report_status (SERVICE_RUNNING);
252 notify_service.run ();
254 catch (const CORBA::Exception& ex)
256 ex._tao_print_exception (ACE_TEXT ("TAO NT Notify Service"));
257 return -1;
260 return 0;
263 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */