KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: OperationDefImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.IOException JavaDoc;
4 import java.rmi.RemoteException JavaDoc;
5
6 import SOFA.SOFAnode.Made.TIR.AbsoluteName;
7 import SOFA.SOFAnode.Made.TIR.ArrayDef;
8 import SOFA.SOFAnode.Made.TIR.CDLType;
9 import SOFA.SOFAnode.Made.TIR.Contained;
10 import SOFA.SOFAnode.Made.TIR.Container;
11 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
12 import SOFA.SOFAnode.Made.TIR.ExceptionDef;
13 import SOFA.SOFAnode.Made.TIR.FixedDef;
14 import SOFA.SOFAnode.Made.TIR.Identification;
15 import SOFA.SOFAnode.Made.TIR.OperationDef;
16 import SOFA.SOFAnode.Made.TIR.ParamDescr;
17 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
18 import SOFA.SOFAnode.Made.TIR.Repository;
19 import SOFA.SOFAnode.Made.TIR.SequenceDef;
20 import SOFA.SOFAnode.Made.TIR.StateKind;
21 import SOFA.SOFAnode.Made.TIR.StringDef;
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.WstringDef;
27
28 public class OperationDefImpl extends ContainedImpl implements OperationDef {
29   DefinitionKindImpl defKindImpl;
30   StateKindImpl stKindImpl;
31   CDLType res;
32   int numOfParams;
33   LiItem firstPar;
34   LiItem lastPar;
35   ExceptionDef[] excepts;
36   CDLType[] loadEx;
37
38   OperationDefImpl normal;
39   boolean hasCont;
40   long workId;
41
42   public OperationDefImpl(Identification ident, Container in, Repository inrep, CDLType r, ExceptionDef[] excps, long workId) throws RemoteException JavaDoc {
43     super(ident,in,inrep);
44     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Operation);
45     stKindImpl = new StateKindImpl(StateKind.sk_work);
46     res = r;
47     if (excps==null)
48       excepts = new ExceptionDef [0];
49     else
50       excepts = excps;
51     numOfParams = 0;
52     firstPar = lastPar = null;
53     normal = null;
54     hasCont = false;
55     this.workId = workId;
56   }
57
58   public OperationDefImpl(Container in, Repository inrep) throws RemoteException JavaDoc {
59     super(null,in,inrep);
60     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Operation);
61     stKindImpl = new StateKindImpl(StateKind.sk_normal);
62     res = null;
63     excepts = null;
64     numOfParams = 0;
65     firstPar = lastPar = null;
66     normal = null;
67     hasCont = false;
68     this.workId = -1L;
69   }
70
71   public OperationDefImpl(OperationDefImpl a, Container in, Repository inrep, long workId) throws RemoteException JavaDoc {
72     super(a.id,in,inrep);
73     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Operation);
74     stKindImpl = new StateKindImpl(StateKind.sk_work);
75     res = a.res;
76     excepts = a.excepts;
77     numOfParams = 0;
78     firstPar = lastPar = null;
79     normal = a;
80     hasCont = false;
81     this.workId = workId;
82   }
83
84   /* from interface TIRObject */
85   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
86     return (DefinitionKind) defKindImpl;
87   }
88
89   /* from interface TIRObject */
90   public StateKind get_state() throws RemoteException JavaDoc {
91     return (StateKind) stKindImpl;
92   }
93
94   public void tag(String JavaDoc t) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
95     if (stKindImpl.value()!=StateKind.sk_work)
96       throw new TIRExceptCreate("you can call create method on work object");
97     super.tag(t);
98   }
99
100   public CDLType result() throws RemoteException JavaDoc {
101     return res;
102   }
103
104   private void fromNormObj() throws RemoteException JavaDoc, TIRExceptLock {
105     if (normal==null)
106       return;
107     if (((WorkRepositoryImpl)rep).lock.isLocked())
108       throw new TIRExceptLock("Repository is locked");
109     int i;
110     LiItem akt;
111     akt = normal.firstPar;
112     for(i=0;i<normal.numOfParams;i++) {
113       addListItem(new ParamDescrImpl((ParamDescrImpl)akt.obj));
114       akt = akt.next;
115     }
116     hasCont = true;
117   }
118
119   private void addListItem(TIRObject what) {
120     LiItem item = new LiItem(what);
121     if (firstPar == null) {
122       firstPar = lastPar = item;
123     } else {
124       LiItem akt = firstPar;
125       while (akt.next != null) akt = akt.next;
126       akt.next = item;
127       item.prev = akt;
128       lastPar = item;
129     }
130     numOfParams++;
131   }
132
133   public ParamDescr[] params() throws RemoteException JavaDoc, TIRExceptLock {
134     if (stKindImpl.value()==StateKind.sk_normal && !hasCont)
135       fromNormObj();
136     ParamDescr[] ret = new ParamDescr [numOfParams];
137     LiItem akt = firstPar;
138     for(int i=0;i<numOfParams;i++) {
139       ret[i] = (ParamDescr) akt.obj;
140       akt = akt.next;
141     }
142     return ret;
143   }
144
145   public ExceptionDef[] exceptions() throws RemoteException JavaDoc {
146     return excepts;
147   }
148
149   public ParamDescr create_parametr(String JavaDoc name, CDLType type, int paramMode) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
150     if (!isNew())
151       throw new TIRExceptCreate("you can call create method on work object");
152     if (type==null || name==null || name.compareTo("")==0)
153       return null;
154     if (type.get_state().value()==StateKind.sk_work ) {
155       ParamDescr[] a = params();
156       boolean found = false;
157       for (int i=0;i<a.length;i++) {
158         if (a[i].name().compareTo(name)==0) {
159           found = true;
160           break;
161         }
162       }
163       if (found)
164         throw new TIRExceptCreate("Parametr with this name exists in operation.");
165       ParamDescr ret = new ParamDescrImpl(new String JavaDoc(name), type, new ParamModeImpl(paramMode));
166       addListItem(ret);
167       return ret;
168     } else
169       throw new TIRExceptCreate("Given object isn't work.");
170   }
171
172   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
173     try {
174       int i;
175       ((IdentificationImpl) id).save(st.curOutFile);
176       Storage.writeCDLType(st, res);
177       st.curOutFile.writeInt(numOfParams);
178       LiItem akt = firstPar;
179       for(i=0;i<numOfParams;i++) {
180         ((TIRImplObject)(akt.obj)).save(st);
181     akt = akt.next;
182       }
183       st.curOutFile.writeInt(excepts.length);
184       for(i=0;i<excepts.length;i++) {
185         Storage.writeCDLType(st, excepts[i]);
186       }
187     } catch (IOException JavaDoc e) {
188       throw new TIRExceptStorage("Can't write in file "+st.currentFile+".");
189     }
190   }
191
192    public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
193     try {
194       int i, num;
195       id = new IdentificationImpl();
196       ((IdentificationImpl) id).load(st.curInFile);
197       res = Storage.readCDLType(st);
198       num = st.curInFile.readInt();
199       ParamDescrImpl par;
200       for(i=0;i<num;i++) {
201         par = new ParamDescrImpl();
202         par.load(st);
203     addListItem(par);
204       }
205       num = st.curInFile.readInt();
206       loadEx = new CDLType [num];
207       for(i=0;i<num;i++) {
208         loadEx[i] = Storage.readCDLType(st);
209       }
210     } catch (IOException JavaDoc e) {
211       throw new TIRExceptStorage("Can't write in file "+st.currentFile+".");
212     }
213   }
214
215   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {
216     try {
217     
218     int i,k;
219     Container con;
220     Contained[] rt;
221     Contained ret;
222     IdentificationImpl id;
223     AbsoluteName nm;
224     boolean found;
225     if (res.get_def_kind().value() == DefinitionKind.dk_none) { // is tp NotYetInRep?
226
if (((NotYetInRep) res).defKind == DefinitionKind.dk_Primitive)
227         res = r.get_prim(((NotYetInRep) res).primKind);
228       else { // in tp there is reference to some type
229
nm = ((NotYetInRep) res).nm;
230     con = r; // begin search from repository
231
for (i=0;i<nm.size()-1;i++) { // last element is name of object
232
id = new IdentificationImpl(nm.elementAt(0), "::"+nm.elementAt(i),"");
233           rt = ((SContainer)con).slookup_name(id);
234       if (rt.length==0) throw new TIRExceptStorage("Can't find type " + nm.name());
235           if (rt.length==1) {
236             con = (Container) rt[0];
237       } else { // type in struct (etc.) - more versions of this struct
238
found = false;
239             id.version(((NotYetInRep) res).id.version()); // substruct version is inherited from parent
240
for (k=0;k<rt.length;k++) {
241           if (id.is_short_equal(rt[k].get_identification())) {
242             found = true;
243         break; // -- break for
244
}
245         } // -- end of for
246
if (!found) throw new TIRExceptStorage("Can't find type " + nm.name());
247         con = (Container) rt[k];
248       }
249     }
250     if ((ret = ((SContainer)con).slookup(((NotYetInRep) res).id))==null)
251       throw new TIRExceptStorage("Can't find type " + nm.name());
252     res = (CDLType) ret;
253     if (res.get_def_kind().value()==DefinitionKind.dk_Array ||
254             res.get_def_kind().value()==DefinitionKind.dk_Sequence ||
255             res.get_def_kind().value()==DefinitionKind.dk_String ||
256             res.get_def_kind().value()==DefinitionKind.dk_Wstring)
257       ((TIRImplObject) res).postLoad(r);
258       }
259     } else {
260       if (res.get_def_kind().value()==DefinitionKind.dk_Array ||
261           res.get_def_kind().value()==DefinitionKind.dk_Sequence ||
262           res.get_def_kind().value()==DefinitionKind.dk_String ||
263           res.get_def_kind().value()==DefinitionKind.dk_Wstring)
264         ((TIRImplObject) res).postLoad(r);
265     }
266     // postLoad excepts
267
excepts = new ExceptionDef [loadEx.length];
268     for (int j=0;j<loadEx.length;j++) {
269       if (loadEx[j].get_def_kind().value() == DefinitionKind.dk_none) { // is tp NotYetInRep?
270
nm = ((NotYetInRep) loadEx[j]).nm;
271     con = r; // begin search from repository
272
for (i=0;i<nm.size()-1;i++) { // last element is name of object
273
id = new IdentificationImpl(nm.elementAt(0), "::"+nm.elementAt(i),"");
274           rt = ((SContainer)con).slookup_name(id);
275       if (rt.length==0) throw new TIRExceptStorage("Can't find type " + nm.name());
276             if (rt.length==1) {
277               con = (Container) rt[0];
278       } else { // type in struct (etc.) - more versions of this struct
279
found = false;
280             id.version(((NotYetInRep) loadEx[j]).id.version()); // substruct version is inherited from parent
281
for (k=0;k<rt.length;k++) {
282           if (id.is_short_equal(rt[k].get_identification())) {
283             found = true;
284         break; // -- break for
285
}
286         } // -- end of for
287
if (!found) throw new TIRExceptStorage("Can't find type " + nm.name());
288         con = (Container) rt[k];
289       }
290     }
291     if ((ret = ((SContainer)con).slookup(((NotYetInRep) loadEx[j]).id))==null)
292       throw new TIRExceptStorage("Can't find type " + nm.name());
293     excepts[j] = (ExceptionDef) ret;
294     if (excepts[j].get_def_kind().value()==DefinitionKind.dk_Array ||
295             excepts[j].get_def_kind().value()==DefinitionKind.dk_Sequence)
296       ((TIRImplObject) excepts[j]).postLoad(r);
297       }
298     }
299
300     LiItem akt = firstPar;
301     for(i=0;i<numOfParams;i++) {
302       ((TIRImplObject) akt.obj).postLoad(r);
303       akt = akt.next;
304     }
305
306     } catch (TIRExceptLock e) {;}
307   }
308
309   public boolean isNew() {
310     return ((stKindImpl.value()==StateKind.sk_work) && normal==null);
311   }
312
313   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {;}
314
315   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
316     if (stKindImpl.value()==StateKind.sk_normal)
317       return;
318     if (isNew()) {
319       stKindImpl.toNormal();
320       switch (res.get_def_kind().value()) {
321         case DefinitionKind.dk_Primitive:
322           res = ((SRepository)rep).get_prim(((PrimitiveDef)res).kind().value());
323           break;
324         case DefinitionKind.dk_Array:
325           ArrayDef nt = new ArrayDefImpl(ExprHelp.toNormal(((ArrayDef) res).length()), ((ArrayDef) res).element_type());
326           res = nt;
327           ((TIRImplObject) res).doCommit(in,rep);
328           break;
329         case DefinitionKind.dk_Sequence:
330           SequenceDef st = new SequenceDefImpl(ExprHelp.toNormal(((SequenceDef) res).bound()), ((SequenceDef) res).element_type());
331           res = st;
332           ((TIRImplObject)res).doCommit(in,rep);
333           break;
334         case DefinitionKind.dk_String:
335           StringDef str = new StringDefImpl(ExprHelp.toNormal(((StringDef) res).bound()));
336           res = str;
337           ((TIRImplObject)res).doCommit(in,rep);
338           break;
339         case DefinitionKind.dk_Wstring:
340           WstringDef wstr = new WstringDefImpl(ExprHelp.toNormal(((WstringDef) res).bound()));
341           res = wstr;
342           ((TIRImplObject) res).doCommit(in,rep);
343           break;
344         case DefinitionKind.dk_Fixed:
345           FixedDef ft = new FixedDefImpl(ExprHelp.toNormal(((FixedDef) res).digits()), ExprHelp.toNormal(((FixedDef) res).scale()));
346           res = ft;
347           ((TIRImplObject)res).doCommit(in,rep);
348           break;
349         default:
350           Identification idl = ((Contained)res).get_identification();
351           res = new NotYetInRep(idl.lang_absolute_name().name(), idl.name(), idl.version());
352           break;
353       }
354       LiItem akt = firstPar;
355       int i;
356       for (i=0;i<numOfParams;i++) {
357         ((TIRImplObject)akt.obj).doCommit(in,rep);
358         akt = akt.next;
359       }
360       loadEx = new CDLType [excepts.length];
361       for (i=0;i<excepts.length;i++) {
362         Identification idl = ((Contained)excepts[i]).get_identification();
363         loadEx[i] = new NotYetInRep(idl.lang_absolute_name().name(), idl.name(), idl.version());
364       }
365       excepts = null;
366       parent = in;
367       this.rep = rep;
368     } else {
369       normal = null;
370     }
371   }
372
373   public void doAbort(long workId) throws RemoteException JavaDoc {}
374 }
375
Popular Tags