/* * Copyright (C) 2005 Alfresco, Inc. * * Licensed under the Mozilla Public License version 1.1 * with a permitted attribution clause. You may obtain a * copy of the License at * * http://www.alfresco.org/legal/license.txt * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific * language governing permissions and limitations under the * License. */ package org.alfresco.filesys.server.state; import org.alfresco.filesys.locking.FileLock; import org.alfresco.filesys.locking.FileLockList; import org.alfresco.filesys.locking.LockConflictException; import org.alfresco.filesys.locking.NotLockedException; import org.alfresco.filesys.server.filesys.FileOpenParams; import org.alfresco.filesys.server.filesys.FileStatus; import org.alfresco.filesys.server.pseudo.PseudoFile; import org.alfresco.filesys.server.pseudo.PseudoFileList; import org.alfresco.filesys.smb.SharingMode; import org.alfresco.service.cmr.repository.NodeRef; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * File State Class * *
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;
/**
* 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 + " <