| 1 | |
package org.webslinger.commons.vfs; |
| 2 | |
|
| 3 | |
import java.util.concurrent.atomic.AtomicBoolean; |
| 4 | |
import java.util.concurrent.atomic.AtomicInteger; |
| 5 | |
import java.util.concurrent.atomic.AtomicMarkableReference; |
| 6 | |
import java.util.concurrent.atomic.AtomicReference; |
| 7 | |
|
| 8 | |
import org.apache.commons.vfs.FileName; |
| 9 | |
import org.apache.commons.vfs.FileObject; |
| 10 | |
import org.apache.commons.vfs.FileSystemException; |
| 11 | |
import org.apache.commons.vfs.FileSystemOptions; |
| 12 | |
import org.apache.commons.vfs.FileType; |
| 13 | |
import org.apache.commons.vfs.NameScope; |
| 14 | |
|
| 15 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 16 | |
import org.webslinger.util.TTLObject; |
| 17 | |
|
| 18 | 0 | public abstract class GenerationalFileSystem<N extends FileName, F extends GenerationalFileObject<N, F, R, S>, R extends GenerationalFileSystem.Resolution<N, F, R, S>, S extends GenerationalFileSystem<N, F, R, S>> extends LayeredFileSystem<N, F, S> { |
| 19 | 59 | protected AtomicInteger generation = new AtomicInteger(); |
| 20 | |
|
| 21 | |
public GenerationalFileSystem(N name, FileObject root, FileSystemOptions options) throws FileSystemException { |
| 22 | 59 | super(name, root, options); |
| 23 | 59 | } |
| 24 | |
|
| 25 | |
protected R resolve(F genFile, R resolution, boolean create) throws FileSystemException { |
| 26 | 992908 | int generation = this.generation(); |
| 27 | 992898 | if (resolution != null && resolution.generation == generation && !resolution.isOutOfDate(create)) return resolution; |
| 28 | 17110 | return newResolution(generation, genFile); |
| 29 | |
} |
| 30 | |
|
| 31 | |
protected R getResolution(F other, boolean create) throws FileSystemException { |
| 32 | 33446 | return other.getResolution(create); |
| 33 | |
} |
| 34 | |
|
| 35 | |
public int generation() { |
| 36 | 992902 | return generation.get(); |
| 37 | |
} |
| 38 | |
|
| 39 | |
protected int newGeneration() { |
| 40 | 464 | return generation.incrementAndGet(); |
| 41 | |
} |
| 42 | |
|
| 43 | |
protected abstract R newResolution(int generation, F file) throws FileSystemException; |
| 44 | |
protected abstract F[] newArray(int length); |
| 45 | |
|
| 46 | |
public abstract static class Resolution<N extends FileName, F extends GenerationalFileObject<N, F, R, S>, R extends GenerationalFileSystem.Resolution<N, F, R, S>, S extends GenerationalFileSystem<N, F, R, S>> { |
| 47 | |
public final int generation; |
| 48 | |
public final F file; |
| 49 | 17110 | private final AtomicReference<FoundFile> foundFile = new AtomicReference<FoundFile>(); |
| 50 | |
|
| 51 | 17110 | protected Resolution(int generation, F file) { |
| 52 | 17110 | this.generation = generation; |
| 53 | 17110 | this.file = file; |
| 54 | 17110 | } |
| 55 | |
|
| 56 | |
private void refreshLocal() { |
| 57 | 87075 | foundFile.set(null); |
| 58 | 87075 | } |
| 59 | |
|
| 60 | |
protected boolean refresh() throws FileSystemException { |
| 61 | 85919 | refreshLocal(); |
| 62 | 85919 | return false; |
| 63 | |
} |
| 64 | |
|
| 65 | |
protected FileObject getFile(boolean create) throws FileSystemException { |
| 66 | |
FoundFile oldFoundFile, newFoundFile; |
| 67 | |
do { |
| 68 | 140028 | oldFoundFile = foundFile.get(); |
| 69 | 140023 | if (oldFoundFile != null && (oldFoundFile.create == create || !create)) return oldFoundFile.file; |
| 70 | 80042 | newFoundFile = new FoundFile(doGetFile(create), create); |
| 71 | 80042 | } while (!foundFile.compareAndSet(oldFoundFile, newFoundFile)); |
| 72 | 80042 | return newFoundFile.file; |
| 73 | |
} |
| 74 | |
|
| 75 | |
protected void delete() throws FileSystemException { |
| 76 | 20 | doDelete(); |
| 77 | 20 | refreshLocal(); |
| 78 | 20 | } |
| 79 | |
|
| 80 | |
protected abstract boolean isWriteable() throws FileSystemException; |
| 81 | |
protected abstract FileObject doGetFile(boolean create) throws FileSystemException; |
| 82 | |
protected abstract boolean isOutOfDate(boolean create) throws FileSystemException; |
| 83 | |
protected abstract void doDelete() throws FileSystemException; |
| 84 | |
protected abstract String[] getChildNames() throws FileSystemException; |
| 85 | |
|
| 86 | |
protected void injectType(FileType newType) throws FileSystemException { |
| 87 | 1136 | refreshLocal(); |
| 88 | 1136 | } |
| 89 | |
|
| 90 | |
protected final class FoundFile { |
| 91 | |
public final boolean create; |
| 92 | |
public final FileObject file; |
| 93 | |
|
| 94 | 80042 | protected FoundFile(FileObject file, boolean create) { |
| 95 | 80042 | this.file = file; |
| 96 | 80042 | this.create = create; |
| 97 | 80042 | } |
| 98 | |
} |
| 99 | |
} |
| 100 | |
} |
| 101 | |
|