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

📄 cmslockmanager.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/lock/CmsLockManager.java,v $
 * Date   : $Date: 2006/03/27 14:52:51 $
 * Version: $Revision: 1.37 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * 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 Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project 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 org.opencms.lock;

import org.opencms.db.CmsDbContext;
import org.opencms.db.CmsDriverManager;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.main.CmsException;
import org.opencms.util.CmsUUID;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * The CmsLockManager is used by the Cms application to detect 
 * the lock state of a resource.<p>
 * 
 * The lock state depends on the path of the resource, and probably 
 * locked parent folders. The result of a query to the lock manager
 * are instances of CmsLock objects.<p>
 * 
 * @author Michael Emmerich 
 * @author Thomas Weckert  
 * @author Andreas Zahner  
 * 
 * @version $Revision: 1.37 $ 
 * 
 * @since 6.0.0 
 * 
 * @see org.opencms.file.CmsObject#getLock(CmsResource)
 * @see org.opencms.lock.CmsLock
 */
public final class CmsLockManager {

    /** The shared lock manager instance. */
    private static CmsLockManager sharedInstance;

    /** A map holding the exclusive CmsLocks. */
    private Map m_exclusiveLocks;

    /**
     * Default constructor.<p>
     */
    private CmsLockManager() {

        super();
        m_exclusiveLocks = Collections.synchronizedMap(new HashMap());
    }
    
    /**
     * Returns the shared instance of the lock manager.<p>
     * 
     * @return the shared instance of the lock manager
     */
    public static CmsLockManager getInstance() {

        if (sharedInstance == null) {            
            // initialize the shared instance 
            sharedInstance = new CmsLockManager();
        }
        return sharedInstance;
    }

    /**
     * Adds a resource to the lock manager.<p>
     * 
     * @param driverManager the driver manager
     * @param dbc the current database context
     * @param resource the resource
     * @param userId the ID of the user who locked the resource
     * @param projectId the ID of the project where the resource is locked
     * @param mode flag indicating the mode (temporary or common) of a lock
     * 
     * @throws CmsLockException if the resource is locked
     * @throws CmsException if somethong goes wrong
     */
    public void addResource(
        CmsDriverManager driverManager,
        CmsDbContext dbc,
        CmsResource resource,
        CmsUUID userId,
        int projectId,
        int mode) throws CmsLockException, CmsException {

        CmsLock lock = getLock(driverManager, dbc, resource);
        String resourceName = resource.getRootPath();

        if (!lock.isNullLock() && !lock.getUserId().equals(dbc.currentUser().getId())) {
            throw new CmsLockException(Messages.get().container(
                Messages.ERR_RESOURCE_LOCKED_1,
                dbc.getRequestContext().getSitePath(resource)));
        }

        if (lock.isNullLock()) {
            // create a new exclusive lock unless the resource has already a shared lock due to a
            // exclusive locked sibling
            CmsLock newLock = new CmsLock(resourceName, userId, projectId, CmsLock.TYPE_EXCLUSIVE, mode);
            m_exclusiveLocks.put(resourceName, newLock);
        }

        // handle collisions with exclusive locked sub-resources in case of a folder
        if (CmsResource.isFolder(resourceName)) {
            Iterator i = m_exclusiveLocks.keySet().iterator();
            String lockedPath = null;

            while (i.hasNext()) {
                lockedPath = (String)i.next();

                if (lockedPath.startsWith(resourceName) && !lockedPath.equals(resourceName)) {
                    i.remove();
                }
            }
        }
    }

    /**
     * Counts the exclusive locked resources inside a folder.<p>
     * 
     * @param foldername the folder
     * 
     * @return the number of exclusive locked resources in the specified folder
     */
    public int countExclusiveLocksInFolder(String foldername) {

        Iterator i = m_exclusiveLocks.values().iterator();
        CmsLock lock = null;
        int count = 0;

        while (i.hasNext()) {
            lock = (CmsLock)i.next();
            if (lock.getResourceName().startsWith(foldername)) {
                count++;
            }
        }

        return count;
    }

    /**
     * Counts the exclusive locked resources in a project.<p>
     * 
     * @param project the project
     * 
     * @return the number of exclusive locked resources in the specified project
     */
    public int countExclusiveLocksInProject(CmsProject project) {

        Iterator i = m_exclusiveLocks.values().iterator();
        CmsLock lock = null;
        int count = 0;

        while (i.hasNext()) {
            lock = (CmsLock)i.next();
            if (lock.getProjectId() == project.getId()) {
                count++;
            }
        }

        return count;
    }

    /**
     * Returns the lock for a resource.<p>
     * 
     * @param driverManager the driver manager
     * @param dbc the current database context
     * @param resource the resource
     * 
     * @return the CmsLock if the specified resource is locked, or the shared Null lock if the resource is not locked
     * @throws CmsException if something goes wrong
     */
    public CmsLock getLock(CmsDriverManager driverManager, CmsDbContext dbc, CmsResource resource) throws CmsException {

        String resourcename = resource.getRootPath();

        // check some abort conditions first

        if (dbc.currentProject().getId() == CmsProject.ONLINE_PROJECT_ID) {
            // resources are never locked in the online project
            return CmsLock.getNullLock();
        }

        if (resource == null) {
            // non-existent resources are never locked
            return CmsLock.getNullLock();
        }

        // try to find an exclusive lock

        if (m_exclusiveLocks.containsKey(resourcename)) {
            // the resource is exclusive locked
            return (CmsLock)m_exclusiveLocks.get(resourcename);
        }

        // calculate the lock state

        // fetch all siblings of the resource to the same content record
        List siblings = internalReadSiblings(driverManager, dbc, resource);

        CmsLock siblingLock;
        CmsResource sibling;

        CmsLock parentFolderLock = getParentFolderLock(resourcename);
        if (parentFolderLock == null) {
            // all parent folders are unlocked

            for (int i = 0; i < siblings.size(); i++) {
                sibling = (CmsResource)siblings.get(i);
                siblingLock = (CmsLock)m_exclusiveLocks.get(sibling.getRootPath());

                if (siblingLock != null) {
                    // a sibling is already exclusive locked
                    return new CmsLock(
                        resourcename,
                        siblingLock.getUserId(),
                        siblingLock.getProjectId(),
                        CmsLock.TYPE_SHARED_EXCLUSIVE);
                }
            }

            // no locked siblings found
            return CmsLock.getNullLock();
        } else {
            // a parent folder is locked

            for (int i = 0; i < siblings.size(); i++) {
                sibling = (CmsResource)siblings.get(i);

                if (m_exclusiveLocks.containsKey(sibling.getRootPath())) {
                    // a sibling is already exclusive locked
                    return new CmsLock(
                        resourcename,
                        parentFolderLock.getUserId(),
                        parentFolderLock.getProjectId(),
                        CmsLock.TYPE_SHARED_INHERITED);
                }
            }

            // no locked siblings found
            return new CmsLock(
                resourcename,
                parentFolderLock.getUserId(),
                parentFolderLock.getProjectId(),
                CmsLock.TYPE_INHERITED);
        }
    }

⌨️ 快捷键说明

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