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