KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > TIR > Impl > ModuleDefImpl


1 /* $Id: ModuleDefImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.BufferedWriter JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.rmi.RemoteException JavaDoc;
8
9 import SOFA.SOFAnode.Made.TIR.AbsoluteName;
10 import SOFA.SOFAnode.Made.TIR.CDLType;
11 import SOFA.SOFAnode.Made.TIR.ConstantDef;
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.ExceptionDef;
17 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
18 import SOFA.SOFAnode.Made.TIR.FrameDef;
19 import SOFA.SOFAnode.Made.TIR.Identification;
20 import SOFA.SOFAnode.Made.TIR.InterfaceDef;
21 import SOFA.SOFAnode.Made.TIR.ModuleDef;
22 import SOFA.SOFAnode.Made.TIR.Repository;
23 import SOFA.SOFAnode.Made.TIR.StateKind;
24 import SOFA.SOFAnode.Made.TIR.StructDef;
25 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
26 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
27 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
28 import SOFA.SOFAnode.Made.TIR.TIRObject;
29 import SOFA.SOFAnode.Made.TIR.TypedefDef;
30 import SOFA.SOFAnode.Made.TIR.UnionDef;
31
32 public class ModuleDefImpl extends CDLContainerImpl implements ModuleDef, SContained {
33   protected Identification id;
34   protected Container parent;
35   protected Repository rep;
36   DefinitionKindImpl defKindImpl;
37   StateKindImpl stKindImpl;
38
39   ModuleDefImpl normal;
40   
41   Lock lockCreate;
42   boolean lockForMe; // used in work object
43
public long lockFor; // used in normal object
44
public long workId; // id of "transaction"
45

46   public ModuleDefImpl(Identification ident, Container in, Repository inrep, int state, long workId) throws RemoteException JavaDoc {
47     id = ident;
48     parent = in;
49     rep = inrep;
50     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Module);
51     stKindImpl = new StateKindImpl(state);
52     normal = null;
53     lockCreate = new Lock(false);
54     lockForMe = false;
55     lockFor = -1L;
56     this.workId = workId;
57   }
58
59   public ModuleDefImpl(Container in, Repository inrep) throws RemoteException JavaDoc {
60     parent = in;
61     rep = inrep;
62     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Module);
63     stKindImpl = new StateKindImpl(StateKind.sk_normal);
64     normal = null;
65     lockCreate = new Lock(false);
66     lockForMe = false;
67     workId = -1L;
68     lockFor = -1L;
69   }
70
71   public ModuleDefImpl(ModuleDefImpl a, Container in, Repository inrep, long workId) throws RemoteException JavaDoc {
72     normal = a;
73     parent = in;
74     rep = inrep;
75     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Module);
76     stKindImpl = new StateKindImpl(StateKind.sk_work);
77     id = a.id;
78     lockCreate = a.lockCreate;
79     lockForMe = false;
80     this.workId = workId;
81     lockFor = -1L;
82   }
83   
84   /* form interface Contained */
85   public Identification get_identification() throws RemoteException JavaDoc {
86     return id;
87   }
88
89   /* form interface Contained */
90   public Container get_defined_in() throws RemoteException JavaDoc {
91     return parent;
92   }
93
94   /* form interface Contained */
95   public Container get_containing_repository() throws RemoteException JavaDoc {
96     return rep;
97   }
98
99   /* form interface Contained */
100   public AbsoluteName get_absolute_name() throws RemoteException JavaDoc {
101     AbsoluteNameImpl absName;
102     if (rep == parent) { // object is contained in repository
103
absName = new AbsoluteNameImpl("::"+id.name());
104     } else {
105       absName = new AbsoluteNameImpl(((SContained)parent).get_absolute_name().name()+"::"+id.name());
106     }
107     return (AbsoluteName) absName;
108   }
109
110   /* from interface TIRObject */
111   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
112     return (DefinitionKind) defKindImpl;
113   }
114   
115   public void tag(String JavaDoc t) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
116     if (stKindImpl.value()!=StateKind.sk_work)
117       throw new TIRExceptCreate("you can call create method on work object");
118     String JavaDoc ebt = id.branchTag();
119     if (t.compareTo(ebt)==0)
120       throw new TIRExceptCreate("This tag exists");
121     String JavaDoc[] et = id.tag();
122     int i,j;
123     for (i=0;i<et.length;i++) {
124       if (t.compareTo(et[i])==0)
125        throw new TIRExceptCreate("This tag exists");
126     }
127     Contained[] sibl = parent.lookup_name(id);
128     for (j=0;j<sibl.length;j++) {
129       Identification idl = sibl[j].get_identification();
130       
131       ebt = idl.branchTag();
132       if (t.compareTo(ebt)==0)
133         throw new TIRExceptCreate("This tag exists");
134       et = idl.tag();
135       for (i=0;i<et.length;i++) {
136         if (t.compareTo(et[i])==0)
137           throw new TIRExceptCreate("This tag exists");
138       }
139     }
140     ((SIdentification) id).tag(t);
141   }
142
143   private void fromNormObj() throws RemoteException JavaDoc, TIRExceptLock {
144     if (normal==null)
145       return;
146     if (((WorkRepositoryImpl)rep).lock.isLocked())
147       throw new TIRExceptLock("Repository is locked");
148     int i;
149     LiItem akt;
150     akt = normal.firstChild;
151     for(i=0;i<normal.numOfItems;i++) {
152       addListItem(WorkRepositoryImpl.newWorkFromNormal(akt.obj,rep,this,workId));
153       akt = akt.next;
154     }
155     hasCont = true;
156   }
157
158   public Contained[] contents(DefinitionKind type) throws RemoteException JavaDoc, TIRExceptLock {
159     if (stKindImpl.value()==StateKind.sk_normal) {
160       if (((RepositoryImpl) rep).lock.isLocked())
161         throw new TIRExceptLock("Repository is locked");
162       else
163         return super.contents(type);
164     } else {
165       if (!hasCont)
166         fromNormObj();
167       if (((WorkRepositoryImpl) rep).lock.isLocked())
168         throw new TIRExceptLock("Repository is locked");
169       else
170         return super.contents(type);
171     }
172   }
173
174   public Contained lookup(Identification id) throws RemoteException JavaDoc, TIRExceptLock {
175     if (stKindImpl.value()==StateKind.sk_normal) {
176       if (((RepositoryImpl) rep).lock.isLocked())
177         throw new TIRExceptLock("Repository is locked");
178       else
179         return super.lookup(id);
180     } else {
181       if (!hasCont)
182         fromNormObj();
183       if (((WorkRepositoryImpl) rep).lock.isLocked())
184         throw new TIRExceptLock("Repository is locked");
185       else
186         return super.lookup(id);
187     }
188   }
189
190   public Contained lookup(String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
191     if (stKindImpl.value()==StateKind.sk_normal) {
192       if (((RepositoryImpl) rep).lock.isLocked())
193         throw new TIRExceptLock("Repository is locked");
194       else
195         return super.lookup(name, version);
196     } else {
197       if (!hasCont)
198         fromNormObj();
199       if (((WorkRepositoryImpl) rep).lock.isLocked())
200         throw new TIRExceptLock("Repository is locked");
201       else
202         return super.lookup(name, version);
203     }
204   }
205
206   public Contained[] lookup_name(Identification id) throws RemoteException JavaDoc, TIRExceptLock {
207     if (stKindImpl.value()==StateKind.sk_normal) {
208       if (((RepositoryImpl) rep).lock.isLocked())
209         throw new TIRExceptLock("Repository is locked");
210       else
211         return super.lookup_name(id);
212     } else {
213       if (!hasCont)
214         fromNormObj();
215       if (((WorkRepositoryImpl) rep).lock.isLocked())
216         throw new TIRExceptLock("Repository is locked");
217       else
218         return super.lookup_name(id);
219     }
220   }
221
222   public Contained[] lookup_name(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptLock {
223     if (stKindImpl.value()==StateKind.sk_normal) {
224       if (((RepositoryImpl) rep).lock.isLocked())
225         throw new TIRExceptLock("Repository is locked");
226       else
227         return super.lookup_name(name);
228     } else {
229       if (!hasCont)
230         fromNormObj();
231       if (((WorkRepositoryImpl) rep).lock.isLocked())
232         throw new TIRExceptLock("Repository is locked");
233       else
234         return super.lookup_name(name);
235     }
236   }
237
238   public Contained lookup_tag(String JavaDoc name, String JavaDoc tag) throws RemoteException JavaDoc, TIRExceptLock {
239     if (stKindImpl.value()==StateKind.sk_normal) {
240       if (((RepositoryImpl) rep).lock.isLocked())
241         throw new TIRExceptLock("Repository is locked");
242       else
243         return super.lookup_tag(name, tag);
244     } else {
245       if (!hasCont)
246         fromNormObj();
247       if (((WorkRepositoryImpl) rep).lock.isLocked())
248         throw new TIRExceptLock("Repository is locked");
249       else
250         return super.lookup_tag(name, tag);
251     }
252   }
253
254   public Contained[] lookup_branchtag(String JavaDoc name, String JavaDoc brtag) throws RemoteException JavaDoc, TIRExceptLock {
255     if (stKindImpl.value()==StateKind.sk_normal) {
256       if (((RepositoryImpl) rep).lock.isLocked())
257         throw new TIRExceptLock("Repository is locked");
258       else
259         return super.lookup_branchtag(name, brtag);
260     } else {
261       if (!hasCont)
262         fromNormObj();
263       if (((WorkRepositoryImpl) rep).lock.isLocked())
264         throw new TIRExceptLock("Repository is locked");
265       else
266         return super.lookup_branchtag(name, brtag);
267     }
268   }
269
270   public Contained lookup_lastinbranch(String JavaDoc name, String JavaDoc brtag) throws RemoteException JavaDoc, TIRExceptLock {
271     if (stKindImpl.value()==StateKind.sk_normal) {
272       if (((RepositoryImpl) rep).lock.isLocked())
273         throw new TIRExceptLock("Repository is locked");
274       else
275         return super.lookup_lastinbranch(name, brtag);
276     } else {
277       if (!hasCont)
278         fromNormObj();
279       if (((WorkRepositoryImpl) rep).lock.isLocked())
280         throw new TIRExceptLock("Repository is locked");
281       else
282         return super.lookup_lastinbranch(name, brtag);
283     }
284   }
285   
286   public Contained lookup_lastfromversion(String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
287     if (stKindImpl.value()==StateKind.sk_normal) {
288       if (((RepositoryImpl) rep).lock.isLocked())
289         throw new TIRExceptLock("Repository is locked");
290       else
291         return super.lookup_lastfromversion(name, version);
292     } else {
293       if (!hasCont)
294         fromNormObj();
295       if (((WorkRepositoryImpl) rep).lock.isLocked())
296         throw new TIRExceptLock("Repository is locked");
297       else
298         return super.lookup_lastfromversion(name, version);
299     }
300   }
301
302   public Contained[] scontents(DefinitionKind type) throws RemoteException JavaDoc, TIRExceptLock {
303     if (stKindImpl.value()==StateKind.sk_normal) {
304       return super.scontents(type);
305     } else {
306       if (!hasCont)
307         fromNormObj();
308       return super.scontents(type);
309     }
310   }
311
312   public Contained slookup(Identification id) throws RemoteException JavaDoc, TIRExceptLock {
313     if (stKindImpl.value()==StateKind.sk_normal) {
314         return super.slookup(id);
315     } else {
316       if (!hasCont)
317         fromNormObj();
318       return super.slookup(id);
319     }
320   }
321
322   public Contained slookup(String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
323     if (stKindImpl.value()==StateKind.sk_normal) {
324         return super.slookup(name, version);
325     } else {
326       if (!hasCont)
327         fromNormObj();
328       return super.slookup(name, version);
329     }
330   }
331
332   public Contained[] slookup_name(Identification id) throws RemoteException JavaDoc, TIRExceptLock {
333     if (stKindImpl.value()==StateKind.sk_normal) {
334         return super.slookup_name(id);
335     } else {
336       if (!hasCont)
337         fromNormObj();
338       return super.slookup_name(id);
339     }
340   }
341
342   public Contained[] slookup_name(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptLock {
343     if (stKindImpl.value()==StateKind.sk_normal) {
344       return super.slookup_name(name);
345     } else {
346       if (!hasCont)
347         fromNormObj();
348       return super.slookup_name(name);
349     }
350   }
351   
352   /* from interface TIRObject */
353   public StateKind get_state() throws RemoteException JavaDoc {
354     return (StateKind) stKindImpl;
355   }
356
357   /*from interface TIRImplObject */
358   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
359     try {
360       String JavaDoc[][] childs = null;
361       try {
362         childs = st.listOfChilds();
363       } catch (TIRExceptStorage e) { // new dir without index.tir
364
;
365       }
366       File JavaDoc f = new File JavaDoc(st.current,Storage.indexFile); // name of index file
367
// ((IdentificationImpl)id).save(f); //saving identification
368
LiItem akt = firstChild;
369       File JavaDoc old = st.current;
370       BufferedWriter JavaDoc index = new BufferedWriter JavaDoc(new FileWriter JavaDoc(f.toString()));
371       index.write("CHILDS",0,6); index.newLine();
372       for(int i=0;i<numOfItems;i++) {
373         String JavaDoc[] achild; // actual child info
374
IdentificationImpl idl; // actual child id
375
File JavaDoc nfile; // path to child's current path
376
if ((achild=st.existsChild(childs,idl = (IdentificationImpl)((Contained)akt.obj).get_identification()))!=null) { // file (dir) exists
377
// saving info to index file
378
index.write(achild[0],0,achild[0].length()); index.write("?",0,1);
379           index.write(achild[1],0,achild[1].length()); index.write("?",0,1);
380           index.write(achild[2],0,achild[2].length()); index.write("?",0,1);
381           index.write(achild[3],0,achild[3].length()); index.newLine();
382       nfile = new File JavaDoc(old,achild[0]);
383       
384         } else { //file (dir) doesn't exist
385
String JavaDoc hlp;
386       //if (idl.name().length() <= Storage.maxLenFileName)
387
// nfile = new File(old,idl.name()); // file (dir) name same as id
388
//else
389
nfile = File.createTempFile("tir","",old);
390           nfile.delete();
391       // saving info to index file
392
index.write(hlp = nfile.getName(),0,hlp.length()); index.write("?",0,1);
393       index.write(hlp=Integer.toString(akt.obj.get_def_kind().value()),0,hlp.length());
394       index.write("?",0,1);
395       index.write(hlp = idl.name(),0,hlp.length()); index.write("?",0,1);
396           index.write(hlp = idl.version(),0,hlp.length()); index.newLine();
397         }
398         if (akt.obj.get_def_kind().value()==DefinitionKind.dk_Module) {
399       st.current = nfile; // directory for module
400
if (!st.current.exists()) //create only if doesn't exist
401
if (!st.current.mkdir())
402           throw new TIRExceptStorage("Can't create directory "+st.current+".");
403     } else {
404       st.currentFile = nfile; // file for all except module
405
st.curOutFile = null;
406     }
407         
408     ((TIRImplObject) akt.obj).save(st); // save akt object
409
akt = akt.next; // go to the next object
410
st.current = old;
411         st.currentFile = null;
412       } // -- end of for
413
index.close();
414       st.current = old;
415     } catch (IOException JavaDoc e) {
416       throw new TIRExceptStorage("Access error in "+st.current+".");
417     }
418   }
419
420   /*from interface TIRImplObject */
421   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage{
422     String JavaDoc[][] childs = st.listOfChilds();
423     File JavaDoc old = st.current;
424     TIRImplObject m;
425     for(int i=0;i<childs.length;i++) {
426       switch (Integer.parseInt(childs[i][1])) {
427         case DefinitionKind.dk_Module:
428       IdentificationImpl idl = new IdentificationImpl(((SIdentification)id).language(), this.id.absolute_name().name()+"::"+childs[i][2],"");
429       m = new ModuleDefImpl((Identification)idl,this, rep,StateKind.sk_normal,-1L);
430           st.current = new File JavaDoc(old,childs[i][0]);
431           m.load(st);
432       addListItem((TIRObject) m);
433       st.current = old;
434       break;
435     case DefinitionKind.dk_Struct:
436       st.currentFile = new File JavaDoc(old,childs[i][0]);
437       st.curInFile = null;
438       m = new StructDefImpl(this,rep);
439       m.load(st);
440           addListItem((TIRObject) m);
441       st.currentFile = null;
442       break;
443     case DefinitionKind.dk_Exception:
444       st.currentFile = new File JavaDoc(old,childs[i][0]);
445       st.curInFile = null;
446       m = new ExceptionDefImpl(this,rep);
447       m.load(st);
448           addListItem((TIRObject) m);
449       st.currentFile = null;
450       break;
451     case DefinitionKind.dk_Enum:
452       st.currentFile = new File JavaDoc(old,childs[i][0]);
453       st.curInFile = null;
454       m = new EnumDefImpl(this,rep);
455       m.load(st);
456           addListItem((TIRObject) m);
457       st.currentFile = null;
458       break;
459     case DefinitionKind.dk_Union:
460       st.currentFile = new File JavaDoc(old,childs[i][0]);
461       st.curInFile = null;
462       m = new UnionDefImpl(this,rep);
463       m.load(st);
464           addListItem((TIRObject) m);
465       st.currentFile = null;
466       break;
467     case DefinitionKind.dk_Constant:
468       st.currentFile = new File JavaDoc(old,childs[i][0]);
469       st.curInFile = null;
470       m = new ConstantDefImpl(this,rep);
471       m.load(st);
472           addListItem((TIRObject) m);
473       st.currentFile = null;
474       break;
475     case DefinitionKind.dk_Typedef:
476       st.currentFile = new File JavaDoc(old,childs[i][0]);
477       st.curInFile = null;
478       m = new TypedefDefImpl(this,rep);
479       m.load(st);
480           addListItem((TIRObject) m);
481       st.currentFile = null;
482       break;
483     case DefinitionKind.dk_Interface:
484       st.currentFile = new File JavaDoc(old,childs[i][0]);
485       st.curInFile = null;
486       m = new InterfaceDefImpl(this,rep);
487       m.load(st);
488           addListItem((TIRObject) m);
489       st.currentFile = null;
490       break;
491     case DefinitionKind.dk_Frame:
492       st.currentFile = new File JavaDoc(old,childs[i][0]);
493       st.curInFile = null;
494       m = new FrameDefImpl(this,rep);
495       m.load(st);
496           addListItem((TIRObject) m);
497       st.currentFile = null;
498       break;
499     default:
500       throw new TIRExceptStorage("Unexpected kind of object in \""
501                                   +st.current+File.separator+childs[i]+"\".");
502       }
503     }
504   }
505
506   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, 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   // test if id of new object is correct
515
private boolean testIdentification(Identification newId) throws RemoteException JavaDoc {
516     AbsoluteName newAbs = newId.lang_absolute_name();
517     AbsoluteName abs = id.lang_absolute_name();
518     if (newAbs.size() != (abs.size() + 1))
519       return false;
520     int len = abs.size();
521     for (int i=0;i<len;i++) {
522       if (abs.elementAt(i).compareTo(newAbs.elementAt(i))!=0)
523         return false;
524     }
525     return true;
526   }
527
528   public ModuleDef create_module(Identification id) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
529     if (stKindImpl.value()==StateKind.sk_normal)
530       throw new TIRExceptCreate("you can call create method on work object");
531     if (id!=null) {
532       if (!testIdentification(id))
533         throw new TIRExceptCreate("Wrong identification for this object.");
534       if (!getCreateLock())
535         throw new TIRExceptLock("This part of repository is locked for creating.");
536       if (!hasCont)
537         fromNormObj();
538       id = new IdentificationImpl(((SIdentification)id).language(), id.absolute_name().name(),"");
539       Contained[] a = lookup_name(id.name());
540       if (a.length != 0) {
541         if (a[0].get_def_kind().value() != DefinitionKind.dk_Module)
542       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
543     boolean found = false;
544     for (int i=0;i<a.length;i++) {
545       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
546         found = true;
547         break;
548       }
549     }
550     if (found)
551       throw new TIRExceptCreate("Object with same identification exists in repository.");
552       }
553       // creating new object
554
ModuleDef ret = new ModuleDefImpl(id, this, rep, StateKind.sk_work, workId);
555       addListItem(ret); // adding object to list
556
return ret; // return object
557
} else
558       return null;
559   }
560
561   public ConstantDef create_constant(Identification id, CDLType type, ExprOperDef value) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
562     if (stKindImpl.value()==StateKind.sk_normal)
563       throw new TIRExceptCreate("you can call create method on work object");
564     if (id==null || type==null || value==null)
565       return null;
566     else {
567       if (type.get_state().value()==StateKind.sk_work &&
568           value.get_state().value()==StateKind.sk_work) {
569         if (!testIdentification(id))
570           throw new TIRExceptCreate("Wrong identification for this object.");
571         if (!getCreateLock())
572           throw new TIRExceptLock("This part of repository is locked for creating.");
573         if (!hasCont)
574           fromNormObj();
575         id = new IdentificationImpl(id);
576         Contained[] a = lookup_name(id.name());
577         if (a.length != 0) {
578           if (a[0].get_def_kind().value() != DefinitionKind.dk_Constant)
579         throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
580       boolean found = false;
581       for (int i=0;i<a.length;i++) {
582         if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
583           found = true;
584           break;
585         }
586       }
587       if (found)
588         throw new TIRExceptCreate("Object with same identification exists in repository.");
589         }
590         // creating new object
591
ConstantDef ret = new ConstantDefImpl(id, this, rep,type, value, workId);
592         addListItem(ret); // adding object to list
593
return ret; // return object
594
} else {
595         throw new TIRExceptCreate("Given objects aren't work.");
596       }
597     }
598   }
599
600   public StructDef create_struct(Identification id) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
601     if (stKindImpl.value()==StateKind.sk_normal)
602       throw new TIRExceptCreate("you can call create method on work object");
603     if (id!=null) {
604       if (!testIdentification(id))
605         throw new TIRExceptCreate("Wrong identification for this object.");
606       if (!getCreateLock())
607         throw new TIRExceptLock("This part of repository is locked for creating.");
608       if (!hasCont)
609         fromNormObj();
610       id = new IdentificationImpl(id);
611       Contained[] a = lookup_name(id.name());
612       if (a.length != 0) {
613         if (a[0].get_def_kind().value() != DefinitionKind.dk_Struct)
614       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
615     boolean found = false;
616     for (int i=0;i<a.length;i++) {
617       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
618         found = true;
619         break;
620       }
621     }
622     if (found)
623       throw new TIRExceptCreate("Object with same identification exists in repository.");
624       }
625       // creating new object
626
StructDef ret = new StructDefImpl(id, this, rep ,workId);
627       addListItem(ret); // adding object to list
628
return ret; // return object
629
} else
630       return null;
631   }
632
633   public UnionDef create_union(Identification id, CDLType switch_type) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
634     if (stKindImpl.value()==StateKind.sk_normal)
635       throw new TIRExceptCreate("you can call create method on work object");
636     if (id==null || switch_type==null)
637       return null;
638     else {
639       if (switch_type.get_state().value()==StateKind.sk_work ) {
640         if (!testIdentification(id))
641           throw new TIRExceptCreate("Wrong identification for this object.");
642         if (!getCreateLock())
643           throw new TIRExceptLock("This part of repository is locked for creating.");
644         if (!hasCont)
645           fromNormObj();
646         id = new IdentificationImpl(id);
647         Contained[] a = lookup_name(id.name());
648         if (a.length != 0) {
649           if (a[0].get_def_kind().value() != DefinitionKind.dk_Union)
650         throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
651       boolean found = false;
652       for (int i=0;i<a.length;i++) {
653         if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
654           found = true;
655           break;
656         }
657       }
658       if (found)
659         throw new TIRExceptCreate("Object with same identification exists in repository.");
660         }
661         // creating new object
662
UnionDef ret = new UnionDefImpl(id, this, rep, switch_type, workId);
663         addListItem(ret); // adding object to list
664
return ret; // return object
665
} else {
666         throw new TIRExceptCreate("Given object isn't work.");
667       }
668     }
669   }
670
671   public EnumDef create_enum(Identification id) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
672     if (stKindImpl.value()==StateKind.sk_normal)
673       throw new TIRExceptCreate("you can call create method on work object");
674     if (id!=null) {
675       if (!testIdentification(id))
676         throw new TIRExceptCreate("Wrong identification for this object.");
677       if (!getCreateLock())
678         throw new TIRExceptLock("This part of repository is locked for creating.");
679       if (!hasCont)
680         fromNormObj();
681       id = new IdentificationImpl(id);
682       Contained[] a = lookup_name(id.name());
683       if (a.length != 0) {
684         if (a[0].get_def_kind().value() != DefinitionKind.dk_Enum)
685       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
686     boolean found = false;
687     for (int i=0;i<a.length;i++) {
688       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
689         found = true;
690         break;
691       }
692     }
693     if (found)
694       throw new TIRExceptCreate("Object with same identification exists in repository.");
695       }
696       // creating new object
697
EnumDef ret = new EnumDefImpl(id, this, rep, workId);
698       addListItem(ret); // adding object to list
699
return ret; // return object
700
} else
701       return null;
702   }
703
704   public TypedefDef create_typedef(Identification id, CDLType orig_type) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
705     if (stKindImpl.value()==StateKind.sk_normal)
706       throw new TIRExceptCreate("you can call create method on work object");
707     if (id==null || orig_type==null)
708       return null;
709     else {
710       if (orig_type.get_state().value()==StateKind.sk_work ) {
711         if (!testIdentification(id))
712           throw new TIRExceptCreate("Wrong identification for this object.");
713         if (!getCreateLock())
714           throw new TIRExceptLock("This part of repository is locked for creating.");
715         if (!hasCont)
716           fromNormObj();
717         id = new IdentificationImpl(id);
718         Contained[] a = lookup_name(id.name());
719         if (a.length != 0) {
720           if (a[0].get_def_kind().value() != DefinitionKind.dk_Typedef)
721         throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
722       boolean found = false;
723       for (int i=0;i<a.length;i++) {
724         if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
725           found = true;
726           break;
727         }
728       }
729       if (found)
730         throw new TIRExceptCreate("Object with same identification exists in repository.");
731         }
732         // creating new object
733
TypedefDef ret = new TypedefDefImpl(id, this, rep, orig_type, workId);
734         addListItem(ret); // adding object to list
735
return ret; // return object
736
} else {
737         throw new TIRExceptCreate("Given object isn't work.");
738       }
739     }
740   }
741
742   public ExceptionDef create_exception(Identification id) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
743     if (stKindImpl.value()==StateKind.sk_normal)
744       throw new TIRExceptCreate("you can call create method on work object");
745     if (id!=null) {
746       if (!testIdentification(id))
747         throw new TIRExceptCreate("Wrong identification for this object.");
748       if (!getCreateLock())
749         throw new TIRExceptLock("This part of repository is locked for creating.");
750       if (!hasCont)
751         fromNormObj();
752       id = new IdentificationImpl(id);
753       Contained[] a = lookup_name(id.name());
754       if (a.length != 0) {
755         if (a[0].get_def_kind().value() != DefinitionKind.dk_Exception)
756       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
757     boolean found = false;
758     for (int i=0;i<a.length;i++) {
759       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
760         found = true;
761         break;
762       }
763     }
764     if (found)
765       throw new TIRExceptCreate("Object with same identification exists in repository.");
766       }
767       // creating new object
768
ExceptionDef ret = new ExceptionDefImpl(id, this, rep, workId);
769       addListItem(ret); // adding object to list
770
return ret; // return object
771
} else
772       return null;
773   }
774
775   public InterfaceDef create_interface(Identification id, InterfaceDef[] base_interfaces) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
776     if (stKindImpl.value()==StateKind.sk_normal)
777       throw new TIRExceptCreate("you can call create method on work object");
778     int i;
779     if (id==null || base_interfaces==null) {
780       return null;
781     } else {
782       if (!testIdentification(id))
783         throw new TIRExceptCreate("Wrong identification for this object.");
784       if (!getCreateLock())
785         throw new TIRExceptLock("This part of repository is locked for creating.");
786       if (!hasCont)
787         fromNormObj();
788       id = new IdentificationImpl(id);
789       Contained[] a = lookup_name(id.name());
790       if (a.length != 0) {
791         if (a[0].get_def_kind().value() != DefinitionKind.dk_Interface)
792       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
793     boolean found = false;
794     for (i=0;i<a.length;i++) {
795       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
796         found = true;
797         break;
798       }
799     }
800     if (found)
801       throw new TIRExceptCreate("Object with same identification exists in repository.");
802       }
803       // creating new object
804
InterfaceDef[] bi = new InterfaceDef [base_interfaces.length];
805       for (i=0;i<base_interfaces.length;i++) {
806         if (base_interfaces[i].get_state().value()==StateKind.sk_work)
807       bi[i]=base_interfaces[i];
808     else
809       throw new TIRExceptCreate("Given objects aren't work.");
810       }
811       InterfaceDef ret = new InterfaceDefImpl(id, this, rep, bi, workId);
812       addListItem(ret); // adding object to list
813
return ret; // return object
814
}
815   }
816
817   public InterfaceDef create_interface(Identification id) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
818     if (stKindImpl.value()==StateKind.sk_normal)
819       throw new TIRExceptCreate("you can call create method on work object");
820     int i;
821     if (id==null) {
822       return null;
823     } else {
824       if (!testIdentification(id))
825         throw new TIRExceptCreate("Wrong identification for this object.");
826       if (!getCreateLock())
827         throw new TIRExceptLock("This part of repository is locked for creating.");
828       if (!hasCont)
829         fromNormObj();
830       id = new IdentificationImpl(id);
831       Contained[] a = lookup_name(id.name());
832       if (a.length != 0) {
833         if (a[0].get_def_kind().value() != DefinitionKind.dk_Interface)
834       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
835     boolean found = false;
836     for (i=0;i<a.length;i++) {
837       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
838         found = true;
839         break;
840       }
841     }
842     if (found)
843       throw new TIRExceptCreate("Object with same identification exists in repository.");
844       }
845       // creating new object
846
InterfaceDef ret = new InterfaceDefImpl(id, this, rep, null, workId);
847       addListItem(ret); // adding object to list
848
return ret; // return object
849
}
850   }
851
852   public FrameDef create_frame(Identification id, boolean system) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
853     if (stKindImpl.value()==StateKind.sk_normal)
854       throw new TIRExceptCreate("you can call create method on work object");
855     if (id!=null) {
856       if (!testIdentification(id))
857         throw new TIRExceptCreate("Wrong identification for this object.");
858       if (!getCreateLock())
859         throw new TIRExceptLock("This part of repository is locked for creating.");
860       if (!hasCont)
861         fromNormObj();
862       id = new IdentificationImpl(id);
863       Contained[] a = lookup_name(id.name());
864       if (a.length != 0) {
865         if (a[0].get_def_kind().value() != DefinitionKind.dk_Frame)
866       throw new TIRExceptCreate("Name \""+id.name()+"\" exists in repository.");
867     boolean found = false;
868     for (int i=0;i<a.length;i++) {
869       if (((SIdentification) a[i].get_identification()).is_short_equal(id)) {
870         found = true;
871         break;
872       }
873     }
874     if (found)
875       throw new TIRExceptCreate("Object with same identification exists in repository.");
876       }
877       // creating new object
878
FrameDef ret = new FrameDefImpl(id, this, rep, system, workId);
879       addListItem(ret); // adding object to list
880
return ret; // return object
881
} else
882       return null;
883   }
884
885   public boolean isNew() {
886     return ((stKindImpl.value()==StateKind.sk_work) && normal==null);
887   }
888   
889   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {
890     if (stKindImpl.value()==StateKind.sk_normal)
891       return;
892     LiItem akt;
893     int i,j;
894     akt = firstChild;
895     for (i=0;i<numOfItems;i++) {
896       if (((TIRImplObject)akt.obj).isNew() && !isNew()) {
897         LiItem nak = normal.firstChild;
898         for (j=0;j<normal.numOfItems;j++) {
899           if (((SIdentification) ((Contained)nak.obj).get_identification()).is_short_name_equal(((Contained)akt.obj).get_identification())) {
900             if (akt.obj.get_def_kind().value()!=nak.obj.get_def_kind().value())
901               throw new TIRExceptCommit("Object with name \""+((SContained)nak.obj).get_absolute_name().name()+"\"exists.");
902             if (((SIdentification) ((Contained)nak.obj).get_identification()).is_short_equal(((Contained)akt.obj).get_identification()))
903               throw new TIRExceptCommit("Object with identification \""+((Contained)nak.obj).get_identification()+"\"in container \""+get_absolute_name().name()+"\" exists.");
904           }
905           nak = nak.next;
906         }
907       }
908       ((TIRImplObject)akt.obj).canCommit();
909       akt = akt.next;
910     }
911   }
912
913   public void doAbort(long workId) throws RemoteException JavaDoc {
914     LiItem akt = firstChild;
915     for(int i=0;i<numOfItems;i++) {
916       //if (((TIRImplObject)akt.obj).isNew())
917
((TIRImplObject)akt.obj).doAbort(workId);
918       akt = akt.next;
919     }
920     if (lockForMe) {
921       normal.lockFor = -1L;
922       lockCreate.unlock();
923       lockForMe = false;
924      /* if (((SIdentification)id).isLocked())
925         if (((SIdentification)id).lockFor()==workId) {
926           ((SIdentification)id).unlock();
927         }*/

928     }
929   }
930
931   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
932     if (stKindImpl.value()==StateKind.sk_normal)
933       return;
934     LiItem akt = firstChild;
935     boolean nn = false;
936     int i;
937     if (isNew()) {
938       stKindImpl.toNormal();
939       for(i=0;i<numOfItems;i++) {
940         ((TIRImplObject)akt.obj).doCommit(this,rep);
941         akt = akt.next;
942       }
943       parent = in;
944       this.rep = rep;
945     } else {
946       for(i=0;i<numOfItems;i++) {
947         if (((TIRImplObject)akt.obj).isNew())
948           nn = true;
949         ((TIRImplObject)akt.obj).doCommit(normal,rep);
950         if (nn)
951           normal.addListItem(akt.obj);
952         akt = akt.next;
953         nn = false;
954       }
955       if (lockForMe) {
956         normal.lockFor = -1L;
957         lockCreate.unlock();
958         lockForMe = false;
959       }
960     }
961 /* if (((SIdentification)id).isLocked())
962       if (((SIdentification)id).lockFor()==workId) {
963         ((SIdentification)id).setNextVersion();
964         ((SIdentification)id).unlock();
965       }*/

966
967   }
968
969   /* return workId for which is locked */
970   public long isLockedForCreate() throws RemoteException JavaDoc {
971     if ((stKindImpl.value()==StateKind.sk_work))
972       if (normal==null)
973         return -1L;
974     if (lockCreate.isLocked()) {
975       return normal.lockFor;
976     }
977     return -1L;
978   }
979
980   /* search if in this branch exists "create lock" */
981   public long canLock() throws RemoteException JavaDoc {
982     long a;
983     if ((a=isLockedForCreate())!=-1L) // if this module is locked
984
return a;
985     // search lock up
986
Container con = parent;
987     while (con.get_def_kind().value()!=DefinitionKind.dk_Repository &&
988            con.get_def_kind().value()!=DefinitionKind.dk_WorkRepository) {
989       if ((a=((SContainer)con).isLockedForCreate())!=-1L)
990         return a;
991       con = ((Contained)con).get_defined_in();
992     }
993     if ((a=((SContainer)con).isLockedForCreate())!=-1L)
994       return a;
995     // search lock down
996
if ((a=canLockChild())!=-1L)
997       return a;
998     return -1L;
999   }
1000  
1001  /* search if in children exists "create lock" */
1002  public long canLockChild() throws RemoteException JavaDoc {
1003    LiItem akt = firstChild;
1004    long a;
1005    for(int i=0;i<numOfItems;i++) {
1006      if (akt.obj.get_def_kind().value()==DefinitionKind.dk_Module) {
1007        if ((a=((ModuleDefImpl)akt.obj).isLockedForCreate())!=-1L)
1008          return a;
1009        else {
1010          if ((a=((ModuleDefImpl)akt.obj).canLockChild())!=-1L)
1011            return a;
1012        }
1013      }
1014    }
1015    return -1L;
1016  }
1017
1018  /* it is called from create_* methods */
1019  boolean getCreateLock() throws RemoteException JavaDoc {
1020    if (!isNew()) {
1021      if (lockForMe)
1022        return true;
1023      if (lockCreate.isLocked())
1024        return false;
1025      ((WorkRepositoryImpl)rep).normalRep.lockGetCreate.lock();
1026      long a = canLock();
1027      if ((a==-1L) || (a==workId)) {
1028        lockForMe = true;
1029        lockCreate.lock();
1030    normal.lockFor = workId;
1031      }
1032      ((WorkRepositoryImpl)rep).normalRep.lockGetCreate.unlock();
1033      return true;
1034    }
1035    return true;
1036  }
1037}
1038
Popular Tags