00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00051
00052 DB(5,"Contained_impl::init("<<id<<","<<name<<","<<version<<")")
00053
00054
00055
00056
00057 int where =0;
00058 try
00059 {
00060
00061
00062 this->name(name);
00063 ++where;
00064
00065
00066 this->id(id);
00067 ++where;
00068
00069
00070
00071
00072
00073 _defined_in->addContained(this);
00074
00075 }
00076 catch(CORBA::BAD_PARAM&)
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
00089 Repository_impl::inst().removeId(_id.in());
00090 break;
00091 default: assert(0);
00092 }
00093
00094
00095
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])
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))
00117 return;
00118 if(Repository_impl::inst().findId(v))
00119 throw CORBA::BAD_PARAM(
00120 IFELSE_OMNIORB4(omni::BAD_PARAM_RIDAlreadyDefinedInIfR,2),
00121 CORBA::COMPLETED_NO
00122 );
00123
00124
00125 if(_id.in())
00126 Repository_impl::inst().removeId(_id.in());
00127 _id=v;
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;
00147
00148 case IdentifierUtil::equivalentMatch:
00149 {
00150 _name=v;
00151 updateAbsoluteName();
00152 }
00153 break;
00154
00155 case IdentifierUtil::noMatch:
00156 {
00157
00158 Contained_impl* match =_defined_in->lookupServant(v,false);
00159 if(match)
00160 throw CORBA::BAD_PARAM(
00161 IFELSE_OMNIORB4(omni::BAD_PARAM_ValueFactoryFailure,1),
00162 CORBA::COMPLETED_NO
00163 );
00164
00165 _name=v;
00166 updateAbsoluteName();
00167 }
00168 break;
00169
00170 default:
00171 assert(false);
00172 };
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
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
00214
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);
00252 }
00253 Container_impl* newContainer =dynamic_cast<Container_impl*>(servant);
00254
00255
00256
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
00264 if(newContainer==_defined_in)
00265 {
00266
00267 name(new_name);
00268 version(new_version);
00269 }
00270 else
00271 {
00272
00273 Container_impl* oldContainer = _defined_in;
00274 Identifier_var oldName = _name.in();
00275 int oldIndex = _index;
00276 _defined_in=newContainer;
00277 _name="";
00278 DB(5,"Contained_impl::move(): MOVE "<<oldName.in()<<"->"<<new_name<<")")
00279 try
00280 {
00281 name(new_name);
00282 newContainer->addContained(this);
00283 }
00284 catch(CORBA::BAD_PARAM&)
00285 {
00286
00287 _defined_in=oldContainer;
00288 _name=oldName;
00289 _index=oldIndex;
00290 throw;
00291 }
00292 version(new_version);
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 }