KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > repositorypersistence > FileSystemRepositoryPersistenceManager


1 package org.enhydra.shark.repositorypersistence;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.enhydra.shark.api.RepositoryTransaction;
7 import org.enhydra.shark.api.RootException;
8 import org.enhydra.shark.api.TransactionException;
9 import org.enhydra.shark.api.common.SharkConstants;
10 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryException;
11 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryPersistenceManager;
12 import org.enhydra.shark.api.internal.working.CallbackUtilities;
13 import org.enhydra.shark.utilities.MiscUtilities;
14 import org.enhydra.shark.xpdl.XMLUtil;
15
16 /**
17  * File system implementation of Repository persistence interface.
18  *
19  * @author Sasa Bojanic
20  *
21  */

22 public class FileSystemRepositoryPersistenceManager implements RepositoryPersistenceManager {
23
24    private CallbackUtilities cus;
25    // The locations to various repositories
26
private String JavaDoc XPDL_REPOSITORY;
27
28    // Changed from private to public
29
private String JavaDoc XPDL_HISTORY_REPOSITORY;
30
31    private final String JavaDoc EXT_REF_FNAME="#ext_references#";
32    private final String JavaDoc NEXT_IDS_FNAME="#next_ids#";
33    private final String JavaDoc SER_PKG ="#ser_pkg#";
34    private final String JavaDoc HASH = "#";
35    
36    private String JavaDoc EXT_REFERENCES_FILE_NAME;
37    private String JavaDoc NEXT_IDS_FILE_NAME;
38
39    private ExternalReferences extRefs;
40    private NextVersions nextVersions;
41
42    public void configure(CallbackUtilities cus) throws RootException {
43       this.cus=cus;
44       String JavaDoc xr=cus.getProperty("FileSystemRepositoryPersistenceManager.XPDL_REPOSITORY");
45       String JavaDoc hxr=cus.getProperty("FileSystemRepositoryPersistenceManager.XPDL_HISTORY_REPOSITORY");
46       XPDL_REPOSITORY=getRepositoryFullPath(xr);
47       XPDL_HISTORY_REPOSITORY=getRepositoryFullPath(hxr);
48       EXT_REFERENCES_FILE_NAME=XPDL_REPOSITORY+File.separator+EXT_REF_FNAME;
49       NEXT_IDS_FILE_NAME=XPDL_REPOSITORY+File.separator+NEXT_IDS_FNAME;
50
51       File xrf=new File(XPDL_REPOSITORY);
52       if (!xrf.exists()) {
53          xrf.mkdir();
54       }
55       File hxrf=new File(XPDL_HISTORY_REPOSITORY);
56       if (!hxrf.exists()) {
57          hxrf.mkdir();
58       }
59
60       File extR=new File(EXT_REFERENCES_FILE_NAME);
61       if (extR.exists()) {
62          try {
63             extRefs=(ExternalReferences)readFile(EXT_REFERENCES_FILE_NAME);
64          } catch (Exception JavaDoc ex) {
65             throw new RootException(ex);
66          }
67       } else {
68          extRefs=new ExternalReferences();
69       }
70
71       File nextV=new File(NEXT_IDS_FILE_NAME);
72       if (nextV.exists()) {
73          try {
74             nextVersions=(NextVersions)readFile(NEXT_IDS_FILE_NAME);
75          } catch (Exception JavaDoc ex) {
76             throw new RootException(ex);
77          }
78       } else {
79          nextVersions=new NextVersions();
80       }
81    }
82
83    public void uploadXPDL (RepositoryTransaction t,String JavaDoc xpdlId,byte[] xpdl,byte[] serializedPkg,long xpdlClassVer) throws RepositoryException {
84       try {
85          String JavaDoc newVersion=nextVersions.updateNextVersion(xpdlId);
86          writeFile(nextVersions,NEXT_IDS_FILE_NAME);
87          FileOutputStream fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+newVersion);
88          fos.write(xpdl);
89          // Write to file
90
fos.flush();
91          fos.close();
92          fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+SER_PKG+xpdlClassVer+HASH+"-"+newVersion);
93          fos.write(serializedPkg);
94          // Write to file
95
fos.flush();
96          fos.close();
97       } catch (Exception JavaDoc ex) {
98          cus.error("FileSystemRepositoryPersistenceManager -> The upload of the file "+xpdlId+" failed");
99          throw new RepositoryException(ex);
100       }
101    }
102
103    public void updateXPDL (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion,byte[] xpdl,byte[] serializedPkg,long xpdlClassVer) throws RepositoryException {
104       try {
105          getXPDLFile(xpdlId,xpdlVersion); // this will throw an exception if file doesn't exist
106
deleteXPDL(t, xpdlId, xpdlVersion);
107          FileOutputStream fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+xpdlVersion);
108          fos.write(xpdl);
109          // Write to file
110
fos.flush();
111          fos.close();
112          fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+SER_PKG+xpdlClassVer+HASH+"-"+xpdlVersion);
113          fos.write(serializedPkg);
114          // Write to file
115
fos.flush();
116          fos.close();
117       } catch (Exception JavaDoc ex) {
118          cus.error("FileSystemRepositoryPersistenceManager -> The update of the file "+xpdlId+"-"+xpdlVersion+" failed");
119          throw new RepositoryException(ex);
120       }
121    }
122
123    public void deleteXPDL (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
124       try {
125          if (!getXPDLFile(xpdlId,xpdlVersion).delete()) {
126             throw new Exception JavaDoc("File "+xpdlId+"-"+xpdlVersion+" is not deleted from repository");
127          }
128          if (!getSerializedXPDLFile(xpdlId,xpdlVersion).delete()) {
129             throw new Exception JavaDoc("The serialized pkg File "+xpdlId+"-"+xpdlVersion+" is not deleted from repository");
130          }
131          extRefs.removeReferrencedIds(xpdlId,xpdlVersion);
132          writeFile(extRefs,EXT_REFERENCES_FILE_NAME);
133       } catch (Exception JavaDoc ex) {
134          throw new RepositoryException(ex);
135       }
136    }
137
138    public void moveToHistory (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
139       try {
140          File f1=getXPDLFile(xpdlId,xpdlVersion);
141          File f2=getSerializedXPDLFile(xpdlId,xpdlVersion);
142
143          String JavaDoc historyFolder=XPDL_HISTORY_REPOSITORY+File.separator+xpdlId;
144          File fhf=new File(historyFolder);
145          if (!fhf.exists()) {
146             fhf.mkdir();
147          }
148
149          String JavaDoc historyFilename1=historyFolder+File.separator+f1.getName()+".xpdl";
150          String JavaDoc historyFilename2=historyFolder+File.separator+f2.getName();
151          MiscUtilities.copyFile(f1.getCanonicalPath(),historyFilename1);
152          MiscUtilities.copyFile(f2.getCanonicalPath(),historyFilename2);
153
154          // delete the removed file from internal repository
155
if (!f1.delete() || !f2.delete()) {
156             throw new Exception JavaDoc("File "+xpdlId+"-"+xpdlVersion+" is not deleted from repository");
157          }
158          extRefs.removeReferrencedIds(xpdlId,xpdlVersion);
159          writeFile(extRefs,EXT_REFERENCES_FILE_NAME);
160       } catch (Exception JavaDoc ex) {
161          throw new RepositoryException(ex);
162       }
163
164    }
165
166    public void deleteFromHistory (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
167       List xpdlFiles=getXPDLFiles(XPDL_HISTORY_REPOSITORY+File.separator+xpdlId,false,new XPDLIdFilter(xpdlId,xpdlVersion,true,false));
168       if (xpdlFiles.size()>0) {
169          if (!((File)xpdlFiles.get(0)).delete()) {
170             throw new RepositoryException("File is not deleted from history repository");
171          }
172       }
173       xpdlFiles=getXPDLFiles(XPDL_HISTORY_REPOSITORY+File.separator+xpdlId,false,new XPDLIdFilter(xpdlId,xpdlVersion,false,true));
174       if (xpdlFiles.size()>0) {
175          if (!((File)xpdlFiles.get(0)).delete()) {
176             throw new RepositoryException("File is not deleted from history repository");
177          }
178       }
179
180       throw new RepositoryException("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the repository");
181    }
182
183    public void clearRepository (RepositoryTransaction t) throws RepositoryException {
184       try {
185          List xpdlFiles=getXPDLFiles(XPDL_REPOSITORY,false,new XPDLIdFilter(null,null,false,false));
186          Iterator itXPDL=xpdlFiles.iterator();
187          while (itXPDL.hasNext()) {
188             if (!((File)itXPDL.next()).delete()) {
189                throw new Exception JavaDoc("Some file is not deleted from repository");
190             }
191          }
192          xpdlFiles=getXPDLFiles(XPDL_REPOSITORY,false,new XPDLIdFilter(null,null,false,true));
193          itXPDL=xpdlFiles.iterator();
194          while (itXPDL.hasNext()) {
195             if (!((File)itXPDL.next()).delete()) {
196                throw new Exception JavaDoc("Some file is not deleted from repository");
197             }
198          }
199       } catch (Exception JavaDoc ex) {
200          throw new RepositoryException(ex);
201       }
202    }
203
204    public String JavaDoc getCurrentVersion (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
205       try {
206          return getFileVersion(getXPDLFile(xpdlId,null));
207       } catch (Exception JavaDoc ex) {
208          throw new RepositoryException(ex);
209       }
210    }
211
212    public String JavaDoc getNextVersion (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
213       try {
214          return nextVersions.getNextVersion(xpdlId);
215       } catch (Exception JavaDoc ex) {
216          throw new RepositoryException(ex);
217       }
218    }
219
220    public long getSerializedXPDLObjectVersion (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
221       try {
222          return Long.parseLong(getSerializedFileVersion(getSerializedXPDLFile(xpdlId,xpdlVersion)));
223       } catch (Exception JavaDoc ex) {
224          throw new RepositoryException(ex);
225       }
226    }
227
228    public byte[] getXPDL (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
229       try {
230          return fileToByteArray(getXPDLFile(xpdlId,null));
231       } catch (Exception JavaDoc ex) {
232          throw new RepositoryException(ex);
233       }
234    }
235
236    public byte[] getSerializedXPDLObject (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
237       try {
238          return fileToByteArray(getSerializedXPDLFile(xpdlId,null));
239       } catch (Exception JavaDoc ex) {
240          throw new RepositoryException(ex);
241       }
242    }
243
244    public byte[] getXPDL (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
245       try {
246          return fileToByteArray(getXPDLFile(xpdlId,xpdlVersion));
247       } catch (Exception JavaDoc ex) {
248          throw new RepositoryException(ex);
249       }
250    }
251
252    public byte[] getSerializedXPDLObject (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
253       try {
254          return fileToByteArray(getSerializedXPDLFile(xpdlId,xpdlVersion));
255       } catch (Exception JavaDoc ex) {
256          throw new RepositoryException(ex);
257       }
258    }
259
260    public List getXPDLVersions (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
261       List xpdlFiles=getXPDLFiles(XPDL_REPOSITORY,false,new XPDLIdFilter(xpdlId,null,false,false));
262       if (xpdlFiles.size()==0) {
263          throw new RepositoryException(
264             "There is no xpdl with Id="+xpdlId+" in the repository");
265       }
266       List xpdlVersions=new ArrayList();
267       Iterator itXPDL=xpdlFiles.iterator();
268       while (itXPDL.hasNext()) {
269          File f=(File)itXPDL.next();
270          try {
271             xpdlVersions.add(getFileVersion(f));
272          } catch (Exception JavaDoc ex) {
273             throw new RepositoryException(ex);
274          }
275       }
276       return xpdlVersions;
277    }
278
279    public boolean doesXPDLExist (RepositoryTransaction t,String JavaDoc xpdlId) throws RepositoryException {
280       try {
281          getXPDLFile(xpdlId,null);
282          return true;
283       } catch (Exception JavaDoc ex) {
284          return false;
285       }
286    }
287
288    public boolean doesXPDLExist (RepositoryTransaction t,String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws RepositoryException {
289       try {
290          getXPDLFile(xpdlId,xpdlVersion);
291          return true;
292       } catch (Exception JavaDoc ex) {
293          return false;
294       }
295    }
296
297    public List getExistingXPDLIds (RepositoryTransaction t) throws RepositoryException {
298       List xpdlFiles=getXPDLFiles(XPDL_REPOSITORY,false,new XPDLIdFilter(null,null,false,false));
299       Set ids=new HashSet();
300       Iterator itXPDL=xpdlFiles.iterator();
301       while (itXPDL.hasNext()) {
302          File f=(File)itXPDL.next();
303          String JavaDoc n=f.getName();
304          int li=n.lastIndexOf("-");
305          String JavaDoc fId=n.substring(0,li);
306          ids.add(fId);
307       }
308
309       return new ArrayList(ids);
310    }
311
312    public void addXPDLReference (RepositoryTransaction t,
313                                  String JavaDoc referredXPDLId,
314                                  String JavaDoc referringXPDLId,
315                                  String JavaDoc referringXPDLVersion,
316                                  int referredXPDLNumber) throws RepositoryException {
317       try {
318          extRefs.addExtRef(referredXPDLId,referringXPDLId,referringXPDLVersion,referredXPDLNumber);
319          writeFile(extRefs,EXT_REFERENCES_FILE_NAME);
320       } catch (Exception JavaDoc ex) {
321          extRefs.remExtRef(referredXPDLId,referringXPDLId,referringXPDLVersion);
322          throw new RepositoryException(ex);
323       }
324    }
325
326    public List getReferringXPDLIds (RepositoryTransaction t,String JavaDoc referredXPDLId) throws RepositoryException {
327       return extRefs.getExtRefIds(referredXPDLId);
328    }
329
330    public List getReferringXPDLVersions (RepositoryTransaction t,String JavaDoc referredXPDLId,String JavaDoc refferingXPDLId) throws RepositoryException {
331       return extRefs.getExtRefVersions(referredXPDLId,refferingXPDLId);
332    }
333
334    public List getReferredXPDLIds (RepositoryTransaction t,String JavaDoc refferingXPDLId,String JavaDoc refferingXPDLVersion) throws RepositoryException {
335       return extRefs.getReferrencedIds(refferingXPDLId,refferingXPDLVersion);
336    }
337
338    public RepositoryTransaction createTransaction() throws TransactionException {
339       return null;
340    }
341
342    private void writeFile (Object JavaDoc obj,String JavaDoc fName) throws Exception JavaDoc {
343       OutputStream fos = new FileOutputStream(fName);
344       ObjectOutputStream oout = new ObjectOutputStream(fos);
345       oout.writeObject(obj);
346       oout.flush();
347       oout.close();
348       fos.close();
349    }
350
351    private Object JavaDoc readFile (String JavaDoc fName) throws Exception JavaDoc {
352       InputStream fis = new FileInputStream(fName);
353       ObjectInputStream oin = new ObjectInputStream(fis);
354       Object JavaDoc obj=oin.readObject();
355       oin.close();
356       return obj;
357    }
358
359    /**
360     * Returns the full path to the repository based on given path.
361     * If repository doesn't exist, the one is created at that location.
362     *
363     * @param path path (maybe relative) of the repository
364     * @return String containing full path to the repository
365     */

366    private String JavaDoc getRepositoryFullPath (String JavaDoc path) {
367       String JavaDoc rdPath=cus.getProperty(SharkConstants.ROOT_DIRECTORY_PATH_PROP);
368       //System.setProperty("user.dir",rdPath);
369
// if repository don't exist, create it
370
File f=new File(path);
371
372       if (!f.isAbsolute()) {
373          f=new File(XMLUtil.createPath(rdPath,path));
374          //f=f.getAbsoluteFile();
375
}
376
377       if (!f.exists()) {
378          if (!f.mkdir()) {
379             return path;
380          }
381       }
382
383       try {
384          return f.getCanonicalPath();
385       } catch (Exception JavaDoc ex) {
386          return f.getAbsolutePath();
387       }
388    }
389
390    /**
391     * Converts a file specified to the array of bytes.
392     */

393    private byte[] fileToByteArray (File xpdlFile) throws Exception JavaDoc {
394       // TODO: put this method in Utilities, and see about the one in SharkUtilties.
395
byte[] utf8Bytes=null;
396       if (xpdlFile != null) {
397          try {
398             FileInputStream fis=new FileInputStream(xpdlFile);
399             int noOfBytes=fis.available();
400             if (noOfBytes>0) {
401                utf8Bytes=new byte[noOfBytes];
402                // must do it byte by byte, otherwise sometimes it will be only partially read
403
int nextB, i=0;
404                while ((nextB=fis.read())!=-1) {
405                   utf8Bytes[i++]=(byte)nextB;
406                }
407             }
408          }
409          catch (Throwable JavaDoc ex) {
410             cus.error("FileSystemRepositoryPersistenceManager -> Problems while getting XPDL file "+xpdlFile);
411             throw new Exception JavaDoc();
412          }
413       }
414       return utf8Bytes;
415    }
416
417    private File getXPDLFile (String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws Exception JavaDoc {
418       List xpdlFiles=getXPDLFiles(XPDL_REPOSITORY,false,new XPDLIdFilter(xpdlId,xpdlVersion,false,false));
419       if (xpdlFiles.size()>0) {
420          return getLastVersionXPDLFile(xpdlFiles);
421       }
422
423       throw new Exception JavaDoc("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the repository");
424    }
425
426    private File getSerializedXPDLFile (String JavaDoc xpdlId,String JavaDoc xpdlVersion) throws Exception JavaDoc {
427       List xpdlFiles=getXPDLFiles(XPDL_REPOSITORY,false,new XPDLIdFilter(xpdlId,xpdlVersion,false,true));
428       if (xpdlFiles.size()>0) {
429          return getLastVersionXPDLFile(xpdlFiles);
430       }
431
432       throw new Exception JavaDoc("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the repository");
433    }
434
435    private File getLastVersionXPDLFile (List xpdlFiles) throws Exception JavaDoc {
436       File lastFileVersion=null;
437       int maxVer=-1;
438       Iterator itXPDL=xpdlFiles.iterator();
439       while (itXPDL.hasNext()) {
440          File f=(File)itXPDL.next();
441          String JavaDoc fv=getFileVersion(f);
442          int ver=Integer.parseInt(fv);
443          if (ver>maxVer) {
444             maxVer=ver;
445             lastFileVersion=f;
446          }
447       }
448
449       if (lastFileVersion==null) throw new Exception JavaDoc ("Something is wrong in XPDL repository - can't determine file version");
450
451       return lastFileVersion;
452    }
453
454    private String JavaDoc getFileVersion (File f) throws RootException {
455       String JavaDoc n=f.getName();
456       int li=n.lastIndexOf("-");
457       String JavaDoc fv=n.substring(li+1);
458       return fv;
459    }
460
461    private String JavaDoc getSerializedFileVersion (File f) throws RootException {
462       String JavaDoc n=f.getName();
463       int li1=n.indexOf(SER_PKG);
464       int li2=n.lastIndexOf(HASH);
465       String JavaDoc fv=n.substring(li1+SER_PKG.length(),li2);
466       return fv;
467    }
468    
469    // TODO: put this method in utilitites, and see about the one in SharkUtilities
470
private List getXPDLFiles (String JavaDoc repository,boolean traverse,FileFilter ff) {
471       File startingFolder=new File(repository);
472       List packageFiles=new ArrayList();
473       if (traverse) {
474          MiscUtilities.traverse(startingFolder,packageFiles,null);
475       } else {
476          packageFiles=Arrays.asList(startingFolder.listFiles(ff));
477       }
478       return packageFiles;
479    }
480
481    class XPDLIdFilter implements FileFilter {
482       private String JavaDoc xpdlId;
483       private String JavaDoc xpdlVersion;
484       private boolean hasExtension;
485       private boolean retrieveOnlySerialized;
486
487       public XPDLIdFilter (String JavaDoc xpdlId,String JavaDoc xpdlVersion,boolean hasExtension,boolean retrieveOnlySerialized) {
488          this.xpdlId=xpdlId;
489          this.xpdlVersion=xpdlVersion;
490          this.hasExtension=hasExtension;
491          this.retrieveOnlySerialized=retrieveOnlySerialized;
492       }
493
494       public boolean accept (File file) {
495
496          if (file.isDirectory()) return false;
497
498          String JavaDoc fileName=file.getName();
499          boolean isSerFile=fileName.indexOf(SER_PKG)>=0;
500          boolean accept=((retrieveOnlySerialized && isSerFile) || (!retrieveOnlySerialized && fileName.indexOf(HASH)<0));
501          if (!accept) return false;
502          if (xpdlId==null) {
503             return true;
504          } else {
505             accept=fileName.startsWith(xpdlId);
506             if (xpdlVersion==null) {
507                return accept;
508             } else {
509                String JavaDoc endsWith=xpdlVersion;
510                if (hasExtension) {
511                   endsWith+=".xpdl";
512                }
513                return accept && fileName.endsWith(endsWith);
514             }
515          }
516       }
517
518    }
519
520 }
521
522 class ExternalReferences extends HashMap implements Serializable {
523
524    public synchronized void addExtRef (String JavaDoc refTo,String JavaDoc refFromId,String JavaDoc refFromVersion,int refToNumber) {
525       if (!containsKey(refTo)) {
526          put(refTo,new HashSet());
527       }
528       Set s=(Set)get(refTo);
529       addToSet(s,new RefFrom(refFromId,refFromVersion,refToNumber));
530    }
531
532    public synchronized void remExtRef (String JavaDoc refTo,String JavaDoc refFromId,String JavaDoc refFromVersion) {
533       Set s=(Set)get(refTo);
534       if (s!=null) {
535          removeFromSet(s,new RefFrom(refFromId,refFromVersion));
536       }
537    }
538
539    public List getExtRefIds (String JavaDoc refTo) {
540       Set ret=new HashSet();
541       Set s=(Set)get(refTo);
542       if (s!=null) {
543          Iterator it=s.iterator();
544          while (it.hasNext()) {
545             RefFrom rf=(RefFrom)it.next();
546             ret.add(rf.getRefFromId());
547          }
548       }
549       return new ArrayList(ret);
550    }
551
552    public List getExtRefVersions (String JavaDoc refTo,String JavaDoc refFromId) {
553       List ret=new ArrayList();
554       Set s=(Set)get(refTo);
555       if (s!=null) {
556          Iterator it=s.iterator();
557          while (it.hasNext()) {
558             RefFrom rf=(RefFrom)it.next();
559             if (rf.getRefFromId().equals(refFromId)) {
560                ret.add(rf.getRefFromVersion());
561             }
562          }
563       }
564       return ret;
565    }
566
567    public List getReferrencedIds (String JavaDoc refFromId,String JavaDoc refFromVersion) {
568       List ret=new ArrayList();
569       Iterator it=entrySet().iterator();
570       while (it.hasNext()) {
571          Map.Entry me=(Map.Entry)it.next();
572          String JavaDoc refTo=(String JavaDoc)me.getKey();
573          Set s=(Set)me.getValue();
574          Iterator itS=s.iterator();
575          while (itS.hasNext()) {
576             RefFrom rf=(RefFrom)itS.next();
577             
578             Map temp=new HashMap();
579             if (rf.getRefFromId().equals(refFromId) && rf.getRefFromVersion().equals(refFromVersion)) {
580                temp.put(new Integer JavaDoc(rf.getRefNo()),refTo);
581             }
582             List tmp=new ArrayList(temp.keySet());
583             // sort by number
584
Collections.sort(tmp);
585             for (int i=0; i<tmp.size(); i++) {
586                ret.add(temp.get(tmp.get(i)));
587             }
588
589          }
590       }
591       return ret;
592    }
593
594    public synchronized void removeReferrencedIds (String JavaDoc refFromId,String JavaDoc refFromVersion) {
595       List ret=getReferrencedIds(refFromId,refFromVersion);
596       Iterator it=ret.iterator();
597       while (it.hasNext()) {
598          String JavaDoc refTo=(String JavaDoc)it.next();
599          Set s=(Set)get(refTo);
600          removeFromSet(s,new RefFrom(refFromId,refFromVersion));
601          if (s.size()==0) {
602             remove(refTo);
603          }
604       }
605    }
606
607    private void addToSet (Set s,RefFrom rf) {
608       Iterator it=s.iterator();
609       boolean contains=false;
610       while (it.hasNext()) {
611          RefFrom rfs=(RefFrom)it.next();
612          if (rfs.equals(rf)) {
613             contains=true;
614             break;
615          }
616       }
617       if (!contains) {
618          s.add(rf);
619       }
620    }
621
622    // we have to do this because set does not properly remove RefFrom (probably
623
// some Java bug or something)
624
private void removeFromSet (Set s,RefFrom rf) {
625       Iterator it=s.iterator();
626       RefFrom toRem=null;
627       while (it.hasNext()) {
628          RefFrom rfs=(RefFrom)it.next();
629          if (rfs.equals(rf)) {
630             toRem=rfs;
631             break;
632          }
633       }
634       s.remove(toRem);
635    }
636 }
637
638 class RefFrom implements Serializable {
639    private String JavaDoc refFromId;
640    private String JavaDoc refFromVersion;
641    private int refNo=-1;
642
643    public RefFrom (String JavaDoc refFromId,String JavaDoc refFromVersion,int refNo) {
644       this.refFromId=refFromId;
645       this.refFromVersion=refFromVersion;
646       this.refNo=refNo;
647    }
648
649    public RefFrom (String JavaDoc refFromId,String JavaDoc refFromVersion) {
650       this.refFromId=refFromId;
651       this.refFromVersion=refFromVersion;
652    }
653
654    public String JavaDoc getRefFromId () {
655       return refFromId;
656    }
657
658    public String JavaDoc getRefFromVersion () {
659       return refFromVersion;
660    }
661
662    public int getRefNo () {
663       return refNo;
664    }
665    
666    public boolean equals (Object JavaDoc obj) {
667       boolean eq=false;
668       if (obj instanceof RefFrom) {
669          RefFrom refFrom=(RefFrom)obj;
670          eq=(refFrom.refFromId.equals(this.refFromId) &&
671                     refFrom.refFromVersion.equals(this.refFromVersion));
672       }
673       return eq;
674    }
675
676    public String JavaDoc toString () {
677       return "[referringId="+refFromId+",referringVersion="+refFromVersion+", refNo="+refNo+"]";
678    }
679
680 }
681
682 class NextVersions extends HashMap implements Serializable {
683    private static final String JavaDoc INITIAL_VERSION = "1";
684
685
686    public synchronized String JavaDoc getNextVersion (String JavaDoc xpdlId) {
687       if (containsKey(xpdlId)) {
688          return (String JavaDoc)get(xpdlId);
689       }
690       return INITIAL_VERSION;
691    }
692
693    public synchronized String JavaDoc updateNextVersion (String JavaDoc xpdlId) throws Exception JavaDoc {
694       String JavaDoc curVersion=INITIAL_VERSION;
695       String JavaDoc nextVersion=INITIAL_VERSION;
696
697       if (containsKey(xpdlId)) {
698          curVersion=(String JavaDoc)get(xpdlId);
699       }
700
701       int nver=Integer.parseInt(curVersion)+1;
702       nextVersion=String.valueOf(nver);
703
704       put(xpdlId,nextVersion);
705
706       return curVersion;
707    }
708 }
709
Popular Tags