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 #include "naming.h"
00082
00083 #include <string>
00084
00085 #ifdef HAVE_IOMANIP
00086 # include <iomanip>
00087 #else
00088 # include <iomanip.h>
00089 #endif
00090
00091 #ifdef HAVE_STDLIB_H
00092 # include <stdlib.h>
00093 #endif
00094
00095 ostream& operator<<(ostream& os, const CosNaming::Name &n)
00096 {
00097 for(CORBA::ULong i=0; i<n.length(); i++)
00098 {
00099 os<<"/"<<n[i].id.in();
00100 const char* kind =n[i].kind.in();
00101 if(kind && kind[0])
00102 os<<"."<<kind;
00103 }
00104 return os;
00105 }
00106
00107
00108 CosNaming::Name str2name(const char* namestr)
00109 {
00110 CosNaming::Name name;
00111 CORBA::ULong nameLen=0;
00112 name.length(nameLen);
00113
00114 string n =namestr;
00115 string::size_type pos=0;
00116 char last='/';
00117 while(true)
00118 {
00119 pos=n.find_first_not_of("/.",pos);
00120 if(pos==string::npos) break;
00121 string::size_type sep=n.find_first_of("/.",pos);
00122 string piece =n.substr(pos,sep-pos);
00123 if(last=='/')
00124 {
00125 name.length(++nameLen);
00126 name[nameLen-1].id=CORBA::string_dup(piece.c_str());
00127 }
00128 else
00129 {
00130 name[nameLen-1].kind=CORBA::string_dup(piece.c_str());
00131 }
00132 pos=sep;
00133 last=n[sep];
00134 }
00135 return name;
00136 }
00137
00138
00139 int bindName2Object(
00140 CosNaming::NamingContext_ptr namingContext,
00141 const CosNaming::Name& name,
00142 CORBA::Object_ptr obj
00143 )
00144 {
00145
00146 if(CORBA::is_nil(namingContext))
00147 return 1;
00148
00149 try
00150 {
00151
00152 CosNaming::Name n;
00153 n.length(1);
00154
00155 for(CORBA::ULong i=0; i<(name.length()-1); ++i)
00156 {
00157 n[0]=name[i];
00158 try
00159 {
00160 namingContext=namingContext->bind_new_context(n);
00161 }
00162 catch(CosNaming::NamingContext::AlreadyBound&)
00163 {
00164 CORBA::Object_var obj2 =namingContext->resolve(n);
00165 namingContext=CosNaming::NamingContext::_narrow(obj2);
00166 }
00167
00168 if(CORBA::is_nil(namingContext))
00169 return 2;
00170 }
00171
00172 n[0]=name[name.length()-1];
00173 try
00174 {
00175 namingContext->bind(n,obj);
00176 }
00177 catch(CosNaming::NamingContext::AlreadyBound& ex)
00178 {
00179
00180 namingContext->rebind(n,obj);
00181 }
00182 return 0;
00183
00184 }
00185 catch (CORBA::COMM_FAILURE& ex)
00186 {
00187 cerr << "Caught system exception COMM_FAILURE, unable to contact the "
00188 << "naming service." << endl;
00189 }
00190 catch (omniORB::fatalException& ex)
00191 {
00192 cerr << "Caught omniORB fatal exception binding " << name << endl;
00193 throw;
00194 }
00195 catch (CORBA::SystemException& ex)
00196 {
00197 const char* exName =NULL;
00198 const char* exMinor =NULL;
00199 #ifdef HAVE_OMNIORB4
00200 exName =ex.NP_minorString();
00201 exMinor=ex.NP_minorString();
00202 #endif
00203 cerr<<"System exception binding "<<name;
00204 if(exName)
00205 cerr<<": "<<exName;
00206 if(exMinor)
00207 cerr<<" ("<<exMinor<<")";
00208 cerr<<endl;
00209 }
00210 catch (CORBA::Exception& ex)
00211 {
00212 cerr<<"CORBA exception binding "<<name
00213 #ifdef HAVE_OMNIORB4
00214 <<": "<<ex._name()
00215 #endif
00216 << endl;
00217 }
00218 ::exit(1);
00219 }