1 11 package org.eclipse.team.internal.core; 12 13 import java.io.File ; 14 import java.util.*; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.jobs.ILock; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.team.core.TeamException; 21 import org.eclipse.team.core.variants.CachedResourceVariant; 22 23 26 public class ResourceVariantCache { 27 28 private static final String CACHE_DIRECTORY = ".cache"; private static final long CACHE_FILE_LIFESPAN = 60*60*1000; 33 private static Map caches = new HashMap(); 36 private String name; 37 private Map cacheEntries; 38 private long lastCacheCleanup; 39 private int cacheDirSize; 40 41 private ILock lock = Job.getJobManager().newLock(); 43 44 50 public static synchronized void enableCaching(String cacheId) { 51 if (isCachingEnabled(cacheId)) return; 52 ResourceVariantCache cache = new ResourceVariantCache(cacheId); 53 cache.createCacheDirectory(); 54 caches.put(cacheId, cache); 55 } 56 57 65 public static boolean isCachingEnabled(String cacheId) { 66 return getCache(cacheId) != null; 67 } 68 69 74 public static void disableCache(String cacheId) { 75 ResourceVariantCache cache = getCache(cacheId); 76 if (cache == null) { 77 return; 79 } 80 caches.remove(cacheId); 81 cache.deleteCacheDirectory(); 82 } 83 84 89 public static synchronized ResourceVariantCache getCache(String cacheId) { 90 return (ResourceVariantCache)caches.get(cacheId); 91 } 92 93 public static synchronized void shutdown() { 94 String [] keys = (String [])caches.keySet().toArray(new String [caches.size()]); 95 for (int i = 0; i < keys.length; i++) { 96 String id = keys[i]; 97 disableCache(id); 98 } 99 } 100 101 private ResourceVariantCache(String name) { 102 this.name = name; 103 } 104 105 110 public boolean hasEntry(String id) { 111 return internalGetCacheEntry(id) != null; 112 } 113 114 protected IPath getCachePath() { 115 return getStateLocation().append(CACHE_DIRECTORY).append(name); 116 } 117 118 private IPath getStateLocation() { 119 return TeamPlugin.getPlugin().getStateLocation(); 120 } 121 122 private synchronized void clearOldCacheEntries() { 123 long current = new Date().getTime(); 124 if ((lastCacheCleanup!=-1) && (current - lastCacheCleanup < CACHE_FILE_LIFESPAN)) return; 125 List stale = new ArrayList(); 126 for (Iterator iter = cacheEntries.values().iterator(); iter.hasNext();) { 127 ResourceVariantCacheEntry entry = (ResourceVariantCacheEntry) iter.next(); 128 long lastHit = entry.getLastAccessTimeStamp(); 129 if ((current - lastHit) > CACHE_FILE_LIFESPAN){ 130 stale.add(entry); 131 } 132 } 133 for (Iterator iter = stale.iterator(); iter.hasNext();) { 134 ResourceVariantCacheEntry entry = (ResourceVariantCacheEntry) iter.next(); 135 entry.dispose(); 136 } 137 } 138 139 private synchronized void purgeFromCache(String id) { 140 ResourceVariantCacheEntry entry = (ResourceVariantCacheEntry)cacheEntries.get(id); 141 File f = entry.getFile(); 142 try { 143 deleteFile(f); 144 } catch (TeamException e) { 145 } 148 cacheEntries.remove(id); 149 } 150 151 private synchronized void createCacheDirectory() { 152 IPath cacheLocation = getCachePath(); 153 File file = cacheLocation.toFile(); 154 if (file.exists()) { 155 try { 156 deleteFile(file); 157 } catch (TeamException e) { 158 if (file.exists() && (!file.isDirectory() || file.listFiles().length != 0)) { 160 TeamPlugin.log(e); 161 } 162 } 163 } 164 if (! file.exists() && ! file.mkdirs()) { 165 TeamPlugin.log(new TeamException(NLS.bind(Messages.RemoteContentsCache_fileError, new String [] { file.getAbsolutePath() }))); 166 } 167 cacheEntries = new HashMap(); 168 lastCacheCleanup = -1; 169 cacheDirSize = 0; 170 } 171 172 private synchronized void deleteCacheDirectory() { 173 cacheEntries = null; 174 lastCacheCleanup = -1; 175 cacheDirSize = 0; 176 IPath cacheLocation = getCachePath(); 177 File file = cacheLocation.toFile(); 178 if (file.exists()) { 179 try { 180 deleteFile(file); 181 } catch (TeamException e) { 182 } 185 } 186 } 187 188 private void deleteFile(File file) throws TeamException { 189 if (file.isDirectory()) { 190 File [] children = file.listFiles(); 191 for (int i = 0; i < children.length; i++) { 192 deleteFile(children[i]); 193 } 194 } 195 if (! file.delete()) { 196 throw new TeamException(NLS.bind(Messages.RemoteContentsCache_fileError, new String [] { file.getAbsolutePath() })); 197 } 198 } 199 200 205 protected void purgeFromCache(ResourceVariantCacheEntry entry) { 206 purgeFromCache(entry.getId()); 207 } 208 209 private synchronized ResourceVariantCacheEntry internalGetCacheEntry(String id) { 210 if (cacheEntries == null) { 211 throw new IllegalStateException (NLS.bind(Messages.RemoteContentsCache_cacheDisposed, new String [] { name })); 213 } 214 ResourceVariantCacheEntry entry = (ResourceVariantCacheEntry)cacheEntries.get(id); 215 if (entry != null) { 216 entry.registerHit(); 217 } 218 return entry; 219 } 220 221 225 public ResourceVariantCacheEntry getCacheEntry(String id) { 226 return internalGetCacheEntry(id); 227 } 228 229 public synchronized ResourceVariantCacheEntry add(String id, CachedResourceVariant resource) { 230 clearOldCacheEntries(); 231 String filePath = String.valueOf(cacheDirSize++); 232 ResourceVariantCacheEntry entry = new ResourceVariantCacheEntry(this, lock, id, filePath); 233 entry.setResourceVariant(resource); 234 cacheEntries.put(id, entry); 235 return entry; 236 } 237 238 public String getName() { 239 return name; 240 } 241 242 245 public ResourceVariantCacheEntry[] getEntries() { 246 return (ResourceVariantCacheEntry[]) cacheEntries.values().toArray(new ResourceVariantCacheEntry[cacheEntries.size()]); 247 } 248 249 } 250 | Popular Tags |