- CosNaming::NamingContext_var rootContext;
-
- try {
- // Obtain a reference to the root context of the Name service:
- CORBA::Object_var obj;
- obj = orb->resolve_initial_references("NameService");
-
- // Narrow the reference returned.
- rootContext = CosNaming::NamingContext::_narrow(obj);
- if( CORBA::is_nil(rootContext) ) {
- DEBTRACE("Failed to narrow the root naming context.");
- return 0;
- }
- }
- catch(CORBA::ORB::InvalidName& ex) {
- // This should not happen!
- DEBTRACE("Service required is invalid [does not exist]." );
- return 0;
- }
-
- try {
- // Bind a context called "test" to the root context:
-
- CosNaming::Name contextName;
- contextName.length(1);
- contextName[0].id = (const char*) "test"; // string copied
- contextName[0].kind = (const char*) "my_context"; // string copied
- // Note on kind: The kind field is used to indicate the type
- // of the object. This is to avoid conventions such as that used
- // by files (name.type -- e.g. test.ps = postscript etc.)
-
- CosNaming::NamingContext_var testContext;
- try {
- // Bind the context to root.
- testContext = rootContext->bind_new_context(contextName);
- }
- catch(CosNaming::NamingContext::AlreadyBound& ex) {
- // If the context already exists, this exception will be raised.
- // In this case, just resolve the name and assign testContext
- // to the object returned:
- CORBA::Object_var obj;
- obj = rootContext->resolve(contextName);
- testContext = CosNaming::NamingContext::_narrow(obj);
- if( CORBA::is_nil(testContext) ) {
- DEBTRACE("Failed to narrow naming context.");
- return 0;
- }
- }
-
- // Bind objref with name Echo to the testContext:
- CosNaming::Name objectName;
- objectName.length(1);
- objectName[0].id = name; // string copied
- objectName[0].kind = (const char*) "Object"; // string copied
-
- try {
- testContext->bind(objectName, objref);
- }
- catch(CosNaming::NamingContext::AlreadyBound& ex) {
- testContext->rebind(objectName, objref);
- }
- // Note: Using rebind() will overwrite any Object previously bound
- // to /test/Echo with obj.
- // Alternatively, bind() can be used, which will raise a
- // CosNaming::NamingContext::AlreadyBound exception if the name
- // supplied is already bound to an object.
-
- // Amendment: When using OrbixNames, it is necessary to first try bind
- // and then rebind, as rebind on it's own will throw a NotFoundexception if
- // the Name has not already been bound. [This is incorrect behaviour -
- // it should just bind].
- }
- catch(CORBA::COMM_FAILURE& ex) {
- DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
- << "naming service.");
- return 0;
- }
- catch(CORBA::SystemException&) {
- DEBTRACE("Caught a CORBA::SystemException while using the naming service.");
- return 0;
- }
-