summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/Config_Backing_Store.cpp
blob: 95668c6f4ac6f5f23cc71f68c31a23d36b2e34e8 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// $Id$

#include "orbsvcs/Log_Macros.h"
#include "Config_Backing_Store.h"
#include "Server_Info.h"
#include "Activator_Info.h"
#include "utils.h"
#include "ace/OS_NS_unistd.h"

static const ACE_TCHAR* STARTUP_COMMAND = ACE_TEXT("StartupCommand");
static const ACE_TCHAR* WORKING_DIR = ACE_TEXT("WorkingDir");
static const ACE_TCHAR* ENVIRONMENT = ACE_TEXT("Environment");
static const ACE_TCHAR* ACTIVATION = ACE_TEXT("Activation");
static const ACE_TCHAR* PARTIAL_IOR = ACE_TEXT("Location");
static const ACE_TCHAR* IOR = ACE_TEXT("IOR");
static const ACE_TCHAR* START_LIMIT = ACE_TEXT("StartLimit");
static const ACE_TCHAR* ACTIVATOR = ACE_TEXT("Activator");
static const ACE_TCHAR* SERVERS_ROOT_KEY = ACE_TEXT("Servers");
static const ACE_TCHAR* ACTIVATORS_ROOT_KEY = ACE_TEXT("Activators");
static const ACE_TCHAR* TOKEN = ACE_TEXT("Token");
static const ACE_TCHAR* SERVER_ID = ACE_TEXT("ServerId");
static const ACE_TCHAR* JACORB_SERVER = ACE_TEXT("JacORBServer");

#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
static const char* WIN32_REG_KEY = "Software\\TAO\\ImplementationRepository";
#endif

Config_Backing_Store::Config_Backing_Store (const Options& opts,
                                            CORBA::ORB_ptr orb,
                                            ACE_Configuration& config)
: Locator_Repository (opts, orb),
  config_ (config),
  status_ (-1)
{
}

Config_Backing_Store::~Config_Backing_Store ()
{
}

void Config_Backing_Store::loadActivators ()
{
  ACE_Configuration_Section_Key root;
  int err =
    config_.open_section (config_.root_section (), ACTIVATORS_ROOT_KEY, 0, root);
  if (err == 0)
    {
      int index = 0;
      ACE_TString name;
      while (config_.enumerate_sections (root, index, name) == 0)
        {
          ACE_CString ior;
          u_int token;

          ACE_Configuration_Section_Key key;

          // Can't fail, because we're enumerating
          config_.open_section (root, name.c_str(), 0, key);

          config_.get_string_value (key, IOR, ior);
          config_.get_integer_value (key, TOKEN, token);

          Activator_Info* ai;
          ACE_NEW (ai, Activator_Info (name, token, ior));

          Activator_Info_Ptr info (ai);
          activators ().bind (lcase (name), info);
          ++index;
        }
    }
}

void
Config_Backing_Store::loadServers ()
{
  ACE_Configuration_Section_Key root;
  int err =
    config_.open_section (config_.root_section (), SERVERS_ROOT_KEY, 0, root);
  if (err == 0)
    {
      int index = 0;
      ACE_TString name;
      while (config_.enumerate_sections (root, index, name) == 0)
        {
          ACE_CString server_id, cmdline, dir, envstr, partial_ior, ior, aname, jacorbstr;
          u_int amodeint = ImplementationRepository::MANUAL;
          u_int start_limit;

          ACE_Configuration_Section_Key key;

          // Can't fail, because we're enumerating
          config_.open_section (root, name.c_str(), 0, key);

          // Ignore any missing values. Server name is enough on its own.
          config_.get_string_value (key, SERVER_ID, server_id);
          config_.get_string_value (key, JACORB_SERVER, jacorbstr);
          config_.get_string_value (key, ACTIVATOR, aname);
          config_.get_string_value (key, STARTUP_COMMAND, cmdline);
          config_.get_string_value (key, WORKING_DIR, dir);
          config_.get_string_value (key, ENVIRONMENT, envstr);
          config_.get_integer_value (key, ACTIVATION, amodeint);
          config_.get_string_value (key, PARTIAL_IOR, partial_ior);
          config_.get_string_value (key, IOR, ior);
          config_.get_integer_value (key, START_LIMIT, start_limit);

          ImplementationRepository::ActivationMode amode =
            static_cast <ImplementationRepository::ActivationMode> (amodeint);

          ImplementationRepository::EnvironmentList env_vars =
            ImR_Utils::parseEnvList (envstr);

          bool jacorb_server = jacorbstr == "1" ? true : false;

          Server_Info *si;
          ACE_NEW (si,
                   Server_Info (server_id, name,  jacorb_server, aname,
                                cmdline, env_vars, dir, amode, start_limit,
                                partial_ior, ior));
          Server_Info_Ptr info (si);
          servers ().bind (name, info);
          ++index;
        }
    }
}

int
Config_Backing_Store::persistent_remove (const ACE_CString& name,
                                         bool activator)
{
  const ACE_TCHAR* const key =
    (activator ? ACTIVATORS_ROOT_KEY : SERVERS_ROOT_KEY);
  return remove (name, key);
}

int
Config_Backing_Store::remove (const ACE_CString& name, const ACE_TCHAR* key)
{
  ACE_Configuration_Section_Key root;
  int err = config_.open_section (config_.root_section (), key, 0, root);
  if (err != 0)
    {
      if (this->opts_.debug () > 9)
        {
          ORBSVCS_DEBUG((LM_INFO, ACE_TEXT ("could not remove %C, already gone!\n"),
            name.c_str()));
        }
      return 0; // Already gone.
    }
  return config_.remove_section (root, name.c_str(), 1);
}

static int get_key (ACE_Configuration& cfg, const ACE_CString& name,
                    const ACE_TCHAR* const sub_section,
                    ACE_Configuration_Section_Key& key)
{
  ACE_Configuration_Section_Key root;
  int err = cfg.open_section (cfg.root_section(), sub_section, 1, root);
  if (err != 0)
    {
      ORBSVCS_ERROR ((LM_ERROR, ACE_TEXT ("Unable to open config section:%s\n"),
        sub_section));
      return err;
    }
  err = cfg.open_section (root, name.c_str (), 1, key);
  if (err != 0)
    {
      ORBSVCS_ERROR((LM_ERROR, ACE_TEXT ("Unable to open config section:%C\n"),
        name.c_str ()));
    }
  return err;
}

int
Config_Backing_Store::persistent_update(const Server_Info_Ptr& info, bool )
{
  ACE_Configuration_Section_Key key;
  int err = get_key(this->config_, info->name, SERVERS_ROOT_KEY, key);
  if (err != 0)
    {
      ORBSVCS_ERROR((LM_ERROR, ACE_TEXT ("ERROR: could not get key for %C\n"),
        info->name.c_str ()));
      return err;
    }

  if (this->opts_.debug() > 9)
    {
      ORBSVCS_DEBUG((LM_INFO, ACE_TEXT ("updating %C\n"), info->name.c_str()));
    }
  ACE_CString envstr = ImR_Utils::envListToString(info->env_vars);

  this->config_.set_string_value (key, SERVER_ID, info->server_id.c_str());
  this->config_.set_string_value (key, JACORB_SERVER, info->jacorb_server ? "1" : "0");
  this->config_.set_string_value (key, ACTIVATOR, info->activator.c_str());
  this->config_.set_string_value (key, STARTUP_COMMAND, info->cmdline.c_str());
  this->config_.set_string_value (key, WORKING_DIR, info->dir.c_str());
  this->config_.set_string_value (key, ENVIRONMENT, envstr);
  this->config_.set_integer_value (key, ACTIVATION, info->activation_mode);
  this->config_.set_integer_value (key, START_LIMIT, info->start_limit);
  this->config_.set_string_value (key, PARTIAL_IOR, info->partial_ior.c_str());
  this->config_.set_string_value (key, IOR, info->ior.c_str());

  return 0;
}

int
Config_Backing_Store::persistent_update(const Activator_Info_Ptr& info, bool )
{
  ACE_Configuration_Section_Key key;
  int err = get_key (this->config_, info->name, ACTIVATORS_ROOT_KEY, key);
  if (err != 0)
    {
      ORBSVCS_DEBUG((LM_INFO,
        ACE_TEXT ("ERROR: could not get key for activator %C\n"),
        info->name.c_str ()));
      return err;
    }

  if (this->opts_.debug() > 9)
    {
      ORBSVCS_DEBUG((LM_INFO, ACE_TEXT ("updating activator %C\n"),
        info->name.c_str ()));
    }
  this->config_.set_integer_value (key, TOKEN, info->token);
  this->config_.set_string_value (key, IOR, info->ior.c_str());

  return 0;
}

int
Config_Backing_Store::init_repo (PortableServer::POA_ptr )
{
  if (status_ != 0)
    {
      if (this->opts_.debug () > 9)
        {
          ORBSVCS_DEBUG ((LM_INFO, ACE_TEXT ("not loading\n")));
        }
      return status_;
    }

  loadActivators();
  loadServers();

  return 0;
}

Heap_Backing_Store::Heap_Backing_Store (const Options& opts,
                                        CORBA::ORB_ptr orb)
: Config_Backing_Store (opts, orb, heap_),
  filename_ (opts.persist_file_name())
{
  if (opts.repository_erase ())
    {
      if (this->opts_.debug () > 9)
        {
          ORBSVCS_DEBUG ((LM_INFO, ACE_TEXT ("Heap start clean\n")));
        }
      ACE_OS::unlink ( this->filename_.c_str() );
    }

  status_ = heap_.open (this->filename_.c_str());
}

Heap_Backing_Store::~Heap_Backing_Store ()
{
}

const ACE_TCHAR*
Heap_Backing_Store::repo_mode() const
{
  return this->filename_.c_str();
}

#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
static HKEY setup_registry (const bool start_clean)
{
  if (start_clean)
    {
      ACE_Configuration_Win32Registry config ( HKEY_LOCAL_MACHINE );
      ACE_Configuration_Section_Key root;
      config.open_section (config.root_section(), "Software\\TAO", 0, root);
      config.remove_section (root, "ImplementationRepository", 1);
    }

  return ACE_Configuration_Win32Registry::
    resolve_key (HKEY_LOCAL_MACHINE, WIN32_REG_KEY);
}
#endif

Registry_Backing_Store::Registry_Backing_Store(const Options& opts,
                                               CORBA::ORB_ptr orb)
#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
: Config_Backing_Store(opts, orb, win32registry_),
  win32registry_ (setup_registry (opts.repository_erase()))
#else
: Config_Backing_Store (opts, orb, invalid_config_)
#endif
{
#if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
  status_ = 0;
#else
  ORBSVCS_ERROR ((LM_ERROR, ACE_TEXT ("Registry persistence is only ")
                        ACE_TEXT ("supported on Windows\n")));
  status_ = -1;
#endif
}

Registry_Backing_Store::~Registry_Backing_Store ()
{
}

const ACE_TCHAR*
Registry_Backing_Store::repo_mode () const
{
  return ACE_TEXT("Registry");
}