mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Moving to root below branch label
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
187
source/java/org/alfresco/filesys/locking/FileLock.java
Normal file
187
source/java/org/alfresco/filesys/locking/FileLock.java
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* 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.locking;
|
||||
|
||||
/**
|
||||
* File Lock Class
|
||||
* <p>
|
||||
* Contains the details of a single file lock.
|
||||
*/
|
||||
public class FileLock
|
||||
{
|
||||
|
||||
// Constants
|
||||
|
||||
public static final long LockWholeFile = 0xFFFFFFFFFFFFFFFFL;
|
||||
|
||||
// Start lock offset and length
|
||||
|
||||
private long m_offset;
|
||||
private long m_length;
|
||||
|
||||
// Owner process id
|
||||
|
||||
private int m_pid;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param offset long
|
||||
* @param len long
|
||||
* @param pid int
|
||||
*/
|
||||
public FileLock(long offset, long len, int pid)
|
||||
{
|
||||
setOffset(offset);
|
||||
setLength(len);
|
||||
setProcessId(pid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the starting lock offset
|
||||
*
|
||||
* @return long Starting lock offset.
|
||||
*/
|
||||
public final long getOffset()
|
||||
{
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the starting lock offset.
|
||||
*
|
||||
* @param long Starting lock offset
|
||||
*/
|
||||
public final void setOffset(long offset)
|
||||
{
|
||||
m_offset = offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the locked section length
|
||||
*
|
||||
* @return long Locked section length
|
||||
*/
|
||||
public final long getLength()
|
||||
{
|
||||
return m_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the locked section length
|
||||
*
|
||||
* @param long Locked section length
|
||||
*/
|
||||
public final void setLength(long len)
|
||||
{
|
||||
if (len < 0)
|
||||
m_length = LockWholeFile;
|
||||
else
|
||||
m_length = len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the owner process id for the lock
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getProcessId()
|
||||
{
|
||||
return m_pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterine if the lock is locking the whole file
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean isWholeFile()
|
||||
{
|
||||
return m_length == LockWholeFile ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the process id of the owner of this lock
|
||||
*
|
||||
* @param pid int
|
||||
*/
|
||||
public final void setProcessId(int pid)
|
||||
{
|
||||
m_pid = pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the specified locks byte range overlaps this locks byte range.
|
||||
*
|
||||
* @param lock FileLock
|
||||
*/
|
||||
public final boolean hasOverlap(FileLock lock)
|
||||
{
|
||||
return hasOverlap(lock.getOffset(), lock.getLength());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the specified locks byte range overlaps this locks byte range.
|
||||
*
|
||||
* @param offset long
|
||||
* @param len long
|
||||
*/
|
||||
public final boolean hasOverlap(long offset, long len)
|
||||
{
|
||||
|
||||
// Check if the lock is for the whole file
|
||||
|
||||
if (isWholeFile())
|
||||
return true;
|
||||
|
||||
// Check if the locks overlap
|
||||
|
||||
long endOff = getOffset() + getLength();
|
||||
|
||||
if (getOffset() < offset && endOff < offset)
|
||||
return false;
|
||||
|
||||
endOff = offset + len;
|
||||
|
||||
if (getOffset() > endOff)
|
||||
return false;
|
||||
|
||||
// Locks overlap
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the lock details as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer str = new StringBuffer();
|
||||
|
||||
str.append("[PID=");
|
||||
str.append(getProcessId());
|
||||
str.append(",Offset=");
|
||||
str.append(getOffset());
|
||||
str.append(",Len=");
|
||||
str.append(getLength());
|
||||
str.append("]");
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.locking;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* File Lock Exception Class
|
||||
*/
|
||||
public class FileLockException extends IOException
|
||||
{
|
||||
private static final long serialVersionUID = 3257845472092893751L;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public FileLockException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param s java.lang.String
|
||||
*/
|
||||
public FileLockException(String s)
|
||||
{
|
||||
super(s);
|
||||
}
|
||||
}
|
246
source/java/org/alfresco/filesys/locking/FileLockList.java
Normal file
246
source/java/org/alfresco/filesys/locking/FileLockList.java
Normal file
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* 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.locking;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* File Lock List Class
|
||||
* <p>
|
||||
* Contains a list of the current locks on a file.
|
||||
*/
|
||||
public class FileLockList
|
||||
{
|
||||
|
||||
// List of file locks
|
||||
|
||||
private Vector<FileLock> m_lockList;
|
||||
|
||||
/**
|
||||
* Construct an empty file lock list.
|
||||
*/
|
||||
public FileLockList()
|
||||
{
|
||||
m_lockList = new Vector<FileLock>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a lock to the list
|
||||
*
|
||||
* @param lock Lock to be added to the list.
|
||||
*/
|
||||
public final void addLock(FileLock lock)
|
||||
{
|
||||
m_lockList.add(lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a lock from the list
|
||||
*
|
||||
* @param lock FileLock
|
||||
* @return FileLock
|
||||
*/
|
||||
public final FileLock removeLock(FileLock lock)
|
||||
{
|
||||
return removeLock(lock.getOffset(), lock.getLength(), lock.getProcessId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a lock from the list
|
||||
*
|
||||
* @param long offset Starting offset of the lock
|
||||
* @param long len Locked section length
|
||||
* @param int pid Owner process id
|
||||
* @return FileLock
|
||||
*/
|
||||
public final FileLock removeLock(long offset, long len, int pid)
|
||||
{
|
||||
|
||||
// Check if there are any locks in the list
|
||||
|
||||
if (numberOfLocks() == 0)
|
||||
return null;
|
||||
|
||||
// Search for the required lock
|
||||
|
||||
for (int i = 0; i < numberOfLocks(); i++)
|
||||
{
|
||||
|
||||
// Get the current lock details
|
||||
|
||||
FileLock curLock = getLockAt(i);
|
||||
if (curLock.getOffset() == offset && curLock.getLength() == len && curLock.getProcessId() == pid)
|
||||
{
|
||||
|
||||
// Remove the lock from the list
|
||||
|
||||
m_lockList.removeElementAt(i);
|
||||
return curLock;
|
||||
}
|
||||
}
|
||||
|
||||
// Lock not found
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all locks from the list
|
||||
*/
|
||||
public final void removeAllLocks()
|
||||
{
|
||||
m_lockList.removeAllElements();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the specified lock details
|
||||
*
|
||||
* @param int Lock index
|
||||
* @return FileLock
|
||||
*/
|
||||
public final FileLock getLockAt(int idx)
|
||||
{
|
||||
if (idx < m_lockList.size())
|
||||
return m_lockList.elementAt(idx);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the new lock should be allowed by comparing with the locks in the list.
|
||||
*
|
||||
* @param lock FileLock
|
||||
* @return boolean true if the lock can be granted, else false.
|
||||
*/
|
||||
public final boolean allowsLock(FileLock lock)
|
||||
{
|
||||
|
||||
// If the list is empty we can allow the lock request
|
||||
|
||||
if (numberOfLocks() == 0)
|
||||
return true;
|
||||
|
||||
// Search for any overlapping locks
|
||||
|
||||
for (int i = 0; i < numberOfLocks(); i++)
|
||||
{
|
||||
|
||||
// Get the current lock details
|
||||
|
||||
FileLock curLock = getLockAt(i);
|
||||
if (curLock.hasOverlap(lock))
|
||||
return false;
|
||||
}
|
||||
|
||||
// The lock does not overlap with any existing locks
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
|
||||
// If the list is empty we can allow the read request
|
||||
|
||||
if (numberOfLocks() == 0)
|
||||
return true;
|
||||
|
||||
// Search for a lock that prevents the read
|
||||
|
||||
for (int i = 0; i < numberOfLocks(); i++)
|
||||
{
|
||||
|
||||
// Get the current lock details
|
||||
|
||||
FileLock curLock = getLockAt(i);
|
||||
|
||||
// Check if the process owns the lock, if not then check if there is an overlap
|
||||
|
||||
if (curLock.getProcessId() != pid)
|
||||
{
|
||||
|
||||
// Check if the read overlaps with the locked area
|
||||
|
||||
if (curLock.hasOverlap(offset, len) == true)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// The lock does not overlap with any existing locks
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
|
||||
// If the list is empty we can allow the read request
|
||||
|
||||
if (numberOfLocks() == 0)
|
||||
return true;
|
||||
|
||||
// Search for a lock that prevents the read
|
||||
|
||||
for (int i = 0; i < numberOfLocks(); i++)
|
||||
{
|
||||
|
||||
// Get the current lock details
|
||||
|
||||
FileLock curLock = getLockAt(i);
|
||||
|
||||
// Check if the process owns the lock, if not then check if there is an overlap
|
||||
|
||||
if (curLock.getProcessId() != pid)
|
||||
{
|
||||
|
||||
// Check if the read overlaps with the locked area
|
||||
|
||||
if (curLock.hasOverlap(offset, len) == true)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// The lock does not overlap with any existing locks
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the count of locks in the list.
|
||||
*
|
||||
* @return int Number of locks in the list.
|
||||
*/
|
||||
public final int numberOfLocks()
|
||||
{
|
||||
return m_lockList.size();
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.locking;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* File Unlock Exception Class
|
||||
*/
|
||||
public class FileUnlockException extends IOException
|
||||
{
|
||||
private static final long serialVersionUID = 3257290240262484786L;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public FileUnlockException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param s java.lang.String
|
||||
*/
|
||||
public FileUnlockException(String s)
|
||||
{
|
||||
super(s);
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.locking;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Lock Conflict Exception Class
|
||||
* <p>
|
||||
* Thrown when a lock request overlaps with an existing lock on a file.
|
||||
*/
|
||||
public class LockConflictException extends IOException
|
||||
{
|
||||
|
||||
// Serializable version id
|
||||
|
||||
private static final long serialVersionUID = 0;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public LockConflictException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param s java.lang.String
|
||||
*/
|
||||
public LockConflictException(String s)
|
||||
{
|
||||
super(s);
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.locking;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Not Locked Exception Class
|
||||
* <p>
|
||||
* Thrown when an unlock request is received that has not active lock on a file.
|
||||
*/
|
||||
public class NotLockedException extends IOException
|
||||
{
|
||||
private static final long serialVersionUID = 3834594296543261488L;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public NotLockedException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param s java.lang.String
|
||||
*/
|
||||
public NotLockedException(String s)
|
||||
{
|
||||
super(s);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user