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

📄 cmssynchronize.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsSynchronize.java,v $
* Date   : $Date: 2001/10/18 07:03:04 $
* Version: $Revision: 1.11 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.opencms.file;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.lang.reflect.*;
import java.security.*;
import com.opencms.core.*;
import com.opencms.util.*;
import com.opencms.template.*;
import org.w3c.dom.*;
import source.org.apache.java.util.*;

/**
 * This class holds the functionality to synchronize resources from the filesystem
 * into the cms and back.
 *
 * @author Edna Falkenhan
 * @version $Revision: 1.11 $ $Date: 2001/10/18 07:03:04 $
 */
public class CmsSynchronize implements I_CmsConstants{

    /**
     * flag to synchronize no filesystem,
     */
    static final int C_SYNC_NONE = 0;

    /**
     * flag to synchronize the server filesystem
     */
    static final int C_SYNC_SFS = 1;

    /**
     * flag to synchronize the virtual filesystem
     */
    static final int C_SYNC_VFS = 2;

    /**
     * flag to synchronize the server filesystem,
     * rename the old file and copy the new file from VFS
     */
    static final int C_SYNC_SFSNEW = 3;

    /**
     * the path in the server filesystem where the resource has to be synchronized
     */
    private String m_synchronizePath = null;

    /**
     * the CmsObject
     */
    private CmsObject m_cms;

    /**
     * the CmsSynchronizeList
     */
    private CmsSynchronizeList m_synchronizeList;

    /**
     * Hashtables for folders and files of the resource in the virtual filesystem
     * is needed to compare with folders and files in server filesystem
     */
    private Hashtable m_vfsFolders;
    private Hashtable m_vfsFiles;

    /**
     * This constructs a new CmsImport-object which imports the resources.
     *
     * @param resourceName the resource to synchronize.
     * @param syncPath the path of the filesystem to synchronize.
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    public CmsSynchronize(CmsObject cms, String resourceName)
        throws CmsException {
        File syncpath = null;
        m_cms = cms;
        m_synchronizePath = m_cms.getRegistry().getSystemValue(C_SYNCHRONISATION_PATH);

        if ((resourceName != null) && (m_synchronizePath != null)){
            // get the synchronisation-list
            m_synchronizeList = new CmsSynchronizeList(m_synchronizePath);
            // start the synchronisation
            m_vfsFolders = new Hashtable();
            m_vfsFiles = new Hashtable();
            // create the syncpath if necessary
            syncpath = new File(m_synchronizePath.replace('/', syncpath.separatorChar));
            if (!syncpath.exists()){
                syncpath.mkdirs();
            }
            // first synchronize the SFS with the resources from VFS
            synchronizeServer(resourceName);
            // if the resource is a folder then synchronize the VFS with the SFS
            if (resourceName.endsWith("/")){
                synchronizeVirtual(resourceName);
            }
            // save the synchronisation-list
            //System.out.println(m_synchronizeList.toString());
            m_synchronizeList.saveSyncList();
            m_vfsFolders.clear();
            m_vfsFiles.clear();
        } else {
            throw new CmsException("["+this.getClass().getName()+"] "+"Cannot read the registry entries for synchronisation.");
        }
    }

    /**
     * starts the synchronisation of the given resource
     *
     * @param resourceName the resource to synchronize.
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    private void synchronizeServer(String resourceName)
        throws CmsException{
        Vector filesInFolder = null;
        Vector foldersInFolder = null;
        CmsResource startResource = null;
        CmsFile syncFile = null;
        CmsFolder syncFolder = null;
        File startFolder = null;
        File sfsFile = null;
        int onlineProject = m_cms.onlineProject().getId();
        startResource = m_cms.readFileHeader(resourceName);
        if (startResource == null || startResource.getProjectId() == onlineProject){
            throw new CmsException("["+this.getClass().getName()+"] "+resourceName, CmsException.C_NOT_FOUND);
        }
        if (startResource.getAbsolutePath().endsWith("/")){
            // it's a folder, so all files and folders in this have to be synchronized
            // first create the start folder
            startFolder = new File((m_synchronizePath+startResource.getAbsolutePath()).replace('/', startFolder.separatorChar));
            startFolder.mkdirs();
            // now put the resource in the hashtable for VFS resources
            m_vfsFolders.put(m_synchronizePath+startResource.getAbsolutePath(), startResource);
            // read the resources from the virtual filesystem
            // and compare each with the file in the server filesystem
            filesInFolder = m_cms.getFilesInFolder(resourceName);
            for (int i = 0; i < filesInFolder.size(); i++){
                syncFile = ((CmsFile) filesInFolder.elementAt(i));
                if ((syncFile.getProjectId() != onlineProject) && (!syncFile.getName().startsWith("~"))){
                    if (syncFile.getState() == C_STATE_DELETED) {
                        // the file has to be deleted from the server filesystem
                        sfsFile = new File(m_synchronizePath+syncFile.getAbsolutePath());
                        if (sfsFile.exists()){
                            sfsFile.delete();
                        }
                        m_synchronizeList.remove(syncFile.getAbsolutePath());
                    } else {
                        syncFile = m_cms.readFile(syncFile.getAbsolutePath());
                        synchronizeFile(syncFile);
                        // now put the resource in the hashtable for VFS resources
                        m_vfsFiles.put(m_synchronizePath+syncFile.getAbsolutePath(), syncFile);
                    }
                }
            }
            foldersInFolder = m_cms.getSubFolders(resourceName);
            for (int i = 0; i < foldersInFolder.size(); i++){
                syncFolder = ((CmsFolder) foldersInFolder.elementAt(i));
                if (syncFolder.getProjectId() != onlineProject){
                    if (syncFolder.getState() == C_STATE_DELETED) {
                        // the folder has to be deleted from the server filesystem
                        sfsFile = new File(m_synchronizePath+syncFolder.getAbsolutePath());
                        if (sfsFile.exists()){
                            if (!deleteDirectory(sfsFile)){
                                throw new CmsException("["+this.getClass().getName()+"] "+"Could not delete "+sfsFile.getPath());
                            }
                        }
                    } else {
                        sfsFile = new File((m_synchronizePath+syncFolder.getAbsolutePath()).replace('/', sfsFile.separatorChar));
                        sfsFile.mkdir();
                        // now put the resource in the hashtable for VFS resources
                        m_vfsFolders.put(m_synchronizePath+syncFolder.getAbsolutePath(), syncFolder);
                        synchronizeServer(syncFolder.getAbsolutePath());
                    }
                }
            }
        } else {
            // it's only one file that has to be synchronized
            if ((startResource.getProjectId() != onlineProject) && (!startResource.getName().startsWith("~"))){
                if (startResource.getState() == C_STATE_DELETED) {
                    // the file has to be deleted from the server filesystem
                    sfsFile = new File(m_synchronizePath+startResource.getAbsolutePath());
                    if (sfsFile.exists()){
                        sfsFile.delete();
                    }
                    m_synchronizeList.remove(startResource.getAbsolutePath());
                } else {
                    syncFile = m_cms.readFile(resourceName);
                    synchronizeFile(syncFile);
                }
            }
        }
    }

    /**
     * Read all resources from the server filesystem and compare it with the
     * virtual filesystem. Add folders and files that does not exist in VFS
     *
     * @param resourceName the resource to synchronize.
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    private void synchronizeVirtual(String resourceName)
        throws CmsException{
        // read the resources from the server filesystem
        // and add the new resources to the virtual filesystem
        File sfsFile = new File(m_synchronizePath+resourceName);
        //String sfsAbsolutePath = sfsFile.getPath().substring(m_synchronizePath.length()).replace(sfsFile.separatorChar,'/');
        String sfsAbsolutePath = resourceName;
        String[] diskFiles = sfsFile.list();
        File currentFile;
        CmsFile hashFile = null;
        CmsFolder hashFolder = null;
        boolean notCreated = false;
        for (int i = 0; i < diskFiles.length; i++){
            currentFile = new File(sfsFile, diskFiles[i]);
            if (currentFile.isDirectory()){
                //hashFolder = (CmsFolder)m_vfsFolders.get(sfsFile.getPath().replace(sfsFile.separatorChar,'/')+"/"+currentFile.getName()+"/");
                hashFolder = (CmsFolder)m_vfsFolders.get(m_synchronizePath+resourceName+currentFile.getName()+"/");
                if (hashFolder == null){
                    // the folder does not exist in the VFS, so add it from SFS
                    try {
                        m_cms.createFolder(sfsAbsolutePath,currentFile.getName());
                        m_cms.lockResource(sfsAbsolutePath+currentFile.getName()+"/");
                    } catch (CmsException e){
                        // if the folder already exists do nothing
                        if (e.getType() != CmsException.C_FILE_EXISTS &&
                            e.getType() != CmsException.C_LOCKED){
                            if (e.getType() == CmsException.C_BAD_NAME) {
                                notCreated = true;
                                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                                    A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO,"["+this.getClass().getName()+"] Bad name: "+currentFile.getName());
                                }
                            }else{
                                throw e;
                            }
                        }
                    }
                }
                hashFolder = null;
                // now read the filelist of this folder
                if (!notCreated){
                    synchronizeVirtual(sfsAbsolutePath+currentFile.getName()+"/");
                }
            } else {
                hashFile = (CmsFile)m_vfsFiles.get(m_synchronizePath+resourceName+currentFile.getName());
                if (hashFile == null){
                    // the file does not exist in the VFS, so add it from SFS
                    try {
                        byte[] content = getFileBytes(currentFile);
                        String type = getFileType(currentFile.getName());
                        if ((!currentFile.getName().startsWith("$")) &&
                            (!currentFile.getName().startsWith("~")) &&
                            (!currentFile.getName().equals(CmsSynchronizeList.C_SYNCLIST_FILE))){
                            try {
                                CmsFile newFile = m_cms.createFile(sfsAbsolutePath, currentFile.getName(), content, type);
                                m_cms.lockResource(sfsAbsolutePath+currentFile.getName());
                                m_synchronizeList.putDates(sfsAbsolutePath+currentFile.getName(), newFile.getDateLastModified(), currentFile.lastModified());
                            } catch (CmsException e) {
                                if (e.getType() != CmsException.C_FILE_EXISTS &&
                                    e.getType() != CmsException.C_LOCKED){
                                    if(e.getType() == CmsException.C_BAD_NAME){
                                        A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO,"["+this.getClass().getName()+"] Bad name: "+currentFile.getName());
                                    } else {
                                        throw e;
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        throw new CmsException("["+this.getClass().getName()+"]"+" Cannot create file in virtual filesystem",e);
                    }
                }

⌨️ 快捷键说明

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