1 33 34 package org.opencms.synchronize; 35 36 import org.opencms.db.CmsDbIoException; 37 import org.opencms.file.CmsFile; 38 import org.opencms.file.CmsObject; 39 import org.opencms.file.CmsResource; 40 import org.opencms.file.CmsResourceFilter; 41 import org.opencms.file.types.CmsResourceTypeFolder; 42 import org.opencms.main.CmsException; 43 import org.opencms.main.CmsLog; 44 import org.opencms.main.OpenCms; 45 import org.opencms.report.I_CmsReport; 46 import org.opencms.util.CmsFileUtil; 47 48 import java.io.DataOutputStream ; 49 import java.io.File ; 50 import java.io.FileOutputStream ; 51 import java.io.FileReader ; 52 import java.io.IOException ; 53 import java.io.LineNumberReader ; 54 import java.io.PrintWriter ; 55 import java.util.ArrayList ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.StringTokenizer ; 60 61 import org.apache.commons.logging.Log; 62 63 72 public class CmsSynchronize { 73 74 75 static final int DELETE_VFS = 3; 76 77 78 static final int EXPORT_VFS = 1; 79 80 81 static final String SYNCLIST_FILENAME = "#synclist.txt"; 82 83 84 static final int UPDATE_VFS = 2; 85 86 87 private static final Log LOG = CmsLog.getLog(CmsSynchronize.class); 88 89 90 private static List m_synchronizeModifications = new ArrayList (); 91 92 93 private CmsObject m_cms; 94 95 96 private int m_count; 97 98 99 private String m_destinationPathInRfs; 100 101 102 private HashMap m_newSyncList; 103 104 105 private I_CmsReport m_report; 106 107 108 private HashMap m_syncList; 109 110 121 public CmsSynchronize(CmsObject cms, CmsSynchronizeSettings settings, I_CmsReport report) 122 throws CmsSynchronizeException, CmsException { 123 124 126 if ((settings != null) && (settings.isSyncEnabled())) { 129 130 m_cms = OpenCms.initCmsObject(cms); 132 m_cms.getRequestContext().setSiteRoot("/"); 134 135 m_report = report; 136 m_count = 1; 137 138 m_destinationPathInRfs = settings.getDestinationPathInRfs(); 140 141 File destinationFolder = new File(m_destinationPathInRfs); 143 if (!destinationFolder.exists() || !destinationFolder.isDirectory()) { 144 throw new CmsSynchronizeException(Messages.get().container( 146 Messages.ERR_RFS_DESTINATION_NOT_THERE_1, 147 m_destinationPathInRfs)); 148 } 149 if (!destinationFolder.canWrite()) { 150 throw new CmsSynchronizeException(Messages.get().container( 152 Messages.ERR_RFS_DESTINATION_NO_WRITE_1, 153 m_destinationPathInRfs)); 154 } 155 156 m_syncList = readSyncList(); 158 m_newSyncList = new HashMap (); 159 160 Iterator i = settings.getSourceListInVfs().iterator(); 161 while (i.hasNext()) { 162 String sourcePathInVfs = (String )i.next(); 164 String destPath = m_destinationPathInRfs + sourcePathInVfs.replace('/', File.separatorChar); 165 166 report.println(org.opencms.workplace.threads.Messages.get().container( 167 org.opencms.workplace.threads.Messages.RPT_SYNCHRONIZE_FOLDERS_2, 168 sourcePathInVfs, 169 destPath), I_CmsReport.FORMAT_HEADLINE); 170 syncVfsToRfs(sourcePathInVfs); 172 } 173 174 removeFromRfs(m_destinationPathInRfs); 176 i = settings.getSourceListInVfs().iterator(); 177 178 while (i.hasNext()) { 179 copyFromRfs((String )i.next()); 181 } 182 183 writeSyncList(); 185 186 m_syncList = null; 188 m_newSyncList = null; 189 m_cms = null; 190 } else { 191 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_INIT_SYNC_0)); 192 } 193 } 194 195 201 private void copyFromRfs(String folder) throws CmsException { 202 203 File[] res; 205 File fsFile = getFileInRfs(folder); 206 try { 208 m_cms.readFolder(translate(folder), CmsResourceFilter.IGNORE_EXPIRATION); 209 } catch (CmsException e) { 210 String foldername = translate(folder); 212 m_report.print(org.opencms.report.Messages.get().container( 213 org.opencms.report.Messages.RPT_SUCCESSION_1, 214 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 215 m_report.print(Messages.get().container(Messages.RPT_IMPORT_FOLDER_0), I_CmsReport.FORMAT_NOTE); 216 m_report.print(org.opencms.report.Messages.get().container( 217 org.opencms.report.Messages.RPT_ARGUMENT_1, 218 fsFile.getAbsolutePath().replace('\\', '/'))); 219 m_report.print(Messages.get().container(Messages.RPT_FROM_FS_TO_0), I_CmsReport.FORMAT_NOTE); 220 m_report.print(org.opencms.report.Messages.get().container( 221 org.opencms.report.Messages.RPT_ARGUMENT_1, 222 foldername)); 223 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 224 225 CmsResource newFolder = m_cms.createResource(foldername, CmsResourceTypeFolder.RESOURCE_TYPE_ID); 226 Iterator i = m_synchronizeModifications.iterator(); 229 while (i.hasNext()) { 230 try { 231 ((I_CmsSynchronizeModification)i.next()).modifyVfs(m_cms, newFolder, fsFile); 232 } catch (CmsSynchronizeException e1) { 233 break; 234 } 235 } 236 newFolder = m_cms.readFolder(foldername, CmsResourceFilter.IGNORE_EXPIRATION); 238 String resourcename = m_cms.getSitePath(newFolder); 239 CmsSynchronizeList sync = new CmsSynchronizeList( 241 folder, 242 resourcename, 243 newFolder.getDateLastModified(), 244 fsFile.lastModified()); 245 m_newSyncList.put(resourcename, sync); 246 247 m_report.println( 248 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 249 I_CmsReport.FORMAT_OK); 250 } 251 if (fsFile.isDirectory()) { 253 res = fsFile.listFiles(); 255 256 for (int i = 0; i < res.length; i++) { 258 String resname = res[i].getAbsolutePath(); 260 resname = resname.substring(m_destinationPathInRfs.length()); 261 resname = resname.replace(File.separatorChar, '/'); 263 if (res[i].isFile()) { 266 if (!m_newSyncList.containsKey(translate(resname))) { 267 importToVfs(res[i], resname, folder); 269 } 270 } else { 271 copyFromRfs(resname + "/"); 273 } 274 } 275 } 276 res = null; 278 } 279 280 286 private void createNewLocalFile(File newFile) throws CmsException { 287 288 if (newFile.exists()) { 289 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_EXISTENT_FILE_1, newFile.getPath())); 290 } 291 FileOutputStream fOut = null; 292 try { 293 File parentFolder = new File(newFile.getPath().replace('/', File.separatorChar).substring( 294 0, 295 newFile.getPath().lastIndexOf(File.separator))); 296 parentFolder.mkdirs(); 297 if (parentFolder.exists()) { 298 fOut = new FileOutputStream (newFile); 299 } else { 300 throw new CmsSynchronizeException( 301 Messages.get().container(Messages.ERR_CREATE_DIR_1, newFile.getPath())); 302 } 303 } catch (IOException e) { 304 throw new CmsSynchronizeException(Messages.get().container( 305 Messages.ERR_CREATE_FILE_1, 306 this.getClass().getName(), 307 newFile.getPath()), e); 308 } finally { 309 if (fOut != null) { 310 try { 311 fOut.close(); 312 } catch (IOException e) { 313 } 315 } 316 } 317 } 318 319 326 private void deleteFromVfs(CmsResource res) throws CmsException { 327 328 String resourcename = m_cms.getSitePath(res); 329 330 m_report.print(org.opencms.report.Messages.get().container( 331 org.opencms.report.Messages.RPT_SUCCESSION_1, 332 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 333 if (res.isFile()) { 334 m_report.print(Messages.get().container(Messages.RPT_DEL_FILE_0), I_CmsReport.FORMAT_NOTE); 335 } else { 336 m_report.print(Messages.get().container(Messages.RPT_DEL_FOLDER_0), I_CmsReport.FORMAT_NOTE); 337 } 338 m_report.print(org.opencms.report.Messages.get().container( 339 org.opencms.report.Messages.RPT_ARGUMENT_1, 340 resourcename)); 341 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 342 343 m_cms.lockResource(resourcename); 345 m_cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS); 346 m_syncList.remove(translate(resourcename)); 348 349 m_report.println( 350 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 351 I_CmsReport.FORMAT_OK); 352 } 353 354 361 private void exportToRfs(CmsResource res) throws CmsException { 362 363 CmsFile vfsFile; 364 File fsFile; 365 String resourcename; 366 CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(m_cms.getSitePath(res))); 370 if (sync != null) { 373 resourcename = sync.getResName(); 374 } else { 375 resourcename = m_cms.getSitePath(res); 377 378 String parent = CmsResource.getParentFolder(resourcename); 382 CmsSynchronizeList parentSync = (CmsSynchronizeList)m_newSyncList.get(parent); 383 if (parentSync != null) { 385 resourcename = parentSync.getResName() + res.getName(); 386 } 387 } 388 if ((res.isFolder()) && (!resourcename.endsWith("/"))) { 389 resourcename += "/"; 390 } 391 fsFile = getFileInRfs(resourcename); 392 393 try { 394 if (res.getState() != CmsResource.STATE_DELETED) { 396 m_report.print(org.opencms.report.Messages.get().container( 398 org.opencms.report.Messages.RPT_SUCCESSION_1, 399 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 400 if (res.isFile()) { 401 402 m_report.print(Messages.get().container(Messages.RPT_EXPORT_FILE_0), I_CmsReport.FORMAT_NOTE); 403 m_report.print(org.opencms.report.Messages.get().container( 404 org.opencms.report.Messages.RPT_ARGUMENT_1, 405 m_cms.getSitePath(res))); 406 m_report.print(Messages.get().container(Messages.RPT_TO_FS_AS_0), I_CmsReport.FORMAT_NOTE); 407 m_report.print(org.opencms.report.Messages.get().container( 408 org.opencms.report.Messages.RPT_ARGUMENT_1, 409 fsFile.getAbsolutePath().replace('\\', '/'))); 410 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 411 412 if (!fsFile.exists()) { 414 createNewLocalFile(fsFile); 415 } 416 vfsFile = m_cms.readFile(m_cms.getSitePath(res), CmsResourceFilter.IGNORE_EXPIRATION); 418 try { 419 writeFileByte(vfsFile.getContents(), fsFile); 420 } catch (IOException e) { 421 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_WRITE_FILE_0)); 422 } 423 Iterator i = m_synchronizeModifications.iterator(); 426 while (i.hasNext()) { 427 try { 428 ((I_CmsSynchronizeModification)i.next()).modifyFs(m_cms, vfsFile, fsFile); 429 } catch (CmsSynchronizeException e) { 430 if (LOG.isWarnEnabled()) { 431 LOG.warn(Messages.get().getBundle().key( 432 Messages.LOG_SYNCHRONIZE_EXPORT_FAILED_1, 433 res.getRootPath()), e); 434 } 435 break; 436 } 437 } 438 fsFile.setLastModified(res.getDateLastModified()); 439 } else { 440 441 m_report.print(Messages.get().container(Messages.RPT_EXPORT_FOLDER_0), I_CmsReport.FORMAT_NOTE); 442 m_report.print(org.opencms.report.Messages.get().container( 443 org.opencms.report.Messages.RPT_ARGUMENT_1, 444 m_cms.getSitePath(res))); 445 m_report.print(Messages.get().container(Messages.RPT_TO_FS_AS_0), I_CmsReport.FORMAT_NOTE); 446 m_report.print(org.opencms.report.Messages.get().container( 447 org.opencms.report.Messages.RPT_ARGUMENT_1, 448 fsFile.getAbsolutePath().replace('\\', '/'))); 449 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 450 451 fsFile.mkdir(); 453 } 454 CmsSynchronizeList syncList = new CmsSynchronizeList( 456 resourcename, 457 translate(resourcename), 458 res.getDateLastModified(), 459 fsFile.lastModified()); 460 m_newSyncList.put(translate(resourcename), syncList); 461 m_syncList.remove(translate(resourcename)); 463 m_report.println( 464 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 465 I_CmsReport.FORMAT_OK); 466 } 467 vfsFile = null; 469 470 } catch (CmsException e) { 471 throw new CmsSynchronizeException(e.getMessageContainer(), e); 472 } 473 } 474 475 481 private File getFileInRfs(String res) { 482 483 String path = m_destinationPathInRfs + res.substring(0, res.lastIndexOf("/")); 484 String fileName = res.substring(res.lastIndexOf("/") + 1); 485 return new File(path, fileName); 486 } 487 488 494 private String getFilenameInVfs(File res) { 495 496 String resname = res.getAbsolutePath(); 497 if (res.isDirectory()) { 498 resname += "/"; 499 } 500 resname = resname.replace(File.separatorChar, '/'); 502 return resname.substring(m_destinationPathInRfs.length()); 503 } 504 505 514 private void importToVfs(File fsFile, String resName, String folder) throws CmsException { 515 516 try { 517 byte[] content = CmsFileUtil.readFile(fsFile); 519 520 String filename = translate(fsFile.getName()); 522 523 m_report.print(org.opencms.report.Messages.get().container( 524 org.opencms.report.Messages.RPT_SUCCESSION_1, 525 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 526 if (fsFile.isFile()) { 527 m_report.print(Messages.get().container(Messages.RPT_IMPORT_FILE_0), I_CmsReport.FORMAT_NOTE); 528 } else { 529 m_report.print(Messages.get().container(Messages.RPT_IMPORT_FOLDER_0), I_CmsReport.FORMAT_NOTE); 530 } 531 532 m_report.print(org.opencms.report.Messages.get().container( 533 org.opencms.report.Messages.RPT_ARGUMENT_1, 534 fsFile.getAbsolutePath().replace('\\', '/'))); 535 m_report.print(Messages.get().container(Messages.RPT_FROM_FS_TO_0), I_CmsReport.FORMAT_NOTE); 536 537 int resType = OpenCms.getResourceManager().getDefaultTypeForName(resName).getTypeId(); 539 CmsResource newFile = m_cms.createResource(translate(folder) + filename, resType, content, new ArrayList ()); 540 541 m_report.print(org.opencms.report.Messages.get().container( 542 org.opencms.report.Messages.RPT_ARGUMENT_1, 543 m_cms.getSitePath(newFile))); 544 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 545 546 Iterator i = m_synchronizeModifications.iterator(); 549 while (i.hasNext()) { 550 try { 551 ((I_CmsSynchronizeModification)i.next()).modifyVfs(m_cms, newFile, fsFile); 552 } catch (CmsSynchronizeException e) { 553 break; 554 } 555 } 556 m_cms.setDateLastModified(m_cms.getSitePath(newFile), fsFile.lastModified(), false); 558 CmsResource newRes = m_cms.readResource(m_cms.getSitePath(newFile)); 559 CmsSynchronizeList syncList = new CmsSynchronizeList( 561 resName, 562 translate(resName), 563 newRes.getDateLastModified(), 564 fsFile.lastModified()); 565 m_newSyncList.put(translate(resName), syncList); 566 newFile = null; 568 content = null; 569 570 m_report.println( 571 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 572 I_CmsReport.FORMAT_OK); 573 } catch (IOException e) { 574 throw new CmsSynchronizeException( 575 Messages.get().container(Messages.ERR_READING_FILE_1, fsFile.getName()), 576 e); 577 } 578 } 579 580 588 private HashMap readSyncList() throws CmsException { 589 590 HashMap syncList = new HashMap (); 591 592 File syncListFile; 594 syncListFile = new File(m_destinationPathInRfs, SYNCLIST_FILENAME); 595 if (syncListFile.exists()) { 597 FileReader fIn = null; 599 LineNumberReader lIn = null; 600 try { 601 fIn = new FileReader (syncListFile); 602 lIn = new LineNumberReader (fIn); 603 String line = lIn.readLine(); 605 while (line != null) { 606 line = lIn.readLine(); 607 if (line != null) { 610 StringTokenizer tok = new StringTokenizer (line, ":"); 611 if (tok != null) { 612 String resName = tok.nextToken(); 613 String tranResName = tok.nextToken(); 614 long modifiedVfs = new Long (tok.nextToken()).longValue(); 615 long modifiedFs = new Long (tok.nextToken()).longValue(); 616 CmsSynchronizeList sync = new CmsSynchronizeList( 617 resName, 618 tranResName, 619 modifiedVfs, 620 modifiedFs); 621 syncList.put(translate(resName), sync); 622 } 623 } 624 } 625 } catch (IOException e) { 626 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_READ_SYNC_LIST_0), e); 627 } finally { 628 try { 630 if (lIn != null) { 631 lIn.close(); 632 } 633 if (fIn != null) { 634 fIn.close(); 635 } 636 } catch (IOException e) { 637 } 639 } 640 } 641 642 return syncList; 643 } 644 645 651 private void removeFromRfs(String folder) throws CmsException { 652 653 File[] res; 655 File rfsFile = new File(folder); 656 res = rfsFile.listFiles(); 658 for (int i = 0; i < res.length; i++) { 660 String vfsFile = getFilenameInVfs(res[i]); 662 if (res[i].isDirectory()) { 665 removeFromRfs(res[i].getAbsolutePath()); 666 } 667 CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(vfsFile)); 671 672 if (sync != null) { 674 675 m_report.print(org.opencms.report.Messages.get().container( 676 org.opencms.report.Messages.RPT_SUCCESSION_1, 677 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 678 if (res[i].isFile()) { 679 m_report.print(Messages.get().container(Messages.RPT_DEL_FS_FILE_0), I_CmsReport.FORMAT_NOTE); 680 } else { 681 m_report.print(Messages.get().container(Messages.RPT_DEL_FS_FOLDER_0), I_CmsReport.FORMAT_NOTE); 682 } 683 m_report.print(org.opencms.report.Messages.get().container( 684 org.opencms.report.Messages.RPT_ARGUMENT_1, 685 res[i].getAbsolutePath().replace('\\', '/'))); 686 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 687 688 res[i].delete(); 689 m_syncList.remove(translate(vfsFile)); 690 691 m_report.println( 692 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 693 I_CmsReport.FORMAT_OK); 694 } 695 } 696 res = null; 698 rfsFile = null; 699 } 700 701 707 private void skipResource(CmsResource res) { 708 709 String resname = m_cms.getSitePath(res); 711 CmsSynchronizeList syncList = (CmsSynchronizeList)m_syncList.get(translate(resname)); 712 m_newSyncList.put(translate(resname), syncList); 713 m_syncList.remove(translate(resname)); 715 m_report.print(org.opencms.report.Messages.get().container( 717 org.opencms.report.Messages.RPT_SUCCESSION_1, 718 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 719 m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE); 720 m_report.println(org.opencms.report.Messages.get().container( 721 org.opencms.report.Messages.RPT_ARGUMENT_1, 722 resname)); 723 } 724 725 741 private void syncVfsToRfs(String folder) throws CmsException { 742 743 int action = 0; 744 List resources = m_cms.getResourcesInFolder(folder, CmsResourceFilter.IGNORE_EXPIRATION); 746 for (int i = 0; i < resources.size(); i++) { 748 CmsResource res = (CmsResource)resources.get(i); 749 if (res.getState() != CmsResource.STATE_DELETED) { 752 if (res.isFolder()) { 754 action = testSyncVfs(res); 756 if (action == EXPORT_VFS) { 758 exportToRfs(res); 759 } else if (action != DELETE_VFS) { 760 skipResource(res); 761 } 762 syncVfsToRfs(m_cms.getSitePath(res)); 765 if (action == DELETE_VFS) { 766 deleteFromVfs(res); 767 } 768 } else { 769 action = testSyncVfs(res); 772 switch (action) { 774 case EXPORT_VFS: 775 exportToRfs(res); 776 break; 777 778 case UPDATE_VFS: 779 updateFromRfs(res); 780 break; 781 782 case DELETE_VFS: 783 deleteFromVfs(res); 784 break; 785 786 default: 787 skipResource(res); 788 789 } 790 } 791 res = null; 793 } 794 } 795 resources = null; 797 } 798 799 805 private int testSyncVfs(CmsResource res) { 806 807 int action = 0; 808 File fsFile; 809 String resourcename = m_cms.getSitePath(res); 811 812 if (m_syncList.containsKey(translate(resourcename))) { 813 CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(resourcename)); 815 fsFile = getFileInRfs(sync.getResName()); 817 if (res.getDateLastModified() > sync.getModifiedVfs()) { 821 824 if ((fsFile.lastModified() > sync.getModifiedFs()) 828 && (fsFile.lastModified() > res.getDateLastModified())) { 829 action = UPDATE_VFS; 830 } else { 831 832 action = EXPORT_VFS; 833 } 834 } else { 835 if (!fsFile.exists()) { 838 action = DELETE_VFS; 839 } else { 840 if (fsFile.lastModified() > sync.getModifiedFs()) { 842 action = UPDATE_VFS; 843 } 844 } 845 } 846 } else { 847 action = EXPORT_VFS; 850 } 851 fsFile = null; 853 return action; 854 } 855 856 865 private String translate(String name) { 866 867 String translation = null; 868 Iterator i = m_synchronizeModifications.iterator(); 870 while (i.hasNext()) { 871 try { 872 translation = ((I_CmsSynchronizeModification)i.next()).translate(m_cms, name); 873 } catch (CmsSynchronizeException e) { 874 if (LOG.isInfoEnabled()) { 875 LOG.info(Messages.get().getBundle().key(Messages.LOG_EXTERNAL_TRANSLATION_1, name), e); 876 } 877 break; 878 } 879 } 880 if (translation == null) { 883 translation = m_cms.getRequestContext().getFileTranslator().translateResource(name); 884 } 885 return translation; 886 } 887 888 897 private void updateFromRfs(CmsResource res) throws CmsSynchronizeException, CmsException { 898 899 CmsFile vfsFile; 900 String resourcename = m_cms.getSitePath(res); 904 CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(resourcename)); 905 File fsFile = getFileInRfs(sync.getResName()); 906 907 m_report.print(org.opencms.report.Messages.get().container( 908 org.opencms.report.Messages.RPT_SUCCESSION_1, 909 String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE); 910 m_report.print(Messages.get().container(Messages.RPT_UPDATE_FILE_0), I_CmsReport.FORMAT_NOTE); 911 m_report.print(org.opencms.report.Messages.get().container( 912 org.opencms.report.Messages.RPT_ARGUMENT_1, 913 resourcename)); 914 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0)); 915 916 m_cms.lockResource(resourcename); 918 vfsFile = m_cms.readFile(resourcename, CmsResourceFilter.IGNORE_EXPIRATION); 920 try { 922 vfsFile.setContents(CmsFileUtil.readFile(fsFile)); 923 } catch (IOException e) { 924 throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_IMPORT_1, fsFile.getName())); 925 } 926 m_cms.writeFile(vfsFile); 927 Iterator i = m_synchronizeModifications.iterator(); 931 while (i.hasNext()) { 932 try { 933 ((I_CmsSynchronizeModification)i.next()).modifyVfs(m_cms, vfsFile, fsFile); 934 } catch (CmsSynchronizeException e) { 935 if (LOG.isInfoEnabled()) { 936 LOG.info( 937 Messages.get().getBundle().key(Messages.LOG_SYNCHRONIZE_UPDATE_FAILED_1, res.getRootPath()), 938 e); 939 } 940 break; 941 } 942 } 943 m_cms.setDateLastModified(resourcename, fsFile.lastModified(), false); 946 res = m_cms.readResource(resourcename); 947 948 CmsSynchronizeList syncList = new CmsSynchronizeList( 950 sync.getResName(), 951 translate(resourcename), 952 res.getDateLastModified(), 953 fsFile.lastModified()); 954 m_newSyncList.put(translate(resourcename), syncList); 955 m_syncList.remove(translate(resourcename)); 957 vfsFile = null; 958 959 m_report.println( 960 org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0), 961 I_CmsReport.FORMAT_OK); 962 } 963 964 973 private void writeFileByte(byte[] content, File file) throws IOException { 974 975 FileOutputStream fOut = null; 976 DataOutputStream dOut = null; 977 try { 978 fOut = new FileOutputStream (file); 980 dOut = new DataOutputStream (fOut); 981 dOut.write(content); 982 dOut.flush(); 983 } catch (IOException e) { 984 throw e; 985 } finally { 986 try { 987 if (fOut != null) { 988 fOut.close(); 989 } 990 } catch (IOException e) { 991 } 993 } 994 } 995 996 1003 private void writeSyncList() throws CmsException { 1004 1005 File syncListFile; 1007 syncListFile = new File(m_destinationPathInRfs, SYNCLIST_FILENAME); 1008 1009 FileOutputStream fOut = null; 1011 PrintWriter pOut = null; 1012 try { 1013 fOut = new FileOutputStream (syncListFile); 1014 pOut = new PrintWriter (fOut); 1015 pOut.println(CmsSynchronizeList.getFormatDescription()); 1016 1017 Iterator values = m_newSyncList.values().iterator(); 1019 while (values.hasNext()) { 1022 CmsSynchronizeList sync = (CmsSynchronizeList)values.next(); 1023 pOut.println(sync.toString()); 1025 } 1026 } catch (IOException e) { 1027 throw new CmsDbIoException(Messages.get().container(Messages.ERR_IO_WRITE_SYNCLIST_0), e); 1028 } finally { 1029 try { 1031 pOut.flush(); 1032 fOut.flush(); 1033 if (pOut != null) { 1034 pOut.close(); 1035 } 1036 if (fOut != null) { 1037 fOut.close(); 1038 } 1039 } catch (IOException e) { 1040 } 1042 } 1043 } 1044} | Popular Tags |