1 2 package SOFA.SOFAnode.Made.TIR.Impl; 3 import java.io.BufferedWriter ; 4 import java.io.File ; 5 import java.io.FileWriter ; 6 import java.io.IOException ; 7 import java.rmi.RemoteException ; 8 9 import SOFA.SOFAnode.Made.TIR.AModuleDef; 10 import SOFA.SOFAnode.Made.TIR.AbsoluteName; 11 import SOFA.SOFAnode.Made.TIR.ArchitectureDef; 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.FrameDef; 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.TIRExceptCommit; 20 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate; 21 import SOFA.SOFAnode.Made.TIR.TIRExceptLock; 22 import SOFA.SOFAnode.Made.TIR.TIRObject; 23 24 public class AModuleDefImpl extends ContainerImpl implements AModuleDef, SContained { 25 protected Identification id; 26 protected Container parent; 27 protected Repository rep; 28 DefinitionKindImpl defKindImpl; 29 StateKindImpl stKindImpl; 30 31 AModuleDefImpl normal; 32 33 Lock lockCreate; 34 boolean lockForMe; public long lockFor; public long workId; 38 public AModuleDefImpl(Identification ident, Container in, Repository inrep, int state, long workId) throws RemoteException { 39 id = ident; 40 parent = in; 41 rep = inrep; 42 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_AModule); 43 stKindImpl = new StateKindImpl(state); 44 normal = null; 45 lockCreate = new Lock(false); 46 lockForMe = false; 47 lockFor = -1L; 48 this.workId = workId; 49 } 50 51 public AModuleDefImpl(Container in, Repository inrep) throws RemoteException { 52 parent = in; 53 rep = inrep; 54 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_AModule); 55 stKindImpl = new StateKindImpl(StateKind.sk_normal); 56 normal = null; 57 lockCreate = new Lock(false); 58 lockForMe = false; 59 workId = -1L; 60 lockFor = -1L; 61 } 62 63 public AModuleDefImpl(AModuleDefImpl a, Container in, Repository inrep, long workId) throws RemoteException { 64 parent = in; 65 rep = inrep; 66 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_AModule); 67 stKindImpl = new StateKindImpl(StateKind.sk_work); 68 normal = a; 69 id = a.id; 70 lockCreate = a.lockCreate; 71 lockForMe = false; 72 this.workId = workId; 73 lockFor = -1L; 74 } 75 76 77 public Identification get_identification() throws RemoteException { 78 return id; 79 } 80 81 82 public Container get_defined_in() throws RemoteException { 83 return parent; 84 } 85 86 87 public Container get_containing_repository() throws RemoteException { 88 return rep; 89 } 90 91 92 public AbsoluteName get_absolute_name() throws RemoteException { 93 AbsoluteNameImpl absName; 94 if (rep == parent) { absName = new AbsoluteNameImpl("::"+id.name()); 96 } else { 97 absName = new AbsoluteNameImpl(((SContained)parent).get_absolute_name().name()+"::"+id.name()); 98 } 99 return (AbsoluteName) absName; 100 } 101 102 public void tag(String t) throws RemoteException , TIRExceptCreate, TIRExceptLock { 103 if (stKindImpl.value()!=StateKind.sk_work) 104 throw new TIRExceptCreate("you can call create method on work object"); 105 String ebt = id.branchTag(); 106 if (t.compareTo(ebt)==0) 107 throw new TIRExceptCreate("This tag exists"); 108 String [] et = id.tag(); 109 int i,j; 110 for (i=0;i<et.length;i++) { 111 if (t.compareTo(et[i])==0) 112 throw new TIRExceptCreate("This tag exists"); 113 } 114 Contained[] sibl = parent.lookup_name(id); 115 for (j=0;j<sibl.length;j++) { 116 Identification idl = sibl[j].get_identification(); 117 118 ebt = idl.branchTag(); 119 if (t.compareTo(ebt)==0) 120 throw new TIRExceptCreate("This tag exists"); 121 et = idl.tag(); 122 for (i=0;i<et.length;i++) { 123 if (t.compareTo(et[i])==0) 124 throw new TIRExceptCreate("This tag exists"); 125 } 126 } 127 ((SIdentification) id).tag(t); 128 } 129 130 131 public DefinitionKind get_def_kind() throws RemoteException { 132 return (DefinitionKind) defKindImpl; 133 } 134 135 136 public StateKind get_state() throws RemoteException { 137 return (StateKind) stKindImpl; 138 } 139 140 private void fromNormObj() throws RemoteException , TIRExceptLock { 141 if (normal==null) 142 return; 143 if (((WorkRepositoryImpl)rep).lock.isLocked()) 144 throw new TIRExceptLock("Repository is locked"); 145 int i; 146 LiItem akt; 147 akt = normal.firstChild; 148 for(i=0;i<normal.numOfItems;i++) { 149 addListItem(WorkRepositoryImpl.newWorkFromNormal(akt.obj,rep,this,workId)); 150 akt = akt.next; 151 } 152 hasCont = true; 153 } 154 155 public Contained[] contents(DefinitionKind type) throws RemoteException , TIRExceptLock { 156 if (stKindImpl.value()==StateKind.sk_normal) { 157 if (((RepositoryImpl) rep).lock.isLocked()) 158 throw new TIRExceptLock("Repository is locked"); 159 else 160 return super.contents(type); 161 } else { 162 if (!hasCont) 163 fromNormObj(); 164 if (((WorkRepositoryImpl) rep).lock.isLocked()) 165 throw new TIRExceptLock("Repository is locked"); 166 else 167 return super.contents(type); 168 } 169 } 170 171 public Contained lookup(Identification id) throws RemoteException , TIRExceptLock { 172 if (stKindImpl.value()==StateKind.sk_normal) { 173 if (((RepositoryImpl) rep).lock.isLocked()) 174 throw new TIRExceptLock("Repository is locked"); 175 else 176 return super.lookup(id); 177 } else { 178 if (!hasCont) 179 fromNormObj(); 180 if (((WorkRepositoryImpl) rep).lock.isLocked()) 181 throw new TIRExceptLock("Repository is locked"); 182 else 183 return super.lookup(id); 184 } 185 } 186 187 public Contained lookup(String name, String version) throws RemoteException , TIRExceptLock { 188 if (stKindImpl.value()==StateKind.sk_normal) { 189 if (((RepositoryImpl) rep).lock.isLocked()) 190 throw new TIRExceptLock("Repository is locked"); 191 else 192 return super.lookup(name, version); 193 } else { 194 if (!hasCont) 195 fromNormObj(); 196 if (((WorkRepositoryImpl) rep).lock.isLocked()) 197 throw new TIRExceptLock("Repository is locked"); 198 else 199 return super.lookup(name, version); 200 } 201 } 202 203 public Contained[] lookup_name(Identification id) throws RemoteException , TIRExceptLock { 204 if (stKindImpl.value()==StateKind.sk_normal) { 205 if (((RepositoryImpl) rep).lock.isLocked()) 206 throw new TIRExceptLock("Repository is locked"); 207 else 208 return super.lookup_name(id); 209 } else { 210 if (!hasCont) 211 fromNormObj(); 212 if (((WorkRepositoryImpl) rep).lock.isLocked()) 213 throw new TIRExceptLock("Repository is locked"); 214 else 215 return super.lookup_name(id); 216 } 217 } 218 219 public Contained[] lookup_name(String name) throws RemoteException , TIRExceptLock { 220 if (stKindImpl.value()==StateKind.sk_normal) { 221 if (((RepositoryImpl) rep).lock.isLocked()) 222 throw new TIRExceptLock("Repository is locked"); 223 else 224 return super.lookup_name(name); 225 } else { 226 if (!hasCont) 227 fromNormObj(); 228 if (((WorkRepositoryImpl) rep).lock.isLocked()) 229 throw new TIRExceptLock("Repository is locked"); 230 else 231 return super.lookup_name(name); 232 } 233 } 234 235 public Contained lookup_tag(String name, String tag) throws RemoteException , TIRExceptLock { 236 if (stKindImpl.value()==StateKind.sk_normal) { 237 if (((RepositoryImpl) rep).lock.isLocked()) 238 throw new TIRExceptLock("Repository is locked"); 239 else 240 return super.lookup_tag(name, tag); 241 } else { 242 if (!hasCont) 243 fromNormObj(); 244 if (((WorkRepositoryImpl) rep).lock.isLocked()) 245 throw new TIRExceptLock("Repository is locked"); 246 else 247 return super.lookup_tag(name, tag); 248 } 249 } 250 251 public Contained[] lookup_branchtag(String name, String brtag) throws RemoteException , TIRExceptLock { 252 if (stKindImpl.value()==StateKind.sk_normal) { 253 if (((RepositoryImpl) rep).lock.isLocked()) 254 throw new TIRExceptLock("Repository is locked"); 255 else 256 return super.lookup_branchtag(name, brtag); 257 } else { 258 if (!hasCont) 259 fromNormObj(); 260 if (((WorkRepositoryImpl) rep).lock.isLocked()) 261 throw new TIRExceptLock("Repository is locked"); 262 else 263 return super.lookup_branchtag(name, brtag); 264 } 265 } 266 267 public Contained lookup_lastinbranch(String name, String brtag) throws RemoteException , TIRExceptLock { 268 if (stKindImpl.value()==StateKind.sk_normal) { 269 if (((RepositoryImpl) rep).lock.isLocked()) 270 throw new TIRExceptLock("Repository is locked"); 271 else 272 return super.lookup_lastinbranch(name, brtag); 273 } else { 274 if (!hasCont) 275 fromNormObj(); 276 if (((WorkRepositoryImpl) rep).lock.isLocked()) 277 throw new TIRExceptLock("Repository is locked"); 278 else 279 return super.lookup_lastinbranch(name, brtag); 280 } 281 } 282 283 public Contained lookup_lastfromversion(String name, String version) throws RemoteException , TIRExceptLock { 284 if (stKindImpl.value()==StateKind.sk_normal) { 285 if (((RepositoryImpl) rep).lock.isLocked()) 286 throw new TIRExceptLock("Repository is locked"); 287 else 288 return super.lookup_lastfromversion(name, version); 289 } else { 290 if (!hasCont) 291 fromNormObj(); 292 if (((WorkRepositoryImpl) rep).lock.isLocked()) 293 throw new TIRExceptLock("Repository is locked"); 294 else 295 return super.lookup_lastfromversion(name, version); 296 } 297 } 298 299 public Contained[] scontents(DefinitionKind type) throws RemoteException , TIRExceptLock { 300 if (stKindImpl.value()==StateKind.sk_normal) { 301 return super.scontents(type); 302 } else { 303 if (!hasCont) 304 fromNormObj(); 305 return super.scontents(type); 306 } 307 } 308 309 public Contained slookup(Identification id) throws RemoteException , TIRExceptLock { 310 if (stKindImpl.value()==StateKind.sk_normal) { 311 return super.slookup(id); 312 } else { 313 if (!hasCont) 314 fromNormObj(); 315 return super.slookup(id); 316 } 317 } 318 319 public Contained slookup(String name, String version) throws RemoteException , TIRExceptLock { 320 if (stKindImpl.value()==StateKind.sk_normal) { 321 return super.slookup(name, version); 322 } else { 323 if (!hasCont) 324 fromNormObj(); 325 return super.slookup(name, version); 326 } 327 } 328 329 public Contained[] slookup_name(Identification id) throws RemoteException , TIRExceptLock { 330 if (stKindImpl.value()==StateKind.sk_normal) { 331 return super.slookup_name(id); 332 } else { 333 if (!hasCont) 334 fromNormObj(); 335 return super.slookup_name(id); 336 } 337 } 338 339 public Contained[] slookup_name(String name) throws RemoteException , TIRExceptLock { 340 if (stKindImpl.value()==StateKind.sk_normal) { 341 return super.slookup_name(name); 342 } else { 343 if (!hasCont) 344 fromNormObj(); 345 return super.slookup_name(name); 346 } 347 } 348 349 350 public void save(Storage st) throws RemoteException , TIRExceptStorage { 351 try { 352 String [][] childs = null; 353 try { 354 childs = st.listOfChilds(); 355 } catch (TIRExceptStorage e) { ; 357 } 358 File f = new File (st.current,Storage.indexFile); LiItem akt = firstChild; 361 File old = st.current; 362 BufferedWriter index = new BufferedWriter (new FileWriter (f.toString())); 363 index.write("CHILDS",0,6); index.newLine(); 364 for(int i=0;i<numOfItems;i++) { 365 String [] achild; IdentificationImpl idl; File nfile; if ((achild=st.existsChild(childs,idl = (IdentificationImpl)((Contained)akt.obj).get_identification()))!=null) { index.write(achild[0],0,achild[0].length()); index.write("?",0,1); 371 index.write(achild[1],0,achild[1].length()); index.write("?",0,1); 372 index.write(achild[2],0,achild[2].length()); index.write("?",0,1); 373 index.write(achild[3],0,achild[3].length()); index.newLine(); 374 nfile = new File (old,achild[0]); 375 376 } else { String hlp; 378 nfile = File.createTempFile("tir","",old); 382 nfile.delete(); 383 index.write(hlp = nfile.getName(),0,hlp.length()); index.write("?",0,1); 385 index.write(hlp=Integer.toString(akt.obj.get_def_kind().value()),0,hlp.length()); 386 index.write("?",0,1); 387 index.write(hlp = idl.name(),0,hlp.length()); index.write("?",0,1); 388 index.write(hlp = idl.version(),0,hlp.length()); index.newLine(); 389 } 390 if (akt.obj.get_def_kind().value()==DefinitionKind.dk_AModule) { 391 st.current = nfile; if (!st.current.exists()) if (!st.current.mkdir()) 394 throw new TIRExceptStorage("Can't create directory "+st.current+"."); 395 } else { 396 st.currentFile = nfile; st.curOutFile = null; 398 } 399 400 ((TIRImplObject) akt.obj).save(st); akt = akt.next; st.current = old; 403 st.currentFile = null; 404 } index.close(); 406 st.current = old; 407 } catch (IOException e) { 408 throw new TIRExceptStorage("Access error in "+st.current+"."); 409 } 410 } 412 413 public void load(Storage st) throws RemoteException , TIRExceptStorage{ 414 String [][] childs = st.listOfChilds(); 415 File old = st.current; 416 TIRImplObject m; 417 IdentificationImpl idl; 418 for(int i=0;i<childs.length;i++) { 419 switch (Integer.parseInt(childs[i][1])) { 420 case DefinitionKind.dk_AModule: 421 idl = new IdentificationImpl(((SIdentification) this.id).language(),this.id.absolute_name().name()+"::"+childs[i][2],""); 422 m = new AModuleDefImpl(idl,this, rep,StateKind.sk_normal,-1L); 423 st.current = new File (old,childs[i][0]); 424 m.load(st); 425 addListItem((TIRObject) m); 426 st.current = old; 427 break; 428 case DefinitionKind.dk_Architecture: 429 st.currentFile = new File (old,childs[i][0]); 430 st.curInFile = null; 431 m = new ArchitectureDefImpl(this, rep); 432 m.load(st); 433 addListItem((TIRObject) m); 434 st.currentFile = null; 435 break; 436 default: 437 throw new TIRExceptStorage("Unexpected kind of object in \"" 438 +st.current+File.separator+childs[i]+"\"."); 439 } 440 } 441 } 442 public AModuleDef create_amodule(Identification id) throws RemoteException , TIRExceptCreate, TIRExceptLock { 443 if (stKindImpl.value()==StateKind.sk_normal) 444 throw new TIRExceptCreate("you can call create method on work object"); 445 if (id!=null) { 446 if (!getCreateLock()) 447 throw new TIRExceptLock("This part of repository is locked for creating."); 448 if (!hasCont) 449 fromNormObj(); 450 id = new IdentificationImpl (((SIdentification)id).language(),id.absolute_name().name(),""); 451 Contained[] a = lookup_name(id.name()); 452 if (a.length != 0) { 453 if (a[0].get_def_kind().value() != DefinitionKind.dk_AModule) 454 throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository."); 455 boolean found = false; 456 for (int i=0;i<a.length;i++) { 457 if (((SIdentification) a[i].get_identification()).is_short_equal(id)) { 458 found = true; 459 break; 460 } 461 } 462 if (found) 463 throw new TIRExceptCreate("Object with same identification exists in repository."); 464 } 465 AModuleDef ret = new AModuleDefImpl(id, this, rep, StateKind.sk_work, workId); 467 addListItem(ret); return ret; } else 470 return null; 471 } 472 473 public ArchitectureDef create_architecture(Identification id, FrameDef frame, boolean system) throws RemoteException , TIRExceptCreate, TIRExceptLock { 474 if (stKindImpl.value()==StateKind.sk_normal) 475 throw new TIRExceptCreate("you can call create method on work object"); 476 if (id!=null && frame!=null) { 477 if (!getCreateLock()) 478 throw new TIRExceptLock("This part of repository is locked for creating."); 479 if (!hasCont) 480 fromNormObj(); 481 id = new IdentificationImpl(id); 482 Contained[] a = lookup_name(id.name()); 483 if (a.length != 0) { 484 if (a[0].get_def_kind().value() != DefinitionKind.dk_Architecture) 485 throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository."); 486 boolean found = false; 487 for (int i=0;i<a.length;i++) { 488 if (((SIdentification) a[i].get_identification()).is_short_equal(id)) { 489 found = true; 490 break; 491 } 492 } 493 if (found) 494 throw new TIRExceptCreate("Object with same identification exists in repository."); 495 } 496 ArchitectureDef ret = new ArchitectureDefImpl(id, this, rep, frame, StateKind.sk_work, system, workId); 498 addListItem(ret); return ret; } else 501 return null; 502 } 503 504 public void postLoad(RepositoryImpl r) throws RemoteException , TIRExceptStorage { 505 LiItem akt = firstChild; 506 for(int i=0;i<numOfItems;i++) { 507 ((TIRImplObject) akt.obj).postLoad(r); 508 akt = akt.next; 509 } 510 } 511 512 public boolean isNew() { 513 return ((stKindImpl.value()==StateKind.sk_work) && normal==null); 514 } 515 516 public void canCommit() throws RemoteException , TIRExceptCommit { 517 if (stKindImpl.value()==StateKind.sk_normal) 518 return; 519 LiItem akt; 520 int i,j; 521 akt = firstChild; 522 for (i=0;i<numOfItems;i++) { 523 if (((TIRImplObject)akt.obj).isNew() && !isNew()) { 524 LiItem nak = normal.firstChild; 525 for (j=0;j<normal.numOfItems;j++) { 526 if (((SIdentification) ((Contained)nak.obj).get_identification()).is_short_name_equal(((Contained)akt.obj).get_identification())) { 527 if (akt.obj.get_def_kind().value()!=nak.obj.get_def_kind().value()) 528 throw new TIRExceptCommit("Object with name \""+((SContained)nak.obj).get_absolute_name().name()+"\"exists."); 529 if (((SIdentification) ((Contained)nak.obj).get_identification()).is_short_equal(((Contained)akt.obj).get_identification())) 530 throw new TIRExceptCommit("Object with identification \""+((Contained)nak.obj).get_identification()+"\"in container \""+get_absolute_name().name()+"\" exists."); 531 } 532 nak = nak.next; 533 } 534 } 535 ((TIRImplObject)akt.obj).canCommit(); 536 akt = akt.next; 537 } 538 } 539 540 public void doCommit(Container in, Repository rep) throws RemoteException { 541 if (stKindImpl.value()==StateKind.sk_normal) 542 return; 543 LiItem akt = firstChild; 544 boolean nn = false; 545 int i; 546 if (isNew()) { 547 stKindImpl.toNormal(); 548 for(i=0;i<numOfItems;i++) { 549 ((TIRImplObject)akt.obj).doCommit(this,rep); 550 akt = akt.next; 551 } 552 parent = in; 553 this.rep = rep; 554 } else { 555 for(i=0;i<numOfItems;i++) { 556 if (((TIRImplObject)akt.obj).isNew()) 557 nn = true; 558 ((TIRImplObject)akt.obj).doCommit(normal,rep); 559 if (nn) 560 normal.addListItem(akt.obj); 561 akt = akt.next; 562 nn = false; 563 } 564 if (lockForMe) { 565 normal.lockFor = -1L; 566 lockCreate.unlock(); 567 lockForMe = false; 568 569 } 570 } 571 } 572 573 574 public long isLockedForCreate() throws RemoteException { 575 if ((stKindImpl.value()==StateKind.sk_work)) 576 if (normal==null) 577 return -1L; 578 if (lockCreate.isLocked()) { 579 return normal.lockFor; 580 } 581 return -1L; 582 } 583 584 585 public long canLock() throws RemoteException { 586 long a; 587 if ((a=isLockedForCreate())!=-1L) return a; 589 Container con = parent; 591 while (con.get_def_kind().value()!=DefinitionKind.dk_Repository && 592 con.get_def_kind().value()!=DefinitionKind.dk_WorkRepository) { 593 if ((a=((SContainer)con).isLockedForCreate())!=-1L) 594 return a; 595 con = ((Contained)con).get_defined_in(); 596 } 597 if ((a=canLockChild())!=-1L) 599 return a; 600 return -1L; 601 } 602 603 604 public long canLockChild() throws RemoteException { 605 LiItem akt = firstChild; 606 long a; 607 for(int i=0;i<numOfItems;i++) { 608 if (akt.obj.get_def_kind().value()==DefinitionKind.dk_AModule) { 609 if ((a=((AModuleDefImpl)akt.obj).isLockedForCreate())!=-1L) 610 return a; 611 else { 612 if ((a=((AModuleDefImpl)akt.obj).canLockChild())!=-1L) 613 return a; 614 } 615 } 616 } 617 return -1L; 618 } 619 620 621 boolean getCreateLock() throws RemoteException { 622 if (!isNew()) { 623 if (lockForMe) 624 return true; 625 if (lockCreate.isLocked()) 626 return false; 627 ((WorkRepositoryImpl)rep).normalRep.lockGetCreate.lock(); 628 long a = canLock(); 629 if ((a==-1L) || (a==workId)) { 630 lockForMe = true; 631 lockCreate.lock(); 632 normal.lockFor = workId; 633 } 634 ((WorkRepositoryImpl)rep).normalRep.lockGetCreate.unlock(); 635 return true; 636 } 637 return true; 638 } 639 640 public void doAbort(long workId) throws RemoteException { 641 LiItem akt = firstChild; 642 for(int i=0;i<numOfItems;i++) { 643 ((TIRImplObject)akt.obj).doAbort(workId); 645 akt = akt.next; 646 } 647 648 if (lockForMe) { 649 normal.lockFor = -1L; 650 lockCreate.unlock(); 651 lockForMe = false; 652 } 653 } 654 } 655 | Popular Tags |