IORManipulation.cpp

Go to the documentation of this file.
00001 #include "tao/IORManipulation/IORManipulation.h"
00002 
00003 #include "tao/MProfile.h"
00004 #include "tao/Profile.h"
00005 #include "tao/Stub.h"
00006 #include "tao/ORB_Core.h"
00007 
00008 #include "ace/Auto_Ptr.h"
00009 #include "ace/OS_NS_string.h"
00010 
00011 
00012 ACE_RCSID (IORManipulation,
00013            IORManipulation,
00014            "$Id: IORManipulation.cpp 76995 2007-02-11 12:51:42Z johnnyw $")
00015 
00016 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00017 
00018 TAO_IOR_Manipulation_impl::TAO_IOR_Manipulation_impl (void)
00019 {
00020 }
00021 
00022 TAO_IOR_Manipulation_impl::~TAO_IOR_Manipulation_impl (void)
00023 {
00024 }
00025 
00026 CORBA::Object_ptr
00027 TAO_IOR_Manipulation_impl::merge_iors (
00028     const TAO_IOP::TAO_IOR_Manipulation::IORList & iors)
00029 {
00030   // we need to create a new CORBA::Object which has the union of the
00031   // two profile lists.  However, if any profiles are duplicates (i.e. in
00032   // bott lisis) then an exception is raised.
00033 
00034   // Deterinine how many profiles we have
00035   // Get an estimate of the size - pfile count could change since we
00036   // neither lock nor get a copy in this loop.
00037   CORBA::ULong i, count=0;
00038   for (i = 0; i < iors.length (); i++)
00039     {
00040       count += iors[i]->_stubobj ()->base_profiles ().profile_count ();
00041     }
00042 
00043   // make sure we have some profiles
00044   if (count == 0)
00045     throw TAO_IOP::EmptyProfileList ();
00046 
00047   // initialize with estimated pfile count.
00048   TAO_MProfile Merged_Profiles (count);
00049 
00050   // get the profile lists, start by initialize the composite reference
00051   // by using the first Object.  Then for each subsequent Object verify
00052   // they are the same type and they do not have duplicate profiles.
00053   auto_ptr<TAO_MProfile> tmp_pfiles (iors[0]->_stubobj ()->make_profiles ());
00054   if (Merged_Profiles.add_profiles (tmp_pfiles.get ())< 0)
00055     throw TAO_IOP::Invalid_IOR ();
00056   CORBA::String_var id =
00057     CORBA::string_dup (iors[0]->_stubobj ()->type_id.in ());
00058 
00059   for (i = 1; i < iors.length () ; i++)
00060     {
00061       // this gets a copy of the MProfile, hense the auto_ptr;
00062 
00063       ACE_AUTO_PTR_RESET (tmp_pfiles,
00064                           iors[i]->_stubobj ()->make_profiles (),
00065                           TAO_MProfile);
00066 
00067       // check to see if any of the profile in tmp_pfiles are already
00068       // in Merged_Profiles.  If so raise exception.
00069       if (Merged_Profiles.is_equivalent (tmp_pfiles.get ()))
00070         throw TAO_IOP::Duplicate ();
00071 
00072       // If the object type_id's differ then raise an exception.
00073       if (id.in () && iors[i]->_stubobj ()->type_id.in () &&
00074           ACE_OS::strcmp (id.in (), iors[i]->_stubobj ()->type_id.in ()))
00075         throw TAO_IOP::Invalid_IOR ();
00076 
00077       // append profiles
00078       if (Merged_Profiles.add_profiles (tmp_pfiles.get ()) < 0)
00079         throw TAO_IOP::Invalid_IOR ();
00080 
00081     }
00082 
00083   // MS C++ knows nothing about reset!
00084   // tmp_pfiles.reset (0); // get rid of last MProfile
00085 
00086   TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
00087 
00088   TAO_Stub *stub = orb_core->create_stub (id.in (), // give the id string
00089                                           Merged_Profiles);
00090 
00091   // Make the stub memory allocation exception safe for the duration
00092   // of this method.
00093   TAO_Stub_Auto_Ptr safe_stub (stub);
00094 
00095   // Create the CORBA level proxy
00096   CORBA::Object_ptr temp_obj = CORBA::Object::_nil ();
00097   ACE_NEW_THROW_EX (temp_obj,
00098                     CORBA::Object (safe_stub.get ()),
00099                     CORBA::NO_MEMORY ());
00100 
00101   CORBA::Object_var new_obj = temp_obj;
00102 
00103 
00104   // Clean up in case of errors.
00105   if (CORBA::is_nil (new_obj.in ()))
00106     {
00107       throw TAO_IOP::Invalid_IOR ();
00108     }
00109 
00110   // Release ownership of the pointers protected by the auto_ptrs since they
00111   // no longer need to be protected by this point.
00112   stub = safe_stub.release ();
00113 
00114   return new_obj._retn ();
00115 }
00116 
00117 CORBA::Object_ptr
00118 TAO_IOR_Manipulation_impl::add_profiles (
00119     CORBA::Object_ptr ior1,
00120     CORBA::Object_ptr ior2)
00121 {
00122 
00123   // Get an estimate of the number of profiles
00124   CORBA::Object_ptr buffer [2];
00125   buffer [0] = ior1;
00126   buffer [1] = ior2;
00127   TAO_IOP::TAO_IOR_Manipulation::IORList iors (2, 2, buffer, 0);
00128   return this->merge_iors (iors);
00129 }
00130 
00131 CORBA::Object_ptr
00132 TAO_IOR_Manipulation_impl::remove_profiles (
00133     CORBA::Object_ptr group,
00134     CORBA::Object_ptr ior2
00135     )
00136 {
00137   // First verify they are the same type!
00138   CORBA::String_var id =
00139     CORBA::string_dup (group->_stubobj ()->type_id.in ());
00140   if (id.in () && ior2->_stubobj ()->type_id.in () &&
00141       ACE_OS::strcmp (id.in (), ior2->_stubobj ()->type_id.in ()))
00142     throw TAO_IOP::Invalid_IOR ();
00143 
00144   // Since we are removing from group ...
00145   CORBA::ULong count = group->_stubobj ()->base_profiles ().profile_count ();
00146 
00147   // make sure we have some profiles
00148   if (count == 0 ||
00149       ior2->_stubobj ()->base_profiles ().profile_count () == 0)
00150     throw TAO_IOP::EmptyProfileList ();
00151 
00152   // initialize with estimated pfile count.
00153   TAO_MProfile Diff_Profiles (count);
00154 
00155   auto_ptr<TAO_MProfile> tmp_pfiles (group->_stubobj ()->make_profiles ());
00156   if (Diff_Profiles.add_profiles (tmp_pfiles.get ()) < 0)
00157     throw TAO_IOP::Invalid_IOR ();
00158 
00159   // We are done with add_profiles.
00160   // At this point, we don't do remove_profiles()
00161   // immediately like before,
00162   // because it could result in an
00163   // Object Reference with 0 profile. And it would not pass
00164   // the ::CORBA::is_nil() evaluation.
00165   // Instead, we create the Object Reference right here, which is
00166   // earlier than before.(Actually, I just moved some code
00167   // from below up to here).
00168   TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
00169 
00170   TAO_Stub *stub = orb_core->create_stub (id.in (), // give the id string
00171                                           Diff_Profiles
00172                                          );
00173 
00174   // Make the stub memory allocation exception safe for the duration
00175   // of this method.
00176   TAO_Stub_Auto_Ptr safe_stub (stub);
00177 
00178   // Create the CORBA level proxy
00179   CORBA::Object_ptr temp_obj = CORBA::Object::_nil ();
00180   ACE_NEW_THROW_EX (temp_obj,
00181                     CORBA::Object (safe_stub.get ()),
00182                     CORBA::NO_MEMORY ());
00183 
00184   CORBA::Object_var new_obj = temp_obj;
00185 
00186   // Exception safety is no longer an issue by this point so release
00187   // the TAO_Stub from the TAO_Stub_Auto_Ptr.
00188   stub = safe_stub.release ();
00189 
00190   // Clean up in case of errors.
00191   if (CORBA::is_nil (new_obj.in ()))
00192     {
00193       throw TAO_IOP::Invalid_IOR ();
00194     }
00195 
00196   // Now we can remove the profiles which we want to elimitate from
00197   // the Object.
00198   ACE_AUTO_PTR_RESET (tmp_pfiles,
00199                       ior2->_stubobj ()->make_profiles (),
00200                       TAO_MProfile);
00201 
00202   TAO_MProfile& mp = stub -> base_profiles();
00203   if (mp.remove_profiles (tmp_pfiles.get ()) < 0)
00204     throw TAO_IOP::NotFound ();
00205 
00206   // MS C++ knows nothing about reset!
00207   // tmp_pfiles.reset (0); // get rid of last MProfile
00208 
00209   return new_obj._retn ();
00210 }
00211 
00212 CORBA::Boolean
00213 TAO_IOR_Manipulation_impl::set_property (
00214     TAO_IOP::TAO_IOR_Property_ptr prop,
00215     CORBA::Object_ptr group
00216     )
00217 {
00218   // make sure we have some profiles
00219   if (group->_stubobj ()->base_profiles ().profile_count () == 0)
00220     throw TAO_IOP::Invalid_IOR ();
00221 
00222   // Call the implementation object to
00223   return prop->set_property (group
00224                             );
00225 }
00226 
00227 //@@ note awkward argument order
00228 CORBA::Boolean
00229 TAO_IOR_Manipulation_impl::set_primary (
00230     TAO_IOP::TAO_IOR_Property_ptr prop,
00231     CORBA::Object_ptr new_primary,
00232     CORBA::Object_ptr group
00233     )
00234 {
00235   // make sure we have some profiles in GROUP
00236   if (group->_stubobj ()->base_profiles ().profile_count () == 0)
00237     throw TAO_IOP::Invalid_IOR ();
00238 
00239   // Make sure we have only one profile in new_primary
00240   // @@ Will fail if the object has been
00241   /*if (new_primary->_stubobj ()->base_profiles ().profile_count () > 1)
00242     throw TAO_IOP::MultiProfileList ();*/
00243 
00244   // Call the callback object to do the rest of the processing.
00245   return prop->set_primary (new_primary,
00246                             group
00247                            );
00248 }
00249 
00250 CORBA::Object_ptr
00251 TAO_IOR_Manipulation_impl::get_primary (
00252       TAO_IOP::TAO_IOR_Property_ptr prop,
00253       CORBA::Object_ptr group
00254 
00255     )
00256 {
00257   // make sure we have some profiles in IOR
00258   if (group->_stubobj ()->base_profiles ().profile_count () == 0)
00259     throw TAO_IOP::NotFound ();
00260   // @@ Bala: this was throwing TAO_IOP::Invalid_IOR, but it was not
00261   // in the throw spec, that will result in a CORBA::UNKNOWN at
00262   // run-time (if it does not crash).  Any idea about what is going on
00263   // here?
00264 
00265   return prop->get_primary (group
00266                            );
00267 }
00268 
00269 CORBA::Boolean
00270 TAO_IOR_Manipulation_impl::is_primary_set (
00271     TAO_IOP::TAO_IOR_Property_ptr prop,
00272     CORBA::Object_ptr group)
00273 {
00274   return prop->is_primary_set (group);
00275 }
00276 
00277 CORBA::Boolean
00278 TAO_IOR_Manipulation_impl:: remove_primary_tag (
00279     TAO_IOP::TAO_IOR_Property_ptr prop,
00280     CORBA::Object_ptr group)
00281 {
00282   return prop->remove_primary_tag (group);
00283 }
00284 
00285 CORBA::ULong
00286 TAO_IOR_Manipulation_impl::is_in_ior (
00287     CORBA::Object_ptr ior1,
00288     CORBA::Object_ptr ior2
00289     )
00290 {
00291   CORBA::ULong count = 0;
00292   TAO_Profile *pfile1, *pfile2;
00293   auto_ptr<TAO_MProfile> tmp_pfiles1 (ior1->_stubobj ()->make_profiles ());
00294   auto_ptr<TAO_MProfile> tmp_pfiles2 (ior2->_stubobj ()->make_profiles ());
00295 
00296   tmp_pfiles1->rewind ();
00297   while ((pfile1 = tmp_pfiles1->get_next ()) > 0)
00298     {
00299       tmp_pfiles2->rewind ();
00300       while ((pfile2 = tmp_pfiles2->get_next ()) > 0)
00301         {
00302           if (pfile1->is_equivalent (pfile2))
00303             count++;
00304         }
00305     }
00306 
00307   if (count == 0)
00308     throw TAO_IOP::NotFound ();
00309 
00310   return count;
00311 }
00312 
00313 CORBA::ULong
00314 TAO_IOR_Manipulation_impl::get_profile_count (CORBA::Object_ptr group)
00315 {
00316   CORBA::ULong const count = group->_stubobj ()->base_profiles ().profile_count ();
00317 
00318   if (count == 0)
00319     throw TAO_IOP::EmptyProfileList ();
00320 
00321   return count;
00322 }
00323 
00324 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 13:38:06 2008 for TAO_IORManipulation by doxygen 1.3.6