client/protocol/corba/NameServerControl.h

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      NameServerControl.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Helper to handle the NameServer stuff (bind, unbind, resolve ...)
00006 Author:    <Michele Laghi> laghi@swissinfo.org
00007 -----------------------------------------------------------------------------*/
00008 
00009 #ifndef _CLIENT_PROTOCOL_CORBA_NAMESERVERCONTROL_H
00010 #define _CLIENT_PROTOCOL_CORBA_NAMESERVERCONTROL_H
00011 
00012 
00013 
00014 #include <vector>
00015 #ifndef STLPORT // Is automatically set by STLport if used, problem is on Linux/g++: STLport-4.5.1/stlport/stl/_algo.h:180: declaration of `operator MICO_LongDouble' as non-function
00016 #  include <algorithm>
00017 #endif
00018 
00019 #include <client/protocol/corba/CompatibleCorba.h> // client side headers
00020 #include COSNAMING
00021 
00022 #include <util/StringStripper2.h>
00023 #include <util/XmlBlasterException.h>
00024 
00025 typedef std::vector<std::string> ListType;
00026 
00027 namespace org { namespace xmlBlaster {
00028 namespace client { namespace protocol { namespace corba {
00029 
00036    class Dll_Export NameServerControl {
00037 
00038    private:
00039 
00040    std::string me() {
00041       return "NameServerControl";
00042    }
00043 
00044    CosNaming::NamingContext_var namingContext_;
00045    org::xmlBlaster::util::StringStripper2              stripper_;
00046 
00047    public:
00055    NameServerControl(CORBA::ORB_ptr orb, std::string sep1="/", std::string sep2=".") : stripper_(sep1,sep2) {
00056       // Get naming service
00057       CORBA::Object_var obj; //  = (CORBA::Object_var)0;
00058       try {
00059          obj = orb->resolve_initial_references("NameService");
00060       }
00061 
00062       catch(const CORBA::ORB::InvalidName ex) {
00063 #        ifndef XMLBLASTER_OMNIORB
00064          std::cerr << "Thrown invalid name exception: " << ex << std::endl;
00065 #        endif
00066          std::string txt = me() + ".NameServerControl()";
00067          std::string msg = "can't resolve the NameService";
00068          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00069       }
00070 
00071       if(CORBA::is_nil(obj.in())) {
00072          std::string txt = me() + ".NameServerControl()";
00073          std::string msg = "NameService in not a nil reference";
00074          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00075       }
00076 
00077       try {
00078          namingContext_ = CosNaming::NamingContext::_narrow(obj.in());
00079       }
00080       catch (const CORBA::Exception & ex) {
00081          std::string msg="Corba Exception " + to_string(ex);
00082          std::string txt = me() + ".NameServerControl()";
00083          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00084       }
00085       
00086       if(CORBA::is_nil(namingContext_.in())) {
00087          std::string txt = me() + ".NameServerControl()";
00088          std::string msg = "NameService is not a NamingContext reference";
00089          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00090       }
00091    }
00092 
00097    NameServerControl(CosNaming::NamingContext_var &relativeContext, std::string sep1="/", std::string sep2=".") :
00098       namingContext_(relativeContext), stripper_(sep1,sep2) {
00099    }
00100 
00107    CORBA::Object_ptr resolve(const std::string &name) {
00108       std::vector<std::pair<std::string,std::string> > nameVector = stripper_.strip(name);
00109       CosNaming::Name objectName;
00110       objectName.length(nameVector.size());
00111       for (std::string::size_type i=0; i < nameVector.size(); i++) {
00112          objectName[i].id   = 
00113             CORBA::string_dup(nameVector[i].first.c_str());
00114          objectName[i].kind = 
00115             CORBA::string_dup(nameVector[i].second.c_str());
00116       }
00117       return resolve(objectName);
00118    }
00119       
00128    CORBA::Object_ptr resolve(CosNaming::Name &nameComponent) {
00129       try {
00130          CORBA::Object_ptr obj = namingContext_->resolve(nameComponent);
00131          if (CORBA::is_nil(obj)) {
00132             std::string txt = "Can't resolve CORBA NameService entry for '" +
00133                            NameServerControl::getString(nameComponent) +"', entry is nil";
00134             //log_.error(me() + ".NoAuthService", txt);
00135             throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", 
00136                                        "client", me(), "en", txt);
00137          }
00138          return obj;
00139       }
00140       catch(const CosNaming::NamingContext::NotFound& ex) {
00141          std::string txt = me() + ".resolve()";
00142          std::string msg = "CORBA CosNaming::NamingContext::NotFound - name not found exception '" + NameServerControl::getString(nameComponent) + "': " + to_string(ex);
00143          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00144       }
00145       catch(const CosNaming::NamingContext::CannotProceed& ex) {
00146          std::string txt = me() + ".bind()";
00147          std::string msg = "CORBA CosNaming::NamingContext::CannotProceed '" + NameServerControl::getString(nameComponent) + "': " + to_string(ex);
00148          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00149       }
00150       catch(const CosNaming::NamingContext::InvalidName & ex) {
00151          std::string txt = me() + ".bind()";
00152          std::string msg = "CORBA CosNaming::NamingContext::InvalidName '" + NameServerControl::getString(nameComponent) + "': " + to_string(ex);
00153          throw org::xmlBlaster::util::XmlBlasterException("communication.noConnection", "client", txt, "en", msg);
00154       }
00155    }
00156 
00163    CORBA::Object_ptr resolve(const std::string &id, const std::string &kind) {
00164       CosNaming::Name objectName;
00165       objectName.length(1);
00166       objectName[0].id =  CORBA::string_dup(id.c_str());
00167       objectName[0].kind = CORBA::string_dup(kind.c_str());
00168       return resolve(objectName);
00169    }
00170 
00175    CosNaming::NamingContext_ptr getNamingService() {
00176       return CosNaming::NamingContext::_duplicate(static_cast<CosNaming::NamingContext_ptr>(namingContext_.in()));
00177    }
00178 
00184    static std::string getString(CosNaming::Name nameComponent, std::string sep1="/", std::string sep2=".") {
00185       std::string ret = "";
00186       for(CORBA::ULong i=0; i<nameComponent.length(); i++) {
00187          if (i > 0) {
00188             ret += sep1;
00189          }
00190          ret += std::string(nameComponent[i].id) + 
00191                      ((nameComponent[i].kind != 0 && *nameComponent[i].kind != 0) ?
00192                      std::string(sep2) + std::string(nameComponent[i].kind) : std::string(""));
00193       }
00194       return ret;
00195    }
00196 
00204    static std::string getString(const std::string &id, const std::string &kind, std::string sep2=".") {
00205       return id + ((kind.size() > 0) ? std::string(sep2) + kind : std::string(""));
00206    }
00207 
00208 }; // class NameServerControl
00209 
00210 }}}}} // namespace
00211 
00212 #endif
00213