| 1 | |
package org.webslinger.commons.vfs.handlers.attributes; |
| 2 | |
|
| 3 | |
import org.apache.commons.vfs.FileContent; |
| 4 | |
import org.apache.commons.vfs.FileName; |
| 5 | |
import org.apache.commons.vfs.FileObject; |
| 6 | |
import org.apache.commons.vfs.FileSystemException; |
| 7 | |
|
| 8 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 9 | |
import org.webslinger.lang.ConcurrentCache; |
| 10 | |
import org.webslinger.util.GeneratedResult; |
| 11 | |
import org.webslinger.util.TTLObject; |
| 12 | |
import org.webslinger.util.TTLCachedObject; |
| 13 | |
|
| 14 | |
public class DefaultAttributeMapper implements AttributeMapper { |
| 15 | |
static { |
| 16 | 0 | TTLObject.setDefaultTTLForClass(AttributeValueTTLCachedObject.class, 1000); |
| 17 | 0 | } |
| 18 | |
|
| 19 | |
protected final FileObject file; |
| 20 | 0 | protected final ConcurrentCache<String, TTLCachedObject<Object>> attributeCache = new ConcurrentCache<String, TTLCachedObject<Object>>(DefaultAttributeMapper.class, "attributeCache", null, ConcurrentCache.HARD) { |
| 21 | |
protected TTLCachedObject<Object> createValue(String key) throws Exception { |
| 22 | 0 | return new AttributeValueTTLCachedObject(key); |
| 23 | |
} |
| 24 | |
}; |
| 25 | |
|
| 26 | |
protected class AttributeValueTTLCachedObject extends TTLCachedObject<Object> { |
| 27 | |
protected final String name; |
| 28 | |
|
| 29 | 0 | protected AttributeValueTTLCachedObject(String name) { |
| 30 | 0 | this.name = name; |
| 31 | 0 | } |
| 32 | |
|
| 33 | |
protected long getTimestamp(Object old) throws FileSystemException { |
| 34 | 0 | FileContent content = getContent(); |
| 35 | 0 | if (!content.getFile().exists()) return NOT_EXISTANT_TIMESTAMP; |
| 36 | 0 | return content.getLastModifiedTime(); |
| 37 | |
} |
| 38 | |
|
| 39 | |
protected GeneratedResult<Object> generate(Object old) throws FileSystemException { |
| 40 | 0 | FileContent content = getContent(); |
| 41 | 0 | if (!content.getFile().exists()) return new GeneratedResult<Object>(NOT_EXISTANT_TIMESTAMP, null); |
| 42 | 0 | return new GeneratedResult<Object>(content.getLastModifiedTime(), content.getAttribute(name)); |
| 43 | |
} |
| 44 | |
} |
| 45 | |
|
| 46 | 0 | public DefaultAttributeMapper(FileObject file) throws FileSystemException { |
| 47 | 0 | this.file = file; |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
protected FileContent getContent() throws FileSystemException { |
| 51 | 0 | return file.getContent(); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public void deleteAttributes() throws FileSystemException { |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
public void refresh() throws FileSystemException { |
| 58 | 0 | attributeCache.clear(); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
public TTLCachedObject<Object> getAttribute(String name) throws FileSystemException { |
| 62 | |
try { |
| 63 | 0 | return attributeCache.get(name); |
| 64 | 0 | } catch (Exception e) { |
| 65 | 0 | throw VFSUtil.makeFileSystemException(e); |
| 66 | |
} |
| 67 | |
} |
| 68 | |
|
| 69 | |
public boolean attributeExists(String name) throws FileSystemException { |
| 70 | 0 | return getContent().attributeExists(name); |
| 71 | |
} |
| 72 | |
|
| 73 | |
public String[] getAttributeNames() throws FileSystemException { |
| 74 | 0 | return getContent().getAttributeNames(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
public void setAttribute(String name, Object value) throws FileSystemException { |
| 78 | 0 | getContent().setAttribute(name, value); |
| 79 | 0 | attributeCache.remove(name); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
public void removeAttribute(String name) throws FileSystemException { |
| 83 | 0 | getContent().removeAttribute(name); |
| 84 | 0 | attributeCache.remove(name); |
| 85 | 0 | } |
| 86 | |
} |