KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > repo > FileStateTable


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.smb.server.repo;
18
19 import java.util.*;
20 import java.io.*;
21
22 import org.apache.commons.logging.*;
23
24 /**
25  * File State Table Class
26  * <p>
27  * Contains an indexed list of the currently open files/folders.
28  */

29 public class FileStateTable implements Runnable JavaDoc
30 {
31     private static final Log logger = LogFactory.getLog(FileStateTable.class);
32
33     // Initial allocation size for the state cache
34

35     private static final int INITIAL_SIZE = 100;
36
37     // Default expire check thread interval
38

39     private static final long DEFAULT_EXPIRECHECK = 15000;
40
41     // File state table, keyed by file path
42

43     private Hashtable<String JavaDoc, FileState> m_stateTable;
44
45     // Wakeup interval for the expire file state checker thread
46

47     private long m_expireInterval = DEFAULT_EXPIRECHECK;
48
49     // File state expiry time in seconds
50

51     private long m_cacheTimer = 2 * 60000L; // 2 minutes default
52

53     /**
54      * Class constructor
55      */

56     public FileStateTable()
57     {
58         m_stateTable = new Hashtable<String JavaDoc, FileState>(INITIAL_SIZE);
59
60         // Start the expired file state checker thread
61

62         Thread JavaDoc th = new Thread JavaDoc(this);
63         th.setDaemon(true);
64         th.setName("FileStateExpire");
65         th.start();
66     }
67
68     /**
69      * Return the expired file state checker interval, in milliseconds
70      *
71      * @return long
72      */

73     public final long getCheckInterval()
74     {
75         return m_expireInterval;
76     }
77
78     /**
79      * Get the file state cache timer, in milliseconds
80      *
81      * @return long
82      */

83     public final long getCacheTimer()
84     {
85         return m_cacheTimer;
86     }
87
88     /**
89      * Return the number of states in the cache
90      *
91      * @return int
92      */

93     public final int numberOfStates()
94     {
95         return m_stateTable.size();
96     }
97
98     /**
99      * Set the default file state cache timer, in milliseconds
100      *
101      * @param tmo long
102      */

103     public final void setCacheTimer(long tmo)
104     {
105         m_cacheTimer = tmo;
106     }
107
108     /**
109      * Set the expired file state checker interval, in milliseconds
110      *
111      * @param chkIntval long
112      */

113     public final void setCheckInterval(long chkIntval)
114     {
115         m_expireInterval = chkIntval;
116     }
117
118     /**
119      * Add a new file state
120      *
121      * @param fstate FileState
122      */

123     public final synchronized void addFileState(FileState fstate)
124     {
125
126         // Check if the file state already exists in the cache
127

128         if (logger.isDebugEnabled() && m_stateTable.get(fstate.getPath()) != null)
129             logger.debug("***** addFileState() state=" + fstate.toString() + " - ALREADY IN CACHE *****");
130
131         // DEBUG
132

133         if (logger.isDebugEnabled() && fstate == null)
134         {
135             logger.debug("addFileState() NULL FileState");
136             return;
137         }
138
139         // Set the file state timeout and add to the cache
140

141         fstate.setExpiryTime(System.currentTimeMillis() + getCacheTimer());
142         m_stateTable.put(fstate.getPath(), fstate);
143     }
144
145     /**
146      * Find the file state for the specified path
147      *
148      * @param path String
149      * @return FileState
150      */

151     public final synchronized FileState findFileState(String JavaDoc path)
152     {
153         return m_stateTable.get(FileState.normalizePath(path));
154     }
155
156     /**
157      * Find the file state for the specified path, and optionally create a new file state if not
158      * found
159      *
160      * @param path String
161      * @param isdir boolean
162      * @param create boolean
163      * @return FileState
164      */

165     public final synchronized FileState findFileState(String JavaDoc path, boolean isdir, boolean create)
166     {
167
168         // Find the required file state, if it exists
169

170         FileState state = m_stateTable.get(FileState.normalizePath(path));
171
172         // Check if we should create a new file state
173

174         if (state == null && create == true)
175         {
176
177             // Create a new file state
178

179             state = new FileState(path, isdir);
180
181             // Set the file state timeout and add to the cache
182

183             state.setExpiryTime(System.currentTimeMillis() + getCacheTimer());
184             m_stateTable.put(state.getPath(), state);
185         }
186
187         // Return the file state
188

189         return state;
190     }
191
192     /**
193      * Update the name that a file state is cached under, and the associated file state
194      *
195      * @param oldName String
196      * @param newName String
197      * @return FileState
198      */

199     public final synchronized FileState updateFileState(String JavaDoc oldName, String JavaDoc newName)
200     {
201
202         // Find the current file state
203

204         FileState state = m_stateTable.remove(FileState.normalizePath(oldName));
205
206         // Rename the file state and add it back into the cache using the new name
207

208         if (state != null)
209         {
210             state.setPath(newName);
211             addFileState(state);
212         }
213
214         // Return the updated file state
215

216         return state;
217     }
218
219     /**
220      * Enumerate the file state cache
221      *
222      * @return Enumeration
223      */

224     public final Enumeration enumerate()
225     {
226         return m_stateTable.keys();
227     }
228
229     /**
230      * Remove the file state for the specified path
231      *
232      * @param path String
233      * @return FileState
234      */

235     public final synchronized FileState removeFileState(String JavaDoc path)
236     {
237
238         // Remove the file state from the cache
239

240         FileState state = m_stateTable.remove(FileState.normalizePath(path));
241
242         // Return the removed file state
243

244         return state;
245     }
246
247     /**
248      * Rename a file state, remove the existing entry, update the path and add the state back into
249      * the cache using the new path.
250      *
251      * @param newPath String
252      * @param state FileState
253      */

254     public final synchronized void renameFileState(String JavaDoc newPath, FileState state)
255     {
256
257         // Remove the existing file state from the cache, using the original name
258

259         m_stateTable.remove(state.getPath());
260
261         // Update the file state path and add it back to the cache using the new name
262

263         state.setPath(FileState.normalizePath(newPath));
264         m_stateTable.put(state.getPath(), state);
265     }
266
267     /**
268      * Remove all file states from the cache
269      */

270     public final synchronized void removeAllFileStates()
271     {
272
273         // Check if there are any items in the cache
274

275         if (m_stateTable == null || m_stateTable.size() == 0)
276             return;
277
278         // Enumerate the file state cache and remove expired file state objects
279

280         Enumeration enm = m_stateTable.keys();
281
282         while (enm.hasMoreElements())
283         {
284
285             // Get the file state
286

287             FileState state = m_stateTable.get(enm.nextElement());
288
289             // DEBUG
290

291             if (logger.isDebugEnabled())
292                 logger.debug("++ Closed: " + state.getPath());
293         }
294
295         // Remove all the file states
296

297         m_stateTable.clear();
298     }
299
300     /**
301      * Remove expired file states from the cache
302      *
303      * @return int
304      */

305     public final int removeExpiredFileStates()
306     {
307
308         // Check if there are any items in the cache
309

310         if (m_stateTable == null || m_stateTable.size() == 0)
311             return 0;
312
313         // Enumerate the file state cache and remove expired file state objects
314

315         Enumeration enm = m_stateTable.keys();
316         long curTime = System.currentTimeMillis();
317
318         int expiredCnt = 0;
319
320         while (enm.hasMoreElements())
321         {
322
323             // Get the file state
324

325             FileState state = m_stateTable.get(enm.nextElement());
326
327             if (state != null && state.hasNoTimeout() == false)
328             {
329
330                 synchronized (state)
331                 {
332
333                     // Check if the file state has expired and there are no open references to the
334
// file
335

336                     if (state.hasExpired(curTime) && state.getOpenCount() == 0)
337                     {
338
339                         // Remove the expired file state
340

341                         m_stateTable.remove(state.getPath());
342
343                         // DEBUG
344

345                         if (logger.isDebugEnabled())
346                             logger.debug("++ Expired file state: " + state);
347
348                         // Update the expired count
349

350                         expiredCnt++;
351                     }
352                 }
353             }
354         }
355
356         // Return the count of expired file states that were removed
357

358         return expiredCnt;
359     }
360
361     /**
362      * Expired file state checker thread
363      */

364     public void run()
365     {
366
367         // Loop forever
368

369         while (true)
370         {
371
372             // Sleep for the required interval
373

374             try
375             {
376                 Thread.sleep(getCheckInterval());
377             }
378             catch (InterruptedException JavaDoc ex)
379             {
380             }
381
382             try
383             {
384
385                 // Check for expired file states
386

387                 int cnt = removeExpiredFileStates();
388
389                 // Debug
390

391                 if (logger.isDebugEnabled() && cnt > 0)
392                 {
393                     logger.debug("++ Expired " + cnt + " file states, cache=" + m_stateTable.size());
394                     Dump();
395                 }
396             }
397             catch (Exception JavaDoc ex)
398             {
399                 logger.debug(ex);
400             }
401         }
402     }
403
404     /**
405      * Dump the state cache entries to the specified stream
406      */

407     public final void Dump()
408     {
409
410         // Dump the file state cache entries to the specified stream
411

412         if (m_stateTable.size() > 0)
413             logger.info("++ FileStateCache Entries:");
414
415         Enumeration enm = m_stateTable.keys();
416         long curTime = System.currentTimeMillis();
417
418         while (enm.hasMoreElements())
419         {
420             String JavaDoc fname = (String JavaDoc) enm.nextElement();
421             FileState state = m_stateTable.get(fname);
422
423             logger.info(" ++ " + fname + "(" + state.getSecondsToExpire(curTime) + ") : " + state);
424         }
425     }
426 }
Popular Tags