1 //=============================================================================
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>
10 //=============================================================================
12 #include "orbsvcs/CosNamingC.h"
13 #include "ace/Log_Msg.h"
14 #include "ace/OS_NS_string.h"
20 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
22 CosNaming::Name
the_name (0);
24 const ACE_TCHAR
*ior
= 0;
31 orb
= CORBA::ORB_init (argc
, argv
);
33 // Scan through the command line options
40 *const pname
= argv
[0];
41 const ACE_TCHAR
*nameService
= 0;
42 ACE_TCHAR kindsep
= ACE_TEXT('.');
43 ACE_TCHAR ctxsep
[] = ACE_TEXT("/");
51 if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--ns")))
56 "Error: --ns requires an argument\n"));
65 "Error: more than one --ns.\n"));
72 else if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT("--quiet")))
76 else if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--name")))
81 "Error: more than one --name\n"));
87 "Error: --name requires an argument\n"));
93 else if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--ctxsep")))
98 "Error: --ctxsep requires a character\n"));
101 else if (1 != ACE_OS::strlen(*++argv
))
103 ACE_DEBUG ((LM_DEBUG
,
104 "Error: --ctxsep takes a single character (not %s)\n", *argv
));
108 ctxsep
[0] = (*argv
)[0];
110 else if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--kindsep")))
114 ACE_DEBUG ((LM_DEBUG
,
115 "Error: --kindsep requires a character\n"));
118 else if (1 != ACE_OS::strlen(*++argv
))
120 ACE_DEBUG ((LM_DEBUG
,
121 "Error: --kindsep takes a single character (not %s)\n", *argv
));
125 kindsep
= (*argv
)[0];
127 else if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--ior")))
131 ACE_DEBUG ((LM_DEBUG
,
132 "Error: ior already specified\n"));
137 ACE_DEBUG ((LM_DEBUG
,
138 "Error: --ior requires an IOR\n"));
144 else if ((0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--ctx")))
145 ||(0 == ACE_OS::strcmp(*argv
, ACE_TEXT ("--newcontext"))))
149 else if (0 == ACE_OS::strcmp (*argv
, ACE_TEXT ("--rebind")))
155 ACE_DEBUG ((LM_DEBUG
,
156 "Unknown option %s\n", *argv
));
162 if (failed
|| !name
|| (!ior
&& !context
))
164 ACE_DEBUG ((LM_DEBUG
,
166 " %s --name <name> --ior <ior>\n"
167 " %s --name <name> --ctx\n"
168 " %s --name <name> --ior <ior> --ctx\n"
172 " --ctxsep <character>\n"
173 " --kindsep <character>\n"
174 " --newcontext {same as --ctx}\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"
186 pname
, pname
, pname
));
191 // Contact the name service
192 CORBA::Object_var nc_obj
;
194 nc_obj
= orb
->string_to_object (nameService
);
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"));
208 // Get the object reference from the IOR string given
209 CORBA::Object_var obj
;
212 obj
= orb
->string_to_object (ior
);
215 // Assemble the name from the user string given
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
));
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
235 obj
= root_nc
->bind_new_context (the_name
);
238 CosNaming::NamingContext_var this_nc
=
239 CosNaming::NamingContext::_narrow (obj
.in ());
242 root_nc
->rebind_context (the_name
, this_nc
.in ());
244 root_nc
->bind_context (the_name
, this_nc
.in ());
247 root_nc
->rebind (the_name
, obj
.in ());
249 root_nc
->bind (the_name
, obj
.in ());
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()));
260 ACE_DEBUG ((LM_DEBUG
, "Found ID: %C\n",
261 the_name
[index
].id
.in()));
264 ACE_DEBUG ((LM_DEBUG
, "Created sub-context"));
266 ACE_DEBUG ((LM_DEBUG
, "ReBound"));
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
);
285 found
= the_name
.length() - nf
.rest_of_name
.length(),
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()));
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
);
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()));
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
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
);
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
351 CosNaming::NamingContext_var this_nc
=
352 CosNaming::NamingContext::_narrow (obj
.in ());
353 // This is not a naming context, report to outer
356 root_nc
->rebind_context (the_name
, this_nc
.in ());
358 root_nc
->bind_context (the_name
, this_nc
.in ());
361 root_nc
->rebind (the_name
, obj
.in ());
363 root_nc
->bind (the_name
, obj
.in ());
367 catch (const CosNaming::NamingContext::NotFound
& nf
)
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()));
378 ACE_DEBUG ((LM_DEBUG
, "ID: %C\n",
379 the_name
[index
].id
.in()));
381 const char *why
= "Unknown reason";
384 case CosNaming::NamingContext::missing_node
:
385 why
= "\nThe following node is missing";
387 case CosNaming::NamingContext::not_context
:
388 why
= "\nThe following is a final object binding, not a naming context";
390 case CosNaming::NamingContext::not_object
:
391 why
= "\nThe following is a naming context, not a final object binding";
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()));
402 ACE_DEBUG ((LM_DEBUG
, "ID: %C\n",
403 nf
.rest_of_name
[index
].id
.in()));
407 catch (const CORBA::Exception
& ex
)
409 ACE_DEBUG ((LM_DEBUG
, "\nError:\n"));
410 ex
._tao_print_exception ("Exception in nsadd");
418 catch (const CORBA::Exception
& ex
)
420 ACE_DEBUG ((LM_DEBUG
, "\nError:\n"));
421 ex
._tao_print_exception ("Exception in while shutting down");