Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

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

Generated on Fri Nov 19 17:42:20 2004 for OmniEvents by doxygen1.2.15