1 2 package SOFA.SOFAnode.Made.TIR.Impl; 3 import java.io.BufferedReader ; 4 import java.io.BufferedWriter ; 5 import java.io.File ; 6 import java.io.FileReader ; 7 import java.io.FileWriter ; 8 import java.io.IOException ; 9 import java.rmi.RemoteException ; 10 11 import SOFA.SOFAnode.Made.TIR.ArrayDef; 12 import SOFA.SOFAnode.Made.TIR.CDLType; 13 import SOFA.SOFAnode.Made.TIR.ConstantDef; 14 import SOFA.SOFAnode.Made.TIR.Contained; 15 import SOFA.SOFAnode.Made.TIR.Container; 16 import SOFA.SOFAnode.Made.TIR.DefinitionKind; 17 import SOFA.SOFAnode.Made.TIR.EnumDef; 18 import SOFA.SOFAnode.Made.TIR.ExprBinOperationDef; 19 import SOFA.SOFAnode.Made.TIR.ExprBoolean; 20 import SOFA.SOFAnode.Made.TIR.ExprChar; 21 import SOFA.SOFAnode.Made.TIR.ExprConstant; 22 import SOFA.SOFAnode.Made.TIR.ExprDefault; 23 import SOFA.SOFAnode.Made.TIR.ExprDouble; 24 import SOFA.SOFAnode.Made.TIR.ExprEnum; 25 import SOFA.SOFAnode.Made.TIR.ExprFixed; 26 import SOFA.SOFAnode.Made.TIR.ExprFloat; 27 import SOFA.SOFAnode.Made.TIR.ExprLong; 28 import SOFA.SOFAnode.Made.TIR.ExprLongDouble; 29 import SOFA.SOFAnode.Made.TIR.ExprLongLong; 30 import SOFA.SOFAnode.Made.TIR.ExprOctet; 31 import SOFA.SOFAnode.Made.TIR.ExprOperDef; 32 import SOFA.SOFAnode.Made.TIR.ExprProperty; 33 import SOFA.SOFAnode.Made.TIR.ExprShort; 34 import SOFA.SOFAnode.Made.TIR.ExprString; 35 import SOFA.SOFAnode.Made.TIR.ExprUnOperationDef; 36 import SOFA.SOFAnode.Made.TIR.ExprUnsigLong; 37 import SOFA.SOFAnode.Made.TIR.ExprUnsigLongLong; 38 import SOFA.SOFAnode.Made.TIR.ExprUnsigShort; 39 import SOFA.SOFAnode.Made.TIR.ExprWchar; 40 import SOFA.SOFAnode.Made.TIR.ExprWstring; 41 import SOFA.SOFAnode.Made.TIR.FixedDef; 42 import SOFA.SOFAnode.Made.TIR.Identification; 43 import SOFA.SOFAnode.Made.TIR.ManageProfiles; 44 import SOFA.SOFAnode.Made.TIR.ManageRepository; 45 import SOFA.SOFAnode.Made.TIR.PrimitiveDef; 46 import SOFA.SOFAnode.Made.TIR.ProfileDef; 47 import SOFA.SOFAnode.Made.TIR.ProviderDef; 48 import SOFA.SOFAnode.Made.TIR.Repository; 49 import SOFA.SOFAnode.Made.TIR.SequenceDef; 50 import SOFA.SOFAnode.Made.TIR.StateKind; 51 import SOFA.SOFAnode.Made.TIR.StringDef; 52 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate; 53 import SOFA.SOFAnode.Made.TIR.TIRExceptLock; 54 import SOFA.SOFAnode.Made.TIR.TIRExceptSave; 55 import SOFA.SOFAnode.Made.TIR.TIRObject; 56 import SOFA.SOFAnode.Made.TIR.ValueGen; 57 import SOFA.SOFAnode.Made.TIR.WorkRepository; 58 import SOFA.SOFAnode.Made.TIR.WstringDef; 59 60 public class RepositoryImpl extends ContainerImpl implements Repository, ManageRepository, SRepository { 61 DefinitionKindImpl defKindImpl; 62 StateKindImpl stKindImpl; 63 Lock lock; Lock lockGetCreate; Lock lockCreate; long lockFor; Storage storage; 71 PrimitiveDef[] primitives; 72 73 String sofaNodeName; 74 long uniq; 76 LiItem firstWork; LiItem lastWork; 78 int numOfWorks; 79 80 ManageProfilesImpl mProfiles; 81 Lock lockProfiles; 83 public RepositoryImpl(String root, String sofaNodeName) throws RemoteException , TIRExceptStorage { 85 super(); 86 System.out.print("Starting repository..."); 87 lock = new Lock(false); 88 lockGetCreate = new Lock(false); 89 lockCreate = new Lock(false); 90 File f = new File (root); 91 if (!f.exists()) 92 throw new TIRExceptStorage(root+" doesn't exit."); 93 if (!f.isDirectory()) 94 throw new TIRExceptStorage(root+" isn't directory."); 95 File fc = new File (root); 96 storage = new Storage(f,fc); 97 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Repository); 98 stKindImpl = new StateKindImpl(StateKind.sk_normal); 99 primitives = new PrimitiveDef [17]; 100 for(int i=0;i<17;i++) { 101 primitives[i] = new PrimitiveDefImpl(i,StateKind.sk_normal); 102 } 103 mProfiles = new ManageProfilesImpl(this); 104 load(storage); 105 this.sofaNodeName = sofaNodeName; 106 System.out.println("OK"); 107 lockFor = -1L; 108 firstWork = lastWork = null; 109 numOfWorks = 0; 110 lockProfiles = new Lock(false); 111 } 112 113 public PrimitiveDef get_prim(int pkind) throws RemoteException { 114 return primitives[pkind]; 115 } 116 117 118 public void load(Storage st) throws RemoteException , TIRExceptStorage { 119 234 try { 235 File iname = new File (st.current, Storage.indexFile); 236 if ( ! iname.exists()) { 237 uniq = 0; 239 addListItem(new CDLRepositoryImpl(this)); 240 return; 241 } 242 BufferedReader index = new BufferedReader (new FileReader (iname.toString())); 243 String un = index.readLine(); 244 uniq = Long.parseLong(un); 245 index.close(); 246 247 boolean ex = true; 248 File old = st.current; 249 st.current = new File (st.current, "cdl"); 250 if ( ! st.current.exists()) 251 ex = false; 252 TIRImplObject m = new CDLRepositoryImpl(this); 253 if (ex) 254 m.load(st); 255 addListItem((TIRObject) m); 256 st.current = old; 257 258 260 postLoad(this); 261 st.current = new File (old,"profiles"); 262 mProfiles = new ManageProfilesImpl(this); 263 mProfiles.load(st); 264 st.current = old; 265 st.currentFile=null; 266 } catch (IOException e) { 267 throw new TIRExceptStorage("Access error in \""+st.current+"\"."); 268 } 269 } 270 271 public void postLoad(RepositoryImpl r) throws RemoteException , TIRExceptStorage { 272 LiItem akt = firstChild; 273 for(int i=0;i<numOfItems;i++) { 274 ((TIRImplObject) akt.obj).postLoad(r); 275 akt = akt.next; 276 } 277 } 278 279 280 public void save(Storage st) throws RemoteException , TIRExceptStorage { 281 try { 282 349 350 File f = new File (st.current,Storage.indexFile); BufferedWriter index = new BufferedWriter (new FileWriter (f.toString())); 353 String un = Long.toString(uniq); 354 index.write(un,0,un.length()); index.newLine(); 355 index.close(); 356 357 LiItem akt = firstChild; 359 File old = st.current; 360 for(int i=0;i<numOfItems;i++) { 361 String lang = ((SIdentification) ((Contained) akt.obj).get_identification()).language(); 362 f = new File (old, lang); 363 if ( ! f.exists()) { 364 if (!f.mkdir()) 365 throw new TIRExceptStorage("Can't create directory "+f+"."); 366 } 367 st.current = f; 368 ((TIRImplObject) akt.obj).save(st); akt = akt.next; } 372 st.current = new File (old,"profiles"); 373 if (!st.current.exists()) if (!st.current.mkdir()) 375 throw new TIRExceptStorage("Can't create directory "+st.current+"."); 376 mProfiles.save(st); 377 st.current = old; 378 } catch (IOException e) { 379 throw new TIRExceptStorage("Access error in "+st.current+"."); 380 } 381 } 382 383 384 void commitSave() throws RemoteException , TIRExceptSave { 385 try { 386 save(storage); 387 lock.unlock(); 388 } catch (TIRExceptStorage e) { 389 throw new TIRExceptSave("Error occured when saving repository.\nError: "+e.getMessage()); 390 } 391 } 392 393 public void save() throws RemoteException , TIRExceptSave { 394 try { 395 lock.lock(); 396 save(storage); 397 lock.unlock(); 398 } catch (TIRExceptStorage e) { 399 throw new TIRExceptSave("Error occured when saving repository.\nError: "+e.getMessage()); 400 } 401 } 402 403 public Contained[] contents(DefinitionKind type) throws RemoteException , TIRExceptLock { 404 if (lock.isLocked()) 405 throw new TIRExceptLock("Repository is locked"); 406 else 407 return super.contents(type); 408 } 409 410 public Contained lookup(Identification id) throws RemoteException , TIRExceptLock { 411 if (lock.isLocked()) 412 throw new TIRExceptLock("Repository is locked"); 413 else { 414 Contained ret = null; 415 LiItem akt = firstChild; 416 for(int i=0;i<numOfItems;i++) { 417 if (((SIdentification) ((Contained)akt.obj).get_identification()).language().compareTo(id.name())==0) { 418 ret = (Contained) akt.obj; 419 break; 420 } else { 421 akt = akt.next; 422 } 423 } 424 return ret; 425 } 426 } 427 428 public Contained lookup(String name, String version) throws RemoteException , TIRExceptLock { 429 if (lock.isLocked()) 430 throw new TIRExceptLock("Repository is locked"); 431 else { 432 Contained ret = null; 433 LiItem akt = firstChild; 434 for(int i=0;i<numOfItems;i++) { 435 if (((SIdentification) ((Contained)akt.obj).get_identification()).language().compareTo(name)==0) { 436 ret = (Contained) akt.obj; 437 break; 438 } else { 439 akt = akt.next; 440 } 441 } 442 return ret; 443 } 444 } 445 446 public Contained[] lookup_name(Identification id) throws RemoteException , TIRExceptLock { 447 if (lock.isLocked()) 448 throw new TIRExceptLock("Repository is locked"); 449 else 450 return super.lookup_name(id); 451 } 452 453 public Contained[] lookup_name(String id) throws RemoteException , TIRExceptLock { 454 if (lock.isLocked()) 455 throw new TIRExceptLock("Repository is locked"); 456 else 457 return super.lookup_name(id); 458 } 459 460 public Contained lookup_tag(String name, String tag) throws RemoteException , TIRExceptLock { 461 if (lock.isLocked()) 462 throw new TIRExceptLock("Repository is locked"); 463 else 464 return super.lookup_tag(name, tag); 465 } 466 467 public Contained[] lookup_branchtag(String name, String brtag) throws RemoteException , TIRExceptLock { 468 if (lock.isLocked()) 469 throw new TIRExceptLock("Repository is locked"); 470 else 471 return super.lookup_branchtag(name, brtag); 472 } 473 474 public Contained lookup_lastinbranch(String name, String brtag) throws RemoteException , TIRExceptLock { 475 if (lock.isLocked()) 476 throw new TIRExceptLock("Repository is locked"); 477 else 478 return super.lookup_lastinbranch(name, brtag); 479 } 480 481 public Contained lookup_lastfromversion(String name, String version) throws RemoteException , TIRExceptLock { 482 if (lock.isLocked()) 483 throw new TIRExceptLock("Repository is locked"); 484 else 485 return super.lookup_lastfromversion(name, version); 486 } 487 488 public Contained[] scontents(DefinitionKind type) throws RemoteException , TIRExceptLock { 489 492 return super.scontents(type); 493 } 494 495 public Contained slookup(Identification id) throws RemoteException , TIRExceptLock { 496 499 Contained ret = null; 500 LiItem akt = firstChild; 501 for(int i=0;i<numOfItems;i++) { 502 if (((SIdentification) ((Contained)akt.obj).get_identification()).language().compareTo(id.name())==0) { 503 ret = (Contained) akt.obj; 504 break; 505 } else { 506 akt = akt.next; 507 } 508 } 509 return ret; 510 } 511 512 public Contained slookup(String name, String version) throws RemoteException , TIRExceptLock { 513 516 Contained ret = null; 517 LiItem akt = firstChild; 518 for(int i=0;i<numOfItems;i++) { 519 if (((SIdentification) ((Contained)akt.obj).get_identification()).language().compareTo(name)==0) { 520 ret = (Contained) akt.obj; 521 break; 522 } else { 523 akt = akt.next; 524 } 525 } 526 return ret; 527 } 528 529 public Contained[] slookup_name(Identification id) throws RemoteException , TIRExceptLock { 530 533 Contained[] ret = null; 534 LiItem akt = firstChild; 535 for(int i=0;i<numOfItems;i++) { 536 if (((SIdentification) ((Contained)akt.obj).get_identification()).language().compareTo(id.name())==0) { 537 ret = new Contained [1]; 538 ret[0] = (Contained) akt.obj; 539 break; 540 } else { 541 akt = akt.next; 542 } 543 } 544 return ret; 545 } 546 547 public Contained[] slookup_name(String name) throws RemoteException , TIRExceptLock { 548 551 Contained[] ret = null; 552 LiItem akt = firstChild; 553 for(int i=0;i<numOfItems;i++) { 554 if (((SIdentification) ((Contained)akt.obj).get_identification()).language().compareTo(name)==0) { 555 ret = new Contained [1]; 556 ret[0] = (Contained) akt.obj; 557 break; 558 } else { 559 akt = akt.next; 560 } 561 } 562 return ret; 563 } 564 565 566 public DefinitionKind get_def_kind() throws RemoteException { 567 return (DefinitionKind) defKindImpl; 568 } 569 570 571 public StateKind get_state() throws RemoteException { 572 return (StateKind) stKindImpl; 573 } 574 575 public DefinitionKind get_spec_def_kind(int kind) throws RemoteException { 576 return new DefinitionKindImpl(kind); 577 } 578 579 580 public Identification create_identification(String lang, String name, String version) throws RemoteException , TIRExceptCreate { 581 throw new TIRExceptCreate("You can call create method on work object only"); 583 } 584 585 586 public Identification create_nextversion(Identification id) throws RemoteException , TIRExceptCreate { 587 throw new TIRExceptCreate("You can call create method on work object only"); 588 } 589 590 591 public Identification create_initialversion(String lang, String name) throws RemoteException , TIRExceptCreate { 592 throw new TIRExceptCreate("You can call create method on work object only"); 593 } 594 595 public Identification create_branch(Identification from, String branchTag) throws RemoteException , TIRExceptCreate { 596 throw new TIRExceptCreate("You can call create method on work object only"); 597 } 598 599 public PrimitiveDef get_primitive(int kind) throws RemoteException , TIRExceptCreate { 600 throw new TIRExceptCreate("You can call create method on work object only"); 601 } 602 603 public StringDef create_string(ExprOperDef bound) throws RemoteException , TIRExceptCreate { 604 throw new TIRExceptCreate("You can call create method on work object only"); 605 } 606 public WstringDef create_wstring(ExprOperDef bound) throws RemoteException , TIRExceptCreate { 607 throw new TIRExceptCreate("You can call create method on work object only"); 608 } 609 public SequenceDef create_sequence(ExprOperDef bound, CDLType element) throws RemoteException , TIRExceptCreate { 610 throw new TIRExceptCreate("You can call create method on work object only"); 611 } 612 public ArrayDef create_array(ExprOperDef length, CDLType element) throws RemoteException , TIRExceptCreate { 613 throw new TIRExceptCreate("You can call create method on work object only"); 614 } 615 public FixedDef create_fixed(ExprOperDef digits, ExprOperDef scale) throws RemoteException , TIRExceptCreate { 616 throw new TIRExceptCreate("You can call create method on work object only"); 617 } 618 619 public ProviderDef create_provider(Identification id) throws RemoteException , TIRExceptCreate { 620 throw new TIRExceptCreate("You can call create method on work object only"); 621 } 622 623 public ExprBoolean create_exprboolean(boolean val) throws RemoteException , TIRExceptCreate { 624 throw new TIRExceptCreate("You can call create method on work object only"); 625 } 626 public ExprShort create_exprshort(short val) throws RemoteException , TIRExceptCreate { 627 throw new TIRExceptCreate("You can call create method on work object only"); 628 } 629 public ExprOctet create_exproctet(byte val) throws RemoteException , TIRExceptCreate { 630 throw new TIRExceptCreate("You can call create method on work object only"); 631 } 632 633 public ExprLong create_exprlong(int val) throws RemoteException , TIRExceptCreate { 634 throw new TIRExceptCreate("You can call create method on work object only"); 635 } 636 public ExprLongLong create_exprlonglong(long val) throws RemoteException , TIRExceptCreate { 637 throw new TIRExceptCreate("You can call create method on work object only"); 638 } 639 public ExprUnsigShort create_exprunsigshort(short val) throws RemoteException , TIRExceptCreate { 640 throw new TIRExceptCreate("You can call create method on work object only"); 641 } 642 public ExprUnsigLong create_exprunsiglong(int val) throws RemoteException , TIRExceptCreate { 643 throw new TIRExceptCreate("You can call create method on work object only"); 644 } 645 public ExprUnsigLongLong create_exprunsiglonglong(long val) throws RemoteException , TIRExceptCreate { 646 throw new TIRExceptCreate("You can call create method on work object only"); 647 } 648 public ExprFloat create_exprfloat(float val) throws RemoteException , TIRExceptCreate { 649 throw new TIRExceptCreate("You can call create method on work object only"); 650 } 651 public ExprDouble create_exprdouble(double val) throws RemoteException , TIRExceptCreate { 652 throw new TIRExceptCreate("You can call create method on work object only"); 653 } 654 public ExprLongDouble create_exprlongdouble(double val) throws RemoteException , TIRExceptCreate { 655 throw new TIRExceptCreate("You can call create method on work object only"); 656 } 657 public ExprFixed create_exprfixed(java.math.BigDecimal val, int digits, int scale) throws RemoteException , TIRExceptCreate { 658 throw new TIRExceptCreate("You can call create method on work object only"); 659 } 660 public ExprProperty create_exprproperty(String pr) throws RemoteException , TIRExceptCreate { 661 throw new TIRExceptCreate("You can call create method on work object only"); 662 } 663 public ExprEnum create_exprenum(EnumDef en, String enMember) throws RemoteException , TIRExceptCreate { 664 throw new TIRExceptCreate("You can call create method on work object only"); 665 } 666 public ExprDefault create_exprdefault() throws RemoteException , TIRExceptCreate { 667 throw new TIRExceptCreate("You can call create method on work object only"); 668 } 669 public ExprChar create_exprchar(char val) throws RemoteException , TIRExceptCreate { 670 throw new TIRExceptCreate("You can call create method on work object only"); 671 } 672 public ExprWchar create_exprwchar(char val) throws RemoteException , TIRExceptCreate { 673 throw new TIRExceptCreate("You can call create method on work object only"); 674 } 675 public ExprString create_exprstring(String val) throws RemoteException , TIRExceptCreate { 676 throw new TIRExceptCreate("You can call create method on work object only"); 677 } 678 public ExprWstring create_exprwstring(String val) throws RemoteException , TIRExceptCreate { 679 throw new TIRExceptCreate("You can call create method on work object only"); 680 } 681 public ExprConstant create_exprconstant(ConstantDef val) throws RemoteException , TIRExceptCreate { 682 throw new TIRExceptCreate("You can call create method on work object only"); 683 } 684 public ExprUnOperationDef create_exprunoperation(int kind, ExprOperDef op) throws RemoteException , TIRExceptCreate { 685 throw new TIRExceptCreate("You can call create method on work object only"); 686 } 687 public ExprBinOperationDef create_exprbinoperation(int kind, ExprOperDef op1, ExprOperDef op2) throws RemoteException , TIRExceptCreate { 688 throw new TIRExceptCreate("You can call create method on work object only"); 689 } 690 public ValueGen create_valuegen() throws RemoteException , TIRExceptCreate { 691 throw new TIRExceptCreate("You can call create method on work object only"); 692 } 693 694 synchronized public WorkRepository beginChanges(ProfileDef p) throws RemoteException , TIRExceptCreate, TIRExceptLock { 695 WorkRepository wr = new WorkRepositoryImpl(this,p); 696 addWork(wr); 697 return wr; 698 } 699 700 public void addWork(WorkRepository wr) { 701 LiItem item = new LiItem(wr); 702 if (firstWork == null) { 703 firstWork = lastWork = item; 704 } else { 705 LiItem akt = firstWork; 706 while (akt.next != null) akt = akt.next; 707 akt.next = item; 708 item.prev = akt; 709 lastWork = item; 710 } 711 numOfWorks++; 712 } 713 714 public void removeWork(long workId) { 715 if (numOfWorks>0) { 716 LiItem akt = firstWork; 717 boolean found = false; 718 for (int i=0;i<numOfWorks;i++) { 719 if (((WorkRepositoryImpl)akt.obj).workId == workId) { 720 found = true; 721 break; 722 } 723 akt = akt.next; 724 } 725 if (!found) 726 return; 727 if (numOfWorks==1) { 728 firstWork = lastWork = null; 729 numOfWorks = 0; 730 } else { 731 if (akt == firstWork) { 732 firstWork = akt.next; 733 firstWork.prev = null; 734 akt.next = null; 735 } else { 737 if (lastWork == akt) { 738 lastWork = akt.prev; 739 lastWork.next = null; 740 akt.prev = null; 741 } else { 743 akt.prev.next = akt.next; 744 akt.next.prev = akt.prev; 745 } 747 } 748 numOfWorks--; 749 } 750 } 751 } 752 753 synchronized public long getUniqueNumber() { 754 long a = uniq; 755 uniq++; 756 return a; 757 } 758 759 public boolean isNew() { 760 return false; 761 } 762 763 public void canCommit() {;} 764 765 public void doCommit(Container in, Repository rep) {;} 766 public void doAbort(long workId) throws RemoteException {} 767 768 public long isLockedForCreate() { 769 if (lockCreate.isLocked()) 770 return lockFor; 771 return -1L; 772 } 773 774 public ManageRepository getManage() throws RemoteException { 775 return (ManageRepository) this; 776 } 777 778 public WorkRepository[] getActiveWork() throws RemoteException { 779 LiItem akt = firstWork; 780 WorkRepository[] ret = new WorkRepository [numOfWorks]; 781 for (int i=0;i<numOfWorks;i++) { 782 ret[i] = (WorkRepository) akt.obj; 783 akt = akt.next; 784 } 785 return ret; 786 } 787 788 public ManageProfiles getProfiles() throws RemoteException { 789 return mProfiles; 790 } 791 } 792 | Popular Tags |