KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: BindOperSubImpl.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 import java.rmi.server.UnicastRemoteObject JavaDoc;
6
7 import SOFA.SOFAnode.Made.TIR.BindOperKind;
8 import SOFA.SOFAnode.Made.TIR.BindOperSub;
9 import SOFA.SOFAnode.Made.TIR.BindType;
10 import SOFA.SOFAnode.Made.TIR.BindTypeArray;
11 import SOFA.SOFAnode.Made.TIR.BindTypeKind;
12 import SOFA.SOFAnode.Made.TIR.BindTypeNormal;
13 import SOFA.SOFAnode.Made.TIR.Container;
14 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
15 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
16 import SOFA.SOFAnode.Made.TIR.Repository;
17 import SOFA.SOFAnode.Made.TIR.StateKind;
18 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
19 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
20
21 public class BindOperSubImpl extends UnicastRemoteObject JavaDoc implements BindOperSub, TIRImplObject {
22   DefinitionKindImpl defKindImpl;
23   StateKindImpl stKindImpl;
24   BindOperKind bok;
25   int numOfEl;
26   LiItem firstEl;
27   LiItem lastEl;
28   int numOfSubs;
29   LiItem firstSub;
30   LiItem lastSub;
31
32   public BindOperSubImpl(int state) throws RemoteException JavaDoc {
33     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_BindOper);
34     stKindImpl = new StateKindImpl(state);
35     bok = new BindOperKindImpl(BindOperKind.bok_sub);
36     numOfEl = numOfSubs = 0;
37     firstEl = lastEl = null;
38     firstSub = lastSub = null;
39   }
40
41   /* from interface TIRObject */
42   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
43     return (DefinitionKind) defKindImpl;
44   }
45
46   /* from interface TIRObject */
47   public StateKind get_state() throws RemoteException JavaDoc {
48     return (StateKind) stKindImpl;
49   }
50
51   public BindOperKind get_bo_kind() throws RemoteException JavaDoc {
52     return bok;
53   }
54
55   public BindType[] elements() throws RemoteException JavaDoc {
56     LiItem l = firstEl;
57     BindType[] ret = new BindType [numOfEl];
58     for(int i = 0;i<numOfEl;i++) {
59       ret[i] = (BindType) l.obj;
60       l = l.next;
61     }
62     return ret;
63   }
64
65   public BindType[] subs() throws RemoteException JavaDoc {
66     LiItem l = firstSub;
67     BindType[] ret = new BindType [numOfSubs];
68     for(int i = 0;i<numOfSubs;i++) {
69       ret[i] = (BindType) l.obj;
70       l = l.next;
71     }
72     return ret;
73   }
74
75   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
76     try {
77       st.curOutFile.writeInt(bok.value());
78       st.curOutFile.writeInt(numOfEl);
79       LiItem l= firstEl;
80       for(int i=0;i<numOfEl;i++) {
81         ((TIRImplObject) l.obj).save(st);
82         l = l.next;
83       }
84       st.curOutFile.writeInt(numOfSubs);
85       l= firstSub;
86       for(int i=0;i<numOfSubs;i++) {
87         ((TIRImplObject) l.obj).save(st);
88         l = l.next;
89       }
90     } catch (IOException JavaDoc e) {
91       throw new TIRExceptStorage("Can't write in directory "+st.current+".");
92     }
93   }
94
95   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
96     try {
97       int len;
98       len = st.curInFile.readInt();
99       for(int i=0;i<len;i++) {
100         BindType akt;
101         switch (st.curInFile.readInt()) {
102         case BindTypeKind.btk_normal:
103           akt = new BindTypeNormalImpl();
104           break;
105         case BindTypeKind.btk_array:
106           akt = new BindTypeArrayImpl();
107           break;
108         default:
109           throw new TIRExceptStorage("Unexpected kind of object in \""+st.currentFile+"\".");
110         }
111         ((TIRImplObject) akt).load(st);
112         addEl(akt);
113       }
114       len = st.curInFile.readInt();
115       for(int i=0;i<len;i++) {
116         BindType akt;
117         switch (st.curInFile.readInt()) {
118         case BindTypeKind.btk_normal:
119           akt = new BindTypeNormalImpl();
120           break;
121         case BindTypeKind.btk_array:
122           akt = new BindTypeArrayImpl();
123           break;
124         default:
125           throw new TIRExceptStorage("Unexpected kind of object in \""+st.currentFile+"\".");
126         }
127         ((TIRImplObject) akt).load(st);
128         addSub(akt);
129       }
130     } catch (IOException JavaDoc e) {
131       throw new TIRExceptStorage("Can't read in directory "+st.current+".");
132     }
133   }
134
135   public void addEl(BindType what) {
136     LiItem item = new LiItem(what);
137     if (firstEl == null) {
138       firstEl = lastEl = item;
139     } else {
140       lastEl.next = item;
141       item.prev = lastEl;
142       lastEl = item;
143     }
144     numOfEl++;
145   }
146
147   public void addSub(BindType what) {
148     LiItem item = new LiItem(what);
149     if (firstSub == null) {
150       firstSub = lastSub = item;
151     } else {
152       lastSub.next = item;
153       item.prev = lastSub;
154       lastSub = item;
155     }
156     numOfSubs++;
157   }
158
159   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {
160     LiItem l = firstEl;
161     for(int i=0;i<numOfEl;i++) {
162       ((TIRImplObject) l.obj).postLoad(r);
163       l = l.next;
164     }
165     l = firstSub;
166     for(int i=0;i<numOfSubs;i++) {
167       ((TIRImplObject) l.obj).postLoad(r);
168       l = l.next;
169     }
170   }
171
172   public boolean isNew() {
173     return (stKindImpl.value()==StateKind.sk_work);
174   }
175
176   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {
177     if (numOfEl==0 || numOfSubs==0)
178       throw new TIRExceptCommit("Empty prov/req bind");
179     LiItem l = firstEl;
180     for(int i=0;i<numOfEl;i++) {
181       ((TIRImplObject) l.obj).canCommit();
182       l = l.next;
183     }
184     l = firstSub;
185     for(int i=0;i<numOfSubs;i++) {
186       ((TIRImplObject) l.obj).canCommit();
187       l = l.next;
188     }
189   }
190
191   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
192     if (stKindImpl.value()==StateKind.sk_normal)
193       return;
194     if (isNew()) {
195       LiItem l = firstEl;
196       for(int i=0;i<numOfEl;i++) {
197         ((TIRImplObject) l.obj).doCommit(in, rep);
198         l = l.next;
199       }
200       l = firstSub;
201       for(int i=0;i<numOfSubs;i++) {
202         ((TIRImplObject) l.obj).doCommit(in, rep);
203         l = l.next;
204       }
205       stKindImpl.toNormal();
206     }
207   }
208
209   public void doAbort(long workId) throws RemoteException JavaDoc {
210     LiItem l = firstEl;
211     for(int i=0;i<numOfEl;i++) {
212       ((TIRImplObject) l.obj).doAbort(workId);
213       l = l.next;
214     }
215     l = firstSub;
216     for(int i=0;i<numOfSubs;i++) {
217       ((TIRImplObject) l.obj).doAbort(workId);
218       l = l.next;
219     }
220   }
221
222   public BindTypeNormal add_bindtypenormal(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptCreate {
223     if (stKindImpl.value() == StateKind.sk_normal)
224       throw new TIRExceptCreate("You can call add method on work objects only");
225     BindTypeNormal ret = new BindTypeNormalImpl(new String JavaDoc(name));
226     addEl(ret);
227     return ret;
228   }
229   
230   public BindTypeArray add_bindtypearray(String JavaDoc name, ExprOperDef[] indexes) throws RemoteException JavaDoc, TIRExceptCreate {
231     if (stKindImpl.value() == StateKind.sk_normal)
232       throw new TIRExceptCreate("You can call add method on work objects only");
233     ExprOperDef[] np = new ExprOperDef [indexes.length];
234     for (int i=0;i<indexes.length;i++)
235       np[i] = indexes[i];
236     BindTypeArray ret = new BindTypeArrayImpl(new String JavaDoc(name), np);
237     addEl(ret);
238     return ret;
239   }
240
241   public BindTypeNormal add_subnormal(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptCreate {
242     if (stKindImpl.value() == StateKind.sk_normal)
243       throw new TIRExceptCreate("You can call add method on work objects only");
244     BindTypeNormal ret = new BindTypeNormalImpl(new String JavaDoc(name));
245     addSub(ret);
246     return ret;
247   }
248
249   public BindTypeArray add_subarray(String JavaDoc name, ExprOperDef[] indexes) throws RemoteException JavaDoc, TIRExceptCreate {
250     if (stKindImpl.value() == StateKind.sk_normal)
251       throw new TIRExceptCreate("You can call add method on work objects only");
252     ExprOperDef[] np = new ExprOperDef [indexes.length];
253     for (int i=0;i<indexes.length;i++)
254       np[i] = indexes[i];
255     BindTypeArray ret = new BindTypeArrayImpl(new String JavaDoc(name), np);
256     addSub(ret);
257     return ret;
258   }
259 }
260
Popular Tags