⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmssynchronize.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        res = null;
        rfsFile = null;
    }

    /**
     * Updates the synchroinisation lists if a resource is not used during the
     * synchronisation process.<p>
     * 
     * @param res the resource whose entry must be updated
     */
    private void skipResource(CmsResource res) {

        // add the file to the new sync list...
        String resname = m_cms.getSitePath(res);
        CmsSynchronizeList syncList = (CmsSynchronizeList)m_syncList.get(translate(resname));
        m_newSyncList.put(translate(resname), syncList);
        // .. and remove it from the old one
        m_syncList.remove(translate(resname));
        // update the report
        m_report.print(org.opencms.report.Messages.get().container(
            org.opencms.report.Messages.RPT_SUCCESSION_1,
            String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE);
        m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
        m_report.println(org.opencms.report.Messages.get().container(
            org.opencms.report.Messages.RPT_ARGUMENT_1,
            resname));
    }

    /**
     * Synchronizes resources from the VFS to the RFS. <p>
     *
     * During the synchronization process, the following actions will be done:<p>
     * 
     * <ul>
     * <li>Export modified resources from the VFS to the FS</li>
     * <li>Update resources in the VFS if the corresponding resource in the FS 
     * has changed</li>
     * <li>Delete resources in the VFS if the corresponding resource in the FS
     * has been deleted</li>
     * </ul>
     * 
     * @param folder The folder in the VFS to be synchronized with the FS
     * @throws CmsException if something goes wrong
     */
    private void syncVfsToRfs(String folder) throws CmsException {

        int action = 0;
        //get all resources in the given folder
        List resources = m_cms.getResourcesInFolder(folder, CmsResourceFilter.IGNORE_EXPIRATION);
        // now look through all resources in the folder
        for (int i = 0; i < resources.size(); i++) {
            CmsResource res = (CmsResource)resources.get(i);
            // test if the resource is marked as deleted. if so,
            // do nothing, the corrsponding file in the FS will be removed later
            if (res.getState() != CmsResource.STATE_DELETED) {
                // do a recursion if the current resource is a folder
                if (res.isFolder()) {
                    // first check if this folder must be synchronised
                    action = testSyncVfs(res);
                    // do the correct action according to the test result
                    if (action == EXPORT_VFS) {
                        exportToRfs(res);
                    } else if (action != DELETE_VFS) {
                        skipResource(res);
                    }
                    // recurse into the subfolders. This must be done before 
                    // the folder might be deleted!
                    syncVfsToRfs(m_cms.getSitePath(res));
                    if (action == DELETE_VFS) {
                        deleteFromVfs(res);
                    }
                } else {
                    // if the current resource is a file, check if it has to 
                    // be synchronized
                    action = testSyncVfs(res);
                    // do the correct action according to the test result
                    switch (action) {
                        case EXPORT_VFS:
                            exportToRfs(res);
                            break;

                        case UPDATE_VFS:
                            updateFromRfs(res);
                            break;

                        case DELETE_VFS:
                            deleteFromVfs(res);
                            break;

                        default:
                            skipResource(res);

                    }
                }
                // free mem
                res = null;
            }
        }
        //  free mem
        resources = null;
    }

    /**
     * Determines the synchronisation status of a VFS resource. <p>
     *  
     * @param res the VFS resource to check
     * @return integer value for the action to be done for this VFS resource
     */
    private int testSyncVfs(CmsResource res) {

        int action = 0;
        File fsFile;
        //data from sync list
        String resourcename = m_cms.getSitePath(res);

        if (m_syncList.containsKey(translate(resourcename))) {
            // this resource was already used in a previous syncprocess
            CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(resourcename));
            // get the corresponding resource from the FS
            fsFile = getFileInRfs(sync.getResName());
            // now check what to do with this resource.
            // if the modification date is newer than the logged modification 
            // date in the sync list, this resource must be exported too
            if (res.getDateLastModified() > sync.getModifiedVfs()) {
                // now check if the resource in the FS is newer, then the 
                // resource from the FS must be imported

                // check if it has been modified since the last sync process 
                // and its newer than the resource in the VFS, only then this 
                // resource must be imported form the FS
                if ((fsFile.lastModified() > sync.getModifiedFs())
                    && (fsFile.lastModified() > res.getDateLastModified())) {
                    action = UPDATE_VFS;
                } else {

                    action = EXPORT_VFS;
                }
            } else {
                // test if the resource in the FS does not exist anymore.
                // if so, remove the resource in the VFS
                if (!fsFile.exists()) {
                    action = DELETE_VFS;
                } else {
                    // now check if the resource in the FS might have changed
                    if (fsFile.lastModified() > sync.getModifiedFs()) {
                        action = UPDATE_VFS;
                    }
                }
            }
        } else {
            // the resource name was not found in the sync list
            // this is a new resource
            action = EXPORT_VFS;
        }
        //free mem
        fsFile = null;
        return action;
    }

    /**
     * Translates the resource name.  <p>
     * 
     * This is nescessary since the server RFS does allow different naming 
     * conventions than the VFS.
     * 
     * @param name the resource name to be translated
     * @return the translated resource name
     */
    private String translate(String name) {

        String translation = null;
        // test if an external translation should be used
        Iterator i = m_synchronizeModifications.iterator();
        while (i.hasNext()) {
            try {
                translation = ((I_CmsSynchronizeModification)i.next()).translate(m_cms, name);
            } catch (CmsSynchronizeException e) {
                if (LOG.isInfoEnabled()) {
                    LOG.info(Messages.get().getBundle().key(Messages.LOG_EXTERNAL_TRANSLATION_1, name), e);
                }
                break;
            }
        }
        // if there was no external method called, do the default OpenCms 
        // RFS-VFS translation
        if (translation == null) {
            translation = m_cms.getRequestContext().getFileTranslator().translateResource(name);
        }
        return translation;
    }

    /**
     * Imports a resource from the FS to the VFS and updates the
     * synchronisation lists.<p>
     * 
     * @param res the resource to be exported
     * 
     * @throws CmsSynchronizeException if the resource could not be synchronized
     * @throws CmsException if something goes wrong
     */
    private void updateFromRfs(CmsResource res) throws CmsSynchronizeException, CmsException {

        CmsFile vfsFile;
        // to get the name of the file in the FS, we must look it up in the
        // sync list. This is nescessary, since the VFS could use a tranlated
        // filename.
        String resourcename = m_cms.getSitePath(res);
        CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(resourcename));
        File fsFile = getFileInRfs(sync.getResName());

        m_report.print(org.opencms.report.Messages.get().container(
            org.opencms.report.Messages.RPT_SUCCESSION_1,
            String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE);
        m_report.print(Messages.get().container(Messages.RPT_UPDATE_FILE_0), I_CmsReport.FORMAT_NOTE);
        m_report.print(org.opencms.report.Messages.get().container(
            org.opencms.report.Messages.RPT_ARGUMENT_1,
            resourcename));
        m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

        // lock the file in the VFS, so that it can be updated
        m_cms.lockResource(resourcename);
        // read the file in the VFS
        vfsFile = m_cms.readFile(resourcename, CmsResourceFilter.IGNORE_EXPIRATION);
        // import the content from the FS
        try {
            vfsFile.setContents(CmsFileUtil.readFile(fsFile));
        } catch (IOException e) {
            throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_IMPORT_1, fsFile.getName()));
        }
        m_cms.writeFile(vfsFile);
        // now check if there is some external method to be called which 
        // should modify
        // the updated resource in the VFS
        Iterator i = m_synchronizeModifications.iterator();
        while (i.hasNext()) {
            try {
                ((I_CmsSynchronizeModification)i.next()).modifyVfs(m_cms, vfsFile, fsFile);
            } catch (CmsSynchronizeException e) {
                if (LOG.isInfoEnabled()) {
                    LOG.info(
                        Messages.get().getBundle().key(Messages.LOG_SYNCHRONIZE_UPDATE_FAILED_1, res.getRootPath()),
                        e);
                }
                break;
            }
        }
        // everything is done now, so unlock the resource
        // read the resource again, nescessary to get the actual timestamps
        m_cms.setDateLastModified(resourcename, fsFile.lastModified(), false);
        res = m_cms.readResource(resourcename);

        //add resource to synchronisation list
        CmsSynchronizeList syncList = new CmsSynchronizeList(
            sync.getResName(),
            translate(resourcename),
            res.getDateLastModified(),
            fsFile.lastModified());
        m_newSyncList.put(translate(resourcename), syncList);
        // and remove it from the old one
        m_syncList.remove(translate(resourcename));
        vfsFile = null;

        m_report.println(
            org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
            I_CmsReport.FORMAT_OK);
    }

    /**
     * This writes the byte content of a resource to the file on the server
     * filesystem.<p>
     *
     * @param content the content of the file in the VFS
     * @param file the file in SFS that has to be updated with content
     * 
     * @throws IOException if something goes wrong
     */
    private void writeFileByte(byte[] content, File file) throws IOException {

        FileOutputStream fOut = null;
        DataOutputStream dOut = null;
        try {
            // write the content to the file in server filesystem
            fOut = new FileOutputStream(file);
            dOut = new DataOutputStream(fOut);
            dOut.write(content);
            dOut.flush();
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (fOut != null) {
                    fOut.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }

    /**
     * Writes the synchronisation list of the current sync process to the 
     * server file system. <p>
     * 
     * The file can be found in the synchronization folder
     * @throws CmsException if something goes wrong
     */
    private void writeSyncList() throws CmsException {

        // the sync list file in the server fs
        File syncListFile;
        syncListFile = new File(m_destinationPathInRfs, SYNCLIST_FILENAME);

        // prepare the streams to write the data
        FileOutputStream fOut = null;
        PrintWriter pOut = null;
        try {
            fOut = new FileOutputStream(syncListFile);
            pOut = new PrintWriter(fOut);
            pOut.println(CmsSynchronizeList.getFormatDescription());

            // get all keys from the hashmap and make an iterator on it
            Iterator values = m_newSyncList.values().iterator();
            // loop throush all values and write them to the sync list file in 
            // a human readable format
            while (values.hasNext()) {
                CmsSynchronizeList sync = (CmsSynchronizeList)values.next();
                //fOut.write(sync.toString().getBytes());
                pOut.println(sync.toString());
            }
        } catch (IOException e) {
            throw new CmsDbIoException(Messages.get().container(Messages.ERR_IO_WRITE_SYNCLIST_0), e);
        } finally {
            // close all streams that were used
            try {
                pOut.flush();
                fOut.flush();
                if (pOut != null) {
                    pOut.close();
                }
                if (fOut != null) {
                    fOut.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -