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 22 public class FileSystemRepositoryPersistenceManager implements RepositoryPersistenceManager { 23 24 private CallbackUtilities cus; 25 private String XPDL_REPOSITORY; 27 28 private String XPDL_HISTORY_REPOSITORY; 30 31 private final String EXT_REF_FNAME="#ext_references#"; 32 private final String NEXT_IDS_FNAME="#next_ids#"; 33 private final String SER_PKG ="#ser_pkg#"; 34 private final String HASH = "#"; 35 36 private String EXT_REFERENCES_FILE_NAME; 37 private String 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 xr=cus.getProperty("FileSystemRepositoryPersistenceManager.XPDL_REPOSITORY"); 45 String 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 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 ex) { 76 throw new RootException(ex); 77 } 78 } else { 79 nextVersions=new NextVersions(); 80 } 81 } 82 83 public void uploadXPDL (RepositoryTransaction t,String xpdlId,byte[] xpdl,byte[] serializedPkg,long xpdlClassVer) throws RepositoryException { 84 try { 85 String 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 fos.flush(); 91 fos.close(); 92 fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+SER_PKG+xpdlClassVer+HASH+"-"+newVersion); 93 fos.write(serializedPkg); 94 fos.flush(); 96 fos.close(); 97 } catch (Exception 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 xpdlId,String xpdlVersion,byte[] xpdl,byte[] serializedPkg,long xpdlClassVer) throws RepositoryException { 104 try { 105 getXPDLFile(xpdlId,xpdlVersion); deleteXPDL(t, xpdlId, xpdlVersion); 107 FileOutputStream fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+xpdlVersion); 108 fos.write(xpdl); 109 fos.flush(); 111 fos.close(); 112 fos = new FileOutputStream(XPDL_REPOSITORY+File.separator+xpdlId+"-"+SER_PKG+xpdlClassVer+HASH+"-"+xpdlVersion); 113 fos.write(serializedPkg); 114 fos.flush(); 116 fos.close(); 117 } catch (Exception 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 xpdlId,String xpdlVersion) throws RepositoryException { 124 try { 125 if (!getXPDLFile(xpdlId,xpdlVersion).delete()) { 126 throw new Exception ("File "+xpdlId+"-"+xpdlVersion+" is not deleted from repository"); 127 } 128 if (!getSerializedXPDLFile(xpdlId,xpdlVersion).delete()) { 129 throw new Exception ("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 ex) { 134 throw new RepositoryException(ex); 135 } 136 } 137 138 public void moveToHistory (RepositoryTransaction t,String xpdlId,String xpdlVersion) throws RepositoryException { 139 try { 140 File f1=getXPDLFile(xpdlId,xpdlVersion); 141 File f2=getSerializedXPDLFile(xpdlId,xpdlVersion); 142 143 String historyFolder=XPDL_HISTORY_REPOSITORY+File.separator+xpdlId; 144 File fhf=new File(historyFolder); 145 if (!fhf.exists()) { 146 fhf.mkdir(); 147 } 148 149 String historyFilename1=historyFolder+File.separator+f1.getName()+".xpdl"; 150 String historyFilename2=historyFolder+File.separator+f2.getName(); 151 MiscUtilities.copyFile(f1.getCanonicalPath(),historyFilename1); 152 MiscUtilities.copyFile(f2.getCanonicalPath(),historyFilename2); 153 154 if (!f1.delete() || !f2.delete()) { 156 throw new Exception ("File "+xpdlId+"-"+xpdlVersion+" is not deleted from repository"); 157 } 158 extRefs.removeReferrencedIds(xpdlId,xpdlVersion); 159 writeFile(extRefs,EXT_REFERENCES_FILE_NAME); 160 } catch (Exception ex) { 161 throw new RepositoryException(ex); 162 } 163 164 } 165 166 public void deleteFromHistory (RepositoryTransaction t,String xpdlId,String 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 ("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 ("Some file is not deleted from repository"); 197 } 198 } 199 } catch (Exception ex) { 200 throw new RepositoryException(ex); 201 } 202 } 203 204 public String getCurrentVersion (RepositoryTransaction t,String xpdlId) throws RepositoryException { 205 try { 206 return getFileVersion(getXPDLFile(xpdlId,null)); 207 } catch (Exception ex) { 208 throw new RepositoryException(ex); 209 } 210 } 211 212 public String getNextVersion (RepositoryTransaction t,String xpdlId) throws RepositoryException { 213 try { 214 return nextVersions.getNextVersion(xpdlId); 215 } catch (Exception ex) { 216 throw new RepositoryException(ex); 217 } 218 } 219 220 public long getSerializedXPDLObjectVersion (RepositoryTransaction t,String xpdlId,String xpdlVersion) throws RepositoryException { 221 try { 222 return Long.parseLong(getSerializedFileVersion(getSerializedXPDLFile(xpdlId,xpdlVersion))); 223 } catch (Exception ex) { 224 throw new RepositoryException(ex); 225 } 226 } 227 228 public byte[] getXPDL (RepositoryTransaction t,String xpdlId) throws RepositoryException { 229 try { 230 return fileToByteArray(getXPDLFile(xpdlId,null)); 231 } catch (Exception ex) { 232 throw new RepositoryException(ex); 233 } 234 } 235 236 public byte[] getSerializedXPDLObject (RepositoryTransaction t,String xpdlId) throws RepositoryException { 237 try { 238 return fileToByteArray(getSerializedXPDLFile(xpdlId,null)); 239 } catch (Exception ex) { 240 throw new RepositoryException(ex); 241 } 242 } 243 244 public byte[] getXPDL (RepositoryTransaction t,String xpdlId,String xpdlVersion) throws RepositoryException { 245 try { 246 return fileToByteArray(getXPDLFile(xpdlId,xpdlVersion)); 247 } catch (Exception ex) { 248 throw new RepositoryException(ex); 249 } 250 } 251 252 public byte[] getSerializedXPDLObject (RepositoryTransaction t,String xpdlId,String xpdlVersion) throws RepositoryException { 253 try { 254 return fileToByteArray(getSerializedXPDLFile(xpdlId,xpdlVersion)); 255 } catch (Exception ex) { 256 throw new RepositoryException(ex); 257 } 258 } 259 260 public List getXPDLVersions (RepositoryTransaction t,String 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 ex) { 273 throw new RepositoryException(ex); 274 } 275 } 276 return xpdlVersions; 277 } 278 279 public boolean doesXPDLExist (RepositoryTransaction t,String xpdlId) throws RepositoryException { 280 try { 281 getXPDLFile(xpdlId,null); 282 return true; 283 } catch (Exception ex) { 284 return false; 285 } 286 } 287 288 public boolean doesXPDLExist (RepositoryTransaction t,String xpdlId,String xpdlVersion) throws RepositoryException { 289 try { 290 getXPDLFile(xpdlId,xpdlVersion); 291 return true; 292 } catch (Exception 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 n=f.getName(); 304 int li=n.lastIndexOf("-"); 305 String 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 referredXPDLId, 314 String referringXPDLId, 315 String 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 ex) { 321 extRefs.remExtRef(referredXPDLId,referringXPDLId,referringXPDLVersion); 322 throw new RepositoryException(ex); 323 } 324 } 325 326 public List getReferringXPDLIds (RepositoryTransaction t,String referredXPDLId) throws RepositoryException { 327 return extRefs.getExtRefIds(referredXPDLId); 328 } 329 330 public List getReferringXPDLVersions (RepositoryTransaction t,String referredXPDLId,String refferingXPDLId) throws RepositoryException { 331 return extRefs.getExtRefVersions(referredXPDLId,refferingXPDLId); 332 } 333 334 public List getReferredXPDLIds (RepositoryTransaction t,String refferingXPDLId,String 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 obj,String fName) throws Exception { 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 readFile (String fName) throws Exception { 352 InputStream fis = new FileInputStream(fName); 353 ObjectInputStream oin = new ObjectInputStream(fis); 354 Object obj=oin.readObject(); 355 oin.close(); 356 return obj; 357 } 358 359 366 private String getRepositoryFullPath (String path) { 367 String rdPath=cus.getProperty(SharkConstants.ROOT_DIRECTORY_PATH_PROP); 368 File f=new File(path); 371 372 if (!f.isAbsolute()) { 373 f=new File(XMLUtil.createPath(rdPath,path)); 374 } 376 377 if (!f.exists()) { 378 if (!f.mkdir()) { 379 return path; 380 } 381 } 382 383 try { 384 return f.getCanonicalPath(); 385 } catch (Exception ex) { 386 return f.getAbsolutePath(); 387 } 388 } 389 390 393 private byte[] fileToByteArray (File xpdlFile) throws Exception { 394 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 int nextB, i=0; 404 while ((nextB=fis.read())!=-1) { 405 utf8Bytes[i++]=(byte)nextB; 406 } 407 } 408 } 409 catch (Throwable ex) { 410 cus.error("FileSystemRepositoryPersistenceManager -> Problems while getting XPDL file "+xpdlFile); 411 throw new Exception (); 412 } 413 } 414 return utf8Bytes; 415 } 416 417 private File getXPDLFile (String xpdlId,String xpdlVersion) throws Exception { 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 ("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the repository"); 424 } 425 426 private File getSerializedXPDLFile (String xpdlId,String xpdlVersion) throws Exception { 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 ("There is no xpdl with Id="+xpdlId+", and version "+xpdlVersion+" in the repository"); 433 } 434 435 private File getLastVersionXPDLFile (List xpdlFiles) throws Exception { 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 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 ("Something is wrong in XPDL repository - can't determine file version"); 450 451 return lastFileVersion; 452 } 453 454 private String getFileVersion (File f) throws RootException { 455 String n=f.getName(); 456 int li=n.lastIndexOf("-"); 457 String fv=n.substring(li+1); 458 return fv; 459 } 460 461 private String getSerializedFileVersion (File f) throws RootException { 462 String n=f.getName(); 463 int li1=n.indexOf(SER_PKG); 464 int li2=n.lastIndexOf(HASH); 465 String fv=n.substring(li1+SER_PKG.length(),li2); 466 return fv; 467 } 468 469 private List getXPDLFiles (String 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 xpdlId; 483 private String xpdlVersion; 484 private boolean hasExtension; 485 private boolean retrieveOnlySerialized; 486 487 public XPDLIdFilter (String xpdlId,String 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 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 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 refTo,String refFromId,String 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 refTo,String refFromId,String 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 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 refTo,String 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 refFromId,String 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 refTo=(String )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 (rf.getRefNo()),refTo); 581 } 582 List tmp=new ArrayList(temp.keySet()); 583 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 refFromId,String refFromVersion) { 595 List ret=getReferrencedIds(refFromId,refFromVersion); 596 Iterator it=ret.iterator(); 597 while (it.hasNext()) { 598 String refTo=(String )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 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 refFromId; 640 private String refFromVersion; 641 private int refNo=-1; 642 643 public RefFrom (String refFromId,String refFromVersion,int refNo) { 644 this.refFromId=refFromId; 645 this.refFromVersion=refFromVersion; 646 this.refNo=refNo; 647 } 648 649 public RefFrom (String refFromId,String refFromVersion) { 650 this.refFromId=refFromId; 651 this.refFromVersion=refFromVersion; 652 } 653 654 public String getRefFromId () { 655 return refFromId; 656 } 657 658 public String getRefFromVersion () { 659 return refFromVersion; 660 } 661 662 public int getRefNo () { 663 return refNo; 664 } 665 666 public boolean equals (Object 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 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 INITIAL_VERSION = "1"; 684 685 686 public synchronized String getNextVersion (String xpdlId) { 687 if (containsKey(xpdlId)) { 688 return (String )get(xpdlId); 689 } 690 return INITIAL_VERSION; 691 } 692 693 public synchronized String updateNextVersion (String xpdlId) throws Exception { 694 String curVersion=INITIAL_VERSION; 695 String nextVersion=INITIAL_VERSION; 696 697 if (containsKey(xpdlId)) { 698 curVersion=(String )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 |