Connector_Registry.cpp

Go to the documentation of this file.
00001 // $Id: Connector_Registry.cpp 76874 2007-02-02 14:12:41Z johnnyw $
00002 
00003 #include "tao/Connector_Registry.h"
00004 #include "tao/ORB_Core.h"
00005 #include "tao/Profile.h"
00006 #include "tao/Transport_Connector.h"
00007 #include "tao/Protocol_Factory.h"
00008 #include "tao/debug.h"
00009 #include "tao/ORB_Constants.h"
00010 #include "tao/CDR.h"
00011 #include "tao/SystemException.h"
00012 #include "ace/Auto_Ptr.h"
00013 
00014 #if !defined(__ACE_INLINE__)
00015 #include "tao/Connector_Registry.inl"
00016 #endif /* __ACE_INLINE__ */
00017 
00018 
00019 ACE_RCSID (tao,
00020            Connector_Registry,
00021            "$Id: Connector_Registry.cpp 76874 2007-02-02 14:12:41Z johnnyw $")
00022 
00023 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00024 
00025 TAO_Connector_Registry::TAO_Connector_Registry (void)
00026   : connectors_ (0),
00027     size_ (0)
00028 {
00029 }
00030 
00031 TAO_Connector_Registry::~TAO_Connector_Registry (void)
00032 {
00033   this->close_all ();
00034 
00035   delete [] this->connectors_;
00036 }
00037 
00038 TAO_Connector *
00039 TAO_Connector_Registry::get_connector (CORBA::ULong tag) const
00040 {
00041   const TAO_ConnectorSetIterator end = this->end ();
00042 
00043   for (TAO_ConnectorSetIterator connector = this->begin ();
00044        connector != end;
00045        ++connector)
00046     {
00047       if ((*connector)->tag () == tag)
00048         return *connector;
00049     }
00050 
00051   return 0;
00052 }
00053 
00054 int
00055 TAO_Connector_Registry::open (TAO_ORB_Core *orb_core)
00056 {
00057   TAO_ProtocolFactorySet * const pfs =
00058     orb_core->protocol_factories ();
00059 
00060   // The array containing the TAO_Connectors will never contain more
00061   // than the number of loaded protocols in the ORB core.
00062   if (this->connectors_ == 0)
00063     ACE_NEW_RETURN (this->connectors_,
00064                     TAO_Connector *[pfs->size ()],
00065                     -1);
00066 
00067   // Open one connector for each loaded protocol!
00068   const TAO_ProtocolFactorySetItor end = pfs->end ();
00069 
00070   for (TAO_ProtocolFactorySetItor factory = pfs->begin ();
00071        factory != end;
00072        ++factory)
00073     {
00074       auto_ptr <TAO_Connector> connector (
00075         (*factory)->factory ()->make_connector ());
00076 
00077       if (connector.get ())
00078         {
00079          if (connector->open (orb_core) != 0)
00080            {
00081              ACE_ERROR_RETURN ((LM_ERROR,
00082                                 ACE_TEXT ("TAO (%P|%t) - TAO_Connector_Registry")
00083                                 ACE_TEXT ("::open: unable to open connector for ")
00084                                 ACE_TEXT ("<%s>.\n"),
00085                                 ACE_TEXT_CHAR_TO_TCHAR((*factory)->protocol_name ().c_str ())),
00086                                -1);
00087            }
00088 
00089          this->connectors_[this->size_++] =
00090            connector.release ();
00091         }
00092       else
00093         return -1;
00094     }
00095 
00096   return 0;
00097 }
00098 
00099 int
00100 TAO_Connector_Registry::close_all (void)
00101 {
00102   const TAO_ConnectorSetIterator end = this->end ();
00103 
00104   for (TAO_ConnectorSetIterator i = this->begin ();
00105        i != end;
00106        ++i)
00107     {
00108       if (*i == 0)
00109         continue;
00110 
00111       (*i)->close ();
00112 
00113       delete *i;
00114     }
00115 
00116   this->size_ = 0;
00117 
00118   return 0;
00119 }
00120 
00121 int
00122 TAO_Connector_Registry::make_mprofile (const char *ior,
00123                                        TAO_MProfile &mprofile
00124                                        )
00125 {
00126   if (!ior)
00127     // Failure: Null IOR string pointer
00128     throw ::CORBA::INV_OBJREF (
00129       CORBA::SystemException::_tao_minor_code (
00130         0,
00131         EINVAL),
00132       CORBA::COMPLETED_NO);
00133 
00134   const TAO_ConnectorSetIterator first_connector = this->begin ();
00135   const TAO_ConnectorSetIterator last_connector = this->end ();
00136 
00137   for (TAO_ConnectorSetIterator connector = first_connector;
00138        connector != last_connector;
00139        ++connector)
00140     {
00141       if (*connector)
00142         {
00143           const int mp_result =
00144             (*connector)->make_mprofile (ior,
00145                                          mprofile
00146                                         );
00147 
00148           if (mp_result == 0)
00149             return 0;  // Success
00150         }
00151       else
00152         // Failure: Null pointer to connector in connector registry.
00153         throw ::CORBA::INV_OBJREF (
00154           CORBA::SystemException::_tao_minor_code (
00155             0,
00156             EINVAL),
00157           CORBA::COMPLETED_NO);
00158     }
00159 
00160   // Failure: None of the connectors were able to parse the URL style
00161   // IOR into an MProfile.
00162   throw ::CORBA::INV_OBJREF (
00163     CORBA::SystemException::_tao_minor_code (
00164       TAO_CONNECTOR_REGISTRY_NO_USABLE_PROTOCOL,
00165       0),
00166     CORBA::COMPLETED_NO);
00167 }
00168 
00169 TAO_Profile *
00170 TAO_Connector_Registry::create_profile (TAO_InputCDR &cdr)
00171 {
00172   CORBA::ULong tag = 0;
00173 
00174   // If there is an error we abort.
00175   if ((cdr >> tag) == 0)
00176     return 0;
00177 
00178   TAO_Connector *connector =
00179     this->get_connector (tag);
00180 
00181   if (connector == 0)
00182     {
00183       if (TAO_debug_level > 0)
00184         {
00185           ACE_DEBUG ((LM_DEBUG,
00186                       ACE_TEXT ("TAO (%P|%t) - TAO_Connector_Registry::")
00187                       ACE_TEXT ("create_profile: Unknown profile tag 0x%x\n"),
00188                       tag));
00189         }
00190 
00191       TAO_ORB_Core *orb_core = cdr.orb_core ();
00192       if (orb_core == 0)
00193         {
00194           orb_core = TAO_ORB_Core_instance ();
00195           if (TAO_debug_level > 0)
00196             {
00197               ACE_DEBUG ((LM_WARNING,
00198                           ACE_TEXT ("TAO (%P|%t) - TAO_Connector_Registry")
00199                           ACE_TEXT ("::create_profile: ")
00200                           ACE_TEXT ("WARNING: extracting object from ")
00201                           ACE_TEXT ("default ORB_Core\n")));
00202             }
00203         }
00204 
00205 
00206       TAO_Profile *pfile = 0;
00207       ACE_NEW_RETURN (pfile,
00208                       TAO_Unknown_Profile (tag,
00209                                            orb_core),
00210                       0);
00211       if (pfile->decode (cdr) == -1)
00212         {
00213           pfile->_decr_refcnt ();
00214           pfile = 0;
00215         }
00216 
00217       return pfile;
00218     }
00219 
00220   // OK, we've got a known profile.  It's going to be encapsulated
00221   // ProfileData.  Create a new decoding stream and context for it,
00222   // and skip the data in the parent stream
00223 
00224   // ProfileData is encoded as a sequence of octet. So first get the
00225   // length of the sequence.
00226   CORBA::ULong encap_len = 0;
00227   if ((cdr >> encap_len) == 0)
00228     return 0;
00229 
00230   // Create the decoding stream from the encapsulation in the buffer,
00231   // and skip the encapsulation.
00232   TAO_InputCDR str (cdr, encap_len);
00233 
00234   if (str.good_bit () == 0
00235       || cdr.skip_bytes (encap_len) == 0)
00236     return 0;
00237 
00238   return connector->create_profile (str);
00239 }
00240 
00241 char
00242 TAO_Connector_Registry::object_key_delimiter (const char *ior)
00243 {
00244   if (!ior)
00245     {
00246       errno = EINVAL;
00247       return 0; // Failure: Null IOR string pointer
00248     }
00249 
00250   const TAO_ConnectorSetIterator first_connector = this->begin ();
00251   const TAO_ConnectorSetIterator last_connector =  this->end ();
00252 
00253   for (TAO_ConnectorSetIterator connector = first_connector;
00254        connector != last_connector;
00255        ++connector)
00256     {
00257       if (*connector)
00258         {
00259           if ((*connector)->check_prefix (ior) == 0)
00260             return (*connector)->object_key_delimiter ();
00261         }
00262     }
00263 
00264   // Failure: None of the connectors were able to match their protocol
00265   // against the provided string.
00266   return 0;
00267 }
00268 
00269 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 13:07:31 2008 for TAO by doxygen 1.3.6