OmniEvents
|
00001 // -*- Mode: C++; -*- 00002 // Package : omniEvents 00003 // naming.cc Created : 1/10/99 00004 // Author : Paul Nader (pwn) 00005 // 00006 // Copyright (C) 1998 Paul Nader, 2003-2005 Alex Tingle. 00007 // 00008 // This file is part of the omniEvents application. 00009 // 00010 // omniEvents is free software; you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public 00012 // License as published by the Free Software Foundation; either 00013 // version 2.1 of the License, or (at your option) any later version. 00014 // 00015 // omniEvents is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 // 00024 // Description: 00025 // 00026 // naming Service Utility functions. 00027 // 00028 00029 /* 00030 $Log: naming.cc,v $ 00031 Revision 1.8.2.2 2005/05/10 14:28:11 alextingle 00032 Updated copyrights to 2005. 00033 00034 Revision 1.8.2.1 2005/04/27 20:49:31 alextingle 00035 Merge across changes from HEAD branch (see CHANGES_262. Change version number ready for release 2.6.2. 00036 00037 Revision 1.9 2005/04/13 14:04:02 alextingle 00038 Fixed bug in str2name() naming.cc, that causes a SEGV on HP-UX. 00039 00040 Revision 1.8 2004/10/08 14:27:59 alextingle 00041 Changed local variable initialisation style back to using '=' in order to please MS VC++. 00042 00043 Revision 1.7 2004/09/25 23:12:28 alextingle 00044 New method: Orb::reportObjectFailure() - flags unexpected failures at a higher 00045 priority than normal non-fatal exceptions. 00046 00047 New macro: NP_MINORSTRING() - a safe interface to 00048 CORBA::SystemException::NP_minorString() that returns "??" when there is no 00049 mapping for the exception's minor code. 00050 00051 Revision 1.6 2004/08/04 08:13:44 alextingle 00052 Unix daemon & Windows service now both working. Accessed through interface class Daemon (in daemon.h). 00053 00054 Revision 1.5 2004/07/26 21:17:49 alextingle 00055 Added missing #include <string> 00056 00057 Revision 1.4 2004/07/26 16:22:25 alextingle 00058 New method: str2name() parses a stringified naming service name info a CosNaming::Name. 00059 00060 Revision 1.3 2004/07/02 15:20:39 alextingle 00061 Added daemonization, syslog & pidfile support on Unix. 00062 Corrected trace levels for consistency with omniORB. 00063 00064 Revision 1.2 2004/04/21 10:01:42 alextingle 00065 Removed unused code. Now silently fails if the Orb has no naming service ref. 00066 00067 Revision 1.1 2003/12/21 16:19:49 alextingle 00068 Moved into 'src' directory as part of the change to POA implementation. 00069 00070 Revision 1.3 2003/12/01 09:03:13 alextingle 00071 Now reports more specific exceptions (only with omniORB4). 00072 00073 Revision 1.2 2003/11/03 22:45:31 alextingle 00074 Removed all platform specific switches. Now uses autoconf, config.h. 00075 00076 Revision 1.1.1.1 2002/09/25 19:00:35 shamus13 00077 Import of OmniEvents source tree from release 2.1.1 00078 00079 Revision 1.3 2000/09/26 08:44:58 naderp 00080 Added stdlib.h include for exit function. 00081 00082 Revision 1.2 2000/09/04 03:45:52 naderp 00083 Changed headers. 00084 00085 Revision 1.1 1999/11/01 17:00:16 naderp 00086 Initial revision 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> // for exit 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 // If there is no naming service, then ignore this call. 00156 if(CORBA::is_nil(namingContext)) 00157 return 1; 00158 00159 try 00160 { 00161 00162 CosNaming::Name n; 00163 n.length(1); 00164 // Drill down through contexts. 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 // One of the context names is already bound to an object. Bail out! 00178 if(CORBA::is_nil(namingContext)) 00179 return 2; 00180 } 00181 // Bind the object 00182 n[0]=name[name.length()-1]; 00183 try 00184 { 00185 namingContext->bind(n,obj); 00186 } 00187 catch(CosNaming::NamingContext::AlreadyBound& ex) 00188 { 00189 // overwrite previously bound object 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 }