mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Replaced the file server code with the Alfresco JLAN project.
Restructured the file server code packages. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7757 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
664
source/java/org/alfresco/filesys/state/FileState.java
Normal file
664
source/java/org/alfresco/filesys/state/FileState.java
Normal file
@@ -0,0 +1,664 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.filesys.state;
|
||||
|
||||
import org.alfresco.jlan.locking.FileLock;
|
||||
import org.alfresco.jlan.locking.FileLockList;
|
||||
import org.alfresco.jlan.locking.LockConflictException;
|
||||
import org.alfresco.jlan.locking.NotLockedException;
|
||||
import org.alfresco.jlan.server.filesys.FileOpenParams;
|
||||
import org.alfresco.jlan.server.filesys.FileStatus;
|
||||
import org.alfresco.jlan.server.filesys.pseudo.PseudoFile;
|
||||
import org.alfresco.jlan.server.filesys.pseudo.PseudoFileList;
|
||||
import org.alfresco.jlan.smb.SharingMode;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* File State Class
|
||||
*
|
||||
* <p>Keeps track of file state across all sessions on the server, to keep track of file sharing modes,
|
||||
* file locks and also for synchronizing access to files/folders.
|
||||
*
|
||||
* @author gkspencer
|
||||
*/
|
||||
public class FileState
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(FileState.class);
|
||||
|
||||
// File state constants
|
||||
|
||||
public final static long NoTimeout = -1L;
|
||||
public final static long DefTimeout = 2 * 60000L; // 2 minutes
|
||||
public final static long RenameTimeout = 1 * 60000L; // 1 minute
|
||||
|
||||
// File status
|
||||
|
||||
public enum FileStateStatus { NotExist, FileExists, FolderExists, Renamed };
|
||||
|
||||
// File name/path
|
||||
|
||||
private String m_path;
|
||||
|
||||
// File state timeout, -1 indicates no timeout
|
||||
|
||||
private long m_tmo;
|
||||
|
||||
// File status, indicates if the file/folder exists and if it is a file or folder.
|
||||
|
||||
private FileStateStatus m_fileStatus = FileStateStatus.NotExist;
|
||||
|
||||
// Open file count
|
||||
|
||||
private int m_openCount;
|
||||
|
||||
// Sharing mode
|
||||
|
||||
private int m_sharedAccess = SharingMode.READWRITE;
|
||||
|
||||
// File lock list, allocated once there are active locks on this file
|
||||
|
||||
private FileLockList m_lockList;
|
||||
|
||||
// Node for this file
|
||||
|
||||
private NodeRef m_nodeRef;
|
||||
|
||||
// Link to the new file state when a file is renamed
|
||||
|
||||
private FileState m_newNameState;
|
||||
|
||||
// Pseudo file list
|
||||
|
||||
private PseudoFileList m_pseudoFiles;
|
||||
|
||||
// Last updated time
|
||||
|
||||
private long m_lastUpdate;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param fname String
|
||||
* @param isdir boolean
|
||||
*/
|
||||
public FileState(String fname, boolean isdir)
|
||||
{
|
||||
|
||||
// Normalize the file path
|
||||
|
||||
setPath(fname);
|
||||
setExpiryTime(System.currentTimeMillis() + DefTimeout);
|
||||
|
||||
// Set the file/folder status
|
||||
|
||||
setFileStatus( isdir ? FileStateStatus.FolderExists : FileStateStatus.FileExists);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file name/path
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public final String getPath()
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file status
|
||||
*
|
||||
* @return FileStateStatus
|
||||
*/
|
||||
public final FileStateStatus getFileStatus()
|
||||
{
|
||||
return m_fileStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the file/folder exists
|
||||
*
|
||||
* @return boolen
|
||||
*/
|
||||
public final boolean exists()
|
||||
{
|
||||
if ( m_fileStatus == FileStateStatus.FileExists ||
|
||||
m_fileStatus == FileStateStatus.FolderExists)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the directory state
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean isDirectory()
|
||||
{
|
||||
return m_fileStatus == FileStateStatus.FolderExists ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the associated node has been set
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasNodeRef()
|
||||
{
|
||||
return m_nodeRef != null ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the associated node
|
||||
*
|
||||
* @return NodeRef
|
||||
*/
|
||||
public final NodeRef getNodeRef()
|
||||
{
|
||||
return m_nodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file open count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getOpenCount()
|
||||
{
|
||||
return m_openCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the shared access mode
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getSharedAccess()
|
||||
{
|
||||
return m_sharedAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there are active locks on this file
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasActiveLocks()
|
||||
{
|
||||
if (m_lockList != null && m_lockList.numberOfLocks() > 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this file state does not expire
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasNoTimeout()
|
||||
{
|
||||
return m_tmo == NoTimeout ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the file can be opened depending on any current file opens and the sharing mode of
|
||||
* the first file open
|
||||
*
|
||||
* @param params FileOpenParams
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean allowsOpen(FileOpenParams params)
|
||||
{
|
||||
|
||||
// If the file is not currently open then allow the file open
|
||||
|
||||
if (getOpenCount() == 0)
|
||||
return true;
|
||||
|
||||
// Check the shared access mode
|
||||
|
||||
if (getSharedAccess() == SharingMode.READWRITE && params.getSharedAccess() == SharingMode.READWRITE)
|
||||
return true;
|
||||
else if ((getSharedAccess() & SharingMode.READ) != 0 && params.isReadOnlyAccess())
|
||||
return true;
|
||||
else if ((getSharedAccess() & SharingMode.WRITE) != 0 && params.isWriteOnlyAccess())
|
||||
return true;
|
||||
|
||||
// Sharing violation, do not allow the file open
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the file open count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final synchronized int incrementOpenCount()
|
||||
{
|
||||
return m_openCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement the file open count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final synchronized int decrementOpenCount()
|
||||
{
|
||||
|
||||
// Debug
|
||||
|
||||
if (m_openCount <= 0)
|
||||
logger.debug("@@@@@ File close name=" + getPath() + ", count=" + m_openCount + " <<ERROR>>");
|
||||
else
|
||||
m_openCount--;
|
||||
|
||||
return m_openCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the file state has expired
|
||||
*
|
||||
* @param curTime long
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasExpired(long curTime)
|
||||
{
|
||||
if (m_tmo == NoTimeout)
|
||||
return false;
|
||||
if (curTime > m_tmo)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of seconds left before the file state expires
|
||||
*
|
||||
* @param curTime long
|
||||
* @return long
|
||||
*/
|
||||
public final long getSecondsToExpire(long curTime)
|
||||
{
|
||||
if (m_tmo == NoTimeout)
|
||||
return -1;
|
||||
return (m_tmo - curTime) / 1000L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the file state has an associated rename state
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasRenameState()
|
||||
{
|
||||
return m_newNameState != null ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the associated rename state
|
||||
*
|
||||
* @return FileState
|
||||
*/
|
||||
public final FileState getRenameState()
|
||||
{
|
||||
return m_newNameState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a folder has pseudo files associated with it
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasPseudoFiles()
|
||||
{
|
||||
if ( m_pseudoFiles != null)
|
||||
return m_pseudoFiles.numberOfFiles() > 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the pseudo file list
|
||||
*
|
||||
* @return PseudoFileList
|
||||
*/
|
||||
public final PseudoFileList getPseudoFileList()
|
||||
{
|
||||
return m_pseudoFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a pseudo file to this folder
|
||||
*
|
||||
* @param pfile PseudoFile
|
||||
*/
|
||||
public final void addPseudoFile(PseudoFile pfile)
|
||||
{
|
||||
if ( m_pseudoFiles == null)
|
||||
m_pseudoFiles = new PseudoFileList();
|
||||
m_pseudoFiles.addFile( pfile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the file status
|
||||
*
|
||||
* @param status FileStateStatus
|
||||
*/
|
||||
public final void setFileStatus(FileStateStatus status)
|
||||
{
|
||||
m_fileStatus = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the file status
|
||||
*
|
||||
* @param fsts int
|
||||
*/
|
||||
public final void setFileStatus(int fsts)
|
||||
{
|
||||
if ( fsts == FileStatus.FileExists)
|
||||
m_fileStatus = FileStateStatus.FileExists;
|
||||
else if ( fsts == FileStatus.DirectoryExists)
|
||||
m_fileStatus = FileStateStatus.FolderExists;
|
||||
else if ( fsts == FileStatus.NotExist)
|
||||
m_fileStatus = FileStateStatus.NotExist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the file state expiry time
|
||||
*
|
||||
* @param expire long
|
||||
*/
|
||||
public final void setExpiryTime(long expire)
|
||||
{
|
||||
m_tmo = expire;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the node ref for the file/folder
|
||||
*
|
||||
* @param nodeRef NodeRef
|
||||
*/
|
||||
public final void setNodeRef(NodeRef nodeRef)
|
||||
{
|
||||
m_nodeRef = nodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the associated file state when a file is renamed, this is the link to the new file state
|
||||
*
|
||||
* @param fstate FileState
|
||||
*/
|
||||
public final void setRenameState(FileState fstate)
|
||||
{
|
||||
m_newNameState = fstate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the shared access mode, from the first file open
|
||||
*
|
||||
* @param mode int
|
||||
*/
|
||||
public final void setSharedAccess(int mode)
|
||||
{
|
||||
if (getOpenCount() == 0)
|
||||
m_sharedAccess = mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the file path
|
||||
*
|
||||
* @param path String
|
||||
*/
|
||||
public final void setPath(String path)
|
||||
{
|
||||
|
||||
// Split the path into directories and file name, only uppercase the directories to
|
||||
// normalize the path.
|
||||
|
||||
m_path = normalizePath(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the last updated time
|
||||
*
|
||||
* @return long
|
||||
*/
|
||||
public final long getLastUpdated()
|
||||
{
|
||||
return m_lastUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the last updated time
|
||||
*
|
||||
* @param updateTime long
|
||||
*/
|
||||
public final void setLastUpdated(long updateTime)
|
||||
{
|
||||
m_lastUpdate = updateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the count of active locks on this file
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int numberOfLocks()
|
||||
{
|
||||
if (m_lockList != null)
|
||||
return m_lockList.numberOfLocks();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a lock to this file
|
||||
*
|
||||
* @param lock FileLock
|
||||
* @exception LockConflictException
|
||||
*/
|
||||
public final void addLock(FileLock lock) throws LockConflictException
|
||||
{
|
||||
|
||||
// Check if the lock list has been allocated
|
||||
|
||||
if (m_lockList == null)
|
||||
{
|
||||
|
||||
synchronized (this)
|
||||
{
|
||||
|
||||
// Allocate the lock list, check if the lock list has been allocated elsewhere
|
||||
// as we may have been waiting for the lock
|
||||
|
||||
if (m_lockList == null)
|
||||
m_lockList = new FileLockList();
|
||||
}
|
||||
}
|
||||
|
||||
// Add the lock to the list, check if there are any lock conflicts
|
||||
|
||||
synchronized (m_lockList)
|
||||
{
|
||||
|
||||
// Check if the new lock overlaps with any existing locks
|
||||
|
||||
if (m_lockList.allowsLock(lock))
|
||||
{
|
||||
|
||||
// Add the new lock to the list
|
||||
|
||||
m_lockList.addLock(lock);
|
||||
}
|
||||
else
|
||||
throw new LockConflictException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a lock on this file
|
||||
*
|
||||
* @param lock FileLock
|
||||
* @exception NotLockedException
|
||||
*/
|
||||
public final void removeLock(FileLock lock) throws NotLockedException
|
||||
{
|
||||
|
||||
// Check if the lock list has been allocated
|
||||
|
||||
if (m_lockList == null)
|
||||
throw new NotLockedException();
|
||||
|
||||
// Remove the lock from the active list
|
||||
|
||||
synchronized (m_lockList)
|
||||
{
|
||||
|
||||
// Remove the lock, check if we found the matching lock
|
||||
|
||||
if (m_lockList.removeLock(lock) == null)
|
||||
throw new NotLockedException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the file is readable for the specified section of the file and process id
|
||||
*
|
||||
* @param offset long
|
||||
* @param len long
|
||||
* @param pid int
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean canReadFile(long offset, long len, int pid)
|
||||
{
|
||||
|
||||
// Check if the lock list is valid
|
||||
|
||||
if (m_lockList == null)
|
||||
return true;
|
||||
|
||||
// Check if the file section is readable by the specified process
|
||||
|
||||
boolean readOK = false;
|
||||
|
||||
synchronized (m_lockList)
|
||||
{
|
||||
|
||||
// Check if the file section is readable
|
||||
|
||||
readOK = m_lockList.canReadFile(offset, len, pid);
|
||||
}
|
||||
|
||||
// Return the read status
|
||||
|
||||
return readOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the file is writeable for the specified section of the file and process id
|
||||
*
|
||||
* @param offset long
|
||||
* @param len long
|
||||
* @param pid int
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean canWriteFile(long offset, long len, int pid)
|
||||
{
|
||||
|
||||
// Check if the lock list is valid
|
||||
|
||||
if (m_lockList == null)
|
||||
return true;
|
||||
|
||||
// Check if the file section is writeable by the specified process
|
||||
|
||||
boolean writeOK = false;
|
||||
|
||||
synchronized (m_lockList)
|
||||
{
|
||||
|
||||
// Check if the file section is writeable
|
||||
|
||||
writeOK = m_lockList.canWriteFile(offset, len, pid);
|
||||
}
|
||||
|
||||
// Return the write status
|
||||
|
||||
return writeOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the path to uppercase the directory names and keep the case of the file name.
|
||||
*
|
||||
* @param path String
|
||||
* @return String
|
||||
*/
|
||||
public final static String normalizePath(String path)
|
||||
{
|
||||
return path.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file state as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer str = new StringBuffer();
|
||||
|
||||
str.append("[");
|
||||
str.append(getPath());
|
||||
str.append(",");
|
||||
str.append(getFileStatus());
|
||||
str.append(":Opn=");
|
||||
str.append(getOpenCount());
|
||||
|
||||
str.append(",Expire=");
|
||||
str.append(getSecondsToExpire(System.currentTimeMillis()));
|
||||
|
||||
str.append(",Locks=");
|
||||
str.append(numberOfLocks());
|
||||
|
||||
str.append(",Ref=");
|
||||
if ( hasNodeRef())
|
||||
str.append(getNodeRef().getId());
|
||||
else
|
||||
str.append("Null");
|
||||
|
||||
if ( isDirectory())
|
||||
{
|
||||
str.append(",Pseudo=");
|
||||
if ( hasPseudoFiles())
|
||||
str.append(getPseudoFileList().numberOfFiles());
|
||||
else
|
||||
str.append(0);
|
||||
}
|
||||
str.append("]");
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
170
source/java/org/alfresco/filesys/state/FileStateLockManager.java
Normal file
170
source/java/org/alfresco/filesys/state/FileStateLockManager.java
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.filesys.state;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.alfresco.jlan.locking.FileLock;
|
||||
import org.alfresco.jlan.locking.LockConflictException;
|
||||
import org.alfresco.jlan.locking.NotLockedException;
|
||||
import org.alfresco.jlan.server.SrvSession;
|
||||
import org.alfresco.jlan.server.filesys.NetworkFile;
|
||||
import org.alfresco.jlan.server.filesys.TreeConnection;
|
||||
import org.alfresco.jlan.server.locking.LockManager;
|
||||
import org.alfresco.filesys.alfresco.AlfrescoNetworkFile;
|
||||
|
||||
/**
|
||||
* File State Lock Manager Class
|
||||
*
|
||||
* <p>Implementation of a lock manager that uses the file state cache to track locks on a file.
|
||||
*
|
||||
* @author gkspencer
|
||||
*/
|
||||
public class FileStateLockManager implements LockManager {
|
||||
|
||||
/**
|
||||
* Lock a byte range within a file, or the whole file.
|
||||
*
|
||||
* @param sess SrvSession
|
||||
* @param tree TreeConnection
|
||||
* @param file NetworkFile
|
||||
* @param lock FileLock
|
||||
* @exception LockConflictException
|
||||
* @exception IOException
|
||||
*/
|
||||
public void lockFile(SrvSession sess, TreeConnection tree, NetworkFile file, FileLock lock)
|
||||
throws LockConflictException, IOException {
|
||||
|
||||
// Get the file state associated with the file
|
||||
|
||||
FileState fstate = null;
|
||||
|
||||
if ( file instanceof AlfrescoNetworkFile) {
|
||||
AlfrescoNetworkFile alfFile = (AlfrescoNetworkFile) file;
|
||||
fstate = alfFile.getFileState();
|
||||
}
|
||||
|
||||
if ( fstate == null)
|
||||
throw new IOException("Open file without state (lock)");
|
||||
|
||||
// Add the lock to the active lock list for the file, check if the new lock conflicts with
|
||||
// any existing locks. Add the lock to the file instance so that locks can be removed if the
|
||||
// file is closed/session abnormally terminates.
|
||||
|
||||
fstate.addLock(lock);
|
||||
file.addLock(lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock a byte range within a file, or the whole file
|
||||
*
|
||||
* @param sess SrvSession
|
||||
* @param tree TreeConnection
|
||||
* @param file NetworkFile
|
||||
* @param lock FileLock
|
||||
* @exception NotLockedException
|
||||
* @exception IOException
|
||||
*/
|
||||
public void unlockFile(SrvSession sess, TreeConnection tree, NetworkFile file, FileLock lock)
|
||||
throws NotLockedException, IOException {
|
||||
|
||||
// Get the file state associated with the file
|
||||
|
||||
FileState fstate = null;
|
||||
|
||||
if ( file instanceof AlfrescoNetworkFile) {
|
||||
AlfrescoNetworkFile alfFile = (AlfrescoNetworkFile) file;
|
||||
fstate = alfFile.getFileState();
|
||||
}
|
||||
|
||||
if ( fstate == null)
|
||||
throw new IOException("Open file without state (unlock)");
|
||||
|
||||
// Remove the lock from the active lock list for the file, and the file instance
|
||||
|
||||
fstate.removeLock(lock);
|
||||
file.removeLock(lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a lock object, use the standard FileLock object.
|
||||
*
|
||||
* @param sess SrvSession
|
||||
* @param tree TreeConnection
|
||||
* @param file NetworkFile
|
||||
* @param offset long
|
||||
* @param len long
|
||||
* @param pid int
|
||||
*/
|
||||
public FileLock createLockObject(SrvSession sess, TreeConnection tree, NetworkFile file, long offset, long len, int pid) {
|
||||
|
||||
// Create a lock object to represent the file lock
|
||||
|
||||
return new FileLock(offset, len, pid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Release all locks that a session has on a file. This method is called to perform cleanup if a file
|
||||
* is closed that has active locks or if a session abnormally terminates.
|
||||
*
|
||||
* @param sess SrvSession
|
||||
* @param tree TreeConnection
|
||||
* @param file NetworkFile
|
||||
*/
|
||||
public void releaseLocksForFile(SrvSession sess, TreeConnection tree, NetworkFile file) {
|
||||
|
||||
// Check if the file has active locks
|
||||
|
||||
if ( file.hasLocks())
|
||||
{
|
||||
|
||||
synchronized ( file)
|
||||
{
|
||||
|
||||
// Enumerate the locks and remove
|
||||
|
||||
while ( file.numberOfLocks() > 0)
|
||||
{
|
||||
// Get the current file lock
|
||||
|
||||
FileLock curLock = file.getLockAt(0);
|
||||
|
||||
// Remove the lock, ignore errors
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
// Unlock will remove the lock from the global list and the local files list
|
||||
|
||||
unlockFile(sess, tree, file, curLock);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
221
source/java/org/alfresco/filesys/state/FileStateReaper.java
Normal file
221
source/java/org/alfresco/filesys/state/FileStateReaper.java
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing" */
|
||||
|
||||
package org.alfresco.filesys.state;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* File State Reaper Class
|
||||
*
|
||||
* <p>FileStateTable objects register with the file state reaper to periodically check for expired file states.
|
||||
*
|
||||
* @author gkspencer
|
||||
*/
|
||||
public class FileStateReaper implements Runnable {
|
||||
|
||||
// Logging
|
||||
|
||||
private static final Log logger = LogFactory.getLog(FileStateReaper.class);
|
||||
|
||||
// Default expire check thread interval
|
||||
|
||||
private static final long DEFAULT_EXPIRECHECK = 15000;
|
||||
|
||||
// Wakeup interval for the expire file state checker thread
|
||||
|
||||
private long m_expireInterval = DEFAULT_EXPIRECHECK;
|
||||
|
||||
// File state checker thread
|
||||
|
||||
private Thread m_thread;
|
||||
|
||||
// Shutdown request flag
|
||||
|
||||
private boolean m_shutdown;
|
||||
|
||||
// List of file state tables to be scanned for expired file states
|
||||
|
||||
private Hashtable<String, FileStateTable> m_stateTables;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public FileStateReaper()
|
||||
{
|
||||
// Create the reaper thread
|
||||
|
||||
m_thread = new Thread(this);
|
||||
m_thread.setDaemon(true);
|
||||
m_thread.setName("FileStateReaper");
|
||||
m_thread.start();
|
||||
|
||||
// Create the file state table list
|
||||
|
||||
m_stateTables = new Hashtable<String, FileStateTable>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the expired file state checker interval, in milliseconds
|
||||
*
|
||||
* @return long
|
||||
*/
|
||||
public final long getCheckInterval()
|
||||
{
|
||||
return m_expireInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the expired file state checker interval, in milliseconds
|
||||
*
|
||||
* @param chkIntval long
|
||||
*/
|
||||
public final void setCheckInterval(long chkIntval)
|
||||
{
|
||||
m_expireInterval = chkIntval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a file state table to the reaper list
|
||||
*
|
||||
* @param filesysName String
|
||||
* @param stateTable FileStateTable
|
||||
*/
|
||||
public final void addStateTable( String filesysName, FileStateTable stateTable)
|
||||
{
|
||||
// DEBUG
|
||||
|
||||
if ( logger.isDebugEnabled())
|
||||
logger.debug( "Added file state table for " + filesysName);
|
||||
|
||||
m_stateTables.put( filesysName, stateTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a state table from the reaper list
|
||||
*
|
||||
* @param filesysName String
|
||||
*/
|
||||
public final void removeStateTable( String filesysName)
|
||||
{
|
||||
m_stateTables.remove( filesysName);
|
||||
|
||||
// DEBUG
|
||||
|
||||
if ( logger.isDebugEnabled())
|
||||
logger.debug( "Removed file state table for " + filesysName);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Expired file state checker thread
|
||||
*/
|
||||
public void run()
|
||||
{
|
||||
// Loop forever
|
||||
|
||||
m_shutdown = false;
|
||||
|
||||
while ( m_shutdown == false)
|
||||
{
|
||||
|
||||
// Sleep for the required interval
|
||||
|
||||
try
|
||||
{
|
||||
Thread.sleep(getCheckInterval());
|
||||
}
|
||||
catch (InterruptedException ex)
|
||||
{
|
||||
}
|
||||
|
||||
// Check for shutdown
|
||||
|
||||
if ( m_shutdown == true)
|
||||
{
|
||||
// Debug
|
||||
|
||||
if ( logger.isDebugEnabled())
|
||||
logger.debug("FileStateReaper thread closing");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if there are any state tables registered
|
||||
|
||||
if ( m_stateTables != null && m_stateTables.size() > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Loop through the registered file state tables and remove expired file states
|
||||
|
||||
Enumeration<String> filesysNames = m_stateTables.keys();
|
||||
|
||||
while ( filesysNames.hasMoreElements())
|
||||
{
|
||||
// Get the current filesystem name and associated state table
|
||||
|
||||
String filesysName = filesysNames.nextElement();
|
||||
FileStateTable stateTable = m_stateTables.get( filesysName);
|
||||
|
||||
// Check for expired file states
|
||||
|
||||
int cnt = stateTable.removeExpiredFileStates();
|
||||
|
||||
// Debug
|
||||
|
||||
if (logger.isDebugEnabled() && cnt > 0)
|
||||
logger.debug("Expired " + cnt + " file states for " + filesysName + ", cache=" + stateTable.numberOfStates());
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log errors if not shutting down
|
||||
|
||||
if ( m_shutdown == false)
|
||||
logger.debug(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the file state checker thread to shutdown
|
||||
*/
|
||||
public final void shutdownRequest() {
|
||||
m_shutdown = true;
|
||||
|
||||
if ( m_thread != null)
|
||||
{
|
||||
try {
|
||||
m_thread.interrupt();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
360
source/java/org/alfresco/filesys/state/FileStateTable.java
Normal file
360
source/java/org/alfresco/filesys/state/FileStateTable.java
Normal file
@@ -0,0 +1,360 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.filesys.state;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.apache.commons.logging.*;
|
||||
|
||||
/**
|
||||
* File State Table Class
|
||||
*
|
||||
* <p>Contains an indexed list of the currently open files/folders.
|
||||
*
|
||||
* @author gkspencer
|
||||
*/
|
||||
public class FileStateTable
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(FileStateTable.class);
|
||||
|
||||
// Initial allocation size for the state cache
|
||||
|
||||
private static final int INITIAL_SIZE = 100;
|
||||
|
||||
// File state table, keyed by file path
|
||||
|
||||
private Hashtable<String, FileState> m_stateTable;
|
||||
|
||||
// File state expiry time in seconds
|
||||
|
||||
private long m_cacheTimer = 2 * 60000L; // 2 minutes default
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
public FileStateTable()
|
||||
{
|
||||
m_stateTable = new Hashtable<String, FileState>(INITIAL_SIZE);
|
||||
|
||||
// Start the expired file state checker thread
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file state cache timer, in milliseconds
|
||||
*
|
||||
* @return long
|
||||
*/
|
||||
public final long getCacheTimer()
|
||||
{
|
||||
return m_cacheTimer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of states in the cache
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int numberOfStates()
|
||||
{
|
||||
return m_stateTable.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default file state cache timer, in milliseconds
|
||||
*
|
||||
* @param tmo long
|
||||
*/
|
||||
public final void setCacheTimer(long tmo)
|
||||
{
|
||||
m_cacheTimer = tmo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new file state
|
||||
*
|
||||
* @param fstate FileState
|
||||
*/
|
||||
public final synchronized void addFileState(FileState fstate)
|
||||
{
|
||||
|
||||
// Check if the file state already exists in the cache
|
||||
|
||||
if (logger.isDebugEnabled() && m_stateTable.get(fstate.getPath()) != null)
|
||||
logger.debug("***** addFileState() state=" + fstate.toString() + " - ALREADY IN CACHE *****");
|
||||
|
||||
// DEBUG
|
||||
|
||||
if (logger.isDebugEnabled() && fstate == null)
|
||||
{
|
||||
logger.debug("addFileState() NULL FileState");
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the file state timeout and add to the cache
|
||||
|
||||
fstate.setExpiryTime(System.currentTimeMillis() + getCacheTimer());
|
||||
m_stateTable.put(fstate.getPath(), fstate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the file state for the specified path
|
||||
*
|
||||
* @param path String
|
||||
* @return FileState
|
||||
*/
|
||||
public final synchronized FileState findFileState(String path)
|
||||
{
|
||||
return m_stateTable.get(FileState.normalizePath(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the file state for the specified path, and optionally create a new file state if not
|
||||
* found
|
||||
*
|
||||
* @param path String
|
||||
* @param isdir boolean
|
||||
* @param create boolean
|
||||
* @return FileState
|
||||
*/
|
||||
public final synchronized FileState findFileState(String path, boolean isdir, boolean create)
|
||||
{
|
||||
|
||||
// Find the required file state, if it exists
|
||||
|
||||
FileState state = m_stateTable.get(FileState.normalizePath(path));
|
||||
|
||||
// Check if we should create a new file state
|
||||
|
||||
if (state == null && create == true)
|
||||
{
|
||||
|
||||
// Create a new file state
|
||||
|
||||
state = new FileState(path, isdir);
|
||||
|
||||
// Set the file state timeout and add to the cache
|
||||
|
||||
state.setExpiryTime(System.currentTimeMillis() + getCacheTimer());
|
||||
m_stateTable.put(state.getPath(), state);
|
||||
}
|
||||
|
||||
// Return the file state
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the name that a file state is cached under, and the associated file state
|
||||
*
|
||||
* @param oldName String
|
||||
* @param newName String
|
||||
* @return FileState
|
||||
*/
|
||||
public final synchronized FileState updateFileState(String oldName, String newName)
|
||||
{
|
||||
|
||||
// Find the current file state
|
||||
|
||||
FileState state = m_stateTable.remove(FileState.normalizePath(oldName));
|
||||
|
||||
// Rename the file state and add it back into the cache using the new name
|
||||
|
||||
if (state != null)
|
||||
{
|
||||
state.setPath(newName);
|
||||
addFileState(state);
|
||||
}
|
||||
|
||||
// Return the updated file state
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerate the file state cache
|
||||
*
|
||||
* @return Enumeration<String>
|
||||
*/
|
||||
public final Enumeration<String> enumerate()
|
||||
{
|
||||
return m_stateTable.keys();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the file state for the specified path
|
||||
*
|
||||
* @param path String
|
||||
* @return FileState
|
||||
*/
|
||||
public final synchronized FileState removeFileState(String path)
|
||||
{
|
||||
|
||||
// Remove the file state from the cache
|
||||
|
||||
FileState state = m_stateTable.remove(FileState.normalizePath(path));
|
||||
|
||||
// Return the removed file state
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename a file state, remove the existing entry, update the path and add the state back into
|
||||
* the cache using the new path.
|
||||
*
|
||||
* @param newPath String
|
||||
* @param state FileState
|
||||
*/
|
||||
public final synchronized void renameFileState(String newPath, FileState state)
|
||||
{
|
||||
|
||||
// Remove the existing file state from the cache, using the original name
|
||||
|
||||
m_stateTable.remove(state.getPath());
|
||||
|
||||
// Update the file state path and add it back to the cache using the new name
|
||||
|
||||
state.setPath(FileState.normalizePath(newPath));
|
||||
m_stateTable.put(state.getPath(), state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all file states from the cache
|
||||
*/
|
||||
public final synchronized void removeAllFileStates()
|
||||
{
|
||||
|
||||
// Check if there are any items in the cache
|
||||
|
||||
if (m_stateTable == null || m_stateTable.size() == 0)
|
||||
return;
|
||||
|
||||
// Enumerate the file state cache and remove expired file state objects
|
||||
|
||||
Enumeration<String> enm = m_stateTable.keys();
|
||||
|
||||
while (enm.hasMoreElements())
|
||||
{
|
||||
|
||||
// Get the file state
|
||||
|
||||
FileState state = m_stateTable.get(enm.nextElement());
|
||||
|
||||
// DEBUG
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("++ Closed: " + state.getPath());
|
||||
}
|
||||
|
||||
// Remove all the file states
|
||||
|
||||
m_stateTable.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired file states from the cache
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int removeExpiredFileStates()
|
||||
{
|
||||
|
||||
// Check if there are any items in the cache
|
||||
|
||||
if (m_stateTable == null || m_stateTable.size() == 0)
|
||||
return 0;
|
||||
|
||||
// Enumerate the file state cache and remove expired file state objects
|
||||
|
||||
Enumeration<String> enm = m_stateTable.keys();
|
||||
long curTime = System.currentTimeMillis();
|
||||
|
||||
int expiredCnt = 0;
|
||||
|
||||
while (enm.hasMoreElements())
|
||||
{
|
||||
|
||||
// Get the file state
|
||||
|
||||
FileState state = m_stateTable.get(enm.nextElement());
|
||||
|
||||
if (state != null && state.hasNoTimeout() == false)
|
||||
{
|
||||
|
||||
synchronized (state)
|
||||
{
|
||||
|
||||
// Check if the file state has expired and there are no open references to the
|
||||
// file
|
||||
|
||||
if (state.hasExpired(curTime) && state.getOpenCount() == 0)
|
||||
{
|
||||
|
||||
// Remove the expired file state
|
||||
|
||||
m_stateTable.remove(state.getPath());
|
||||
|
||||
// DEBUG
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Expired file state: " + state);
|
||||
|
||||
// Update the expired count
|
||||
|
||||
expiredCnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the count of expired file states that were removed
|
||||
|
||||
return expiredCnt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the state cache entries to the specified stream
|
||||
*/
|
||||
public final void Dump()
|
||||
{
|
||||
|
||||
// Dump the file state cache entries to the specified stream
|
||||
|
||||
if (m_stateTable.size() > 0)
|
||||
logger.debug("FileStateCache Entries:");
|
||||
|
||||
Enumeration<String> enm = m_stateTable.keys();
|
||||
long curTime = System.currentTimeMillis();
|
||||
|
||||
while (enm.hasMoreElements())
|
||||
{
|
||||
String fname = enm.nextElement();
|
||||
FileState state = m_stateTable.get(fname);
|
||||
|
||||
logger.debug(" " + fname + "(" + state.getSecondsToExpire(curTime) + ") : " + state);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user