Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / utils / nslist / nsadd.cpp
blobac036f56f2cb584b9bf0cbaebfb3e3185aeb04fc
1 //=============================================================================
2 /**
3 * @file nsadd.cpp
5 * Naming Service add utility
7 * @author Carlos O'Ryan enhanced Nov 6
8 * @author 2002 Paul Caffrey <denginere@hotmail.com> redone Jun 21 2006 Simon Massey <sma@prismtech.com>
9 */
10 //=============================================================================
12 #include "orbsvcs/CosNamingC.h"
13 #include "ace/Log_Msg.h"
14 #include "ace/OS_NS_string.h"
16 int showIOR = 0;
17 int showNSonly = 0;
19 int
20 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
22 CosNaming::Name the_name (0);
23 CORBA::ORB_var orb;
24 const ACE_TCHAR *ior = 0;
26 int err = 0;
28 try
30 // Contact the orb
31 orb = CORBA::ORB_init (argc, argv);
33 // Scan through the command line options
34 bool
35 failed = false,
36 quiet = false,
37 rebind = false,
38 context = false;
39 const ACE_TCHAR
40 *const pname = argv[0];
41 const ACE_TCHAR *nameService = 0;
42 ACE_TCHAR kindsep = ACE_TEXT('.');
43 ACE_TCHAR ctxsep[] = ACE_TEXT("/");
44 ACE_TCHAR *name = 0;
46 if (0 < argc)
48 while (--argc > 0)
50 ++argv;
51 if (0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--ns")))
53 if (!--argc)
55 ACE_DEBUG ((LM_DEBUG,
56 "Error: --ns requires an argument\n"));
57 failed= true;
59 else
61 ++argv;
62 if (nameService)
64 ACE_DEBUG ((LM_DEBUG,
65 "Error: more than one --ns.\n"));
66 failed= true;
68 else
69 nameService = *argv;
72 else if (0 == ACE_OS::strcmp (*argv, ACE_TEXT("--quiet")))
74 quiet = true;
76 else if (0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--name")))
78 if (name)
80 ACE_DEBUG ((LM_DEBUG,
81 "Error: more than one --name\n"));
82 failed = true;
84 else if (!--argc)
86 ACE_DEBUG ((LM_DEBUG,
87 "Error: --name requires an argument\n"));
88 failed = true;
90 else
91 name = *++argv;
93 else if (0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--ctxsep")))
95 if (!--argc)
97 ACE_DEBUG ((LM_DEBUG,
98 "Error: --ctxsep requires a character\n"));
99 failed = true;
101 else if (1 != ACE_OS::strlen(*++argv))
103 ACE_DEBUG ((LM_DEBUG,
104 "Error: --ctxsep takes a single character (not %s)\n", *argv));
105 failed = true;
107 else
108 ctxsep[0] = (*argv)[0];
110 else if (0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--kindsep")))
112 if (!--argc)
114 ACE_DEBUG ((LM_DEBUG,
115 "Error: --kindsep requires a character\n"));
116 failed = true;
118 else if (1 != ACE_OS::strlen(*++argv))
120 ACE_DEBUG ((LM_DEBUG,
121 "Error: --kindsep takes a single character (not %s)\n", *argv));
122 failed = true;
124 else
125 kindsep = (*argv)[0];
127 else if (0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--ior")))
129 if (ior)
131 ACE_DEBUG ((LM_DEBUG,
132 "Error: ior already specified\n"));
133 failed = true;
135 else if (!--argc)
137 ACE_DEBUG ((LM_DEBUG,
138 "Error: --ior requires an IOR\n"));
139 failed = true;
141 else
142 ior = *++argv;
144 else if ((0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--ctx")))
145 ||(0 == ACE_OS::strcmp(*argv, ACE_TEXT ("--newcontext"))))
147 context = true;
149 else if (0 == ACE_OS::strcmp (*argv, ACE_TEXT ("--rebind")))
151 rebind = true;
153 else
155 ACE_DEBUG ((LM_DEBUG,
156 "Unknown option %s\n", *argv));
157 failed = true;
162 if (failed || !name || (!ior && !context))
164 ACE_DEBUG ((LM_DEBUG,
165 "\nUsage:\n"
166 " %s --name <name> --ior <ior>\n"
167 " %s --name <name> --ctx\n"
168 " %s --name <name> --ior <ior> --ctx\n"
169 "optional:\n"
170 " --ns <ior>\n"
171 " --rebind\n"
172 " --ctxsep <character>\n"
173 " --kindsep <character>\n"
174 " --newcontext {same as --ctx}\n"
175 " --quiet\n\n"
176 "where <name> uses the --ctxsep (defaults to /) to separate\n"
177 "sub-contexts and --kindsep (defaults to .) to separate ID\n"
178 "and Kind. If --ctx is NOT given; binds a final object.\n"
179 "If --ctx is given without --ior; binds a new naming context.\n"
180 "If both --ior and --ctx are specified; binds the existing\n"
181 "naming context specified. If --rebind is given, will replace\n"
182 "existing bindings of the same type (object or context).\n"
183 "Connects to the default NameService unless --ns is given.\n"
184 "Displays all ID/Kinds found/created on path unless --quiet\n"
185 "is given.\n",
186 pname, pname, pname));
187 orb->destroy ();
188 return 1;
191 // Contact the name service
192 CORBA::Object_var nc_obj;
193 if (nameService)
194 nc_obj = orb->string_to_object (nameService);
195 else
196 nc_obj = orb->resolve_initial_references ("NameService");
198 CosNaming::NamingContext_var root_nc =
199 CosNaming::NamingContext::_narrow (nc_obj.in ());
200 if (CORBA::is_nil (root_nc.in ()))
202 ACE_DEBUG ((LM_DEBUG,
203 "Error: nil naming context\n"));
204 orb->destroy ();
205 return 1;
208 // Get the object reference from the IOR string given
209 CORBA::Object_var obj;
210 if (ior)
212 obj = orb->string_to_object (ior);
215 // Assemble the name from the user string given
216 ACE_TCHAR *cp;
217 while (0 != (cp = ACE_OS::strtok (name, ctxsep)))
219 const int index= the_name.length();
220 the_name.length (index+1);
221 ACE_TCHAR *kind = const_cast<ACE_TCHAR*> (ACE_OS::strchr (cp, kindsep));
222 if (kind)
224 *kind = '\0';
225 the_name[index].kind= CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(++kind));
227 the_name[index].id = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(cp));
228 name = 0; // way strtok works
231 // Now attempt the (re)bind
234 if (!ior)
235 obj= root_nc->bind_new_context (the_name);
236 else if (context)
238 CosNaming::NamingContext_var this_nc =
239 CosNaming::NamingContext::_narrow (obj.in ());
241 if (rebind)
242 root_nc->rebind_context (the_name, this_nc.in ());
243 else
244 root_nc->bind_context (the_name, this_nc.in ());
246 else if (rebind)
247 root_nc->rebind (the_name, obj.in ());
248 else
249 root_nc->bind (the_name, obj.in ());
250 if (!quiet)
252 unsigned int index;
253 for (index= 0u; index < the_name.length()-1u; ++index)
255 if (the_name[index].kind && the_name[index].kind[0])
256 ACE_DEBUG ((LM_DEBUG, "Found ID: %C (Kind: %C)\n",
257 the_name[index].id.in(),
258 the_name[index].kind.in()));
259 else
260 ACE_DEBUG ((LM_DEBUG, "Found ID: %C\n",
261 the_name[index].id.in()));
263 if (!ior)
264 ACE_DEBUG ((LM_DEBUG, "Created sub-context"));
265 else if (rebind)
266 ACE_DEBUG ((LM_DEBUG, "ReBound"));
267 else
268 ACE_DEBUG ((LM_DEBUG, "Bound"));
269 ACE_DEBUG ((LM_DEBUG, " ID: %C",
270 the_name[index].id.in()));
271 if (the_name[index].kind && the_name[index].kind[0])
272 ACE_DEBUG ((LM_DEBUG, " (Kind: %C)\n",
273 the_name[index].kind.in()));
274 ACE_DEBUG ((LM_DEBUG, "\n"));
277 catch (const CosNaming::NamingContext::NotFound& nf)
279 if (CosNaming::NamingContext::missing_node != nf.why)
280 throw; // report error to outer try/catch
282 CosNaming::Name fullName (the_name);
284 index,
285 found= the_name.length() - nf.rest_of_name.length(),
286 &limit= found;
287 if (quiet)
288 index= found;
289 else
291 for (index= 0; index < found; ++index)
293 if (the_name[index].kind && the_name[index].kind[0])
294 ACE_DEBUG ((LM_DEBUG, "Found ID: %C (Kind: %C)\n",
295 the_name[index].id.in(),
296 the_name[index].kind.in()));
297 else
298 ACE_DEBUG ((LM_DEBUG, "Found ID: %C\n",
299 the_name[index].id.in()));
303 // need to create the following missing naming contexts
304 limit= fullName.length() - ((ior)?1:0);
305 while (index < limit)
307 // As soon as we limit the length, later names are gone so
308 // we have to recopy them.
309 the_name.length( index + 1 );
310 the_name[index].id= CORBA::string_dup (fullName[index].id);
311 the_name[index].kind= CORBA::string_dup (fullName[index].kind);
313 if (!quiet)
315 if (the_name[index].kind && the_name[index].kind[0])
316 ACE_DEBUG ((LM_DEBUG, "Creating sub-context ID: %C (Kind: %C)\n",
317 the_name[index].id.in(),
318 the_name[index].kind.in()));
319 else
320 ACE_DEBUG ((LM_DEBUG, "Creating sub-context ID: %C\n",
321 the_name[index].id.in()));
324 CosNaming::NamingContext_var this_nc =
325 root_nc->bind_new_context (the_name);
326 // report error to outer try/catch
327 ++index;
330 if (ior)
332 // As soon as we limit the length, later names are gone so
333 // we have to recopy them.
334 the_name.length( index + 1 );
335 the_name[index].id= CORBA::string_dup (fullName[index].id);
336 the_name[index].kind= CORBA::string_dup (fullName[index].kind);
337 if (!quiet)
339 if (rebind)
340 ACE_DEBUG ((LM_DEBUG, "Re"));
341 ACE_DEBUG ((LM_DEBUG, "Binding ID: %C",
342 the_name[index].id.in()));
343 if (the_name[index].kind && the_name[index].kind[0])
344 ACE_DEBUG ((LM_DEBUG, " (Kind: %C)\n",
345 the_name[index].kind.in()));
346 ACE_DEBUG ((LM_DEBUG, "\n"));
348 // Now re-attempt the (re)bind
349 if (context)
351 CosNaming::NamingContext_var this_nc =
352 CosNaming::NamingContext::_narrow (obj.in ());
353 // This is not a naming context, report to outer
355 if (rebind)
356 root_nc->rebind_context (the_name, this_nc.in ());
357 else
358 root_nc->bind_context (the_name, this_nc.in ());
360 else if (rebind)
361 root_nc->rebind (the_name, obj.in ());
362 else
363 root_nc->bind (the_name, obj.in ());
367 catch (const CosNaming::NamingContext::NotFound& nf)
369 unsigned int index;
370 ACE_DEBUG ((LM_DEBUG, "\nError:\n"));
371 for (index= 0u; index < the_name.length()-nf.rest_of_name.length(); ++index)
373 if (the_name[index].kind && the_name[index].kind[0])
374 ACE_DEBUG ((LM_DEBUG, "ID: %C (Kind: %C)\n",
375 the_name[index].id.in(),
376 the_name[index].kind.in()));
377 else
378 ACE_DEBUG ((LM_DEBUG, "ID: %C\n",
379 the_name[index].id.in()));
381 const char *why= "Unknown reason";
382 switch (nf.why)
384 case CosNaming::NamingContext::missing_node:
385 why= "\nThe following node is missing";
386 break;
387 case CosNaming::NamingContext::not_context:
388 why= "\nThe following is a final object binding, not a naming context";
389 break;
390 case CosNaming::NamingContext::not_object:
391 why= "\nThe following is a naming context, not a final object binding";
392 break;
394 nf._tao_print_exception (why);
395 for (index= 0u; index < nf.rest_of_name.length(); ++index)
397 if (nf.rest_of_name[index].kind && nf.rest_of_name[index].kind[0])
398 ACE_DEBUG ((LM_DEBUG, "ID: %C (Kind: %C)\n",
399 nf.rest_of_name[index].id.in(),
400 nf.rest_of_name[index].kind.in()));
401 else
402 ACE_DEBUG ((LM_DEBUG, "ID: %C\n",
403 nf.rest_of_name[index].id.in()));
405 ++err;
407 catch (const CORBA::Exception& ex)
409 ACE_DEBUG ((LM_DEBUG, "\nError:\n"));
410 ex._tao_print_exception ("Exception in nsadd");
411 ++err;
416 orb->destroy ();
418 catch (const CORBA::Exception& ex)
420 ACE_DEBUG ((LM_DEBUG, "\nError:\n"));
421 ex._tao_print_exception ("Exception in while shutting down");
422 ++err;
424 return err;