KickJava   Java API By Example, From Geeks To Geeks.

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


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