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

📄 davlockmanager.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
package com.sslexplorer.vfs.webdav;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DAVLockManager {

	/**
	 * A simple class to maintain global locks on files. Locks 
	 * should only be requested when a resource is being edited.
	 * 
	 * TODO: A user interface to enable locks to be removed?
	 */
	static DAVLockManager instance;
	
	static Log log = LogFactory.getLog(DAVLockManager.class);
	HashMap nonexclusiveLocks = new HashMap();
    HashMap exclusiveLocks = new HashMap();
    
	DAVLockManager() {
	}
	
	public static DAVLockManager getInstance() {
		return instance == null ? instance = new DAVLockManager() : instance;
	}
	
	
	public synchronized void lock(DAVResource resource, String username, boolean exclusive) throws LockedException, IOException {
		
		String key = resource.getFile().getName().getURI();
		
		if(log.isInfoEnabled())
			log.info("Attempting to " + (exclusive ? "exclusively" : "non-exclusively" ) + " lock resource " + key);
		
		if(resource.isCollection()) {
			if(exclusiveLocks.containsKey(key)) {
				ExclusiveLock lock = (ExclusiveLock) exclusiveLocks.get(key);
				throw new LockedException("Folder \"" + resource.getWebFolderPath() + "\" is currently locked exclusively by user " + lock.getUsername());
			} else if(nonexclusiveLocks.containsKey(key)) {
				NonExclusiveLock lock = (NonExclusiveLock) nonexclusiveLocks.get(key);
				
				if(exclusive)
					throw new LockedException("Cannot lock folder \"" + resource.getWebFolderPath() + "\" exclusively; there are currently " + lock.getUsernames().size() + " users using it");
				
				lock.incrementLock(username);
				
			} else if(exclusive) {
				exclusiveLocks.put(key, new ExclusiveLock(resource, username));
			} else {
				nonexclusiveLocks.put(key, new NonExclusiveLock(resource, username));
			}
			

		} else {

			// First of all try to soft lock the parent
			lock(resource.getParent(), username, false);
			
			if(exclusiveLocks.containsKey(key)) {
				ExclusiveLock lock = (ExclusiveLock) exclusiveLocks.get(key);
				throw new LockedException("File is currently locked exclusively by user " + lock.getUsername());
			} else if(nonexclusiveLocks.containsKey(key)) {
				NonExclusiveLock lock = (NonExclusiveLock) nonexclusiveLocks.get(key);
				
				if(exclusive)
					throw new LockedException("Cannot lock file exclusivley; there are currently " + lock.getUsernames().size() + " users using it");
				
				lock.incrementLock(username);
				
			} else if(exclusive) {
				exclusiveLocks.put(key, new ExclusiveLock(resource, username));
			} else {
				nonexclusiveLocks.put(key, new NonExclusiveLock(resource, username));
			}			
			
		}
		
		if(log.isInfoEnabled())
			log.info("Locked " + key);		
		
	}
	
	public synchronized void unlock(DAVResource resource, String username) throws IOException {
		
		
		if(!resource.isCollection())
			unlock(resource.getParent(), username);
		
		String key = resource.getFile().getName().getURI();
		
		
		if(log.isInfoEnabled())
			log.info("Unlocking resource " + key);
		
		if(exclusiveLocks.containsKey(key)) {
			
			if(log.isDebugEnabled())
				log.debug("Exclusive lock for " + key + " has been removed");
			exclusiveLocks.remove(key);
			
		} else if(nonexclusiveLocks.containsKey(key)) {
			NonExclusiveLock lock = (NonExclusiveLock) nonexclusiveLocks.get(key);
			
			if(log.isDebugEnabled())
				log.debug("There are " + lock.count + " non-exclusive locks remaining.. decrementing by 1");

			if(lock.decrementLock(username)) {
				if(log.isInfoEnabled())
					log.info("All non-exclusive locks for " + key + " has been removed");
				nonexclusiveLocks.remove(key);
			}
		}
		
	}
	
	public Map getLocks() {
		return (Map)nonexclusiveLocks.clone();
	}
	
	
	class ExclusiveLock {
		DAVResource resource;
		String username;
		
		ExclusiveLock(DAVResource resource, String username) {
			this.resource = resource;
			this.username = username;
		}
		
		
		String getUsername() {
			return username;
		}
		
		DAVResource getResource() {
			return resource;
		}
	}
	
	class NonExclusiveLock {
		DAVResource resource;
		int count;
		ArrayList usernames;
		
		NonExclusiveLock(DAVResource resource, String username) {
			this.resource = resource;
			this.usernames = new ArrayList();
			this.usernames.add(username);
			this.count = 1;
		}
		
		void incrementLock(String username) {
			this.count++;
			usernames.add(username);
		}
		
		boolean decrementLock(String username) throws IOException {
			
			if(count==0)
				throw new IOException("Cannot unlock resource as there are no outstanding locks");
			
			usernames.remove(usernames.indexOf(username));
			this.count--;
			return count == 0;
		}
		
		List getUsernames() {
			return (List) usernames.clone();
		}
		
		DAVResource getResource() {
			return resource;
		}		
	}
}

⌨️ 快捷键说明

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