1 16 package org.apache.commons.vfs.cache; 17 18 import org.apache.commons.collections.map.AbstractLinkedMap; 19 import org.apache.commons.collections.map.LRUMap; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.commons.vfs.FileName; 23 import org.apache.commons.vfs.FileObject; 24 import org.apache.commons.vfs.FileSystem; 25 import org.apache.commons.vfs.FileSystemException; 26 import org.apache.commons.vfs.VfsLog; 27 import org.apache.commons.vfs.provider.AbstractFileObject; 28 import org.apache.commons.vfs.util.Messages; 29 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 39 public class LRUFilesCache extends AbstractFilesCache 40 { 41 44 private Log log = LogFactory.getLog(LRUFilesCache.class); 45 46 private final Map filesystemCache = new HashMap (10); 47 private final int lruSize; 48 49 private class MyLRUMap extends LRUMap 50 { 51 final FileSystem filesystem; 52 53 public MyLRUMap(final FileSystem filesystem, int size) 54 { 55 super(size, true); 56 this.filesystem = filesystem; 57 } 58 59 protected boolean removeLRU(final AbstractLinkedMap.LinkEntry linkEntry) 60 { 61 synchronized (LRUFilesCache.this) 62 { 63 AbstractFileObject file = (AbstractFileObject) linkEntry.getValue(); 64 66 if (file.isAttached() || file.isContentOpen()) 67 { 68 return false; 71 } 72 73 if (super.removeLRU(linkEntry)) 75 { 76 try 77 { 78 file.close(); 80 } 81 catch (FileSystemException e) 82 { 83 VfsLog.warn(getLogger(), log, Messages.getString("vfs.impl/LRUFilesCache-remove-ex.warn"), e); 84 } 85 86 Map files = (Map) filesystemCache.get(filesystem); 87 if (files.size() < 1) 88 { 89 filesystemCache.remove(filesystem); 90 } 91 92 return true; 93 } 94 95 return false; 96 } 97 } 98 } 99 100 103 public LRUFilesCache() 104 { 105 this(100); 106 } 107 108 113 public LRUFilesCache(int lruSize) 114 { 115 this.lruSize = lruSize; 116 } 117 118 public void putFile(final FileObject file) 119 { 120 synchronized (this) 121 { 122 Map files = getOrCreateFilesystemCache(file.getFileSystem()); 123 124 126 files.put(file.getName(), file); 127 } 128 } 129 130 public FileObject getFile(final FileSystem filesystem, final FileName name) 131 { 132 synchronized (this) 133 { 134 Map files = getOrCreateFilesystemCache(filesystem); 135 136 139 return (FileObject) files.get(name); 140 } 141 } 142 143 public void clear(final FileSystem filesystem) 144 { 145 synchronized (this) 146 { 147 149 Map files = getOrCreateFilesystemCache(filesystem); 150 files.clear(); 151 152 filesystemCache.remove(filesystem); 153 } 154 } 155 156 protected Map getOrCreateFilesystemCache(final FileSystem filesystem) 157 { 158 Map files = (Map) filesystemCache.get(filesystem); 159 if (files == null) 160 { 161 163 files = new MyLRUMap(filesystem, lruSize); 164 filesystemCache.put(filesystem, files); 165 } 166 167 return files; 168 } 169 170 public void close() 171 { 172 super.close(); 173 174 synchronized (this) 175 { 176 178 filesystemCache.clear(); 179 } 180 } 181 182 public void removeFile(final FileSystem filesystem, final FileName name) 183 { 184 synchronized (this) 185 { 186 Map files = getOrCreateFilesystemCache(filesystem); 187 188 190 files.remove(name); 191 192 if (files.size() < 1) 193 { 194 filesystemCache.remove(filesystem); 195 } 196 } 197 } 198 199 public void touchFile(final FileObject file) 200 { 201 getFile(file.getFileSystem(), file.getName()); 203 } 204 } 205 | Popular Tags |