summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/Naming_Service/NT_Naming_Service.cpp
blob: c2adf6a50d959fc7b1705ea234efde71bc6eeee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// -*- C++ -*-

#include /**/ "NT_Naming_Service.h"

#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)

#include /**/ "Naming_Service.h"
#include "tao/ORB_Core.h"
#include "ace/ARGV.h"
#include "orbsvcs/Log_Macros.h"

#define REGISTRY_KEY_ROOT HKEY_LOCAL_MACHINE
#define TAO_REGISTRY_SUBKEY ACE_TEXT ("SOFTWARE\\ACE\\TAO")
#define TAO_NAMING_SERVICE_OPTS_NAME ACE_TEXT ("TaoNamingServiceOptions")
#define TAO_SERVICE_PARAM_COUNT "TaoServiceParameterCount"

AutoFinalizer::AutoFinalizer (TAO_NT_Naming_Service &service)
  : service_ (service)
{
}

AutoFinalizer::~AutoFinalizer ()
{
  service_.report_status (SERVICE_STOPPED);
  ORBSVCS_DEBUG ((LM_DEBUG, "Reported service stoped\n"));
}


TAO_NT_Naming_Service::TAO_NT_Naming_Service (void)
  : argc_ (0),
    argc_save_ (0),
    argv_ (0),
    argv_save_ (0)
{
}

TAO_NT_Naming_Service::~TAO_NT_Naming_Service (void)
{
  if (argv_save_)
    {
      for (int i = 0; i < argc_save_; i++)
        ACE_OS::free (argv_save_[i]);

      ACE_OS::free (argv_save_);
    }
}

void
TAO_NT_Naming_Service::handle_control (DWORD control_code)
{
  if (control_code == SERVICE_CONTROL_SHUTDOWN
      || control_code == SERVICE_CONTROL_STOP)
    {
      // Just in case any of the following method calls
      // throws in a way we do not expect.
      // This instance's destructor will notify the OS.
      AutoFinalizer afinalizer (*this);

      report_status (SERVICE_STOP_PENDING);

      // This must be all that needs to be done since this method is executing
      // in a separate thread from the one running the reactor.
      // When the reactor is stopped it calls ORB::destroy(), which in turn
      // calls ORB::shutdown(1) *and* unbinds the ORB from the ORB table.

      try
        {
          TAO_ORB_Core_instance ()->orb ()->shutdown (1);
        }
      catch (const CORBA::Exception&)
        {
          // What should we do here? Even the log messages are not
          // showing up, since the thread that runs this is not an ACE
          // thread. It is allways spawned/controlled by Windows ...
        }
    }
  else
  {
    ACE_NT_Service::handle_control (control_code);
  }
}

int
TAO_NT_Naming_Service::handle_exception (ACE_HANDLE)
{
  return 0;
}

void
TAO_NT_Naming_Service::report_error (const ACE_TCHAR *format,
                                     const ACE_TCHAR *val,
                                     LONG result)
{
  ACE_TCHAR msg[100];
  ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
                          0,
                          result,
                          LANG_SYSTEM_DEFAULT,
                          msg,
                          100,0);
  ORBSVCS_DEBUG ((LM_DEBUG, format, val, msg));
}

void
TAO_NT_Naming_Service::arg_manip (char *args, DWORD arglen, bool query)
{
  HKEY hkey = 0;

  // It looks in the NT Registry under
  // \\HKEY_LOCAL_MACHINE\SOFTWARE\ACE\TAO for the value of
  // "TaoNamingServiceOptions" for any Naming Service options such as
  // "-ORBListenEndpoints".

  // Get/Set Naming Service options from the NT Registry.

  LONG result = ACE_TEXT_RegOpenKeyEx (REGISTRY_KEY_ROOT,
                                       TAO_REGISTRY_SUBKEY,
                                       0,
                                       query ? KEY_READ : KEY_WRITE,
                                       &hkey);

  DWORD type = REG_EXPAND_SZ;

  if (query)
    {
      *args = '\0';
      if (result == ERROR_SUCCESS)
        {
          result = ACE_TEXT_RegQueryValueEx (hkey,
                                             TAO_NAMING_SERVICE_OPTS_NAME,
                                             0,
                                             &type,
                                             (BYTE *)args,
                                             &arglen);
          if (result != ERROR_SUCCESS)
            {
              this->report_error (ACE_TEXT ("Could not query %s, %s\n"),
                                  TAO_NAMING_SERVICE_OPTS_NAME, result);
            }
        }
      else
        {
          this->report_error (ACE_TEXT ("No key for %s, %s\n"),
                              TAO_REGISTRY_SUBKEY, result);
        }
    }
  else
    {
      if (result != ERROR_SUCCESS)
        {
          result = ACE_TEXT_RegCreateKeyEx (REGISTRY_KEY_ROOT,
                                            TAO_REGISTRY_SUBKEY,
                                            0,
                                            0,
                                            0,
                                            KEY_WRITE,
                                            0,
                                            &hkey,
                                            0);
        }
      DWORD bufSize = static_cast<DWORD>(ACE_OS::strlen (args) + 1);
      if (result == ERROR_SUCCESS)
        {
          result = ACE_TEXT_RegSetValueEx (hkey,
                                           TAO_NAMING_SERVICE_OPTS_NAME,
                                           0,
                                           type,
                                           (BYTE *)args,
                                           bufSize);
          if (result != ERROR_SUCCESS)
            {
               this->report_error (ACE_TEXT ("Could not set %s, %s\n"),
                                   TAO_NAMING_SERVICE_OPTS_NAME, result);
            }
        }
      else
        {
          this->report_error (ACE_TEXT ("Could not create key %s, %s\n"),
                              TAO_REGISTRY_SUBKEY, result);
        }
    }

  RegCloseKey (hkey);

}

int
TAO_NT_Naming_Service::set_args (const ACE_TCHAR *args)
{
  char argbuf[ACE_DEFAULT_ARGV_BUFSIZ];
  if (args == 0)
    {
      this->arg_manip (argbuf, ACE_DEFAULT_ARGV_BUFSIZ, true);
      ACE_OS::printf ("%s\n", argbuf);
    }
  else
    {
      ACE_OS::strcpy (argbuf, ACE_TEXT_ALWAYS_CHAR (args));
      this->arg_manip (argbuf, 0, false);
    }
  return 0;
}

int
TAO_NT_Naming_Service::init (int argc,
                             ACE_TCHAR *argv[])
{
  // Add options to the args list (if any).
  char argbuf[ACE_DEFAULT_ARGV_BUFSIZ];
  this->arg_manip (argbuf, ACE_DEFAULT_ARGV_BUFSIZ, true);

  if (ACE_OS::strlen (argbuf) > 0)
    {
      ACE_ARGV args (argbuf);
      // Allocate the internal args list to be one bigger than the
      // args list passed into the function. We use a 'save' list in
      // case we use a 'destructive' args list processor - this way we
      // maintain the correct argv and argc for memory freeing
      // operations in the destructor.
      argv_save_ = (ACE_TCHAR **) ACE_OS::malloc (sizeof (ACE_TCHAR *) * (argc + args.argc ()));

      // Copy the values into the internal args buffer.
      int i;
      for (i = 0; i < argc; i++)
        argv_save_[i] = ACE_OS::strdup (argv[i]);

      int j = 0;
      for (i = argc; i < static_cast<int> ((args.argc () + argc)); i++)
        argv_save_[i] = ACE_OS::strdup (args.argv ()[j++]);

      // Set the arg counter.
      argc_save_ = argc + args.argc ();
      argc_ = argc_save_;
      argv_ = argv_save_;
    }
  else
    {
      argc_ = argc;
      argv_ = argv;
    }

  return 0;
}

int
TAO_NT_Naming_Service::svc (void)
{
  TAO_Naming_Service naming_service;

  if (naming_service.init (argc_, argv_) == -1)
    return -1;

  try
    {
      // Just in case handle_control does not get the chance
      // to execute, or is never called by Windows. This instance's
      // destructor will inform the OS of our demise.
      AutoFinalizer afinalizer (*this);

      ORBSVCS_DEBUG ((LM_INFO, "Notifying Windows of service startup\n"));
      report_status (SERVICE_RUNNING);

      naming_service.run ();
    }
  catch (const CORBA::Exception& ex)
    {
      ORBSVCS_DEBUG ((LM_INFO, "Exception in service - exitting\n"));
      ex._tao_print_exception ("TAO NT Naming Service");
      return -1;
    }

  ORBSVCS_DEBUG ((LM_INFO, "Exiting gracefully\n"));
  return 0;
}

#endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */