1 19 package org.openharmonise.localversioningfilesystem; 20 21 import java.io.*; 22 import java.net.*; 23 import java.util.*; 24 25 import javax.xml.parsers.*; 26 27 import org.openharmonise.commons.xml.*; 28 import org.openharmonise.commons.xml.namespace.*; 29 import org.openharmonise.localversioningfilesystem.value.*; 30 import org.openharmonise.vfs.*; 31 import org.openharmonise.vfs.authentication.*; 32 import org.openharmonise.vfs.metadata.*; 33 import org.openharmonise.vfs.metadata.range.*; 34 import org.openharmonise.vfs.search.*; 35 import org.openharmonise.vfs.status.*; 36 import org.w3c.dom.*; 37 import org.xml.sax.*; 38 39 40 46 public class LocalVersioningFileSystem extends AbstractVersioningVFS { 47 48 private VirtualFileCache m_cache = null; 49 50 private VirtualFileSystemView m_vfView = null; 51 52 56 public LocalVersioningFileSystem(URI uri) { 57 super(uri); 58 this.setup(); 59 } 60 61 66 public LocalVersioningFileSystem(URI uri, AuthInfo authInfo) { 67 super(uri, authInfo); 68 this.setup(); 69 } 70 71 76 public LocalVersioningFileSystem( 77 URI uri, 78 AbstractAuthenticationStore authStore) { 79 super(uri, authStore); 80 this.setup(); 81 } 82 83 private void setup() { 84 this.m_sInitialPath = 85 this.m_sInitialPath.substring(1, this.m_sInitialPath.length()); 86 87 this.m_sInitialPath = this.m_sInitialPath.replace('/', '\\'); 88 this.m_cache = new VirtualFileCache(); 89 } 90 91 94 public List getOptions() { 95 return null; 96 } 97 98 101 public ResourceStatusWrapper getVirtualFile(String sFullPath) { 102 VirtualFile vfFile = null; 103 104 String sRealPath = this.getInitialPath() + sFullPath; 105 sRealPath = sRealPath.replace('/', File.separatorChar); 106 107 if (sFullPath != null && this.m_cache.hasFile(sFullPath)) { 108 vfFile = this.m_cache.getFile(sFullPath); 109 } else if (sFullPath != null) { 110 vfFile = new VersionedVirtualFile(sFullPath); 111 vfFile.setVFS(this); 112 File fRealFile = new File(sRealPath); 113 this.populateVirtualFile(fRealFile, vfFile); 114 this.m_cache.addFile(vfFile); 115 } 116 117 if (vfFile == null) { 118 this.fireErrorEvent( 119 "FileNotFound", 120 "The file " + sFullPath + " cannot be found."); 121 } 122 123 return new ResourceStatusWrapper(vfFile, new VFSStatus()); 124 } 125 126 private void populateVirtualFile(File fRealFile, VirtualFile vfFile) { 127 vfFile.setIsDirectory(fRealFile.isDirectory()); 128 if(vfFile.isDirectory()) { 129 this.setOrderableDirectory(vfFile, true); 130 } 131 132 File[] children = fRealFile.listFiles(); 133 if (children != null) { 134 for (int i = 0; i < children.length; i++) { 135 if (!children[i].getName().equals("CMHistory") 136 && !children[i].getName().equals("CMPending") 137 && !children[i].getName().equals("CMMetadata") 138 && !children[i].getName().equals("CMOrder.xml")) { 139 vfFile.addChild( 140 vfFile.getFullPath() + "/" + children[i].getName()); 141 } 142 } 143 } 144 this.setFileChildrenPopulated(vfFile, true); 145 if(vfFile.isDirectory()) { 146 this.orderChildren(vfFile); 147 } 148 149 if(!vfFile.getFilePath().equals("")) { 150 VirtualFile vfParent = this.getVirtualFile(vfFile.getFilePath()).getResource(); 151 if(vfParent!=null && vfParent.getFileName().equals("CMHistory") ) { 152 this.setFileState(vfFile, VirtualFile.STATE_HISTORICAL); 153 } 154 } 155 156 File fPendingDir = 157 new File( 158 fRealFile.getParentFile().getAbsolutePath() 159 + File.separatorChar 160 + "CMPending"); 161 if (fPendingDir.exists() && fPendingDir.isDirectory()) { 162 children = fPendingDir.listFiles(); 163 if (children != null) { 164 for (int i = 0; i < children.length; i++) { 165 if (children[i] 166 .getName() 167 .startsWith("p-" + fRealFile.getName())) { 168 VersionedVirtualFile vfPending = 169 (VersionedVirtualFile) this.getVirtualFile( 170 vfFile.getFilePath() 171 + "/CMPending/" 172 + children[i].getName()).getResource(); 173 this.setFileState(vfPending, VirtualFile.STATE_PENDING); 174 this.setFileLiveVersionPath((VersionedVirtualFile) vfPending, 175 vfFile.getFullPath()); 176 this.setFilePendingVersionPath( 177 (VersionedVirtualFile) vfFile, 178 vfPending.getFullPath()); 179 } else if ( 180 children[i].getName().startsWith( 181 "n-" + fRealFile.getName())) { 182 this.setFileState(vfFile, VirtualFile.STATE_PENDING); 183 } 184 } 185 } 186 } else if (!fPendingDir.exists()) { 187 fPendingDir.mkdir(); 188 } 189 } 190 191 194 public ResourceStatusWrapper addVirtualFile(String sPath, VirtualFile content) { 195 VirtualFile vfFile = null; 196 197 String sFullPath = sPath + "/" + content.getFileName(); 198 199 String sRealPath = 200 this.getInitialPath() + sPath + "/" + content.getFileName(); 201 sRealPath = sRealPath.replace('/', File.separatorChar); 202 203 if (this.m_cache.hasFile(sFullPath)) { 204 } else { 206 File fRealFile = new File(sRealPath); 207 try { 208 FileOutputStream fos = new FileOutputStream(fRealFile); 209 fos.write(content.getContent()); 210 } catch (FileNotFoundException e) { 211 this.fireErrorEvent( 212 "FileNotAdded", 213 "The file " 214 + sFullPath 215 + " cannot be added here, this might be because a file already exists here with the same name."); 216 e.printStackTrace(); 217 } catch (IOException e) { 218 this.fireErrorEvent( 219 "FileNotAdded", 220 "The file " 221 + sFullPath 222 + " cannot be added here, this might be because a file already exists here with the same name."); 223 e.printStackTrace(); 224 } 225 } 226 227 if (vfFile == null) { 228 this.fireErrorEvent( 229 "FileNotAdded", 230 "The file " 231 + sFullPath 232 + " cannot be added here, this might be because a file already exists here with the same name."); 233 } 234 235 return new ResourceStatusWrapper(vfFile, new VFSStatus()); 236 } 237 238 241 public StatusData moveVirtualFile(String sFromFullPath, String sToFullPath) { 242 VFSStatus retnStatus = new VFSStatus(); 243 244 boolean bError = false; 245 246 String sRealFromPath = this.getInitialPath() + sFromFullPath; 247 sRealFromPath = sRealFromPath.replace('/', File.separatorChar); 248 String sRealToPath = this.getInitialPath() + sToFullPath; 249 sRealToPath = sRealToPath.replace('/', File.separatorChar); 250 251 VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource(); 252 if (!vfFromFile.isDirectory()) { 253 254 File from = new File(sRealFromPath); 255 File to = new File(sRealToPath); 256 257 bError = this.moveFile(from, to, true); 258 if(!bError) { 259 this.m_cache.removeFile(sFromFullPath); 260 } else { 261 this.fireErrorEvent( 262 "FileNotMoved", 263 "The file " 264 + vfFromFile.getFullPath() 265 + " cannot be copied here, this might be because a file already exists here with the same name."); 266 } 267 } 268 269 if(bError) { 270 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 271 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 272 } 273 274 return retnStatus; 275 } 276 277 280 public StatusData copyVirtualFile(String sFromFullPath, String sToFullPath) { 281 VFSStatus retnStatus = new VFSStatus(); 282 283 boolean bError = false; 284 285 String sRealFromPath = this.getInitialPath() + sFromFullPath; 286 sRealFromPath = sRealFromPath.replace('/', File.separatorChar); 287 String sRealToPath = this.getInitialPath() + sToFullPath; 288 sRealToPath = sRealToPath.replace('/', File.separatorChar); 289 290 VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource(); 291 if (!vfFromFile.isDirectory()) { 292 File from = new File(sRealFromPath); 293 File to = new File(sRealToPath); 294 295 bError = this.moveFile(from, to, false); 296 if(!bError) { 297 this.m_cache.removeFile(sFromFullPath); 298 this.m_cache.removeFile(this.getParentPath(sFromFullPath)); 299 this.m_cache.removeFile(this.getParentPath(sToFullPath)); 300 } else { 301 this.fireErrorEvent( 302 "FileNotCopied", 303 "The file " 304 + vfFromFile.getFullPath() 305 + " cannot be copied here, this might be because a file already exists here with the same name."); 306 } 307 } 308 309 if(bError) { 310 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 311 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 312 } 313 314 return retnStatus; 315 } 316 317 private File getMetadataDir(File file) { 318 File fDir = new File(file.getParentFile().getAbsolutePath() + File.separatorChar + "CMMetadata"); 319 if(!fDir.exists()) { 320 fDir.mkdir(); 321 } 322 return fDir; 323 } 324 325 private File getMetadataFile(File file) { 326 File fRetn = new File(this.getMetadataDir(file).getAbsolutePath() + File.separatorChar + file.getName()); 327 328 File fMetadataDir = this.getMetadataDir(file); 329 File[] children = fMetadataDir.listFiles(); 330 for (int i = 0; i < children.length; i++) { 331 File file2 = children[i]; 332 if(file2.getName().equals(file.getName())) { 333 fRetn=file2; 334 break; 335 } 336 } 337 return fRetn; 338 } 339 340 private File getRealFile(VirtualFile vfFile) { 341 String sRealPath = this.getInitialPath() + vfFile.getFullPath(); 342 sRealPath = sRealPath.replace('/', File.separatorChar); 343 344 File file = new File(sRealPath); 345 if(file.exists()) { 346 return file; 347 } else { 348 return null; 349 } 350 } 351 352 private File getOrderFile(File file) { 353 File fOrderFile = new File(file.getAbsolutePath() + File.separatorChar + "CMOrder.xml"); 354 355 if(!fOrderFile.exists()) { 356 try { 357 fOrderFile.createNewFile(); 358 Document xml = null; 359 try { 360 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 361 factory.setNamespaceAware(true); 362 xml = factory.newDocumentBuilder().newDocument(); 363 } catch (FactoryConfigurationError e) { 364 e.printStackTrace(); 365 } catch (ParserConfigurationException e) { 366 e.printStackTrace(); 367 } 368 369 Element rootEl = xml.createElementNS("http://www.simulacramedia.com/harmoniseclient/propinstances", "Order"); 370 xml.appendChild(rootEl); 371 XMLPrettyPrint printer = new XMLPrettyPrint(); 372 printer.setNamespaceAware(true); 373 printer.printNodeToFile(rootEl, fOrderFile); 374 } catch (IOException e1) { 375 e1.printStackTrace(); 376 } 377 378 } 379 380 return fOrderFile; 381 } 382 383 384 385 private void orderChildren(VirtualFile vfDir) { 386 File fDir = this.getRealFile(vfDir); 387 File fOrderFile = this.getOrderFile(fDir); 388 389 Document m_xml; 390 try { 391 m_xml = 392 DocumentBuilderFactory 393 .newInstance() 394 .newDocumentBuilder() 395 .parse( 396 new org.xml.sax.InputSource ( 397 new FileInputStream(fOrderFile))); 398 399 Element rootEl = m_xml.getDocumentElement(); 400 401 NodeList nlProps = rootEl.getChildNodes(); 402 403 ArrayList aOrderedPaths = new ArrayList(); 404 405 for (int i = 0; i < nlProps.getLength(); i++) { 406 Node node = nlProps.item(i); 407 if (node.getNodeType() != Node.ELEMENT_NODE) { 408 continue; 409 } else { 410 Element elChild = (Element)node; 411 String sPath = elChild.getFirstChild().getNodeValue(); 412 aOrderedPaths.add(sPath); 413 } 414 } 415 416 List children = vfDir.getChildren(); 417 if(children.containsAll(aOrderedPaths) && aOrderedPaths.containsAll(children)) { 418 this.clearVirtualFileChildren(vfDir); 419 for (Iterator iter = aOrderedPaths.iterator(); iter.hasNext();) { 420 String element = (String ) iter.next(); 421 vfDir.addChild(element); 422 } 423 } 424 } catch (FileNotFoundException e) { 425 e.printStackTrace(); 426 } catch (SAXException e) { 427 e.printStackTrace(); 428 } catch (IOException e) { 429 e.printStackTrace(); 430 } catch (ParserConfigurationException e) { 431 e.printStackTrace(); 432 } catch (FactoryConfigurationError e) { 433 e.printStackTrace(); 434 } 435 } 436 437 private File getHistoryDir(File file) { 438 File fDir = new File(file.getParentFile().getAbsolutePath() + File.separatorChar + "CMHistory"); 439 if(!fDir.exists()) { 440 fDir.mkdir(); 441 } 442 return fDir; 443 } 444 445 private List getHistoryFiles(File file) { 446 ArrayList aFiles = new ArrayList(); 447 File fHistoryDir = this.getHistoryDir(file); 448 File[] children = fHistoryDir.listFiles(); 449 for (int i = 0; i < children.length; i++) { 450 File file2 = children[i]; 451 if(file2.getName().startsWith(file.getName())) { 452 aFiles.add(file2); 453 } 454 } 455 456 return aFiles; 457 } 458 459 private File getPendingDir(File file) { 460 File fDir = new File(file.getParentFile().getAbsolutePath() + File.separatorChar + "CMPending"); 461 if(!fDir.exists()) { 462 fDir.mkdir(); 463 } 464 return fDir; 465 } 466 467 private File getPendingFile(File file) { 468 File fRetn = new File(this.getPendingDir(file).getAbsolutePath() + File.separatorChar + "p-" + file.getName()); 469 470 File fPendingDir = this.getPendingDir(file); 471 File[] children = fPendingDir.listFiles(); 472 for (int i = 0; i < children.length; i++) { 473 File file2 = children[i]; 474 if(file2.getName().equals("p-" + file.getName())) { 475 fRetn=file2; 476 break; 477 } 478 } 479 return fRetn; 480 } 481 482 private File getNewMarkerFile(File file) { 483 File fRetn = new File(this.getPendingDir(file).getAbsolutePath() + File.separatorChar + "n-" + file.getName()); 484 485 File fPendingDir = this.getPendingDir(file); 486 File[] children = fPendingDir.listFiles(); 487 for (int i = 0; i < children.length; i++) { 488 File file2 = children[i]; 489 if(file2.getName().equals("n-" + file.getName())) { 490 fRetn=file2; 491 break; 492 } 493 } 494 return fRetn; 495 } 496 497 private boolean moveFile(File from, File to, boolean bDelete) { 498 boolean bError = false; 499 500 bError = this.moveRealFile(from, to, bDelete); 501 502 File fMetadataFromDir = new File(from.getParentFile().getAbsolutePath() + File.separatorChar + "CMMetadata"); 503 File fMetadataToDir = new File(to.getParentFile().getAbsolutePath() + File.separatorChar + "CMMetadata"); 504 if(fMetadataFromDir.exists()) { 505 File fMetadataFromFile = new File(fMetadataFromDir.getAbsolutePath() + File.separatorChar + from.getName()); 506 if(fMetadataFromFile.exists()) { 507 if(!fMetadataToDir.exists()) { 508 fMetadataToDir.mkdir(); 509 } 510 File fMetadataToFile = new File(fMetadataToDir.getAbsolutePath() + File.separatorChar + from.getName()); 511 boolean bErrTemp = this.moveRealFile(fMetadataFromFile, fMetadataToFile, bDelete); 512 if(bErrTemp) { 513 bError=true; 514 } 515 } 516 } else { 517 fMetadataFromDir.mkdir(); 518 } 519 520 File fPendingFromDir = new File(from.getParentFile().getAbsolutePath() + File.separatorChar + "CMPending"); 521 File fPendingToDir = new File(to.getParentFile().getAbsolutePath() + File.separatorChar + "CMPending"); 522 if(fPendingFromDir.exists()) { 523 File fPendingFromFile = new File(fPendingFromDir.getAbsolutePath() + File.separatorChar + "p-"+from.getName()); 524 if(fPendingFromFile.exists()) { 525 if(!fPendingToDir.exists()) { 526 fPendingToDir.mkdir(); 527 } 528 File fPendingToFile = new File(fPendingToDir.getAbsolutePath() + File.separatorChar + "p-"+from.getName()); 529 boolean bErrTemp = this.moveFile(fPendingFromFile, fPendingToFile, bDelete); 530 if(bErrTemp) { 531 bError=true; 532 } 533 } 534 fPendingFromFile = new File(fPendingFromDir.getAbsolutePath() + File.separatorChar + "n-"+from.getName()); 535 if(fPendingFromFile.exists()) { 536 if(!fPendingToDir.exists()) { 537 fPendingToDir.mkdir(); 538 } 539 File fPendingToFile = new File(fPendingToDir.getAbsolutePath() + File.separatorChar + "n-"+from.getName()); 540 boolean bErrTemp = this.moveFile(fPendingFromFile, fPendingToFile, bDelete); 541 if(bErrTemp) { 542 bError=true; 543 } 544 } 545 } else { 546 fPendingFromDir.mkdir(); 547 } 548 549 File fHistoryFromDir = new File(from.getParentFile().getAbsolutePath() + File.separatorChar + "CMHistory"); 550 File fHistoryToDir = new File(to.getParentFile().getAbsolutePath() + File.separatorChar + "CMHistory"); 551 if(fHistoryFromDir.exists()) { 552 if(!fPendingToDir.exists()) { 553 fPendingToDir.mkdir(); 554 } 555 File[] children = fHistoryFromDir.listFiles(); 556 for (int i = 0; i < children.length; i++) { 557 if(children[i].getName().startsWith(from.getName())) { 558 File fHistoryToFile = new File(fHistoryToDir.getAbsolutePath() + File.separatorChar + children[i].getName()); 559 this.moveFile(children[i], fHistoryToFile, bDelete); 560 } 561 } 562 563 } else { 564 fHistoryFromDir.mkdir(); 565 } 566 567 return bError; 568 } 569 570 private boolean moveRealFile(File from, File to, boolean bDelete) { 571 boolean bError = false; 572 573 FileInputStream fi = null; 574 FileOutputStream fo = null; 575 576 try { 577 Long lLength = new Long (from.length()); 578 byte[] bytes = new byte[lLength.intValue()]; 579 fi = new FileInputStream(from); 580 fo = new FileOutputStream(to); 581 582 fi.read(bytes); 583 fo.write(bytes); 584 585 } catch (Exception e) { 586 e.printStackTrace(System.out); 587 bError = true; 588 } finally { 589 try { 590 fo.close(); 591 fi.close(); 592 } catch (IOException ioe) { 593 bError = true; 594 ioe.printStackTrace(System.out); 595 } 596 } 597 598 if (!bError && bDelete) { 599 from.delete(); 600 } 601 602 return bError; 603 } 604 605 608 public StatusData deleteVirtualFile(String sFullPath) { 609 VFSStatus retnStatus = new VFSStatus(); 610 611 boolean bError = false; 612 613 String sRealPath = this.getInitialPath() + sFullPath; 614 sRealPath = sRealPath.replace('/', File.separatorChar); 615 616 File fRealFile = new File(sRealPath); 617 618 bError = this.deleteRealFile(fRealFile); 619 620 if (bError) { 621 this.fireErrorEvent( 622 "FileNotDeleted", 623 "The file " + sFullPath + " cannot be deleted."); 624 } else { 625 this.m_cache.removeFile(sFullPath); 626 } 627 628 if(bError) { 629 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 630 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 631 } 632 633 return retnStatus; 634 } 635 636 private boolean deleteRealFile(File fRealFile) { 637 boolean bError = false; 638 639 File fMetadata = this.getMetadataFile(fRealFile); 640 if(fMetadata.exists()) { 641 if(!fMetadata.delete() ) { 642 bError=true; 643 } 644 } 645 646 if(!bError) { 647 File fPending = this.getPendingFile(fRealFile); 648 if(fPending.exists()) { 649 if(this.deleteRealFile(fPending) ) { 650 bError=true; 651 } 652 } 653 } 654 655 if(!bError) { 656 File fNew = this.getNewMarkerFile(fRealFile); 657 if(fNew.exists()) { 658 if(this.deleteRealFile(fNew) ) { 659 bError=true; 660 } 661 } 662 } 663 664 if(!bError) { 665 List aHistory = this.getHistoryFiles(fRealFile); 666 Iterator itor = aHistory.iterator(); 667 while (itor.hasNext()) { 668 File element = (File) itor.next(); 669 if(this.deleteRealFile(element)) { 670 bError=true; 671 break; 672 } 673 } 674 } 675 676 if(!bError) { 677 bError = !fRealFile.delete(); 678 } 679 680 return bError; 681 } 682 683 686 public StatusData lockVirtualFile(String sFullPath) { 687 VFSStatus retnStatus = new VFSStatus(); 688 689 String sRealPath = this.getInitialPath() + sFullPath; 690 sRealPath = sRealPath.replace('/', File.separatorChar); 691 this.fireErrorEvent( 692 "FileNotLocked", 693 "This server does not support the locking of files."); 694 return retnStatus; 695 } 696 697 700 public StatusData unlockVirtualFile(String sFullPath) { 701 VFSStatus retnStatus = new VFSStatus(); 702 703 String sRealPath = this.getInitialPath() + sFullPath; 704 sRealPath = sRealPath.replace('/', File.separatorChar); 705 this.fireErrorEvent( 706 "FileNotUnlocked", 707 "This server does not support the locking of files."); 708 return retnStatus; 709 } 710 711 714 public StatusData createVirtualDirectory(String sFullPath) { 715 VFSStatus retnStatus = new VFSStatus(); 716 717 boolean bError = false; 718 719 String sRealPath = this.getInitialPath() + sFullPath; 720 sRealPath = sRealPath.replace('/', File.separatorChar); 721 722 File fRealDir = new File(sRealPath); 723 724 bError = fRealDir.mkdirs(); 725 726 if (!bError) { 727 this.fireErrorEvent( 728 "DirectoryNotCreated", 729 "The directory " + sFullPath + " cannot be created."); 730 } 731 732 if(bError) { 733 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 734 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 735 } 736 737 return retnStatus; 738 } 739 740 743 public StatusData createShortcut(String sFullPath, String sToFullPath) { 744 VFSStatus retnStatus = new VFSStatus(); 745 746 String sRealPath = this.getInitialPath() + sFullPath; 747 sRealPath = sRealPath.replace('/', File.separatorChar); 748 String sRealToPath = this.getInitialPath() + sToFullPath; 749 sRealToPath = sRealToPath.replace('/', File.separatorChar); 750 this.fireErrorEvent( 751 "ShortcutNotCreted", 752 "This server does not support shortcuts."); 753 return retnStatus; 754 } 755 756 759 public ResourceListStatusWrapper search(Query query) { 760 return null; 761 } 762 763 766 public VirtualFileSystemView getVirtualFileSystemView() { 767 if (this.m_vfView == null) { 768 this.m_vfView = 769 (VirtualFileSystemView) new LocalVersioningFileSystemView(); 770 } 771 772 return this.m_vfView; 773 } 774 775 778 public byte[] getVirtualFileContent(String sFullPath) { 779 boolean bError = false; 780 781 String sRealPath = this.getInitialPath() + sFullPath; 782 sRealPath = sRealPath.replace('/', File.separatorChar); 783 784 byte[] content = null; 785 786 File from = new File(sRealPath); 787 FileInputStream fi = null; 788 789 try { 790 Long lLength = new Long (from.length()); 791 content = new byte[lLength.intValue()]; 792 fi = new FileInputStream(from); 793 794 fi.read(content); 795 this.setFileContentPopulated(this.m_cache.getFile(sFullPath), true); 796 797 } catch (Exception e) { 798 e.printStackTrace(System.out); 799 this.fireErrorEvent( 800 "ContentNotRetrieved", 801 "The content for " + sFullPath + " could not be retrieved."); 802 bError = true; 803 } finally { 804 try { 805 fi.close(); 806 } catch (IOException ioe) { 807 this.fireErrorEvent( 808 "ContentNotRetrieved", 809 "The content for " 810 + sFullPath 811 + " could not be retrieved."); 812 bError = true; 813 ioe.printStackTrace(System.out); 814 } 815 } 816 817 return content; 818 } 819 820 823 protected void fullyPopulateFileMetadata(VirtualFile vfFile) { 824 String sRealPath = this.getInitialPath() + vfFile.getFullPath(); 825 sRealPath = sRealPath.replace('/', File.separatorChar); 826 827 File fRealFile = new File(sRealPath); 828 829 File fMetadataDir = 830 new File( 831 fRealFile.getParentFile().getAbsolutePath() 832 + File.separatorChar 833 + "CMMetadata"); 834 835 if (fMetadataDir.exists() && Arrays 836 .asList(fMetadataDir.list()) 837 .contains(fRealFile.getName())) { 838 839 840 File fMetadata = 841 new File( 842 fRealFile.getParentFile().getAbsolutePath() 843 + File.separatorChar 844 + "CMMetadata" 845 + File.separatorChar 846 + fRealFile.getName()); 847 848 Document m_xml; 849 try { 850 m_xml = 851 DocumentBuilderFactory 852 .newInstance() 853 .newDocumentBuilder() 854 .parse( 855 new org.xml.sax.InputSource ( 856 new FileInputStream(fMetadata))); 857 858 Element rootEl = m_xml.getDocumentElement(); 859 860 NodeList nlProps = rootEl.getChildNodes(); 861 862 for (int i = 0; i < nlProps.getLength(); i++) { 863 Node node = nlProps.item(i); 864 if (node.getNodeType() != Node.ELEMENT_NODE) { 865 continue; 866 } else { 867 Element propEl = (Element) node; 868 String sPropName = propEl.getAttribute("name"); 869 String sPropNamespace = 870 propEl.getAttribute("namespace"); 871 872 PropertyInstance propInst = 873 new PropertyInstance(sPropNamespace, sPropName); 874 875 ArrayList vVals = new ArrayList(); 876 877 NodeList nlVals = propEl.getChildNodes(); 878 for (int j = 0; j < nlVals.getLength(); j++) { 879 Node node2 = nlVals.item(j); 880 if (node2.getNodeType() != Node.ELEMENT_NODE) { 881 continue; 882 } else { 883 Element valEl = (Element) node2; 884 vVals.add(valEl.getFirstChild().getNodeValue()); 885 } 886 } 887 888 propInst.setValues(vVals); 889 890 vfFile.addProperty(propInst); 891 } 892 } 893 } catch (FileNotFoundException e) { 894 e.printStackTrace(); 895 } catch (SAXException e) { 896 e.printStackTrace(); 897 } catch (IOException e) { 898 e.printStackTrace(); 899 } catch (ParserConfigurationException e) { 900 e.printStackTrace(); 901 } catch (FactoryConfigurationError e) { 902 e.printStackTrace(); 903 } 904 905 } else if(!fMetadataDir.exists()) { 906 fMetadataDir.mkdir(); 907 } 908 909 this.setFileMetadataPopulated(vfFile, true); 910 911 912 if(vfFile.getFullPath().indexOf("/Properties/")>-1 && vfFile.getProperty(NamespaceType.OHRM.getURI(), "domain")==null) { 913 914 } 915 } 916 917 920 protected void fullyPopulateFileChildren(VirtualFile vfFile) { 921 if (vfFile.isDirectory()) { 922 String sRealPath = this.getInitialPath() + vfFile.getFullPath(); 923 sRealPath = sRealPath.replace('/', File.separatorChar); 924 925 File fRealFile = new File(sRealPath); 926 927 File[] children = fRealFile.listFiles(); 928 if (children != null) { 929 for (int i = 0; i < children.length; i++) { 930 if (!children[i].getName().equals("CMHistory") 931 && !children[i].getName().equals("CMPending") 932 && !children[i].getName().equals("CMMetadata") 933 && !children[i].getName().equals("CMOrder.xml")) { 934 vfFile.addChild( 935 vfFile.getFullPath() + "/" + children[i].getName()); 936 } 937 } 938 } 939 this.setFileChildrenPopulated(vfFile, true); 940 if(vfFile.isDirectory()) { 941 this.orderChildren(vfFile); 942 } 943 } 944 } 945 946 private Document generatePropertyXML(VirtualFile vfFile) { 947 Document xml = null; 948 try { 949 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 950 factory.setNamespaceAware(true); 951 xml = factory.newDocumentBuilder().newDocument(); 952 } catch (FactoryConfigurationError e) { 953 e.printStackTrace(); 954 } catch (ParserConfigurationException e) { 955 e.printStackTrace(); 956 } 957 958 Element rootEl = xml.createElementNS("http://www.simulacramedia.com/harmoniseclient/propinstances", "Properties"); 959 xml.appendChild(rootEl); 960 961 Iterator itor = vfFile.getProperties().iterator(); 962 while (itor.hasNext()) { 963 PropertyInstance propInst = (PropertyInstance) itor.next(); 964 if(!propInst.getName().equals("datemodified") 965 && !propInst.getName().equals("filesize")) { 966 rootEl.appendChild( this.generatePropXML(xml, propInst) ); 967 } 968 } 969 970 return xml; 971 } 972 973 978 private Node generatePropXML(Document xml, PropertyInstance propInst) { 979 Element elProp = xml.createElementNS("http://www.simulacramedia.com/harmoniseclient/propinstances", "Prop"); 980 elProp.setAttribute("name", propInst.getName()); 981 elProp.setAttribute("namespace", "http://www.simulacramedia.com/XRM/DAV"); 982 983 Iterator itor = propInst.getValues().iterator(); 984 while (itor.hasNext()) { 985 String element = (String ) itor.next(); 986 987 Element elVal = xml.createElementNS("http://www.simulacramedia.com/harmoniseclient/propinstances", "Value"); 988 Text txt = xml.createTextNode(element); 989 elVal.appendChild(txt); 990 elProp.appendChild(elVal); 991 } 992 993 return elProp; 994 } 995 996 999 public StatusData synchroniseFile(VirtualFile vfFile) { 1000 VFSStatus retnStatus = new VFSStatus(); 1001 1002 boolean bError = false; 1003 1004 VersionedVirtualFile vfSyncFile = (VersionedVirtualFile) vfFile; 1005 1006 if (vfSyncFile.isChanged()) { 1007 if(vfSyncFile.getState().equals(VirtualFile.STATE_LIVE) && !vfSyncFile.hasPendingVersion()) { 1008 File file = this.getRealFile(vfSyncFile); 1009 File fPending = this.getPendingFile(file); 1010 if(this.moveFile(file, fPending, false)) { 1011 bError=true; 1012 } else { 1013 FileOutputStream fos = null; 1014 1015 try { 1016 fos = new FileOutputStream(fPending.getAbsolutePath()); 1017 fos.write(vfSyncFile.getContent()); 1018 } catch (FileNotFoundException e) { 1019 bError = true; 1020 e.printStackTrace(); 1021 } catch (IOException e) { 1022 bError = true; 1023 e.printStackTrace(); 1024 } finally { 1025 try { 1026 fos.close(); 1027 } catch (IOException e1) { 1028 e1.printStackTrace(); 1029 } 1030 } 1031 } 1032 if(!bError) { 1033 if(this.moveFile( this.getMetadataFile(file), this.getMetadataFile(fPending), false)) { 1034 bError=true; 1035 } else { 1036 Document xml = this.generatePropertyXML(vfSyncFile); 1037 XMLPrettyPrint printer = new XMLPrettyPrint(); 1038 printer.setNamespaceAware(true); 1039 printer.printNodeToFile(xml.getDocumentElement(), this.getMetadataFile(fPending)); 1040 } 1041 } 1042 1043 if(!bError) { 1044 this.m_cache.removeFile(vfSyncFile.getFullPath()); 1045 } 1046 } else if(vfSyncFile.getState().equals(VirtualFile.STATE_PENDING) 1047 && vfSyncFile.getLiveVersionPath()==null) { 1048 String sFullPath = vfSyncFile.getFullPath(); 1049 1050 String sRealPath = this.getInitialPath() + sFullPath; 1051 sRealPath = sRealPath.replace('/', File.separatorChar); 1052 1053 FileOutputStream fos = null; 1054 1055 try { 1056 fos = new FileOutputStream(sRealPath); 1057 fos.write(vfSyncFile.getContent()); 1058 } catch (FileNotFoundException e) { 1059 bError = true; 1060 e.printStackTrace(); 1061 } catch (IOException e) { 1062 bError = true; 1063 e.printStackTrace(); 1064 } finally { 1065 try { 1066 fos.close(); 1067 } catch (IOException e1) { 1068 e1.printStackTrace(); 1069 } 1070 } 1071 if(!bError) { 1072 Document xml = this.generatePropertyXML(vfSyncFile); 1073 XMLPrettyPrint printer = new XMLPrettyPrint(); 1074 printer.setNamespaceAware(true); 1075 printer.printNodeToFile(xml.getDocumentElement(), this.getMetadataFile( this.getRealFile(vfSyncFile) )); 1076 } 1077 1078 1079 if(!bError) { 1080 this.m_cache.removeFile(vfSyncFile.getFullPath()); 1081 } 1082 } else if( vfSyncFile.getState().equals(VirtualFile.STATE_PENDING) ) { 1083 String sFullPath = vfSyncFile.getFullPath(); 1084 1085 String sRealPath = this.getInitialPath() + sFullPath; 1086 sRealPath = sRealPath.replace('/', File.separatorChar); 1087 1088 FileOutputStream fos = null; 1089 1090 try { 1091 fos = new FileOutputStream(sRealPath); 1092 fos.write(vfSyncFile.getContent()); 1093 } catch (FileNotFoundException e) { 1094 bError = true; 1095 e.printStackTrace(); 1096 } catch (IOException e) { 1097 bError = true; 1098 e.printStackTrace(); 1099 } finally { 1100 try { 1101 fos.close(); 1102 } catch (IOException e1) { 1103 e1.printStackTrace(); 1104 } 1105 } 1106 if(!bError) { 1107 Document xml = this.generatePropertyXML(vfSyncFile); 1108 XMLPrettyPrint printer = new XMLPrettyPrint(); 1109 printer.setNamespaceAware(true); 1110 printer.printNodeToFile(xml.getDocumentElement(), this.getMetadataFile( this.getRealFile(vfSyncFile) )); 1111 } 1112 1113 if(!bError) { 1114 this.m_cache.removeFile(vfSyncFile.getFullPath()); 1115 this.m_cache.removeFile(vfSyncFile.getLiveVersionPath()); 1116 } 1117 } else { 1118 } 1119 } 1120 1121 if(bError) { 1122 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 1123 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 1124 } 1125 1126 return retnStatus; 1127 } 1128 1129 1132 public StatusData synchroniseAllFiles() { 1133 VFSStatus retnStatus = new VFSStatus(); 1134 1135 boolean bError = false; 1136 1137 Iterator itor = this.m_cache.getPaths().iterator(); 1138 while (itor.hasNext()) { 1139 String sFullPath = (String ) itor.next(); 1140 VirtualFile vfFile = this.m_cache.getFile(sFullPath); 1141 if (vfFile.isChanged()) { 1142 StatusData status = vfFile.sync(); 1143 if (status.isOK()) { 1144 bError = true; 1145 } 1146 } 1147 } 1148 1149 if(bError) { 1150 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 1151 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 1152 } 1153 1154 return retnStatus; 1155 } 1156 1157 1160 public boolean exists(String sFullPath) { 1161 boolean bExists = true; 1162 1163 String sRealPath = this.getInitialPath() + sFullPath; 1164 sRealPath = sRealPath.replace('/', File.separatorChar); 1165 1166 File file = new File(sRealPath); 1167 bExists = file.exists(); 1168 1169 return bExists; 1170 } 1171 1172 1175 protected void refreshChildren(VirtualFile vfFile) { 1176 Iterator itor = vfFile.getChildren().iterator(); 1177 while (itor.hasNext()) { 1178 String sChildPath = (String ) itor.next(); 1179 VirtualFile vfChild = this.getVirtualFile(sChildPath).getResource(); 1180 if (!vfChild.isChanged()) { 1181 this.m_cache.removeFile(sChildPath); 1182 } 1183 } 1184 } 1185 1186 1189 public StatusData checkoutVirtualFile(String sFullPath) { 1190 VFSStatus retnStatus = new VFSStatus(); 1191 return retnStatus; 1192 } 1193 1194 1197 public StatusData uncheckoutVirtualFile(String sFullPath) { 1198 VFSStatus retnStatus = new VFSStatus(); 1199 return retnStatus; 1200 } 1201 1202 1205 public StatusData checkinVirtualFile(String sFullPath) { 1206 VFSStatus retnStatus = new VFSStatus(); 1207 1208 boolean bError = false; 1209 VersionedVirtualFile vfFile = (VersionedVirtualFile) this.getVirtualFile(sFullPath).getResource(); 1210 1211 if(vfFile.getState().equals(VirtualFile.STATE_PENDING)) { 1212 if(vfFile.isChanged()) { 1213 vfFile.sync(); 1214 } 1215 vfFile = (VersionedVirtualFile) this.getVirtualFile(sFullPath).getResource(); 1216 if(vfFile.getLiveVersionPath()==null) { 1217 File fNewMarker = this.getNewMarkerFile( this.getRealFile(vfFile) ); 1218 if(!fNewMarker.delete()) { 1219 bError=true; 1220 } 1221 } else { 1222 VirtualFile vfParent = this.getVirtualFile(vfFile.getLiveVersionPath()).getResource(); 1223 File fRealFile = this.getRealFile(vfFile); 1224 File fParent = this.getRealFile(vfParent); 1225 File fHistorical = this.getNextHistoricalFile(fParent); 1226 1227 if(!this.moveRealFile(this.getMetadataFile(fParent), this.getMetadataFile(fHistorical), false)) { 1228 if(!this.moveRealFile(fParent, fHistorical, false)) { 1229 if(!this.moveRealFile(this.getMetadataFile(fRealFile), this.getMetadataFile(fParent), true)) { 1230 if(this.moveRealFile(fRealFile, fParent, true)) { 1231 bError=true; 1232 } 1233 } else { 1234 bError=true; 1235 } 1236 } else { 1237 bError=true; 1238 } 1239 } else { 1240 bError=true; 1241 } 1242 1243 this.m_cache.removeFile(sFullPath); 1244 this.m_cache.removeFile(vfParent.getFullPath()); 1245 1246 } 1247 } else { 1248 bError=false; 1249 } 1250 1251 if(bError) { 1252 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 1253 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 1254 } 1255 1256 return retnStatus; 1257 } 1258 1259 private File getNextHistoricalFile(File file) { 1260 File fRetn = null; 1261 1262 int nVersion = 0; 1263 Iterator iter = this.getHistoryFiles(file).iterator(); 1264 while (iter.hasNext()) { 1265 File element = (File) iter.next(); 1266 String sName = element.getName(); 1267 int nVerTemp = Integer.parseInt( sName.substring(sName.length()-3) ); 1268 if(nVerTemp>nVersion) { 1269 nVersion = nVerTemp; 1270 } 1271 } 1272 nVersion++; 1273 1274 String sVerExt = "00" + nVersion; 1275 if(nVersion>9 && nVersion<100) { 1276 sVerExt = "0" + nVersion; 1277 } else if(nVersion>99) { 1278 sVerExt = "" + nVersion; 1279 } 1280 1281 fRetn = new File( this.getHistoryDir(file).getAbsolutePath() + File.separatorChar + file.getName() + ".v" + sVerExt ); 1282 1283 return fRetn; 1284 } 1285 1286 1289 public StatusData tagVirtualFile(String sFullPath, String sTag) { 1290 VFSStatus retnStatus = new VFSStatus(); 1291 return retnStatus; 1292 } 1293 1294 1297 public StatusData reactivateVersion(String sFullPath) { 1298 VFSStatus retnStatus = new VFSStatus(); 1299 1300 boolean bError = false; 1301 VersionedVirtualFile vfFile = (VersionedVirtualFile) this.getVirtualFile(sFullPath).getResource(); 1302 1303 if(vfFile.getState().equals(VirtualFile.STATE_HISTORICAL)) { 1304 if(vfFile.getLiveVersionPath()==null || ((VersionedVirtualFile)this.getVirtualFile(vfFile.getLiveVersionPath()).getResource()).hasPendingVersion() ) { 1305 bError=true; 1306 } else { 1307 File fRealFile = this.getRealFile(vfFile); 1308 File fLiveFile = this.getRealFile(this.getVirtualFile(vfFile.getLiveVersionPath()).getResource()); 1309 1310 File fNewPendingFile = this.getPendingFile(fLiveFile); 1311 1312 if(!this.moveRealFile(this.getMetadataFile(fRealFile), this.getMetadataFile(fNewPendingFile), false)) { 1313 if(this.moveRealFile(fRealFile, fNewPendingFile, false)) { 1314 bError=true; 1315 } 1316 } else { 1317 bError=true; 1318 } 1319 1320 this.m_cache.removeFile(vfFile.getLiveVersionPath()); 1321 1322 } 1323 } else { 1324 bError=false; 1325 } 1326 1327 if(bError) { 1328 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 1329 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 1330 } 1331 1332 return retnStatus; 1333 } 1334 1335 1338 protected void fullyPopulateFileHistory(VersionedVirtualFile vfFile) { 1339 1340 String sRealPath = this.getInitialPath() + vfFile.getFullPath(); 1341 sRealPath = sRealPath.replace('/', File.separatorChar); 1342 File fRealFile = new File(sRealPath); 1343 1344 File fHistoryDir = 1345 new File( 1346 fRealFile.getParentFile().getAbsolutePath() 1347 + File.separatorChar 1348 + "CMHistory"); 1349 if (fHistoryDir.exists() && fHistoryDir.isDirectory()) { 1350 File[] children = fHistoryDir.listFiles(); 1351 if (children != null) { 1352 for (int i = 0; i < children.length; i++) { 1353 if (children[i] 1354 .getName() 1355 .startsWith(fRealFile.getName())) { 1356 VirtualFile vfHistorical = 1357 this.getVirtualFile( 1358 vfFile.getFilePath() 1359 + "/CMHistory/" 1360 + children[i].getName()).getResource(); 1361 this.setFileState( 1362 vfHistorical, 1363 VirtualFile.STATE_HISTORICAL); 1364 this.setFileLiveVersionPath((VersionedVirtualFile) vfHistorical, 1365 vfFile.getFullPath()); 1366 this.addHistoricalVersionPath( 1367 vfFile, 1368 vfHistorical.getFullPath()); 1369 } 1370 } 1371 } 1372 this.setFileHistoryPopulated(vfFile, true); 1373 } else if (!fHistoryDir.exists()) { 1374 fHistoryDir.mkdir(); 1375 } 1376 1377 } 1378 1379 1382 public StatusData orderVirtualFileChildren(List aPaths, VirtualFile vfDir) { 1383 VFSStatus retnStatus = new VFSStatus(); 1384 1385 boolean bError = false; 1386 1387 File fDir = this.getRealFile(vfDir); 1388 File fOrderFile = this.getOrderFile(fDir); 1389 1390 Document xml = null; 1391 try { 1392 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 1393 factory.setNamespaceAware(true); 1394 xml = factory.newDocumentBuilder().newDocument(); 1395 } catch (FactoryConfigurationError e) { 1396 bError=true; 1397 e.printStackTrace(); 1398 } catch (ParserConfigurationException e) { 1399 bError=true; 1400 e.printStackTrace(); 1401 } 1402 1403 Element rootEl = xml.createElementNS("http://www.simulacramedia.com/harmoniseclient/propinstances", "Order"); 1404 xml.appendChild(rootEl); 1405 1406 for (int i = 0; i < aPaths.size(); i++) { 1407 String sPath = (String ) aPaths.get(i); 1408 Element elChild = xml.createElementNS("http://www.simulacramedia.com/harmoniseclient/propinstances", "child"); 1409 Text txt = xml.createTextNode(sPath); 1410 elChild.appendChild(txt); 1411 rootEl.appendChild(elChild); 1412 } 1413 1414 XMLPrettyPrint printer = new XMLPrettyPrint(); 1415 printer.setNamespaceAware(true); 1416 printer.printNodeToFile(rootEl, fOrderFile); 1417 1418 if(bError) { 1419 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 1420 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 1421 } 1422 1423 return retnStatus; 1424 } 1425 1426 1429 public ValueInstance getNewValueInstance(PropertyInstance propInst) { 1430 ValueInstance val = null; 1431 1432 Range range = propInst.getDefinition().getRange(); 1433 1434 if(range instanceof BooleanRange) { 1435 val = new LVVFSBooleanValue(); 1436 } else if(range instanceof DateTimeRange) { 1437 val = new LVVFSDateTimeValue(); 1438 } else if(range instanceof DateRange) { 1439 val = new LVVFSDateValue(); 1440 } else if(range instanceof DomainRange) { 1441 val = new LVVFSDomainValue(); 1442 } else if(range instanceof FloatRange) { 1443 val = new LVVFSFloatValue(); 1444 } else if(range instanceof IntegerRange) { 1445 val = new LVVFSIntegerValue(); 1446 } else if(range instanceof PropertyRange) { 1447 val = new LVVFSPropertyValue(); 1448 } else if(range instanceof RangeRange) { 1449 val = new LVVFSRangeValue(); 1450 } else if(range instanceof ResourceRange) { 1451 val = new LVVFSResourceValue(); 1452 } else if(range instanceof URIRange) { 1453 val = new LVVFSURIValue(); 1454 } else if(range instanceof ValueRange) { 1455 val = new LVVFSValueValue(); 1456 } else { 1457 val = new LVVFSStringValue(); 1458 } 1459 1460 return val; 1461 } 1462 1463 1466 public boolean rejectAllChanges() { 1467 return false; 1468 } 1469 1470 1473 protected void fullyPopulateFileAllowedMethods(VirtualFile vfFile) { 1474 } 1475 1476 1479 public String currentUserResourcePath(AuthInfo authInfo) { 1480 return null; 1481 } 1482 1483 1486 public List getChangedVirtualFiles() { 1487 return new ArrayList(); 1488 } 1489 1490 1493 public VirtualFile getPropertyVirtualFile(String sPropPath) { 1494 return null; 1495 } 1496 1497} 1498
| Popular Tags
|