KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > locking > FileLockList


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.locking;
18
19 import java.util.Vector JavaDoc;
20
21 /**
22  * File Lock List Class
23  * <p>
24  * Contains a list of the current locks on a file.
25  */

26 public class FileLockList
27 {
28
29     // List of file locks
30

31     private Vector JavaDoc<FileLock> m_lockList;
32
33     /**
34      * Construct an empty file lock list.
35      */

36     public FileLockList()
37     {
38         m_lockList = new Vector JavaDoc<FileLock>();
39     }
40
41     /**
42      * Add a lock to the list
43      *
44      * @param lock Lock to be added to the list.
45      */

46     public final void addLock(FileLock lock)
47     {
48         m_lockList.add(lock);
49     }
50
51     /**
52      * Remove a lock from the list
53      *
54      * @param lock FileLock
55      * @return FileLock
56      */

57     public final FileLock removeLock(FileLock lock)
58     {
59         return removeLock(lock.getOffset(), lock.getLength(), lock.getProcessId());
60     }
61
62     /**
63      * Remove a lock from the list
64      *
65      * @param long offset Starting offset of the lock
66      * @param long len Locked section length
67      * @param int pid Owner process id
68      * @return FileLock
69      */

70     public final FileLock removeLock(long offset, long len, int pid)
71     {
72
73         // Check if there are any locks in the list
74

75         if (numberOfLocks() == 0)
76             return null;
77
78         // Search for the required lock
79

80         for (int i = 0; i < numberOfLocks(); i++)
81         {
82
83             // Get the current lock details
84

85             FileLock curLock = getLockAt(i);
86             if (curLock.getOffset() == offset && curLock.getLength() == len && curLock.getProcessId() == pid)
87             {
88
89                 // Remove the lock from the list
90

91                 m_lockList.removeElementAt(i);
92                 return curLock;
93             }
94         }
95
96         // Lock not found
97

98         return null;
99     }
100
101     /**
102      * Remove all locks from the list
103      */

104     public final void removeAllLocks()
105     {
106         m_lockList.removeAllElements();
107     }
108
109     /**
110      * Return the specified lock details
111      *
112      * @param int Lock index
113      * @return FileLock
114      */

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     /**
123      * Check if the new lock should be allowed by comparing with the locks in the list.
124      *
125      * @param lock FileLock
126      * @return boolean true if the lock can be granted, else false.
127      */

128     public final boolean allowsLock(FileLock lock)
129     {
130
131         // If the list is empty we can allow the lock request
132

133         if (numberOfLocks() == 0)
134             return true;
135
136         // Search for any overlapping locks
137

138         for (int i = 0; i < numberOfLocks(); i++)
139         {
140
141             // Get the current lock details
142

143             FileLock curLock = getLockAt(i);
144             if (curLock.hasOverlap(lock))
145                 return false;
146         }
147
148         // The lock does not overlap with any existing locks
149

150         return true;
151     }
152
153     /**
154      * Check if the file is readable for the specified section of the file and process id
155      *
156      * @param offset long
157      * @param len long
158      * @param pid int
159      * @return boolean
160      */

161     public final boolean canReadFile(long offset, long len, int pid)
162     {
163
164         // If the list is empty we can allow the read request
165

166         if (numberOfLocks() == 0)
167             return true;
168
169         // Search for a lock that prevents the read
170

171         for (int i = 0; i < numberOfLocks(); i++)
172         {
173
174             // Get the current lock details
175

176             FileLock curLock = getLockAt(i);
177
178             // Check if the process owns the lock, if not then check if there is an overlap
179

180             if (curLock.getProcessId() != pid)
181             {
182
183                 // Check if the read overlaps with the locked area
184

185                 if (curLock.hasOverlap(offset, len) == true)
186                     return false;
187             }
188         }
189
190         // The lock does not overlap with any existing locks
191

192         return true;
193     }
194
195     /**
196      * Check if the file is writeable for the specified section of the file and process id
197      *
198      * @param offset long
199      * @param len long
200      * @param pid int
201      * @return boolean
202      */

203     public final boolean canWriteFile(long offset, long len, int pid)
204     {
205
206         // If the list is empty we can allow the read request
207

208         if (numberOfLocks() == 0)
209             return true;
210
211         // Search for a lock that prevents the read
212

213         for (int i = 0; i < numberOfLocks(); i++)
214         {
215
216             // Get the current lock details
217

218             FileLock curLock = getLockAt(i);
219
220             // Check if the process owns the lock, if not then check if there is an overlap
221

222             if (curLock.getProcessId() != pid)
223             {
224
225                 // Check if the read overlaps with the locked area
226

227                 if (curLock.hasOverlap(offset, len) == true)
228                     return false;
229             }
230         }
231
232         // The lock does not overlap with any existing locks
233

234         return true;
235     }
236
237     /**
238      * Return the count of locks in the list.
239      *
240      * @return int Number of locks in the list.
241      */

242     public final int numberOfLocks()
243     {
244         return m_lockList.size();
245     }
246 }
Popular Tags