1 17 package org.alfresco.filesys.locking; 18 19 import java.util.Vector ; 20 21 26 public class FileLockList 27 { 28 29 31 private Vector <FileLock> m_lockList; 32 33 36 public FileLockList() 37 { 38 m_lockList = new Vector <FileLock>(); 39 } 40 41 46 public final void addLock(FileLock lock) 47 { 48 m_lockList.add(lock); 49 } 50 51 57 public final FileLock removeLock(FileLock lock) 58 { 59 return removeLock(lock.getOffset(), lock.getLength(), lock.getProcessId()); 60 } 61 62 70 public final FileLock removeLock(long offset, long len, int pid) 71 { 72 73 75 if (numberOfLocks() == 0) 76 return null; 77 78 80 for (int i = 0; i < numberOfLocks(); i++) 81 { 82 83 85 FileLock curLock = getLockAt(i); 86 if (curLock.getOffset() == offset && curLock.getLength() == len && curLock.getProcessId() == pid) 87 { 88 89 91 m_lockList.removeElementAt(i); 92 return curLock; 93 } 94 } 95 96 98 return null; 99 } 100 101 104 public final void removeAllLocks() 105 { 106 m_lockList.removeAllElements(); 107 } 108 109 115 public final FileLock getLockAt(int idx) 116 { 117 if (idx < m_lockList.size()) 118 return m_lockList.elementAt(idx); 119 return null; 120 } 121 122 128 public final boolean allowsLock(FileLock lock) 129 { 130 131 133 if (numberOfLocks() == 0) 134 return true; 135 136 138 for (int i = 0; i < numberOfLocks(); i++) 139 { 140 141 143 FileLock curLock = getLockAt(i); 144 if (curLock.hasOverlap(lock)) 145 return false; 146 } 147 148 150 return true; 151 } 152 153 161 public final boolean canReadFile(long offset, long len, int pid) 162 { 163 164 166 if (numberOfLocks() == 0) 167 return true; 168 169 171 for (int i = 0; i < numberOfLocks(); i++) 172 { 173 174 176 FileLock curLock = getLockAt(i); 177 178 180 if (curLock.getProcessId() != pid) 181 { 182 183 185 if (curLock.hasOverlap(offset, len) == true) 186 return false; 187 } 188 } 189 190 192 return true; 193 } 194 195 203 public final boolean canWriteFile(long offset, long len, int pid) 204 { 205 206 208 if (numberOfLocks() == 0) 209 return true; 210 211 213 for (int i = 0; i < numberOfLocks(); i++) 214 { 215 216 218 FileLock curLock = getLockAt(i); 219 220 222 if (curLock.getProcessId() != pid) 223 { 224 225 227 if (curLock.hasOverlap(offset, len) == true) 228 return false; 229 } 230 } 231 232 234 return true; 235 } 236 237 242 public final int numberOfLocks() 243 { 244 return m_lockList.size(); 245 } 246 } | Popular Tags |