Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Contained.cc

Go to the documentation of this file.
00001 //                            Package   : omniIFR
00002 //  Contained.cc              Created   : 2004/02/22
00003 //                            Author    : Alex Tingle
00004 //
00005 //    Copyright (C) 2004 Alex Tingle.
00006 //
00007 //    This file is part of the omniIFR application.
00008 //
00009 //    omniIFR is free software; you can redistribute it and/or
00010 //    modify it under the terms of the GNU Lesser General Public
00011 //    License as published by the Free Software Foundation; either
00012 //    version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //    omniIFR is distributed in the hope that it will be useful,
00015 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //    Lesser General Public License for more details.
00018 //
00019 //    You should have received a copy of the GNU Lesser General Public
00020 //    License along with this library; if not, write to the Free Software
00021 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 #include "Contained.h"
00024 
00025 #include "Container.h"
00026 #include "Repository.h"
00027 #include "IdentifierUtil.h"
00028 
00029 #include <string>
00030 #include <string.h>
00031 
00032 namespace Omniifr {
00033 
00034 void Contained_impl::init(
00035   const char*     id,
00036   const char*     name,
00037   const char*     version,
00038   Container_impl* defined_in,
00039   int             index
00040 )
00041 {
00042   _index=index;
00043   _version=version;
00044   _defined_in=defined_in;
00045 
00046   assert(id);
00047   assert(name);
00048   assert(version);
00049   assert(defined_in);
00050   // containing_repository //?? why isn't this a member of this class?
00051 
00052   DB(5,"Contained_impl::init("<<id<<","<<name<<","<<version<<")")
00053 
00054   // Note: The order of the following three actions is important. Each may
00055   // throw BAD_PARAM, in which case the changes made so far must be backed out.
00056 
00057   int where =0; // Only for error reporting (see catch)
00058   try
00059   {
00060 
00061     // name() requires _defined_in to be set. Has no side-effects.
00062     this->name(name);
00063     ++where;
00064 
00065     // External-effect: adds id into the Repository.
00066     this->id(id);
00067     ++where;
00068 
00069     // External-effects:
00070     //  - adds 'this' to container's list;
00071     //  - calls _defined_in->_add_ref()
00072     // BUT only if the call completes succcessfully.
00073     _defined_in->addContained(this);
00074 
00075   }
00076   catch(CORBA::BAD_PARAM&) // Name already in use (or bad name)
00077   {
00078     switch(where)
00079     {
00080     case 0:
00081         DB(5,"Contained_impl::init() - failed to define name as "<<name)
00082         break;
00083     case 1:
00084         DB(5,"Contained_impl::init() - failed to define id as "<<id)
00085         break;
00086     case 2:
00087         DB(5,"Contained_impl::init() - failed to add to container.")
00088         // Remove the RID from the Repository.
00089         Repository_impl::inst().removeId(_id.in());
00090         break;
00091     default: assert(0);
00092     }
00093 
00094     // "_defined_in->addContained(this)" never succeeded, so we are
00095     // not defined anywhere --> uncheckedDestroy won't try to undefine us.
00096     _defined_in=NULL;
00097     throw;
00098   }
00099 }
00100 
00101 char* Contained_impl::id() 
00102 {
00103   return CORBA::string_dup(_id);
00104 }
00105 
00106 void Contained_impl::id(const char* v)
00107 {
00108   checkReadonly();
00109   assert(v);
00110 
00111   if(!v[0]) // empty string? invalid RID!
00112       throw CORBA::BAD_PARAM(
00113         IFELSE_OMNIORB4(omni::BAD_PARAM_InvalidRepositoryId,16),
00114         CORBA::COMPLETED_NO
00115       );
00116   if(_id.in() && 0==strcmp(_id,v)) // Unchanged? do nothing!
00117       return;
00118   if(Repository_impl::inst().findId(v)) // RID already in use.
00119       throw CORBA::BAD_PARAM(
00120         IFELSE_OMNIORB4(omni::BAD_PARAM_RIDAlreadyDefinedInIfR,2),
00121         CORBA::COMPLETED_NO
00122       );
00123 
00124   // Do it.
00125   if(_id.in())
00126       Repository_impl::inst().removeId(_id.in());
00127   _id=v; // Change the id
00128   Repository_impl::inst().addId(v,this);
00129 }
00130 
00131 char* Contained_impl::name() 
00132 {
00133   return CORBA::string_dup(_name);
00134 }
00135 
00136 void Contained_impl::name(const char* v) 
00137 {
00138   checkReadonly();
00139   assert(v);
00140   assert(_defined_in);
00141 
00142   IdentifierUtil::checkInvalid(v);
00143   switch(IdentifierUtil::compare(_name.in(),v))
00144   {
00145     case IdentifierUtil::equalMatch:
00146         break; // Unchanged -> do nothing!
00147 
00148     case IdentifierUtil::equivalentMatch:
00149       {
00150         _name=v; // Equivalent name -> just change it!
00151         updateAbsoluteName();
00152       }
00153       break;
00154 
00155     case IdentifierUtil::noMatch:
00156       {
00157         // Check that the new name is not already in use (case INsensitive)
00158         Contained_impl* match =_defined_in->lookupServant(v,false);
00159         if(match) // Name already in use.
00160             throw CORBA::BAD_PARAM(
00161               IFELSE_OMNIORB4(omni::BAD_PARAM_ValueFactoryFailure,1),
00162               CORBA::COMPLETED_NO
00163             );
00164         // Do it
00165         _name=v;
00166         updateAbsoluteName();
00167       }
00168       break;
00169 
00170     default:
00171         assert(false); // Never get here.
00172   }; // end switch.
00173 }
00174 
00175 char* Contained_impl::version() 
00176 {
00177   return CORBA::string_dup(_version);
00178 }
00179 
00180 void Contained_impl::version(const char* v)
00181 {
00182   checkReadonly();
00183   // Empty strings ARE allowed.
00184   assert(v);
00185   _version=v;
00186 }
00187 
00188 Container_ptr Contained_impl::defined_in() 
00189 {
00190   assert(_defined_in);
00191   return _defined_in->_this();
00192 }
00193 
00194 char *Contained_impl::absolute_name() 
00195 {
00196   return CORBA::string_dup(_absolute_name);
00197 }
00198 
00199 void Contained_impl::updateAbsoluteName()
00200 {
00201   assert(_name.in());
00202   assert(_name.in()[0]);
00203 
00204   string parent="";
00205 
00206   Contained_impl* contained=dynamic_cast<Contained_impl*>(_defined_in);
00207   if(contained)
00208   {
00209     CORBA::String_var containerName =contained->absolute_name();
00210     parent=containerName.in();
00211   }
00212   _absolute_name=(parent+"::"+_name.in()).c_str();
00213   // ?? If this object is a Container, we should also call updateAbsoluteName()
00214   // ?? of any contained object.
00215 }
00216 
00217 Repository_ptr Contained_impl::containing_repository() 
00218 {
00219   return Repository_impl::inst()._this();
00220 }
00221 
00222 void Contained_impl::move(
00223   Container_ptr new_container,
00224   const char*   new_name,
00225   const char*   new_version
00226 )
00227 {
00228   assert(new_name);
00229   assert(new_version);
00230   DB(5,"Contained_impl::move("<<new_name<<":"<<new_version<<")")
00231   checkReadonly();
00232     
00233   if(CORBA::is_nil(new_container))
00234       throw CORBA::BAD_PARAM(
00235         IFELSE_OMNIORB4(omni::BAD_PARAM_TargetIsInvalidContainer,4),
00236         CORBA::COMPLETED_NO
00237       );
00238 
00239   PortableServer::Servant servant =NULL;
00240   try
00241   {
00242     PortableServer::POA_var poa=_default_POA();
00243     servant=poa->reference_to_servant(new_container);
00244   }
00245   catch(PortableServer::POA::ObjectNotActive&){servant=NULL;}
00246   catch(PortableServer::POA::WrongAdapter&){servant=NULL;}
00247   catch(PortableServer::POA::WrongPolicy&)
00248   {
00249     cerr<<"Can't move contained object.\n"
00250       "POA has wrong policy for reference_to_servant()."<<endl;
00251     exit(1); // Programming error - so quit.
00252   }
00253   Container_impl* newContainer =dynamic_cast<Container_impl*>(servant);
00254 
00255   // the containers must be in the same repository
00256   // If newContainer is NULL, then we didn't find it in our repository's POA.
00257   if(!newContainer || !newContainer->canContain(this->def_kind()))
00258       throw CORBA::BAD_PARAM(
00259         IFELSE_OMNIORB4(omni::BAD_PARAM_TargetIsInvalidContainer,4),
00260         CORBA::COMPLETED_NO
00261       );
00262       
00263   // Have we been asked to move the object to the container it started in?
00264   if(newContainer==_defined_in)
00265   {
00266     // Just change the name and version.
00267     name(new_name); // May throw exception if new_name is already in use.
00268     version(new_version);
00269   }
00270   else
00271   {
00272     // Move to the new container.
00273     Container_impl* oldContainer = _defined_in;
00274     Identifier_var  oldName      = _name.in();
00275     int             oldIndex     = _index;
00276     _defined_in=newContainer;
00277     _name=""; // Clear, as this object current has no name in newContainer.
00278     DB(5,"Contained_impl::move(): MOVE "<<oldName.in()<<"->"<<new_name<<")")
00279     try
00280     {
00281       name(new_name); // Throws BAD_PARAM if new_name is already in use.
00282       newContainer->addContained(this); // May throw BAD_PARAM
00283     }
00284     catch(CORBA::BAD_PARAM&)
00285     {
00286       // Restore _defined_in & _name.
00287       _defined_in=oldContainer;
00288       _name=oldName;
00289       _index=oldIndex;
00290       throw;
00291     }
00292     version(new_version); // never throws
00293     oldContainer->removeContained(this);
00294   }
00295 }
00296 
00297 void Contained_impl::uncheckedDestroy()
00298 {
00299   if(_defined_in)
00300   {
00301     _defined_in->removeContained(this);
00302     _defined_in=NULL;
00303   }
00304   if(_id.in())
00305   {
00306     Repository_impl::inst().removeId(_id.in());
00307   }
00308 }
00309 
00310 char* Contained_impl::definedInId()
00311 {
00312   Contained_impl* contained =dynamic_cast<Contained_impl*>(_defined_in);
00313   if(contained)
00314       return contained->id();
00315   else
00316       return string_dup("");
00317 }
00318 
00319 } // end namespace Omniifr

Generated on Fri Mar 4 13:03:22 2005 for OmniIFR by  doxygen 1.4.1