KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ExceptionDefImpl.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.ExceptionDef;
17 import SOFA.SOFAnode.Made.TIR.Identification;
18 import SOFA.SOFAnode.Made.TIR.Repository;
19 import SOFA.SOFAnode.Made.TIR.StateKind;
20 import SOFA.SOFAnode.Made.TIR.StructDef;
21 import SOFA.SOFAnode.Made.TIR.StructMember;
22 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
23 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
24 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
25 import SOFA.SOFAnode.Made.TIR.TIRObject;
26 import SOFA.SOFAnode.Made.TIR.UnionDef;
27
28 public class ExceptionDefImpl extends ContainerImpl implements ExceptionDef, TIRImplObject, SContained {
29   protected Identification id;
30   protected Container parent;
31   protected Repository rep;
32   DefinitionKindImpl defKindImpl;
33   StateKindImpl stKindImpl;
34
35   ExceptionDefImpl normal;
36   long workId;
37
38   public ExceptionDefImpl(Identification ident, Container in, Repository inrep, long workId) throws RemoteException JavaDoc {
39     id = ident;
40     parent = in;
41     rep = inrep;
42     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Exception);
43     stKindImpl = new StateKindImpl(StateKind.sk_work);
44     normal = null;
45     this.workId = workId;
46   }
47
48   public ExceptionDefImpl(Container in, Repository inrep) throws RemoteException JavaDoc {
49     id = null;
50     parent = in;
51     rep = inrep;
52     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Exception);
53     stKindImpl = new StateKindImpl(StateKind.sk_normal);
54     normal = null;
55     this.workId = -1L;
56   }
57
58   public ExceptionDefImpl(ExceptionDefImpl a, Container in, Repository inrep, long workId) throws RemoteException JavaDoc {
59     id = a.id;
60     parent = in;
61     rep = inrep;
62     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Exception);
63     stKindImpl = new StateKindImpl(StateKind.sk_work);
64     normal = a;
65     this.workId = workId;
66   }
67
68   /* form interface Contained */
69   public Identification get_identification() throws RemoteException JavaDoc {
70     return id;
71   }
72
73   /* form interface Contained */
74   public Container get_defined_in() throws RemoteException JavaDoc {
75     return parent;
76   }
77
78   /* form interface Contained */
79   public Container get_containing_repository() throws RemoteException JavaDoc {
80     return rep;
81   }
82
83   /* form interface Contained */
84   public AbsoluteName get_absolute_name() throws RemoteException JavaDoc {
85     AbsoluteNameImpl absName;
86     if (rep == parent) { // object is contained in repository
87
absName = new AbsoluteNameImpl("::"+id.name());
88     } else {
89       absName = new AbsoluteNameImpl(((SContained)parent).get_absolute_name().name()+"::"+id.name());
90     }
91     return (AbsoluteName) absName;
92   }
93
94   /* from interface TIRObject */
95   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
96     return (DefinitionKind) defKindImpl;
97   }
98
99   /* from interface TIRObject */
100   public StateKind get_state() throws RemoteException JavaDoc {
101     return (StateKind) stKindImpl;
102   }
103
104   public void tag(String JavaDoc t) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
105     if (stKindImpl.value()!=StateKind.sk_work)
106       throw new TIRExceptCreate("you can call create method on work object");
107     String JavaDoc ebt = id.branchTag();
108     if (t.compareTo(ebt)==0)
109       throw new TIRExceptCreate("This tag exists");
110     String JavaDoc[] et = id.tag();
111     int i,j;
112     for (i=0;i<et.length;i++) {
113       if (t.compareTo(et[i])==0)
114        throw new TIRExceptCreate("This tag exists");
115     }
116     Contained[] sibl = parent.lookup_name(id);
117     for (j=0;j<sibl.length;j++) {
118       Identification idl = sibl[j].get_identification();
119       
120       ebt = idl.branchTag();
121       if (t.compareTo(ebt)==0)
122         throw new TIRExceptCreate("This tag exists");
123       et = idl.tag();
124       for (i=0;i<et.length;i++) {
125         if (t.compareTo(et[i])==0)
126           throw new TIRExceptCreate("This tag exists");
127       }
128     }
129     ((SIdentification) id).tag(t);
130   }
131
132   private void fromNormObj() throws RemoteException JavaDoc, TIRExceptLock {
133     if (normal==null)
134       return;
135     if (((WorkRepositoryImpl)rep).lock.isLocked())
136       throw new TIRExceptLock("Repository is locked");
137     int i;
138     LiItem akt;
139     akt = normal.firstChild;
140     for(i=0;i<normal.numOfItems;i++) {
141       addListItem(WorkRepositoryImpl.newWorkFromNormal(akt.obj,rep,this,workId));
142       akt = akt.next;
143     }
144     hasCont = true;
145   }
146
147   public StructMember[] members() throws RemoteException JavaDoc, TIRExceptLock {
148     if (stKindImpl.value()==StateKind.sk_normal) {
149       if (((RepositoryImpl) rep).lock.isLocked())
150         throw new TIRExceptLock("Repository is locked");
151     } else {
152       if (!hasCont)
153         fromNormObj();
154       if (((WorkRepositoryImpl) rep).lock.isLocked())
155         throw new TIRExceptLock("Repository is locked");
156     }
157     StructMember[] all = new StructMember [numOfItems];
158     LiItem akt = firstChild;
159     int i,j = 0;
160     for(i=0;i<numOfItems;i++) {
161       if (akt.obj.get_def_kind().value()==DefinitionKind.dk_StructMember) {
162         all[j] = (StructMember) akt.obj;
163     j++;
164       }
165       akt = akt.next;
166     }
167     StructMember[] ret = new StructMember [j];
168     for(i=0;i<j;i++) {
169       ret[i] = all[i];
170     }
171     return ret;
172   }
173
174   public Contained[] contents(DefinitionKind type) 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.contents(type);
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.contents(type);
187     }
188   }
189
190   public Contained lookup(Identification id) 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(id);
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(id);
203     }
204   }
205
206   public Contained lookup(String JavaDoc name, String JavaDoc ver) 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, ver);
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, ver);
219     }
220   }
221
222   public Contained[] lookup_name(Identification id) 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(id);
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(id);
235     }
236   }
237
238   public Contained[] lookup_name(String JavaDoc name) 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_name(name);
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_name(name);
251     }
252   }
253
254   public Contained lookup_tag(String JavaDoc name, String JavaDoc tag) 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_tag(name, tag);
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_tag(name, tag);
267     }
268   }
269
270   public Contained[] lookup_branchtag(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_branchtag(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_branchtag(name, brtag);
283     }
284   }
285
286   public Contained lookup_lastinbranch(String JavaDoc name, String JavaDoc brtag) 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_lastinbranch(name, brtag);
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_lastinbranch(name, brtag);
299     }
300   }
301   
302   public Contained lookup_lastfromversion(String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
303     if (stKindImpl.value()==StateKind.sk_normal) {
304       if (((RepositoryImpl) rep).lock.isLocked())
305         throw new TIRExceptLock("Repository is locked");
306       else
307         return super.lookup_lastfromversion(name, version);
308     } else {
309       if (!hasCont)
310         fromNormObj();
311       if (((WorkRepositoryImpl) rep).lock.isLocked())
312         throw new TIRExceptLock("Repository is locked");
313       else
314         return super.lookup_lastfromversion(name, version);
315     }
316   }
317
318   public Contained[] scontents(DefinitionKind type) throws RemoteException JavaDoc, TIRExceptLock {
319     if (stKindImpl.value()==StateKind.sk_normal) {
320       return super.scontents(type);
321     } else {
322       if (!hasCont)
323         fromNormObj();
324       return super.scontents(type);
325     }
326   }
327
328   public Contained slookup(Identification id) throws RemoteException JavaDoc, TIRExceptLock {
329     if (stKindImpl.value()==StateKind.sk_normal) {
330         return super.slookup(id);
331     } else {
332       if (!hasCont)
333         fromNormObj();
334       return super.slookup(id);
335     }
336   }
337
338   public Contained slookup(String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
339     if (stKindImpl.value()==StateKind.sk_normal) {
340         return super.slookup(name, version);
341     } else {
342       if (!hasCont)
343         fromNormObj();
344       return super.slookup(name, version);
345     }
346   }
347
348   public Contained[] slookup_name(Identification id) throws RemoteException JavaDoc, TIRExceptLock {
349     if (stKindImpl.value()==StateKind.sk_normal) {
350         return super.slookup_name(id);
351     } else {
352       if (!hasCont)
353         fromNormObj();
354       return super.slookup_name(id);
355     }
356   }
357
358   public Contained[] slookup_name(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptLock {
359     if (stKindImpl.value()==StateKind.sk_normal) {
360       return super.slookup_name(name);
361     } else {
362       if (!hasCont)
363         fromNormObj();
364       return super.slookup_name(name);
365     }
366   }
367
368   public StructMember create_member(String JavaDoc name, CDLType type) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
369     if (!isNew())
370       throw new TIRExceptCreate("you can call create method on work object");
371     if (name!=null && name.compareTo("")!=0 && type!=null) {
372       if (type.get_state().value()==StateKind.sk_work ) {
373     StructMember[] a = members();
374     boolean found = false;
375     for (int i=0;i<a.length;i++) {
376           if (a[i].name().compareTo(name)==0) {
377         found = true;
378         break;
379       }
380     }
381         if (found)
382       throw new TIRExceptCreate("Member with this name exists in exception.");
383     StructMember m = new StructMemberImpl(new IdentificationImpl(((SIdentification)this.id).language(), this.id.absolute_name().name()+"::"+name,this.id.version()), this, rep, type);
384     addListItem(m);
385     return m;
386       } else {
387         throw new TIRExceptCreate("Given object isn't work.");
388       }
389     } else
390       return null;
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