KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: CDLRepositoryImpl.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.ArrayDef;
11 import SOFA.SOFAnode.Made.TIR.CDLRepository;
12 import SOFA.SOFAnode.Made.TIR.CDLType;
13 import SOFA.SOFAnode.Made.TIR.ConstantDef;
14 import SOFA.SOFAnode.Made.TIR.Contained;
15 import SOFA.SOFAnode.Made.TIR.Container;
16 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
17 import SOFA.SOFAnode.Made.TIR.EnumDef;
18 import SOFA.SOFAnode.Made.TIR.ExceptionDef;
19 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
20 import SOFA.SOFAnode.Made.TIR.FixedDef;
21 import SOFA.SOFAnode.Made.TIR.FrameDef;
22 import SOFA.SOFAnode.Made.TIR.Identification;
23 import SOFA.SOFAnode.Made.TIR.InterfaceDef;
24 import SOFA.SOFAnode.Made.TIR.ModuleDef;
25 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
26 import SOFA.SOFAnode.Made.TIR.ProviderDef;
27 import SOFA.SOFAnode.Made.TIR.Repository;
28 import SOFA.SOFAnode.Made.TIR.SequenceDef;
29 import SOFA.SOFAnode.Made.TIR.StateKind;
30 import SOFA.SOFAnode.Made.TIR.StringDef;
31 import SOFA.SOFAnode.Made.TIR.StructDef;
32 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
33 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
34 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
35 import SOFA.SOFAnode.Made.TIR.TIRObject;
36 import SOFA.SOFAnode.Made.TIR.TypedefDef;
37 import SOFA.SOFAnode.Made.TIR.UnionDef;
38 import SOFA.SOFAnode.Made.TIR.WstringDef;
39
40 public class CDLRepositoryImpl extends CDLContainerImpl implements CDLRepository, Contained, SContained {
41   protected Identification id;
42   protected Container parent;
43   protected Repository rep;
44   DefinitionKindImpl defKindImpl;
45   StateKindImpl stKindImpl;
46
47   CDLRepositoryImpl normal;
48
49   Lock lockCreate;
50   boolean lockForMe; // used in work object
51
public long lockFor; // used in normal object
52
public long workId; // id of "transaction"
53

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

1017    }
1018  }
1019
1020  public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
1021    if (stKindImpl.value()==StateKind.sk_normal)
1022      return;
1023    LiItem akt = firstChild;
1024    boolean nn = false;
1025    int i;
1026    if (isNew()) {
1027      stKindImpl.toNormal();
1028      for(i=0;i<numOfItems;i++) {
1029        ((TIRImplObject)akt.obj).doCommit(this,rep);
1030        akt = akt.next;
1031      }
1032      parent = in;
1033      this.rep = rep;
1034    } else {
1035      for(i=0;i<numOfItems;i++) {
1036        if (((TIRImplObject)akt.obj).isNew())
1037          nn = true;
1038        ((TIRImplObject)akt.obj).doCommit(normal,rep);
1039        if (nn)
1040          normal.addListItem(akt.obj);
1041        akt = akt.next;
1042        nn = false;
1043      }
1044      if (lockForMe) {
1045        normal.lockFor = -1L;
1046        lockCreate.unlock();
1047        lockForMe = false;
1048      }
1049    }
1050  }
1051
1052  /* return workId for which is locked */
1053  public long isLockedForCreate() throws RemoteException JavaDoc {
1054    if ((stKindImpl.value()==StateKind.sk_work))
1055      if (normal==null)
1056        return -1L;
1057    if (lockCreate.isLocked()) {
1058      return normal.lockFor;
1059    }
1060    return -1L;
1061  }
1062
1063  /* search if in this branch exists "create lock" */
1064  public long canLock() throws RemoteException JavaDoc {
1065    long a;
1066    if ((a=isLockedForCreate())!=-1L) // if this module is locked
1067
return a;
1068    // search lock up
1069
Container con = parent;
1070    while (con.get_def_kind().value()!=DefinitionKind.dk_Repository &&
1071           con.get_def_kind().value()!=DefinitionKind.dk_WorkRepository) {
1072      if ((a=((SContainer)con).isLockedForCreate())!=-1L)
1073        return a;
1074      con = ((Contained)con).get_defined_in();
1075    }
1076    if ((a=((SContainer)con).isLockedForCreate())!=-1L)
1077      return a;
1078    // search lock down
1079
if ((a=canLockChild())!=-1L)
1080      return a;
1081    return -1L;
1082  }
1083  
1084  /* search if in children exists "create lock" */
1085  public long canLockChild() throws RemoteException JavaDoc {
1086    LiItem akt = firstChild;
1087    long a;
1088    for(int i=0;i<numOfItems;i++) {
1089      if (akt.obj.get_def_kind().value()==DefinitionKind.dk_Module) {
1090        if ((a=((ModuleDefImpl)akt.obj).isLockedForCreate())!=-1L)
1091          return a;
1092        else {
1093          if ((a=((ModuleDefImpl)akt.obj).canLockChild())!=-1L)
1094            return a;
1095        }
1096      }
1097    }
1098    return -1L;
1099  }
1100
1101  /* it is called from create_* methods */
1102  boolean getCreateLock() throws RemoteException JavaDoc {
1103    if (!isNew()) {
1104      if (lockForMe)
1105        return true;
1106      if (lockCreate.isLocked())
1107        return false;
1108      ((WorkRepositoryImpl)rep).normalRep.lockGetCreate.lock();
1109      long a = canLock();
1110      if ((a==-1L) || (a==workId)) {
1111        lockForMe = true;
1112        lockCreate.lock();
1113    normal.lockFor = workId;
1114      }
1115      ((WorkRepositoryImpl)rep).normalRep.lockGetCreate.unlock();
1116      return true;
1117    }
1118    return true;
1119  }
1120
1121}
1122
Popular Tags