KickJava   Java API By Example, From Geeks To Geeks.

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


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