1 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 29 public class FileStateTable implements Runnable 30 { 31 private static final Log logger = LogFactory.getLog(FileStateTable.class); 32 33 35 private static final int INITIAL_SIZE = 100; 36 37 39 private static final long DEFAULT_EXPIRECHECK = 15000; 40 41 43 private Hashtable<String , FileState> m_stateTable; 44 45 47 private long m_expireInterval = DEFAULT_EXPIRECHECK; 48 49 51 private long m_cacheTimer = 2 * 60000L; 53 56 public FileStateTable() 57 { 58 m_stateTable = new Hashtable<String , FileState>(INITIAL_SIZE); 59 60 62 Thread th = new Thread (this); 63 th.setDaemon(true); 64 th.setName("FileStateExpire"); 65 th.start(); 66 } 67 68 73 public final long getCheckInterval() 74 { 75 return m_expireInterval; 76 } 77 78 83 public final long getCacheTimer() 84 { 85 return m_cacheTimer; 86 } 87 88 93 public final int numberOfStates() 94 { 95 return m_stateTable.size(); 96 } 97 98 103 public final void setCacheTimer(long tmo) 104 { 105 m_cacheTimer = tmo; 106 } 107 108 113 public final void setCheckInterval(long chkIntval) 114 { 115 m_expireInterval = chkIntval; 116 } 117 118 123 public final synchronized void addFileState(FileState fstate) 124 { 125 126 128 if (logger.isDebugEnabled() && m_stateTable.get(fstate.getPath()) != null) 129 logger.debug("***** addFileState() state=" + fstate.toString() + " - ALREADY IN CACHE *****"); 130 131 133 if (logger.isDebugEnabled() && fstate == null) 134 { 135 logger.debug("addFileState() NULL FileState"); 136 return; 137 } 138 139 141 fstate.setExpiryTime(System.currentTimeMillis() + getCacheTimer()); 142 m_stateTable.put(fstate.getPath(), fstate); 143 } 144 145 151 public final synchronized FileState findFileState(String path) 152 { 153 return m_stateTable.get(FileState.normalizePath(path)); 154 } 155 156 165 public final synchronized FileState findFileState(String path, boolean isdir, boolean create) 166 { 167 168 170 FileState state = m_stateTable.get(FileState.normalizePath(path)); 171 172 174 if (state == null && create == true) 175 { 176 177 179 state = new FileState(path, isdir); 180 181 183 state.setExpiryTime(System.currentTimeMillis() + getCacheTimer()); 184 m_stateTable.put(state.getPath(), state); 185 } 186 187 189 return state; 190 } 191 192 199 public final synchronized FileState updateFileState(String oldName, String newName) 200 { 201 202 204 FileState state = m_stateTable.remove(FileState.normalizePath(oldName)); 205 206 208 if (state != null) 209 { 210 state.setPath(newName); 211 addFileState(state); 212 } 213 214 216 return state; 217 } 218 219 224 public final Enumeration enumerate() 225 { 226 return m_stateTable.keys(); 227 } 228 229 235 public final synchronized FileState removeFileState(String path) 236 { 237 238 240 FileState state = m_stateTable.remove(FileState.normalizePath(path)); 241 242 244 return state; 245 } 246 247 254 public final synchronized void renameFileState(String newPath, FileState state) 255 { 256 257 259 m_stateTable.remove(state.getPath()); 260 261 263 state.setPath(FileState.normalizePath(newPath)); 264 m_stateTable.put(state.getPath(), state); 265 } 266 267 270 public final synchronized void removeAllFileStates() 271 { 272 273 275 if (m_stateTable == null || m_stateTable.size() == 0) 276 return; 277 278 280 Enumeration enm = m_stateTable.keys(); 281 282 while (enm.hasMoreElements()) 283 { 284 285 287 FileState state = m_stateTable.get(enm.nextElement()); 288 289 291 if (logger.isDebugEnabled()) 292 logger.debug("++ Closed: " + state.getPath()); 293 } 294 295 297 m_stateTable.clear(); 298 } 299 300 305 public final int removeExpiredFileStates() 306 { 307 308 310 if (m_stateTable == null || m_stateTable.size() == 0) 311 return 0; 312 313 315 Enumeration enm = m_stateTable.keys(); 316 long curTime = System.currentTimeMillis(); 317 318 int expiredCnt = 0; 319 320 while (enm.hasMoreElements()) 321 { 322 323 325 FileState state = m_stateTable.get(enm.nextElement()); 326 327 if (state != null && state.hasNoTimeout() == false) 328 { 329 330 synchronized (state) 331 { 332 333 336 if (state.hasExpired(curTime) && state.getOpenCount() == 0) 337 { 338 339 341 m_stateTable.remove(state.getPath()); 342 343 345 if (logger.isDebugEnabled()) 346 logger.debug("++ Expired file state: " + state); 347 348 350 expiredCnt++; 351 } 352 } 353 } 354 } 355 356 358 return expiredCnt; 359 } 360 361 364 public void run() 365 { 366 367 369 while (true) 370 { 371 372 374 try 375 { 376 Thread.sleep(getCheckInterval()); 377 } 378 catch (InterruptedException ex) 379 { 380 } 381 382 try 383 { 384 385 387 int cnt = removeExpiredFileStates(); 388 389 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 ex) 398 { 399 logger.debug(ex); 400 } 401 } 402 } 403 404 407 public final void Dump() 408 { 409 410 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 fname = (String ) enm.nextElement(); 421 FileState state = m_stateTable.get(fname); 422 423 logger.info(" ++ " + fname + "(" + state.getSecondsToExpire(curTime) + ") : " + state); 424 } 425 } 426 } | Popular Tags |