| 1 | |
package org.webslinger.commons.vfs.handlers.attributes; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.LinkedHashSet; |
| 5 | |
|
| 6 | |
import org.apache.commons.vfs.FileContent; |
| 7 | |
import org.apache.commons.vfs.FileName; |
| 8 | |
import org.apache.commons.vfs.FileObject; |
| 9 | |
import org.apache.commons.vfs.FileSystem; |
| 10 | |
import org.apache.commons.vfs.FileSystemException; |
| 11 | |
import org.apache.commons.vfs.Selectors; |
| 12 | |
|
| 13 | |
import org.webslinger.io.IOUtil; |
| 14 | |
import org.webslinger.lang.ConcurrentCache; |
| 15 | |
import org.webslinger.util.GeneratedResult; |
| 16 | |
import org.webslinger.util.TTLObject; |
| 17 | |
import org.webslinger.util.TTLCachedObject; |
| 18 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 19 | |
import org.webslinger.commons.vfs.handlers.ExclusionHandler; |
| 20 | |
|
| 21 | |
public abstract class FlatAttributeMapper implements AttributeMapper { |
| 22 | |
static { |
| 23 | 1 | TTLObject.setDefaultTTLForClass(Attribute.class, 1000); |
| 24 | 1 | } |
| 25 | |
|
| 26 | 22119 | protected class FlatAttributeMapperCache extends ConcurrentCache<String, Attribute> { |
| 27 | 1595 | protected FlatAttributeMapperCache(Class<?> owner, String field, String label) { |
| 28 | 1595 | super(owner, field, label, ConcurrentCache.HARD); |
| 29 | 1595 | } |
| 30 | |
|
| 31 | |
public Attribute get(String key, boolean create) throws FileSystemException { |
| 32 | |
try { |
| 33 | 34868 | return super.get(key, create); |
| 34 | 0 | } catch (Exception e) { |
| 35 | 0 | throw VFSUtil.makeFileSystemException(e); |
| 36 | |
} |
| 37 | |
} |
| 38 | |
|
| 39 | |
protected Attribute createValue(String key) throws Exception { |
| 40 | 36 | FileObject attrDir = getAttributeDir(); |
| 41 | |
FileObject attrFile; |
| 42 | 36 | if (attrDir.getType().hasChildren()) { |
| 43 | 24 | attrFile = attrDir.getChild(key); |
| 44 | 24 | if (attrFile == null) attrFile = attrDir.resolveFile(key); |
| 45 | |
} else { |
| 46 | 12 | attrFile = attrDir.resolveFile(key); |
| 47 | |
} |
| 48 | 36 | return new Attribute(attrFile); |
| 49 | |
} |
| 50 | |
|
| 51 | |
protected Attribute findIfExists(String key) throws Exception { |
| 52 | 22079 | FileObject attrDir = getAttributeDir(); |
| 53 | 22091 | if (!attrDir.getType().hasChildren()) return null; |
| 54 | 11745 | FileObject attrFile = attrDir.getChild(key); |
| 55 | 11742 | if (attrFile != null && attrFile.exists()) return new Attribute(attrFile); |
| 56 | 10845 | return null; |
| 57 | |
} |
| 58 | |
} |
| 59 | 1595 | protected final FlatAttributeMapperCache attributes = new FlatAttributeMapperCache(FlatAttributeMapper.class, "attributes", null); |
| 60 | |
protected final ExclusionHandler exclusionHandler; |
| 61 | |
|
| 62 | 1595 | public FlatAttributeMapper(ExclusionHandler exclusionHandler) throws FileSystemException { |
| 63 | 1595 | this.exclusionHandler = exclusionHandler; |
| 64 | 1595 | } |
| 65 | |
|
| 66 | |
protected abstract FileObject getAttributeDir() throws FileSystemException; |
| 67 | |
protected abstract FileObject getAttributeFile(FileName name) throws FileSystemException; |
| 68 | |
|
| 69 | |
public void deleteAttributes() throws FileSystemException { |
| 70 | 6 | FileObject attrDir = getAttributeDir(); |
| 71 | 6 | attrDir.delete(Selectors.SELECT_CHILDREN); |
| 72 | 6 | attrDir.delete(Selectors.SELECT_SELF); |
| 73 | 6 | attributes.clear(); |
| 74 | 6 | } |
| 75 | |
|
| 76 | |
public void refresh() throws FileSystemException { |
| 77 | 6768 | getAttributeDir().refresh(); |
| 78 | 6768 | attributes.clear(); |
| 79 | 6768 | } |
| 80 | |
|
| 81 | |
public TTLCachedObject<Object> getAttribute(String name) throws FileSystemException { |
| 82 | 13194 | Attribute attribute = attributes.get(name, false); |
| 83 | 13193 | return attribute != null ? attribute : null; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public boolean attributeExists(String name) throws FileSystemException { |
| 87 | 21628 | Attribute attribute = attributes.get(name, false); |
| 88 | 21619 | return attribute != null ? attribute.exists() : false; |
| 89 | |
} |
| 90 | |
|
| 91 | |
|
| 92 | |
public String[] getAttributeNames() throws FileSystemException { |
| 93 | 81 | FileObject attrDir = getAttributeDir(); |
| 94 | 81 | attrDir.refresh(); |
| 95 | 81 | if (!attrDir.getType().hasChildren()) return new String[0]; |
| 96 | 55 | FileObject[] children = getAttributeDir().getChildren(); |
| 97 | 55 | if (children == null) return new String[0]; |
| 98 | 55 | LinkedHashSet<String> names = new LinkedHashSet<String>(children.length); |
| 99 | 208 | for (FileObject child: children) { |
| 100 | 153 | names.add(child.getName().getBaseName()); |
| 101 | |
} |
| 102 | 55 | exclusionHandler.excludeNames(names); |
| 103 | 55 | return names.toArray(new String[names.size()]); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public void setAttribute(String name, Object value) throws FileSystemException { |
| 107 | 38 | if (value == null) { |
| 108 | 2 | removeAttribute(name); |
| 109 | 2 | return; |
| 110 | |
} |
| 111 | 36 | attributes.get(name, true).set(value); |
| 112 | 36 | } |
| 113 | |
|
| 114 | |
public void removeAttribute(String name) throws FileSystemException { |
| 115 | 8 | attributes.remove(name); |
| 116 | 8 | FileObject attrDir = getAttributeDir(); |
| 117 | |
FileObject attrFile; |
| 118 | 8 | if (attrDir.getType().hasChildren()) { |
| 119 | 6 | attrFile = attrDir.getChild(name); |
| 120 | 6 | if (attrFile == null) attrFile = attrDir.resolveFile(name); |
| 121 | |
} else { |
| 122 | 2 | attrFile = attrDir.resolveFile(name); |
| 123 | |
} |
| 124 | 8 | if (!attrFile.exists()) attrFile.refresh(); |
| 125 | 8 | attrFile.delete(); |
| 126 | 8 | } |
| 127 | |
|
| 128 | |
protected class Attribute extends TTLCachedObject<Object> { |
| 129 | |
protected final FileObject file; |
| 130 | |
|
| 131 | 937 | protected Attribute(FileObject file) throws FileSystemException { |
| 132 | 936 | this.file = file; |
| 133 | 936 | } |
| 134 | |
|
| 135 | |
protected FileObject getFile() throws FileSystemException { |
| 136 | 8889 | return file; |
| 137 | |
} |
| 138 | |
|
| 139 | |
protected boolean exists() throws FileSystemException { |
| 140 | 6726 | return getFile().exists(); |
| 141 | |
} |
| 142 | |
|
| 143 | |
protected void set(Object value) throws FileSystemException { |
| 144 | 36 | FileObject file = getFile(); |
| 145 | |
try { |
| 146 | 36 | IOUtil.writeObject(file.getContent().getOutputStream(), value); |
| 147 | 36 | lastModifiedTime = file.getContent().getLastModifiedTime(); |
| 148 | 36 | setObject(value); |
| 149 | 0 | } catch (IOException e) { |
| 150 | 0 | throw VFSUtil.makeFileSystemException(e); |
| 151 | 36 | } |
| 152 | 36 | } |
| 153 | |
|
| 154 | |
protected long getTimestamp(Object old) throws IOException { |
| 155 | 1301 | FileObject file = getFile(); |
| 156 | 1301 | file.refresh(); |
| 157 | 1301 | return file.exists() ? file.getContent().getLastModifiedTime() : NOT_EXISTANT_TIMESTAMP; |
| 158 | |
} |
| 159 | |
|
| 160 | |
protected GeneratedResult<Object> generate(Object old) throws IOException { |
| 161 | 817 | FileObject file = getFile(); |
| 162 | 817 | file.refresh(); |
| 163 | |
try { |
| 164 | 817 | if (file.exists()) return new GeneratedResult<Object>(file.getContent().getLastModifiedTime(), IOUtil.readObject(file.getContent().getInputStream())); |
| 165 | 0 | } catch (Exception e) { |
| 166 | 0 | return IOUtil.checkException(e); |
| 167 | 0 | } |
| 168 | 0 | return new GeneratedResult<Object>(NOT_EXISTANT_TIMESTAMP, null); |
| 169 | |
} |
| 170 | |
} |
| 171 | |
} |