1 2 package SOFA.SOFAnode.Made.TIR.Impl; 3 import java.io.DataInputStream ; 4 import java.io.DataOutputStream ; 5 import java.io.FileInputStream ; 6 import java.io.FileOutputStream ; 7 import java.io.IOException ; 8 import java.rmi.RemoteException ; 9 10 import SOFA.SOFAnode.Made.TIR.AbsoluteName; 11 import SOFA.SOFAnode.Made.TIR.CDLType; 12 import SOFA.SOFAnode.Made.TIR.Contained; 13 import SOFA.SOFAnode.Made.TIR.Container; 14 import SOFA.SOFAnode.Made.TIR.DefinitionKind; 15 import SOFA.SOFAnode.Made.TIR.EnumDef; 16 import SOFA.SOFAnode.Made.TIR.Identification; 17 import SOFA.SOFAnode.Made.TIR.Repository; 18 import SOFA.SOFAnode.Made.TIR.StateKind; 19 import SOFA.SOFAnode.Made.TIR.StructDef; 20 import SOFA.SOFAnode.Made.TIR.StructMember; 21 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit; 22 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate; 23 import SOFA.SOFAnode.Made.TIR.TIRExceptLock; 24 import SOFA.SOFAnode.Made.TIR.TIRObject; 25 import SOFA.SOFAnode.Made.TIR.UnionDef; 26 27 public class StructDefImpl extends ContainerImpl implements StructDef, TIRImplObject, SContained { 28 protected Identification id; 29 protected Container parent; 30 protected Repository rep; 31 DefinitionKindImpl defKindImpl; 32 StateKindImpl stKindImpl; 33 34 StructDefImpl normal; 35 long workId; 36 37 public StructDefImpl(Identification ident, Container in, Repository inrep, long workId) throws RemoteException { 38 id = ident; 39 parent = in; 40 rep = inrep; 41 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Struct); 42 stKindImpl = new StateKindImpl(StateKind.sk_work); 43 normal = null; 44 this.workId = workId; 45 } 46 47 public StructDefImpl(Container in, Repository inrep) throws RemoteException { 48 id = null; 49 parent = in; 50 rep = inrep; 51 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Struct); 52 stKindImpl = new StateKindImpl(StateKind.sk_normal); 53 normal = null; 54 workId = -1L; 55 } 56 57 public StructDefImpl(StructDefImpl a, Container in, Repository inrep, long workId) throws RemoteException { 58 id = a.id; 59 parent = in; 60 rep = inrep; 61 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Struct); 62 stKindImpl = new StateKindImpl(StateKind.sk_work); 63 normal = a; 64 this.workId = workId; 65 } 66 67 68 public Identification get_identification() throws RemoteException { 69 return id; 70 } 71 72 73 public Container get_defined_in() throws RemoteException { 74 return parent; 75 } 76 77 78 public Container get_containing_repository() throws RemoteException { 79 return rep; 80 } 81 82 83 public AbsoluteName get_absolute_name() throws RemoteException { 84 AbsoluteNameImpl absName; 85 if (rep == parent) { absName = new AbsoluteNameImpl("::"+id.name()); 87 } else { 88 absName = new AbsoluteNameImpl(((SContained)parent).get_absolute_name().name()+"::"+id.name()); 89 } 90 return (AbsoluteName) absName; 91 } 92 93 public void tag(String t) throws RemoteException , TIRExceptCreate, TIRExceptLock { 94 if (stKindImpl.value()!=StateKind.sk_work) 95 throw new TIRExceptCreate("you can call create method on work object"); 96 String ebt = id.branchTag(); 97 if (t.compareTo(ebt)==0) 98 throw new TIRExceptCreate("This tag exists"); 99 String [] et = id.tag(); 100 int i,j; 101 for (i=0;i<et.length;i++) { 102 if (t.compareTo(et[i])==0) 103 throw new TIRExceptCreate("This tag exists"); 104 } 105 Contained[] sibl = parent.lookup_name(id); 106 for (j=0;j<sibl.length;j++) { 107 Identification idl = sibl[j].get_identification(); 108 109 ebt = idl.branchTag(); 110 if (t.compareTo(ebt)==0) 111 throw new TIRExceptCreate("This tag exists"); 112 et = idl.tag(); 113 for (i=0;i<et.length;i++) { 114 if (t.compareTo(et[i])==0) 115 throw new TIRExceptCreate("This tag exists"); 116 } 117 } 118 ((SIdentification) id).tag(t); 119 } 120 121 122 public DefinitionKind get_def_kind() throws RemoteException { 123 return (DefinitionKind) defKindImpl; 124 } 125 126 127 public StateKind get_state() throws RemoteException { 128 return (StateKind) stKindImpl; 129 } 130 131 private void fromNormObj() throws RemoteException , TIRExceptLock { 132 if (normal==null) 133 return; 134 if (((WorkRepositoryImpl)rep).lock.isLocked()) 135 throw new TIRExceptLock("Repository is locked"); 136 int i; 137 LiItem akt; 138 akt = normal.firstChild; 139 for(i=0;i<normal.numOfItems;i++) { 140 addListItem(WorkRepositoryImpl.newWorkFromNormal(akt.obj,rep,this, workId)); 141 akt = akt.next; 142 } 143 hasCont = true; 144 } 145 146 public StructMember[] members() throws RemoteException , TIRExceptLock { 147 if (stKindImpl.value()==StateKind.sk_normal) { 148 if (((RepositoryImpl) rep).lock.isLocked()) 149 throw new TIRExceptLock("Repository is locked"); 150 } else { 151 if (!hasCont) 152 fromNormObj(); 153 if (((WorkRepositoryImpl) rep).lock.isLocked()) 154 throw new TIRExceptLock("Repository is locked"); 155 } 156 StructMember[] all = new StructMember [numOfItems]; 157 LiItem akt = firstChild; 158 int i,j = 0; 159 for(i=0;i<numOfItems;i++) { 160 if (akt.obj.get_def_kind().value()==DefinitionKind.dk_StructMember) { 161 all[j] = (StructMember) akt.obj; 162 j++; 163 } 164 akt = akt.next; 165 } 166 StructMember[] ret = new StructMember [j]; 167 for(i=0;i<j;i++) { 168 ret[i] = all[i]; 169 } 170 return ret; 171 } 172 173 public Contained[] contents(DefinitionKind type) throws RemoteException , TIRExceptLock { 174 if (stKindImpl.value()==StateKind.sk_normal) { 175 if (((RepositoryImpl) rep).lock.isLocked()) 176 throw new TIRExceptLock("Repository is locked"); 177 else 178 return super.contents(type); 179 } else { 180 if (!hasCont) 181 fromNormObj(); 182 if (((WorkRepositoryImpl) rep).lock.isLocked()) 183 throw new TIRExceptLock("Repository is locked"); 184 else 185 return super.contents(type); 186 } 187 } 188 189 public Contained lookup(Identification id) throws RemoteException , TIRExceptLock { 190 if (stKindImpl.value()==StateKind.sk_normal) { 191 if (((RepositoryImpl) rep).lock.isLocked()) 192 throw new TIRExceptLock("Repository is locked"); 193 else 194 return super.lookup(id); 195 } else { 196 if (!hasCont) 197 fromNormObj(); 198 if (((WorkRepositoryImpl) rep).lock.isLocked()) 199 throw new TIRExceptLock("Repository is locked"); 200 else 201 return super.lookup(id); 202 } 203 } 204 205 public Contained lookup(String name, String ver) throws RemoteException , TIRExceptLock { 206 if (stKindImpl.value()==StateKind.sk_normal) { 207 if (((RepositoryImpl) rep).lock.isLocked()) 208 throw new TIRExceptLock("Repository is locked"); 209 else 210 return super.lookup(name, ver); 211 } else { 212 if (!hasCont) 213 fromNormObj(); 214 if (((WorkRepositoryImpl) rep).lock.isLocked()) 215 throw new TIRExceptLock("Repository is locked"); 216 else 217 return super.lookup(name, ver); 218 } 219 } 220 221 public Contained[] lookup_name(Identification id) throws RemoteException , TIRExceptLock { 222 if (stKindImpl.value()==StateKind.sk_normal) { 223 if (((RepositoryImpl) rep).lock.isLocked()) 224 throw new TIRExceptLock("Repository is locked"); 225 else 226 return super.lookup_name(id); 227 } else { 228 if (!hasCont) 229 fromNormObj(); 230 if (((WorkRepositoryImpl) rep).lock.isLocked()) 231 throw new TIRExceptLock("Repository is locked"); 232 else 233 return super.lookup_name(id); 234 } 235 } 236 237 public Contained[] lookup_name(String name) throws RemoteException , TIRExceptLock { 238 if (stKindImpl.value()==StateKind.sk_normal) { 239 if (((RepositoryImpl) rep).lock.isLocked()) 240 throw new TIRExceptLock("Repository is locked"); 241 else 242 return super.lookup_name(name); 243 } else { 244 if (!hasCont) 245 fromNormObj(); 246 if (((WorkRepositoryImpl) rep).lock.isLocked()) 247 throw new TIRExceptLock("Repository is locked"); 248 else 249 return super.lookup_name(name); 250 } 251 } 252 253 public Contained lookup_tag(String name, String tag) throws RemoteException , TIRExceptLock { 254 if (stKindImpl.value()==StateKind.sk_normal) { 255 if (((RepositoryImpl) rep).lock.isLocked()) 256 throw new TIRExceptLock("Repository is locked"); 257 else 258 return super.lookup_tag(name, tag); 259 } else { 260 if (!hasCont) 261 fromNormObj(); 262 if (((WorkRepositoryImpl) rep).lock.isLocked()) 263 throw new TIRExceptLock("Repository is locked"); 264 else 265 return super.lookup_tag(name, tag); 266 } 267 } 268 269 public Contained[] lookup_branchtag(String name, String brtag) throws RemoteException , TIRExceptLock { 270 if (stKindImpl.value()==StateKind.sk_normal) { 271 if (((RepositoryImpl) rep).lock.isLocked()) 272 throw new TIRExceptLock("Repository is locked"); 273 else 274 return super.lookup_branchtag(name, brtag); 275 } else { 276 if (!hasCont) 277 fromNormObj(); 278 if (((WorkRepositoryImpl) rep).lock.isLocked()) 279 throw new TIRExceptLock("Repository is locked"); 280 else 281 return super.lookup_branchtag(name, brtag); 282 } 283 } 284 285 public Contained lookup_lastinbranch(String name, String brtag) throws RemoteException , TIRExceptLock { 286 if (stKindImpl.value()==StateKind.sk_normal) { 287 if (((RepositoryImpl) rep).lock.isLocked()) 288 throw new TIRExceptLock("Repository is locked"); 289 else 290 return super.lookup_lastinbranch(name, brtag); 291 } else { 292 if (!hasCont) 293 fromNormObj(); 294 if (((WorkRepositoryImpl) rep).lock.isLocked()) 295 throw new TIRExceptLock("Repository is locked"); 296 else 297 return super.lookup_lastinbranch(name, brtag); 298 } 299 } 300 301 public Contained lookup_lastfromversion(String name, String version) throws RemoteException , TIRExceptLock { 302 if (stKindImpl.value()==StateKind.sk_normal) { 303 if (((RepositoryImpl) rep).lock.isLocked()) 304 throw new TIRExceptLock("Repository is locked"); 305 else 306 return super.lookup_lastfromversion(name, version); 307 } else { 308 if (!hasCont) 309 fromNormObj(); 310 if (((WorkRepositoryImpl) rep).lock.isLocked()) 311 throw new TIRExceptLock("Repository is locked"); 312 else 313 return super.lookup_lastfromversion(name, version); 314 } 315 } 316 317 public Contained[] scontents(DefinitionKind type) throws RemoteException , TIRExceptLock { 318 if (stKindImpl.value()==StateKind.sk_normal) { 319 return super.scontents(type); 320 } else { 321 if (!hasCont) 322 fromNormObj(); 323 return super.scontents(type); 324 } 325 } 326 327 public Contained slookup(Identification id) throws RemoteException , TIRExceptLock { 328 if (stKindImpl.value()==StateKind.sk_normal) { 329 return super.slookup(id); 330 } else { 331 if (!hasCont) 332 fromNormObj(); 333 return super.slookup(id); 334 } 335 } 336 337 public Contained slookup(String name, String version) throws RemoteException , TIRExceptLock { 338 if (stKindImpl.value()==StateKind.sk_normal) { 339 return super.slookup(name, version); 340 } else { 341 if (!hasCont) 342 fromNormObj(); 343 return super.slookup(name, version); 344 } 345 } 346 347 public Contained[] slookup_name(Identification id) throws RemoteException , TIRExceptLock { 348 if (stKindImpl.value()==StateKind.sk_normal) { 349 return super.slookup_name(id); 350 } else { 351 if (!hasCont) 352 fromNormObj(); 353 return super.slookup_name(id); 354 } 355 } 356 357 public Contained[] slookup_name(String name) throws RemoteException , TIRExceptLock { 358 if (stKindImpl.value()==StateKind.sk_normal) { 359 return super.slookup_name(name); 360 } else { 361 if (!hasCont) 362 fromNormObj(); 363 return super.slookup_name(name); 364 } 365 } 366 367 public StructMember create_member(String name, CDLType type) throws RemoteException , TIRExceptCreate, TIRExceptLock { 368 if (!isNew()) 369 throw new TIRExceptCreate("you can call create method on work object"); 370 if (name!=null && name.compareTo("")!=0 && type!=null) { 371 if (type.get_state().value()==StateKind.sk_work ) { 372 StructMember[] a = members(); 373 boolean found = false; 374 for (int i=0;i<a.length;i++) { 375 if (a[i].name().compareTo(name)==0) { 376 found = true; 377 break; 378 } 379 } 380 if (found) 381 throw new TIRExceptCreate("Member with this name exists in stucture."); 382 StructMember m = new StructMemberImpl(new IdentificationImpl(((SIdentification)this.id).language(), this.id.absolute_name().name()+"::"+name, this.id.version()), this, rep, type); 383 addListItem(m); 384 return m; 385 } else { 386 throw new TIRExceptCreate("Given object isn't work."); 387 } 388 } else { 389 return null; 390 } 391 } 392 393 public StructDef create_struct(Identification id) throws RemoteException , TIRExceptCreate, TIRExceptLock { 394 if (!isNew()) 395 throw new TIRExceptCreate("you can call create method on work object"); 396 if (id!=null) { 397 if (!hasCont) 398 fromNormObj(); 399 id = new IdentificationImpl(((SIdentification) id).language(), id.absolute_name().name(),this.id.version()); 401 Contained[] a = lookup_name(id.name()); 402 if (a.length != 0) { 403 if (a[0].get_def_kind().value() != DefinitionKind.dk_Struct) 404 throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository."); 405 boolean found = false; 406 for (int i=0;i<a.length;i++) { 407 if (((SIdentification) a[i].get_identification()).is_short_equal(id)) { 408 found = true; 409 break; 410 } 411 } 412 if (found) 413 throw new TIRExceptCreate("Object with same identification exists in repository."); 414 } 415 StructDef ret = new StructDefImpl(id, this, rep, workId); 417 addListItem(ret); return ret; } else 420 return null; 421 } 422 423 public UnionDef create_union(Identification id, CDLType switch_type) throws RemoteException , TIRExceptCreate, TIRExceptLock { 424 if (!isNew()) 425 throw new TIRExceptCreate("you can call create method on work object"); 426 if (id==null || switch_type==null) 427 return null; 428 else { 429 if (switch_type.get_state().value()==StateKind.sk_work ) { 430 if (!hasCont) 431 fromNormObj(); 432 id = new IdentificationImpl(((SIdentification) id).language(), id.absolute_name().name(),this.id.version()); 434 Contained[] a = lookup_name(id.name()); 435 if (a.length != 0) { 436 if (a[0].get_def_kind().value() != DefinitionKind.dk_Union) 437 throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository."); 438 boolean found = false; 439 for (int i=0;i<a.length;i++) { 440 if (((SIdentification) a[i].get_identification()).is_short_equal(id)) { 441 found = true; 442 break; 443 } 444 } 445 if (found) 446 throw new TIRExceptCreate("Object with same identification exists in repository."); 447 } 448 UnionDef ret = new UnionDefImpl(id, this, rep, switch_type,workId); 450 addListItem(ret); return ret; } else { 453 throw new TIRExceptCreate("Given object isn't work."); 454 } 455 } 456 } 457 458 public EnumDef create_enum(Identification id) throws RemoteException , TIRExceptCreate, TIRExceptLock { 459 if (!isNew()) 460 throw new TIRExceptCreate("you can call create method on work object"); 461 if (id!=null) { 462 if (!hasCont) 463 fromNormObj(); 464 id = new IdentificationImpl(((SIdentification) id).language(), id.absolute_name().name(),this.id.version()); 466 Contained[] a = lookup_name(id.name()); 467 if (a.length != 0) { 468 if (a[0].get_def_kind().value() != DefinitionKind.dk_Enum) 469 throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository."); 470 boolean found = false; 471 for (int i=0;i<a.length;i++) { 472 if (((SIdentification) a[i].get_identification()).is_short_equal(id)) { 473 found = true; 474 break; 475 } 476 } 477 if (found) 478 throw new TIRExceptCreate("Object with same identification exists in repository."); 479 } 480 EnumDef ret = new EnumDefImpl(id, this, rep, workId); 482 addListItem(ret); return ret; } else 485 return null; 486 } 487 488 489 public void save(Storage st) throws RemoteException , TIRExceptStorage { 490 try { 491 boolean closeFile = false; 492 if (st.curOutFile==null) { st.curOutFile = new DataOutputStream (new FileOutputStream (st.currentFile)); 494 closeFile = true; 495 } else { ; 497 } 498 ((IdentificationImpl)id).save(st.curOutFile); st.curOutFile.writeInt(numOfItems); 500 LiItem akt = firstChild; 501 for(int i=0;i<numOfItems;i++) { st.curOutFile.writeInt(akt.obj.get_def_kind().value()); 503 ((TIRImplObject)(akt.obj)).save(st); 504 akt = akt.next; 505 } 506 if (closeFile) { st.curOutFile.close(); 508 st.curOutFile = null; 509 } 510 } catch (IOException e) { 511 throw new TIRExceptStorage("Access error in "+st.current+"."); 512 } 513 } 514 515 516 public void load(Storage st) throws RemoteException , TIRExceptStorage{ 517 try { 518 boolean closeFile = false; 519 if (st.curInFile==null) { 520 st.curInFile = new DataInputStream (new FileInputStream (st.currentFile)); 521 closeFile = true; 522 } else { 523 ; 524 } 525 id = new IdentificationImpl(); 526 ((IdentificationImpl)id).load(st.curInFile); 527 int num = st.curInFile.readInt(); 528 int dkind; 529 TIRImplObject obj; 530 for (int i=0;i<num;i++) { 531 dkind = st.curInFile.readInt(); 532 switch (dkind) { 533 case DefinitionKind.dk_StructMember: 534 obj = new StructMemberImpl(this,rep); 535 obj.load(st); 536 addListItem((TIRObject)obj); 537 break; 538 case DefinitionKind.dk_Struct: 539 obj = new StructDefImpl(this, rep); 540 obj.load(st); 541 addListItem((TIRObject)obj); 542 break; 543 case DefinitionKind.dk_Enum: 544 obj = new EnumDefImpl(this, rep); 545 obj.load(st); 546 addListItem((TIRObject)obj); 547 break; 548 case DefinitionKind.dk_Union: 549 obj = new UnionDefImpl(this, rep); 550 obj.load(st); 551 addListItem((TIRObject)obj); 552 break; 553 default: 554 throw new TIRExceptStorage("Unexpected kind of object in \"" 555 +st.currentFile+"\"."); 556 } } 558 if (closeFile) { st.curInFile.close(); 560 st.curInFile = null; 561 } 562 } catch (IOException e) { 563 throw new TIRExceptStorage("Access error in "+st.current+"."); 564 } 565 } 566 567 public void postLoad(RepositoryImpl r) throws RemoteException , TIRExceptStorage { 568 LiItem akt = firstChild; 569 for(int i=0;i<numOfItems;i++) { 570 ((TIRImplObject) akt.obj).postLoad(r); 571 akt = akt.next; 572 } 573 } 574 575 public boolean isNew() { 576 return ((stKindImpl.value()==StateKind.sk_work) && normal==null); 577 } 578 579 public void canCommit() throws RemoteException , TIRExceptCommit { 580 if (stKindImpl.value()==StateKind.sk_normal) 581 return; 582 LiItem akt; 583 int i; 584 akt = firstChild; 585 for (i=0;i<numOfItems;i++) { 586 ((TIRImplObject)akt.obj).canCommit(); 587 akt = akt.next; 588 } 589 } 590 591 public void doCommit(Container in, Repository rep) throws RemoteException { 592 if (stKindImpl.value()==StateKind.sk_normal) 593 return; 594 if (isNew()) { 595 stKindImpl.toNormal(); 596 LiItem akt = firstChild; 597 for (int i=0;i<numOfItems;i++) { 598 ((TIRImplObject)akt.obj).doCommit(this,rep); 599 akt = akt.next; 600 } 601 parent = in; 602 this.rep = rep; 603 } else { 604 normal = null; 605 } 606 } 607 608 public void doAbort(long workId) throws RemoteException { 609 LiItem akt = firstChild; 610 for(int i=0;i<numOfItems;i++) { 611 if (((TIRImplObject)akt.obj).isNew()) 612 ((TIRImplObject)akt.obj).doAbort(workId); 613 akt = akt.next; 614 } 615 } 616 } 617 | Popular Tags |