00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream.h>
00021 #include "NameServer.h"
00022
00028 using namespace utils;
00029
00030 CORBA::Object_ptr NameServer::get_object( const CORBA::ORB_ptr orb, const utils::URI &uri) throw(Exception) {
00031 string str_contextname = uri.nameServerURL();
00032 string str_servicename = uri.serviceName();
00033 const char *contextname = str_contextname.c_str();
00034 const char *servicename = str_servicename.c_str();
00035 CosNaming::NamingContextExt_var nc;
00036
00037 try {
00038 CORBA::Object_var obj = orb->string_to_object( contextname );
00039 nc = CosNaming::NamingContextExt::_narrow(obj);
00040 if( CORBA::is_nil(nc) ) {
00041 cerr << "Failed to narrow to naming context";
00042 throw Exception();
00043 };
00044 CORBA::Object_ptr return_object = nc->resolve_str( servicename );
00045 return return_object;
00046 }
00047 catch(CosNaming::NamingContext::InvalidName &) {
00048 cout << "invalid name\n";
00049 throw InvalidNameException();
00050 }
00051 catch(CosNaming::NamingContext::NotFound &) {
00052 cout << "not found\n";
00053 throw NotFoundException();
00054 }
00055 catch(...) {
00056 cout << "other\n";
00057 throw Exception();
00058 };
00059 };
00060
00061 CORBA::Boolean NameServer::unbindObject(const char* context, const char* name, const char* kind, const CORBA::ORB_ptr orb) {
00062 CosNaming::NamingContext_var rootContext;
00063 try {
00064
00065 CORBA::Object_var obj;
00066 obj = orb->resolve_initial_references("NameService");
00067
00068
00069 rootContext = CosNaming::NamingContext::_narrow(obj);
00070 if( CORBA::is_nil(rootContext) ) {
00071 cerr << "Failed to narrow the root naming context." << endl;
00072 return false;
00073 }
00074 }
00075 catch(CORBA::ORB::InvalidName& ex) {
00076
00077 cerr << "Service required is invalid [does not exist]." << endl;
00078 return false;
00079 }
00080
00081 try {
00082
00083 CosNaming::Name contextName;
00084 contextName.length(1);
00085 contextName[0].id = (const char*) context;
00086 contextName[0].kind = (const char*) "utils";
00087
00088 CORBA::Object_var obj = rootContext->resolve(contextName);
00089 CosNaming::NamingContext_var testContext = CosNaming::NamingContext::_narrow(obj);
00090 if( CORBA::is_nil(testContext) ) {
00091 cerr << "Failed to narrow naming context." << endl;
00092 return false;
00093 }
00094
00095
00096 CosNaming::Name objectName;
00097 objectName.length(1);
00098 objectName[0].id = (const char*) name;
00099 objectName[0].kind = (const char*) kind;
00100
00101 try {
00102 testContext->unbind(objectName);
00103 }
00104 catch(CosNaming::NamingContext::NotFound& ex) {
00105 cerr << "The object in not registered in the selected context." << endl;
00106 }
00107
00108
00109
00110
00111
00112 }
00113 catch(CORBA::COMM_FAILURE& ex) {
00114 cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
00115 << "naming service." << endl;
00116 return false;
00117 }
00118 catch(CORBA::SystemException&) {
00119 cerr << "Caught a CORBA::SystemException while using the naming service." << endl;
00120 return false;
00121 }
00122 return true;
00123 }
00124
00125 CORBA::Boolean NameServer::bindObjectToName(const char* context, const char* name, const char* kind, const CORBA::ORB_ptr orb, const CORBA::Object_ptr objref)
00126 {
00127 CosNaming::NamingContext_var rootContext;
00128 try {
00129
00130 CORBA::Object_var obj;
00131 obj = orb->resolve_initial_references("NameService");
00132
00133
00134 rootContext = CosNaming::NamingContext::_narrow(obj);
00135 if( CORBA::is_nil(rootContext) ) {
00136 cerr << "Failed to narrow the root naming context." << endl;
00137 return false;
00138 }
00139 }
00140 catch(CORBA::ORB::InvalidName& ex) {
00141
00142 cerr << "Service required is invalid [does not exist]." << endl;
00143 return false;
00144 }
00145
00146 try {
00147
00148 CosNaming::Name contextName;
00149 contextName.length(1);
00150 contextName[0].id = (const char*) context;
00151 contextName[0].kind = (const char*) PROJECT_NAME;
00152
00153 CosNaming::NamingContext_var testContext;
00154 try {
00155
00156 testContext = rootContext->bind_new_context(contextName);
00157 }
00158 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00159
00160
00161
00162 CORBA::Object_var obj;
00163 obj = rootContext->resolve(contextName);
00164 testContext = CosNaming::NamingContext::_narrow(obj);
00165 if( CORBA::is_nil(testContext) ) {
00166 cerr << "Failed to narrow naming context." << endl;
00167 return false;
00168 }
00169 }
00170
00171
00172 CosNaming::Name objectName;
00173 objectName.length(1);
00174 objectName[0].id = (const char*) name;
00175 objectName[0].kind = (const char*) kind;
00176
00177 try {
00178 testContext->bind(objectName, objref);
00179 }
00180 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00181 testContext->rebind(objectName, objref);
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 }
00194 catch(CORBA::COMM_FAILURE& ex) {
00195 cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
00196 << "naming service." << endl;
00197 return false;
00198 }
00199 catch(CORBA::SystemException&) {
00200 cerr << "Caught a CORBA::SystemException while using the naming service." << endl;
00201 return false;
00202 }
00203 return true;
00204 }