KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ProfileDefImpl.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 import java.rmi.server.UnicastRemoteObject JavaDoc;
10
11 import SOFA.SOFAnode.Made.TIR.AbsoluteName;
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.Identification;
16 import SOFA.SOFAnode.Made.TIR.ProfileDef;
17 import SOFA.SOFAnode.Made.TIR.Repository;
18 import SOFA.SOFAnode.Made.TIR.StateKind;
19 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
20 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
21 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
22
23 public class ProfileDefImpl extends UnicastRemoteObject JavaDoc implements ProfileDef, TIRImplObject, SProfileDef {
24   DefinitionKindImpl defKindImpl;
25   StateKindImpl stKindImpl;
26
27   String JavaDoc nm;
28   LiIdItem firstId;
29   LiIdItem lastId;
30   int numOfIds;
31  
32   Repository rep;
33  
34   boolean isNewP;
35   boolean hasCont;
36   ProfileDefImpl normal;
37   boolean changed;
38
39   Lock plock;
40   long lockFor; // in normal
41

42   long contentId; // in work and normal, if normal > work -> reload content to work
43
boolean lockForMe; // in work
44

45   public ProfileDefImpl(String JavaDoc name, Repository r, int state) throws RemoteException JavaDoc {
46     nm = name;
47     firstId = lastId = null;
48     numOfIds = 0;
49     plock = new Lock(false);
50     lockFor = -1L;
51     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Profile);
52     stKindImpl = new StateKindImpl(state);
53     if (state==StateKind.sk_work)
54       isNewP = true;
55     else
56       isNewP = false;
57     hasCont = true;
58     normal = null;
59     rep = r;
60     changed = true;
61     contentId = 0;
62   }
63
64   public ProfileDefImpl(Repository r) throws RemoteException JavaDoc {
65     nm = null;
66     firstId = lastId = null;
67     numOfIds = 0;
68     plock = new Lock(false);
69     lockFor = -1L;
70     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Profile);
71     stKindImpl = new StateKindImpl(StateKind.sk_normal);
72     isNewP = false;
73     hasCont = true;
74     normal = null;
75     rep = r;
76     changed = true;
77     contentId = 0;
78   }
79
80   public ProfileDefImpl(ProfileDefImpl a, Repository r) throws RemoteException JavaDoc {
81     nm = a.nm;
82     firstId = lastId = null;
83     numOfIds = 0;
84     plock = a.plock;
85     lockFor = -1L;
86     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Profile);
87     stKindImpl = new StateKindImpl(StateKind.sk_work);
88     isNewP = false;
89     hasCont = false;
90     normal = a;
91     rep = r;
92     changed = false;
93     contentId = 0;
94   }
95
96   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
97     return (DefinitionKind) defKindImpl;
98   }
99
100   public StateKind get_state() throws RemoteException JavaDoc {
101     return (StateKind) stKindImpl;
102   }
103
104   public String JavaDoc name() throws RemoteException JavaDoc {
105     return nm;
106   }
107   
108   public Identification[] contents() throws RemoteException JavaDoc, TIRExceptLock {
109     int i;
110     if (stKindImpl.value() == StateKind.sk_normal) {
111       if (((RepositoryImpl) rep).lock.isLocked())
112         throw new TIRExceptLock("Repository is locked");
113       Identification[] ret = new Identification [numOfIds];
114       LiIdItem akt = firstId;
115       for(i=0;i<numOfIds;i++) {
116         ret[i] = akt.obj;
117         akt = akt.next;
118       }
119       return ret;
120     } else {
121       if (((WorkRepositoryImpl) rep).lock.isLocked())
122         throw new TIRExceptLock("Repository is locked");
123       if (normal!=null) {
124         if ((!hasCont) || (normal.contentId > this.contentId)) {
125           firstId = lastId = null;
126           numOfIds = 0;
127           LiIdItem akt = normal.firstId;
128           for(i=0;i<normal.numOfIds;i++) {
129             LiIdItem item = new LiIdItem(akt.obj);
130             if (firstId == null) {
131               firstId = lastId = item;
132             } else {
133               lastId.next = item;
134               item.prev = lastId;
135               lastId = item;
136             }
137             numOfIds++;
138             
139             akt = akt.next;
140           }
141           this.contentId = normal.contentId;
142         }
143         hasCont=true;
144       }
145       Identification[] ret = new Identification [numOfIds];
146       LiIdItem akt = firstId;
147       for(i=0;i<numOfIds;i++) {
148         ret[i] = akt.obj;
149         akt = akt.next;
150       }
151       return ret;
152     }
153   }
154   
155   public Identification lookup(String JavaDoc fullName) throws RemoteException JavaDoc, TIRExceptLock {
156     int i;
157     fullName = fullName.trim();
158     if (stKindImpl.value() == StateKind.sk_normal) {
159       if (((RepositoryImpl) rep).lock.isLocked())
160         throw new TIRExceptLock("Repository is locked");
161       //AbsoluteName an = new AbsoluteNameImpl(fullName);
162
LiIdItem akt = firstId;
163       //AbsoluteName akan;
164
for (i=0;i<numOfIds;i++) {
165         //akan = akt.obj.lang_absolute_name();
166
//if (akan.isEqual(an))
167
if (akt.key.compareTo(fullName)==0)
168           return akt.obj;
169         akt = akt.next;
170       }
171       return null;
172     } else {
173       if (((WorkRepositoryImpl) rep).lock.isLocked())
174         throw new TIRExceptLock("Repository is locked");
175       if (normal!=null) {
176         if ((!hasCont) || (normal.contentId > this.contentId)) {
177           firstId = lastId = null;
178           numOfIds = 0;
179           LiIdItem akt = normal.firstId;
180           for(i=0;i<normal.numOfIds;i++) {
181             LiIdItem item = new LiIdItem(akt.obj);
182             if (firstId == null) {
183               firstId = lastId = item;
184             } else {
185               lastId.next = item;
186               item.prev = lastId;
187               lastId = item;
188             }
189             numOfIds++;
190             
191             akt = akt.next;
192           }
193           contentId = normal.contentId;
194         }
195         hasCont=true;
196       }
197       //AbsoluteName an = new AbsoluteNameImpl(fullName);
198
LiIdItem akt = firstId;
199       //AbsoluteName akan;
200
for (i=0;i<numOfIds;i++) {
201         //akan = akt.obj.lang_absolute_name();
202
//if (akan.isEqual(an))
203
if (akt.key.compareTo(fullName)==0)
204           return akt.obj;
205         akt = akt.next;
206       }
207       return null;
208     }
209   }
210   
211   public boolean add(Identification a) throws RemoteException JavaDoc, TIRExceptLock, TIRExceptCreate {
212     if (stKindImpl.value() == StateKind.sk_normal)
213       throw new TIRExceptCreate("You can call create only on work object");
214
215     if (((WorkRepositoryImpl) rep).lock.isLocked())
216       throw new TIRExceptLock("Repository is locked");
217     if (normal!=null) {
218       if ((!hasCont) || (normal.contentId > this.contentId)) {
219         firstId = lastId = null;
220         numOfIds = 0;
221         LiIdItem akt = normal.firstId;
222         for(int i=0;i<normal.numOfIds;i++) {
223           LiIdItem item = new LiIdItem(akt.obj);
224           if (firstId == null) {
225             firstId = lastId = item;
226           } else {
227             lastId.next = item;
228             item.prev = lastId;
229             lastId = item;
230           }
231           numOfIds++;
232           akt = akt.next;
233         }
234         contentId = normal.contentId;
235       }
236       hasCont=true;
237     }
238
239     ((WorkRepositoryImpl) rep).normalRep.lockProfiles.lock();
240     if (this.isLocked()) {
241       if (!lockForMe) {
242         ((WorkRepositoryImpl) rep).normalRep.lockProfiles.unlock();
243         throw new TIRExceptLock("Profile is locked.");
244       }
245     }
246     if (!this.lock()) {
247       ((WorkRepositoryImpl) rep).normalRep.lockProfiles.unlock();
248       throw new TIRExceptLock("Profile can not lock.");
249     }
250     ((WorkRepositoryImpl) rep).normalRep.lockProfiles.unlock();
251     
252     //AbsoluteName an = a.lang_absolute_name();
253
String JavaDoc ans = a.lang_absolute_name().name();
254     //AbsoluteName akan;
255
LiIdItem akt = firstId;
256     boolean found = false;
257     for(int i=0;i<numOfIds;i++) {
258       //akan = akt.obj.lang_absolute_name();
259
//if (akan.isEqual(an))
260
if (akt.key.compareTo(ans)==0)
261         return false;
262       akt = akt.next;
263     }
264     
265     LiIdItem item = new LiIdItem(a);
266     if (firstId == null) {
267        firstId = lastId = item;
268     } else {
269       lastId.next = item;
270       item.prev = lastId;
271       lastId = item;
272     }
273     numOfIds++;
274     changed = true;
275     return true;
276   }
277   
278   public void remove(String JavaDoc fullName) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
279     if (stKindImpl.value() == StateKind.sk_normal)
280       throw new TIRExceptCreate("You can call create only on work object");
281
282     if (((WorkRepositoryImpl) rep).lock.isLocked())
283       throw new TIRExceptLock("Repository is locked");
284     if (normal!=null) {
285       if ((!hasCont) || (normal.contentId > this.contentId)) {
286         firstId = lastId = null;
287         numOfIds = 0;
288         LiIdItem akt = normal.firstId;
289         for(int i=0;i<normal.numOfIds;i++) {
290           LiIdItem item = new LiIdItem(akt.obj);
291           if (firstId == null) {
292             firstId = lastId = item;
293           } else {
294             lastId.next = item;
295             item.prev = lastId;
296             lastId = item;
297           }
298           numOfIds++;
299           akt = akt.next;
300         }
301         contentId = normal.contentId;
302       }
303       hasCont=true;
304     }
305
306     ((WorkRepositoryImpl) rep).normalRep.lockProfiles.lock();
307     if (this.isLocked()) {
308       if (!lockForMe) {
309         ((WorkRepositoryImpl) rep).normalRep.lockProfiles.unlock();
310         throw new TIRExceptLock("Profile is locked.");
311       }
312     }
313     if (!this.lock()) {
314       ((WorkRepositoryImpl) rep).normalRep.lockProfiles.unlock();
315       throw new TIRExceptLock("Profile can not lock.");
316     }
317     ((WorkRepositoryImpl) rep).normalRep.lockProfiles.unlock();
318
319     AbsoluteName an = new AbsoluteNameImpl(fullName);
320     LiIdItem akt = firstId;
321     AbsoluteName akan;
322     for (int i=0;i<numOfIds;i++) {
323       akan = akt.obj.lang_absolute_name();
324       if (akan.isEqual(an)) {
325         if (numOfIds==1) {
326           firstId = lastId = null;
327           numOfIds = 0;
328         } else {
329           if (akt == firstId) {
330             firstId = akt.next;
331         firstId.prev = null;
332         akt.next = null;
333       } else {
334             if (lastId == akt) {
335           lastId = akt.prev;
336               lastId.next = null;
337               akt.prev = null;
338         } else {
339           akt.prev.next = akt.next;
340               akt.next.prev = akt.prev;
341         }
342       }
343           numOfIds--;
344         }
345         changed = true;
346         return;
347       }
348       akt = akt.next;
349     }
350   }
351
352   public boolean isLocked() throws RemoteException JavaDoc {
353     return plock.isLocked();
354   }
355   
356   public boolean lock() throws RemoteException JavaDoc {
357     if (this.lockForMe)
358       return true;
359     boolean a = plock.tryLock();
360     if (a) {
361       if (normal!=null)
362         normal.lockFor = ((WorkRepositoryImpl) rep).workId;
363       this.lockForMe = true;
364     }
365     return a;
366   }
367   
368   public void unlock() throws RemoteException JavaDoc {
369     lockForMe = false;
370     if (normal!=null)
371       normal.lockFor = -1L;
372     plock.unlock();
373   }
374
375   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
376     try {
377       st.curOutFile = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(st.currentFile));
378       Storage.writeString(st.curOutFile,nm);
379       LiIdItem akt = firstId;
380       st.curOutFile.writeInt(numOfIds);
381       for (int i=0;i<numOfIds;i++) {
382         Storage.writeString(st.curOutFile,akt.obj.lang_absolute_name().name());
383         Storage.writeString(st.curOutFile,akt.obj.version());
384         akt = akt.next;
385       }
386       st.curOutFile.close();
387     } catch (IOException JavaDoc e) {
388       throw new TIRExceptStorage("Access error in "+st.current+".");
389     }
390   }
391
392   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
393     try {
394       st.curInFile = new DataInputStream JavaDoc(new FileInputStream JavaDoc(st.currentFile));
395       nm = Storage.readString(st.curInFile);
396       int num = st.curInFile.readInt();
397       for (int j=0;j<num;j++) {
398         String JavaDoc absName = Storage.readString(st.curInFile);
399         String JavaDoc version = Storage.readString(st.curInFile);
400         try {
401           AbsoluteName nm = new AbsoluteNameImpl(absName);
402           Container con = rep; // begin search from repository
403
Contained[] rt;
404           Contained ret;
405           IdentificationImpl id;
406           for (int i=0;i<nm.size()-1;i++) { // last element is name of object
407
id = new IdentificationImpl(nm.elementAt(0), "::"+nm.elementAt(i),"");
408             rt = ((SContainer) con).slookup_name(id);
409             if (rt.length==0) throw new TIRExceptStorage("Can't find type " + nm.name());
410             if (rt.length==1) {
411               con = (Container) rt[0];
412             } else { // type in struct (etc.) - more versions of this struct
413
boolean found = false;
414               id.version(version); // substruct version is inherited from parent
415
int k = 0;
416               for (k=0;k<rt.length;k++) {
417                 if (id.is_short_equal(rt[k].get_identification())) {
418                   found = true;
419                   break; // -- break for
420
}
421               } // -- end of for
422
if (!found) throw new TIRExceptStorage("Can't find type " + nm.name());
423               con = (Container) rt[k];
424             }
425           }
426           if ((ret = ((SContainer)con).slookup(nm.elementAt(nm.size()-1),version))==null)
427             throw new TIRExceptStorage("Can't find type " + nm.name());
428           LiIdItem item = new LiIdItem(ret.get_identification());
429
430           if (firstId == null) {
431             firstId = lastId = item;
432           } else {
433             lastId.next = item;
434             item.prev = lastId;
435             lastId = item;
436           }
437           numOfIds++;
438         } catch (TIRExceptLock e) {;}
439       }
440       
441       st.curInFile.close();
442     } catch (IOException JavaDoc e) {
443       throw new TIRExceptStorage("Access error in "+st.current+".");
444     }
445   }
446
447   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {}
448   public boolean isNew() { return isNewP; }
449   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {
450     // testing if all identification in this profile is OK
451
try {
452     LiIdItem akt = firstId;
453     for (int k=0;k<numOfIds;k++) {
454       AbsoluteName nm = akt.obj.lang_absolute_name();
455       Container con = rep; // begin search from repository
456
Contained[] rt;
457       Contained ret;
458       IdentificationImpl id;
459       for (int i=0;i<nm.size()-1;i++) { // last element is name of object
460
id = new IdentificationImpl(nm.elementAt(0), "::"+nm.elementAt(i),"");
461         rt = ((SContainer) con).slookup_name(id);
462         if (rt.length==0) throw new TIRExceptCommit("Can't find (1) object " + nm.name());
463         if (rt.length==1) {
464           if (!(rt[0] instanceof Container))
465             throw new TIRExceptCommit("Can't find (2) object " + nm.name());
466           con = (Container) rt[0];
467         } else { // type in struct (etc.) - more versions of this struct
468
boolean found = false;
469           id.version(akt.obj.version()); // substruct version is inherited from parent
470
int j;
471           for (j=0;j<rt.length;j++) {
472             if (id.is_short_equal(rt[j].get_identification())) {
473               found = true;
474               break; // -- break for
475
}
476       } // -- end of for
477
if (!found) throw new TIRExceptCommit("Can't find (3) object " + nm.name());
478           if (!(rt[j] instanceof Container))
479             throw new TIRExceptCommit("Can't find (4) object " + nm.name());
480       con = (Container) rt[j];
481     }
482       }
483       if ((ret = ((SContainer)con).slookup(akt.obj))==null)
484         throw new TIRExceptCommit("Can't find (5) object " + nm.name());
485         
486       akt = akt.next;
487     }
488     } catch (TIRExceptLock e) {}
489   }
490   
491   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
492     if (normal!=null)
493       normal.contentId = ((WorkRepositoryImpl) rep).workId;
494     if (isNewP) {
495       stKindImpl.toNormal();
496       this.rep = rep;
497     }
498   }
499   
500   public void doAbort(long workId) throws RemoteException JavaDoc {}
501 }
502
Popular Tags