naming.cc

Go to the documentation of this file.
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.
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.1  2005/04/27 20:49:31  alextingle
00032   Merge across changes from HEAD branch (see CHANGES_262. Change version number ready for release 2.6.2.
00033 
00034   Revision 1.9  2005/04/13 14:04:02  alextingle
00035   Fixed bug in str2name() naming.cc, that causes a SEGV on HP-UX.
00036 
00037   Revision 1.8  2004/10/08 14:27:59  alextingle
00038   Changed local variable initialisation style back to using '=' in order to please MS VC++.
00039 
00040   Revision 1.7  2004/09/25 23:12:28  alextingle
00041   New method: Orb::reportObjectFailure() - flags unexpected failures at a higher
00042   priority than normal non-fatal exceptions.
00043 
00044   New macro: NP_MINORSTRING() - a safe interface to
00045   CORBA::SystemException::NP_minorString() that returns "??" when there is no
00046   mapping for the exception's minor code.
00047 
00048   Revision 1.6  2004/08/04 08:13:44  alextingle
00049   Unix daemon & Windows service now both working. Accessed through interface class Daemon (in daemon.h).
00050 
00051   Revision 1.5  2004/07/26 21:17:49  alextingle
00052   Added missing #include <string>
00053 
00054   Revision 1.4  2004/07/26 16:22:25  alextingle
00055   New method: str2name() parses a stringified naming service name info a CosNaming::Name.
00056 
00057   Revision 1.3  2004/07/02 15:20:39  alextingle
00058   Added daemonization, syslog & pidfile support on Unix.
00059   Corrected trace levels for consistency with omniORB.
00060 
00061   Revision 1.2  2004/04/21 10:01:42  alextingle
00062   Removed unused code. Now silently fails if the Orb has no naming service ref.
00063 
00064   Revision 1.1  2003/12/21 16:19:49  alextingle
00065   Moved into 'src' directory as part of the change to POA implementation.
00066 
00067   Revision 1.3  2003/12/01 09:03:13  alextingle
00068   Now reports more specific exceptions (only with omniORB4).
00069 
00070   Revision 1.2  2003/11/03 22:45:31  alextingle
00071   Removed all platform specific switches. Now uses autoconf, config.h.
00072 
00073   Revision 1.1.1.1  2002/09/25 19:00:35  shamus13
00074   Import of OmniEvents source tree from release 2.1.1
00075 
00076   Revision 1.3  2000/09/26 08:44:58  naderp
00077   Added stdlib.h include for exit function.
00078 
00079   Revision 1.2  2000/09/04 03:45:52  naderp
00080   Changed headers.
00081 
00082   Revision 1.1  1999/11/01 17:00:16  naderp
00083   Initial revision
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> // for exit
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   // If there is no naming service, then ignore this call.
00153   if(CORBA::is_nil(namingContext))
00154       return 1;
00155 
00156   try
00157   {
00158 
00159       CosNaming::Name n;
00160       n.length(1);
00161       // Drill down through contexts.
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         // One of the context names is already bound to an object. Bail out!
00175         if(CORBA::is_nil(namingContext))
00176             return 2;
00177       }
00178       // Bind the object
00179       n[0]=name[name.length()-1];
00180       try
00181       {
00182         namingContext->bind(n,obj);
00183       }
00184       catch(CosNaming::NamingContext::AlreadyBound& ex)
00185       {
00186         // overwrite previously bound object
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 }

Generated on Mon Jan 9 03:52:13 2006 for OmniEvents by  doxygen 1.4.6