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

📄 cmssynchronize.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
            I_CmsReport.FORMAT_OK);
    }

    /**
     * Exports a resource from the VFS to the FS and updates the 
     * synchronisation lists.<p>
     * 
     * @param res the resource to be exported
     * @throws CmsException if something goes wrong
     */
    private void exportToRfs(CmsResource res) throws CmsException {

        CmsFile vfsFile;
        File fsFile;
        String resourcename;
        // 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.
        CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(m_cms.getSitePath(res)));
        // if no entry in the sync list was found, its a new resource and we 
        // can use the name of the VFS resource.
        if (sync != null) {
            resourcename = sync.getResName();
        } else {
            // otherwise use the original non-translated name
            resourcename = m_cms.getSitePath(res);

            // the parent folder could contain a translated names as well, so 
            // make a lookup in the sync list ot get its original 
            // non-translated name
            String parent = CmsResource.getParentFolder(resourcename);
            CmsSynchronizeList parentSync = (CmsSynchronizeList)m_newSyncList.get(parent);
            // use the non-translated pathname
            if (parentSync != null) {
                resourcename = parentSync.getResName() + res.getName();
            }
        }
        if ((res.isFolder()) && (!resourcename.endsWith("/"))) {
            resourcename += "/";
        }
        fsFile = getFileInRfs(resourcename);

        try {
            // if the resource is marked for deletion, do not export it!
            if (!res.getState().isDeleted()) {
                // if its a file, create export the file to the FS
                m_report.print(org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_SUCCESSION_1,
                    String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE);
                if (res.isFile()) {

                    m_report.print(Messages.get().container(Messages.RPT_EXPORT_FILE_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        m_cms.getSitePath(res)));
                    m_report.print(Messages.get().container(Messages.RPT_TO_FS_AS_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        fsFile.getAbsolutePath().replace('\\', '/')));
                    m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

                    // create the resource if nescessary
                    if (!fsFile.exists()) {
                        createNewLocalFile(fsFile);
                    }
                    // write the file content to the FS
                    vfsFile = m_cms.readFile(m_cms.getSitePath(res), CmsResourceFilter.IGNORE_EXPIRATION);
                    try {
                        writeFileByte(vfsFile.getContents(), fsFile);
                    } catch (IOException e) {
                        throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_WRITE_FILE_0));
                    }
                    // now check if there is some external method to be called 
                    // which should modify the exported resource in the FS
                    Iterator i = m_synchronizeModifications.iterator();
                    while (i.hasNext()) {
                        try {
                            ((I_CmsSynchronizeModification)i.next()).modifyFs(m_cms, vfsFile, fsFile);
                        } catch (CmsSynchronizeException e) {
                            if (LOG.isWarnEnabled()) {
                                LOG.warn(Messages.get().getBundle().key(
                                    Messages.LOG_SYNCHRONIZE_EXPORT_FAILED_1,
                                    res.getRootPath()), e);
                            }
                            break;
                        }
                    }
                    fsFile.setLastModified(res.getDateLastModified());
                } else {

                    m_report.print(Messages.get().container(Messages.RPT_EXPORT_FOLDER_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        m_cms.getSitePath(res)));
                    m_report.print(Messages.get().container(Messages.RPT_TO_FS_AS_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        fsFile.getAbsolutePath().replace('\\', '/')));
                    m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

                    // its a folder, so create a folder in the FS
                    fsFile.mkdir();
                }
                // add resource to synchronisation list
                CmsSynchronizeList syncList = new CmsSynchronizeList(
                    resourcename,
                    translate(resourcename),
                    res.getDateLastModified(),
                    fsFile.lastModified());
                m_newSyncList.put(translate(resourcename), syncList);
                // and remove it fomr the old one
                m_syncList.remove(translate(resourcename));
                m_report.println(
                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                    I_CmsReport.FORMAT_OK);
            }
            // free mem
            vfsFile = null;

        } catch (CmsException e) {
            throw new CmsSynchronizeException(e.getMessageContainer(), e);
        }
    }

    /**
     * Gets the corresponding file to a resource in the VFS. <p>
     * 
     * @param res path to the resource inside the VFS
     * @return the corresponding file in the FS
     */
    private File getFileInRfs(String res) {

        String path = m_destinationPathInRfs + res.substring(0, res.lastIndexOf("/"));
        String fileName = res.substring(res.lastIndexOf("/") + 1);
        return new File(path, fileName);
    }

    /**
     * Gets the corresponding filename of the VFS to a resource in the FS. <p>
     * 
     * @param res the resource in the FS
     * @return the corresponding filename in the VFS
     */
    private String getFilenameInVfs(File res) {

        String resname = res.getAbsolutePath();
        if (res.isDirectory()) {
            resname += "/";
        }
        // translate the folder seperator if nescessary
        resname = resname.replace(File.separatorChar, '/');
        return resname.substring(m_destinationPathInRfs.length());
    }

    /**
     * Imports a new resource from the FS into the VFS and updates the 
     * synchronisation lists.<p>
     * 
     * @param fsFile the file in the FS
     * @param resName the name of the resource in the VFS
     * @param folder the folder to import the file into
     * @throws CmsException if something goes wrong
     */
    private void importToVfs(File fsFile, String resName, String folder) throws CmsException {

        try {
            // get the content of the FS file
            byte[] content = CmsFileUtil.readFile(fsFile);

            // create the file
            String filename = translate(fsFile.getName());

            m_report.print(org.opencms.report.Messages.get().container(
                org.opencms.report.Messages.RPT_SUCCESSION_1,
                String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE);
            if (fsFile.isFile()) {
                m_report.print(Messages.get().container(Messages.RPT_IMPORT_FILE_0), I_CmsReport.FORMAT_NOTE);
            } else {
                m_report.print(Messages.get().container(Messages.RPT_IMPORT_FOLDER_0), I_CmsReport.FORMAT_NOTE);
            }

            m_report.print(org.opencms.report.Messages.get().container(
                org.opencms.report.Messages.RPT_ARGUMENT_1,
                fsFile.getAbsolutePath().replace('\\', '/')));
            m_report.print(Messages.get().container(Messages.RPT_FROM_FS_TO_0), I_CmsReport.FORMAT_NOTE);

            // get the file type of the FS file
            int resType = OpenCms.getResourceManager().getDefaultTypeForName(resName).getTypeId();
            CmsResource newFile = m_cms.createResource(translate(folder) + filename, resType, content, new ArrayList());

            m_report.print(org.opencms.report.Messages.get().container(
                org.opencms.report.Messages.RPT_ARGUMENT_1,
                m_cms.getSitePath(newFile)));
            m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

            // now check if there is some external method to be called which
            // should modify the imported resource in the VFS
            Iterator i = m_synchronizeModifications.iterator();
            while (i.hasNext()) {
                try {
                    ((I_CmsSynchronizeModification)i.next()).modifyVfs(m_cms, newFile, fsFile);
                } catch (CmsSynchronizeException e) {
                    break;
                }
            }
            // we have to read the new resource again, to get the correct timestamp
            m_cms.setDateLastModified(m_cms.getSitePath(newFile), fsFile.lastModified(), false);
            CmsResource newRes = m_cms.readResource(m_cms.getSitePath(newFile));
            // add resource to synchronisation list
            CmsSynchronizeList syncList = new CmsSynchronizeList(
                resName,
                translate(resName),
                newRes.getDateLastModified(),
                fsFile.lastModified());
            m_newSyncList.put(translate(resName), syncList);

            m_report.println(
                org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                I_CmsReport.FORMAT_OK);
        } catch (IOException e) {
            throw new CmsSynchronizeException(
                Messages.get().container(Messages.ERR_READING_FILE_1, fsFile.getName()),
                e);
        }
    }

    /**
     * Reads the synchronisation list from the last sync process form the file
     * system and stores the information in a HashMap. <p>
     * 
     * Filenames are stored as keys, CmsSynchronizeList objects as values.
     * @return HashMap with synchronisation infomration of the last sync process
     * @throws CmsException if something goes wrong
     */
    private HashMap readSyncList() throws CmsException {

        HashMap syncList = new HashMap();

        // the sync list file in the server fs
        File syncListFile;
        syncListFile = new File(m_destinationPathInRfs, SYNCLIST_FILENAME);
        // try to read the sync list file if it is there
        if (syncListFile.exists()) {
            // prepare the streams to write the data
            FileReader fIn = null;
            LineNumberReader lIn = null;
            try {
                fIn = new FileReader(syncListFile);
                lIn = new LineNumberReader(fIn);
                // read one line from the file
                String line = lIn.readLine();
                while (line != null) {
                    line = lIn.readLine();
                    // extract the data and create a CmsSychroizedList object
                    //  from it
                    if (line != null) {
                        StringTokenizer tok = new StringTokenizer(line, ":");
                        String resName = tok.nextToken();
                        String tranResName = tok.nextToken();
                        long modifiedVfs = new Long(tok.nextToken()).longValue();
                        long modifiedFs = new Long(tok.nextToken()).longValue();
                        CmsSynchronizeList sync = new CmsSynchronizeList(resName, tranResName, modifiedVfs, modifiedFs);
                        syncList.put(translate(resName), sync);
                    }
                }
            } catch (IOException e) {
                throw new CmsSynchronizeException(Messages.get().container(Messages.ERR_READ_SYNC_LIST_0), e);
            } finally {
                // close all streams that were used
                try {
                    if (lIn != null) {
                        lIn.close();
                    }
                    if (fIn != null) {
                        fIn.close();
                    }
                } catch (IOException e) {
                    // ignore
                }
            }
        }

        return syncList;
    }

    /**
     * Removes all resources in the RFS which are deleted in the VFS.<p>
     * 
     * @param folder the folder in the FS to check
     * @throws CmsException if something goes wrong
     */
    private void removeFromRfs(String folder) throws CmsException {

        // get the corresponding folder in the FS
        File[] res;
        File rfsFile = new File(folder);
        // get all resources in this folder
        res = rfsFile.listFiles();
        // now loop through all resources
        for (int i = 0; i < res.length; i++) {
            // get the corrsponding name in the VFS
            String vfsFile = getFilenameInVfs(res[i]);
            // recurse if it is an directory, we must go depth first to delete 
            // files
            if (res[i].isDirectory()) {
                removeFromRfs(res[i].getAbsolutePath());
            }
            // now check if this resource is still in the old sync list.
            // if so, then it does not exist in the FS anymore andm ust be 
            // deleted
            CmsSynchronizeList sync = (CmsSynchronizeList)m_syncList.get(translate(vfsFile));

            // there is an entry, so delete the resource
            if (sync != null) {

                m_report.print(org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_SUCCESSION_1,
                    String.valueOf(m_count++)), I_CmsReport.FORMAT_NOTE);
                if (res[i].isFile()) {
                    m_report.print(Messages.get().container(Messages.RPT_DEL_FS_FILE_0), I_CmsReport.FORMAT_NOTE);
                } else {
                    m_report.print(Messages.get().container(Messages.RPT_DEL_FS_FOLDER_0), I_CmsReport.FORMAT_NOTE);
                }
                m_report.print(org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_ARGUMENT_1,
                    res[i].getAbsolutePath().replace('\\', '/')));
                m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

                res[i].delete();
                m_syncList.remove(translate(vfsFile));

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

    /**
     * Updates the synchroinisation lists if a resource is not used during the
     * synchronisation process.<p>
     * 
     * @param res the resource whose entry must be updated
     */

⌨️ 快捷键说明

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