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"
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)
25 TAO_NT_Notify_Service::~TAO_NT_Notify_Service (void)
29 for (int i
= 0; i
< argc_save_
; i
++)
30 ACE_OS::free (argv_save_
[i
]);
32 ACE_OS::free (argv_save_
);
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
);
48 ACE_NT_Service::handle_control (control_code
);
52 TAO_NT_Notify_Service::handle_exception (ACE_HANDLE
)
58 TAO_NT_Notify_Service::report_error (const ACE_TCHAR
*format
,
63 ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
,
69 ORBSVCS_DEBUG ((LM_DEBUG
, format
, val
, msg
));
73 TAO_NT_Notify_Service::arg_manip (char *args
, DWORD arglen
, bool query
)
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
,
87 query
? KEY_READ
: KEY_WRITE
,
90 DWORD type
= REG_EXPAND_SZ
;
95 if (result
== ERROR_SUCCESS
)
97 result
= ACE_TEXT_RegQueryValueEx (hkey
,
98 TAO_NOTIFY_SERVICE_OPTS_NAME
,
103 if (result
!= ERROR_SUCCESS
)
105 this->report_error (ACE_TEXT ("Could not query %s, %s\n"),
106 TAO_NOTIFY_SERVICE_OPTS_NAME
, result
);
111 this->report_error (ACE_TEXT ("No key for %s, %s\n"),
112 TAO_REGISTRY_SUBKEY
, result
);
117 if (result
!= ERROR_SUCCESS
)
119 result
= ACE_TEXT_RegCreateKeyEx (REGISTRY_KEY_ROOT
,
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
,
138 if (result
!= ERROR_SUCCESS
)
140 this->report_error (ACE_TEXT ("Could not set %s, %s\n"),
141 TAO_NOTIFY_SERVICE_OPTS_NAME
, result
);
146 this->report_error (ACE_TEXT ("Could not create key %s, %s\n"),
147 TAO_REGISTRY_SUBKEY
, result
);
156 TAO_NT_Notify_Service::set_args (const ACE_TCHAR
*args
)
158 char argbuf
[ACE_DEFAULT_ARGV_BUFSIZ
];
161 this->arg_manip (argbuf
, ACE_DEFAULT_ARGV_BUFSIZ
, true);
162 ACE_OS::printf ("%s\n", argbuf
);
166 ACE_OS::strcpy (argbuf
, ACE_TEXT_ALWAYS_CHAR (args
));
167 this->arg_manip (argbuf
, 0, false);
173 TAO_NT_Notify_Service::init (int argc
,
177 BYTE buf
[ACE_DEFAULT_ARGV_BUFSIZ
];
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
186 // Get Notify Service options from the NT Registry.
188 ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT
,
195 DWORD bufSize
= sizeof (buf
);
197 ACE_TEXT_RegQueryValueEx (hkey
,
198 TAO_NOTIFY_SERVICE_OPTS_NAME
,
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.
220 for (i
= 0; i
< argc
; i
++)
221 argv_save_
[i
] = ACE_OS::strdup (argv
[i
]);
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 ();
242 TAO_NT_Notify_Service::svc (void)
244 TAO_Notify_Service_Driver notify_service
;
248 if (notify_service
.init (argc_
, argv_
) == -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"));
263 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */