KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ManageProfilesImpl.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 import java.rmi.server.UnicastRemoteObject JavaDoc;
9
10 import SOFA.SOFAnode.Made.TIR.Container;
11 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
12 import SOFA.SOFAnode.Made.TIR.ManageProfiles;
13 import SOFA.SOFAnode.Made.TIR.ProfileDef;
14 import SOFA.SOFAnode.Made.TIR.Repository;
15 import SOFA.SOFAnode.Made.TIR.StateKind;
16 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
17 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
18 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
19 import SOFA.SOFAnode.Made.TIR.TIRObject;
20
21 public class ManageProfilesImpl extends UnicastRemoteObject JavaDoc implements ManageProfiles, TIRImplObject {
22   DefinitionKindImpl defKindImpl;
23   StateKindImpl stKindImpl;
24   LiItem firstProf;
25   LiItem lastProf;
26   int numOfProfs;
27   Repository rep;
28   boolean hasCont;
29   ManageProfilesImpl normal;
30
31   public ManageProfilesImpl(Repository rep) throws RemoteException JavaDoc {
32     firstProf = lastProf = null;
33     numOfProfs = 0;
34     this.rep = rep;
35     hasCont = true;
36     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_ManageProfiles);
37     stKindImpl = new StateKindImpl(StateKind.sk_normal);
38     normal = null;
39   }
40
41   public ManageProfilesImpl(ManageProfilesImpl a, Repository rep) throws RemoteException JavaDoc {
42     firstProf = lastProf = null;
43     numOfProfs = 0;
44     this.rep = rep;
45     hasCont = false;
46     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_ManageProfiles);
47     stKindImpl = new StateKindImpl(StateKind.sk_work);
48     normal = a;
49   }
50
51   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
52     return (DefinitionKind) defKindImpl;
53   }
54
55   public StateKind get_state() throws RemoteException JavaDoc {
56     return (StateKind) stKindImpl;
57   }
58   
59   public ProfileDef[] all_profiles() throws RemoteException JavaDoc, TIRExceptLock {
60     int i;
61     if (stKindImpl.value() == StateKind.sk_normal) {
62       if (((RepositoryImpl) rep).lock.isLocked())
63         throw new TIRExceptLock("Repository is locked");
64       ProfileDef[] ret = new ProfileDef [numOfProfs];
65       LiItem akt = firstProf;
66       for(i=0;i<numOfProfs;i++) {
67         ret[i] = (ProfileDef) akt.obj;
68         akt = akt.next;
69       }
70       return ret;
71     } else {
72       if (((WorkRepositoryImpl) rep).lock.isLocked())
73         throw new TIRExceptLock("Repository is locked");
74       if (!hasCont) {
75         if (normal!=null) {
76           LiItem akt = normal.firstProf;
77           for(i=0;i<normal.numOfProfs;i++) {
78             LiItem item = new LiItem(new ProfileDefImpl((ProfileDefImpl) akt.obj, rep));
79             if (firstProf == null)
80               firstProf = lastProf = item;
81             else {
82               lastProf.next = item;
83               item.prev = lastProf;
84               lastProf = item;
85             }
86             numOfProfs++;
87             akt = akt.next;
88           }
89         }
90         hasCont=true;
91       }
92       ProfileDef[] ret = new ProfileDef [numOfProfs];
93       LiItem akt = firstProf;
94       for(i=0;i<numOfProfs;i++) {
95         ret[i] = (ProfileDef) akt.obj;
96         akt = akt.next;
97       }
98       return ret;
99     }
100   }
101   
102   public ProfileDef lookup(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptLock {
103     int i;
104     if (stKindImpl.value() == StateKind.sk_normal) {
105       if (((RepositoryImpl) rep).lock.isLocked())
106         throw new TIRExceptLock("Repository is locked");
107       LiItem akt = firstProf;
108       for(i=0;i<numOfProfs;i++) {
109         if ((name.compareTo(((ProfileDef) akt.obj).name()))==0)
110           return (ProfileDef) akt.obj;
111         akt = akt.next;
112       }
113       return null;
114     } else {
115       if (((WorkRepositoryImpl) rep).lock.isLocked())
116         throw new TIRExceptLock("Repository is locked");
117       if (!hasCont) {
118         if (normal!=null) {
119           LiItem akt = normal.firstProf;
120           for(i=0;i<normal.numOfProfs;i++) {
121             LiItem item = new LiItem(new ProfileDefImpl((ProfileDefImpl) akt.obj, rep));
122             if (firstProf == null)
123               firstProf = lastProf = item;
124             else {
125               lastProf.next = item;
126               item.prev = lastProf;
127               lastProf = item;
128             }
129             numOfProfs++;
130             akt = akt.next;
131           }
132         }
133         hasCont = true;
134       }
135       LiItem akt = firstProf;
136       for(i=0;i<numOfProfs;i++) {
137         if ((name.compareTo(((ProfileDef) akt.obj).name()))==0)
138           return (ProfileDef) akt.obj;
139         akt = akt.next;
140       }
141       return null;
142     }
143   }
144   
145   synchronized public ProfileDef create_profile(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptLock, TIRExceptCreate {
146     if (stKindImpl.value() == StateKind.sk_normal)
147       throw new TIRExceptCreate("You can call create only on work object");
148
149     if (((WorkRepositoryImpl) rep).lock.isLocked())
150       throw new TIRExceptLock("Repository is locked");
151     if (!hasCont) {
152       if (normal!=null) {
153         LiItem akt = normal.firstProf;
154         for(int i=0;i<normal.numOfProfs;i++) {
155           LiItem item = new LiItem(new ProfileDefImpl((ProfileDefImpl) akt.obj, rep));
156           if (firstProf == null)
157             firstProf = lastProf = item;
158           else {
159             lastProf.next = item;
160             item.prev = lastProf;
161             lastProf = item;
162           }
163           numOfProfs++;
164           akt = akt.next;
165         }
166       }
167       hasCont = true;
168     }
169
170     LiItem akt = firstProf;
171     for(int i=0;i<numOfProfs;i++) {
172       if ((name.compareTo(((ProfileDef) akt.obj).name()))==0)
173         throw new TIRExceptCreate("Profile with this name exists");
174       akt = akt.next;
175     }
176     
177     ProfileDef ret = new ProfileDefImpl(new String JavaDoc(name), rep, StateKind.sk_work);
178     LiItem item = new LiItem(ret);
179     if (firstProf == null) {
180        firstProf = lastProf = item;
181     } else {
182       lastProf.next = item;
183       item.prev = lastProf;
184       lastProf = item;
185     }
186     numOfProfs++;
187     return ret;
188   }
189
190   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
191     try {
192       String JavaDoc[][] childs = null;
193       try {
194         childs = st.listOfChilds();
195       } catch (TIRExceptStorage e) { // new dir without index.tir
196
;
197       }
198       File JavaDoc f = new File JavaDoc(st.current,Storage.indexFile); // name of index file
199
// ((IdentificationImpl)id).save(f); //saving identification
200
LiItem akt = firstProf;
201       File JavaDoc old = st.current;
202       BufferedWriter JavaDoc index = new BufferedWriter JavaDoc(new FileWriter JavaDoc(f.toString()));
203       index.write("CHILDS",0,6); index.newLine();
204       for(int i=0;i<numOfProfs;i++) {
205         String JavaDoc[] achild; // actual child info
206
IdentificationImpl idl;
207     File JavaDoc nfile; // path to child's current path
208
if ((achild=st.existsChild(childs,idl = new IdentificationImpl("","::"+((ProfileDef)akt.obj).name(),"")))!=null) { // file (dir) exists
209
// saving info to index file
210
index.write(achild[0],0,achild[0].length()); index.write("?",0,1);
211           index.write(achild[1],0,achild[1].length()); index.write("?",0,1);
212           index.write(achild[2],0,achild[2].length()); index.write("?",0,1);
213           index.write(achild[3],0,achild[3].length()); index.newLine();
214       nfile = new File JavaDoc(old,achild[0]);
215       
216         } else { //file (dir) doesn't exist
217
String JavaDoc hlp;
218           nfile = File.createTempFile("prf","",old);
219           nfile.delete();
220       // saving info to index file
221
index.write(hlp = nfile.getName(),0,hlp.length()); index.write("?",0,1);
222       index.write(hlp=Integer.toString(akt.obj.get_def_kind().value()),0,hlp.length());
223       index.write("?",0,1);
224       index.write(hlp = ((ProfileDef)akt.obj).name(),0,hlp.length()); index.write("?",0,1);
225           index.write("",0,0); index.newLine();
226         }
227     st.currentFile = nfile;
228     st.curOutFile = null;
229         
230     ((TIRImplObject) akt.obj).save(st); // save akt object
231
akt = akt.next; // go to the next object
232
} // -- end of for
233
index.close();
234       st.current = old;
235     } catch (IOException JavaDoc e) {
236       throw new TIRExceptStorage("Access error in "+st.current+".");
237     }
238   }
239
240   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage{
241     String JavaDoc[][] childs = st.listOfChilds();
242     File JavaDoc old = st.current;
243     TIRImplObject m;
244     for(int i=0;i<childs.length;i++) {
245        m = new ProfileDefImpl(rep);
246        st.currentFile = new File JavaDoc(old,childs[i][0]);
247        m.load(st);
248        LiItem item = new LiItem((TIRObject) m);
249        if (firstProf == null)
250          firstProf = lastProf = item;
251        else {
252          lastProf.next = item;
253          item.prev = lastProf;
254          lastProf = item;
255        }
256        numOfProfs++;
257        st.currentFile = null;
258     }
259   }
260
261   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {
262     LiItem akt = firstProf;
263     for (int i=0;i<numOfProfs; i++) {
264       if (((TIRImplObject) akt.obj).isNew()) {
265         LiItem nak = normal.firstProf;
266         for (int j=0;j<normal.numOfProfs;j++) {
267           if (((ProfileDef) akt.obj).name().compareTo(((ProfileDef) nak.obj).name())==0)
268             throw new TIRExceptCommit("Profile with name " + ((ProfileDef) akt.obj).name() + " exists");
269           nak = nak.next;
270         }
271       }
272       ((TIRImplObject) akt.obj).canCommit();
273       akt = akt.next;
274     }
275   }
276
277   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {}
278   public boolean isNew() { return false; }
279
280   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
281     LiItem akt = firstProf;
282     for (int i=0;i<numOfProfs;i++) {
283       if (((TIRImplObject)akt.obj).isNew()) {
284         ((TIRImplObject) akt.obj).doCommit(in, rep);
285         LiItem item = new LiItem(akt.obj);
286         if (normal.numOfProfs==0) {
287           normal.firstProf = normal.lastProf = item;
288         } else {
289           normal.lastProf.next = item;
290           item.prev = normal.lastProf;
291           normal.lastProf = item;
292         }
293         normal.numOfProfs++;
294       } else { // profile exists
295
if (((ProfileDefImpl) akt.obj).changed) {
296           ProfileDefImpl work = (ProfileDefImpl) akt.obj;
297           ProfileDefImpl norm = searchNormProfile(((ProfileDefImpl)akt.obj).name());
298           mergeProfiles(norm,work);
299         }
300         if (((ProfileDefImpl) akt.obj).isLocked()) {
301           if (((ProfileDefImpl) akt.obj).lockForMe) {
302             ((ProfileDefImpl) akt.obj).unlock();
303           }
304         }
305       }
306
307       akt = akt.next;
308     }
309   }
310
311   private ProfileDefImpl searchNormProfile(String JavaDoc name) throws RemoteException JavaDoc {
312     LiItem akt = normal.firstProf;
313     for (int i=0;i<normal.numOfProfs;i++) {
314       if (name.compareTo(((ProfileDef)akt.obj).name())==0)
315         return (ProfileDefImpl) akt.obj;
316       akt = akt.next;
317     }
318     return null;
319   }
320
321   private void mergeProfiles(ProfileDefImpl normal, ProfileDefImpl work) throws RemoteException JavaDoc {
322     normal.firstId = normal.lastId = null;
323     normal.numOfIds = 0;
324     LiIdItem akt = work.firstId;
325     for (int i=0;i<work.numOfIds;i++) {
326       LiIdItem item = new LiIdItem(akt.obj);
327       if (normal.firstId == null) {
328         normal.firstId = normal.lastId = item;
329       } else {
330         normal.lastId.next = item;
331         item.prev = normal.lastId;
332         normal.lastId = item;
333       }
334       normal.numOfIds++;
335       akt = akt.next;
336     }
337   }
338   
339   public void doAbort(long workId) throws RemoteException JavaDoc {
340     LiItem akt = firstProf;
341     for (int i=0;i<numOfProfs;i++) {
342       if (!((TIRImplObject)akt.obj).isNew()) {
343         if (((ProfileDefImpl) akt.obj).isLocked()) {
344           if (((ProfileDefImpl) akt.obj).lockForMe) {
345             ((ProfileDefImpl) akt.obj).unlock();
346           }
347         }
348       }
349       akt = akt.next;
350     }
351   }
352 }
353
Popular Tags