1 21 22 package org.armedbear.j; 23 24 import java.util.Iterator ; 25 import java.util.Vector ; 26 27 public final class DirectoryCache 28 { 29 private static final int timeout = 1800000; 31 private static DirectoryCache cache; 32 33 private Vector entries = new Vector (); 34 35 public static synchronized DirectoryCache getDirectoryCache() 36 { 37 if (cache == null) { 38 cache = new DirectoryCache(); 39 IdleThread idleThread = IdleThread.getInstance(); 40 if (idleThread != null) 41 idleThread.maybeAddTask(PruneDirectoryCacheTask.getInstance()); 42 } 43 return cache; 44 } 45 46 public synchronized String getListing(File file) 47 { 48 String netPath = file.netPath(); 49 for (int i = entries.size(); i-- > 0;) { 50 DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i); 51 if (entry.getFile().netPath().equals(netPath)) { 52 if (entry.getWhen() + timeout < System.currentTimeMillis()) { 53 Log.debug("removing cache entry for " + entry.getFile().netPath()); 54 entries.remove(i); 55 return null; 56 } 57 return entry.getListing(); 58 } 59 } 60 return null; 61 } 62 63 public synchronized void put(File file, String listing) 64 { 65 String netPath = file.netPath(); 66 for (int i = entries.size(); i-- > 0;) { 67 DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i); 68 if (entry.getFile().netPath().equals(netPath)) { 69 entries.remove(i); 70 break; 71 } 72 } 73 if (listing != null && listing.length() > 0) { 74 entries.add(new DirectoryCacheEntry(file, listing, 75 System.currentTimeMillis())); 76 } 77 } 78 79 public synchronized void purge(String hostname) 80 { 81 for (int i = entries.size(); i-- > 0;) { 82 DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i); 83 if (entry.getFile().getHostName().equals(hostname)) { 84 Log.debug("removing cache entry for " + entry.getFile().netPath()); 85 entries.remove(i); 86 } 87 } 88 } 89 90 public synchronized void purge(File file) 91 { 92 String netPath = file.netPath(); 93 for (int i = entries.size(); i-- > 0;) { 94 DirectoryCacheEntry entry = (DirectoryCacheEntry) entries.get(i); 95 if (entry.getFile().netPath().equals(netPath)) { 96 Log.debug("removing cache entry for " + netPath); 97 entries.remove(i); 98 } 99 } 100 } 101 102 private static class PruneDirectoryCacheTask extends IdleThreadTask 103 { 104 private static PruneDirectoryCacheTask instance; 105 106 private long lastRun; 107 108 private PruneDirectoryCacheTask() 109 { 110 lastRun = System.currentTimeMillis(); 111 setIdle(300000); setRunnable(runnable); 113 } 114 115 private static synchronized PruneDirectoryCacheTask getInstance() 116 { 117 if (instance == null) 118 instance = new PruneDirectoryCacheTask(); 119 return instance; 120 } 121 122 private final Runnable runnable = new Runnable () { 123 public void run() 124 { 125 if (System.currentTimeMillis() - lastRun > 300000) { 127 long now = System.currentTimeMillis(); 128 synchronized (cache) { 129 Iterator it = cache.entries.iterator(); 130 while (it.hasNext()) { 131 DirectoryCacheEntry entry = 132 (DirectoryCacheEntry) it.next(); 133 if (entry.getWhen() + timeout < now) 134 it.remove(); 135 } 136 } 137 lastRun = now; 138 } 139 } 140 }; 141 } 142 } 143 | Popular Tags |