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