00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 #include "naming.h"
00091
00092 #include <string>
00093
00094 #ifdef HAVE_IOMANIP
00095 # include <iomanip>
00096 #else
00097 # include <iomanip.h>
00098 #endif
00099
00100 #ifdef HAVE_STDLIB_H
00101 # include <stdlib.h>
00102 #endif
00103
00104 ostream& operator<<(ostream& os, const CosNaming::Name &n)
00105 {
00106 for(CORBA::ULong i=0; i<n.length(); i++)
00107 {
00108 os<<"/"<<n[i].id.in();
00109 const char* kind =n[i].kind.in();
00110 if(kind && kind[0])
00111 os<<"."<<kind;
00112 }
00113 return os;
00114 }
00115
00116
00117 CosNaming::Name str2name(const char* namestr)
00118 {
00119 CosNaming::Name name;
00120 CORBA::ULong nameLen=0;
00121 name.length(nameLen);
00122
00123 string n =namestr;
00124 string::size_type pos=0;
00125 char last='/';
00126 while(true)
00127 {
00128 pos=n.find_first_not_of("/.",pos);
00129 if(string::npos==pos) break;
00130 string::size_type sep =n.find_first_of("/.",pos);
00131 string piece =n.substr(pos, (string::npos==sep? sep: sep-pos) );
00132 if(last=='/')
00133 {
00134 name.length(++nameLen);
00135 name[nameLen-1].id=CORBA::string_dup(piece.c_str());
00136 }
00137 else
00138 {
00139 name[nameLen-1].kind=CORBA::string_dup(piece.c_str());
00140 }
00141 if(string::npos==sep) break;
00142 pos=sep;
00143 last=n[sep];
00144 }
00145 return name;
00146 }
00147
00148
00149 int bindName2Object(
00150 CosNaming::NamingContext_ptr namingContext,
00151 const CosNaming::Name& name,
00152 CORBA::Object_ptr obj
00153 )
00154 {
00155
00156 if(CORBA::is_nil(namingContext))
00157 return 1;
00158
00159 try
00160 {
00161
00162 CosNaming::Name n;
00163 n.length(1);
00164
00165 for(CORBA::ULong i=0; i<(name.length()-1); ++i)
00166 {
00167 n[0]=name[i];
00168 try
00169 {
00170 namingContext=namingContext->bind_new_context(n);
00171 }
00172 catch(CosNaming::NamingContext::AlreadyBound&)
00173 {
00174 CORBA::Object_var obj2 =namingContext->resolve(n);
00175 namingContext=CosNaming::NamingContext::_narrow(obj2);
00176 }
00177
00178 if(CORBA::is_nil(namingContext))
00179 return 2;
00180 }
00181
00182 n[0]=name[name.length()-1];
00183 try
00184 {
00185 namingContext->bind(n,obj);
00186 }
00187 catch(CosNaming::NamingContext::AlreadyBound& ex)
00188 {
00189
00190 namingContext->rebind(n,obj);
00191 }
00192 return 0;
00193
00194 }
00195 catch (CORBA::COMM_FAILURE& ex)
00196 {
00197 cerr << "Caught system exception COMM_FAILURE, unable to contact the "
00198 << "naming service." << endl;
00199 }
00200 catch (omniORB::fatalException& ex)
00201 {
00202 cerr << "Caught omniORB fatal exception binding " << name << endl;
00203 throw;
00204 }
00205 catch (CORBA::SystemException& ex)
00206 {
00207 const char* exName =NULL;
00208 const char* exMinor =NULL;
00209 #ifdef HAVE_OMNIORB4
00210 exName =ex.NP_minorString();
00211 exMinor=ex.NP_minorString();
00212 #endif
00213 cerr<<"System exception binding "<<name;
00214 if(exName)
00215 cerr<<": "<<exName;
00216 if(exMinor)
00217 cerr<<" ("<<exMinor<<")";
00218 cerr<<endl;
00219 }
00220 catch (CORBA::Exception& ex)
00221 {
00222 cerr<<"CORBA exception binding "<<name
00223 #ifdef HAVE_OMNIORB4
00224 <<": "<<ex._name()
00225 #endif
00226 << endl;
00227 }
00228 ::exit(1);
00229 }