| 1 | |
package org.webslinger.ext.commons.vfs.plan9; |
| 2 | |
|
| 3 | |
import java.io.InputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
import java.security.cert.Certificate; |
| 7 | |
import java.util.Map; |
| 8 | |
|
| 9 | |
import org.apache.commons.vfs.FileName; |
| 10 | |
import org.apache.commons.vfs.FileObject; |
| 11 | |
import org.apache.commons.vfs.FileSystem; |
| 12 | |
import org.apache.commons.vfs.FileSystemException; |
| 13 | |
import org.apache.commons.vfs.FileSystemOptions; |
| 14 | |
import org.apache.commons.vfs.FileType; |
| 15 | |
import org.apache.commons.vfs.RandomAccessContent; |
| 16 | |
import org.apache.commons.vfs.provider.AbstractFileObject; |
| 17 | |
import org.apache.commons.vfs.util.RandomAccessMode; |
| 18 | |
|
| 19 | |
import uk.ac.rdg.resc.jstyx.StyxUtils; |
| 20 | |
import uk.ac.rdg.resc.jstyx.client.CStyxFile; |
| 21 | |
import uk.ac.rdg.resc.jstyx.client.CStyxFileInputStream; |
| 22 | |
import uk.ac.rdg.resc.jstyx.client.CStyxFileOutputStream; |
| 23 | |
import uk.ac.rdg.resc.jstyx.types.DirEntry; |
| 24 | |
import uk.ac.rdg.resc.jstyx.client.StyxConnection; |
| 25 | |
|
| 26 | |
public class Plan9FileObject extends AbstractFileObject { |
| 27 | |
protected CStyxFile styxFile; |
| 28 | |
protected StyxConnection connection; |
| 29 | |
protected Plan9FileSystem fileSystem; |
| 30 | |
|
| 31 | |
public Plan9FileObject(FileName name, Plan9FileSystem fileSystem) { |
| 32 | 0 | super(name, fileSystem); |
| 33 | 0 | this.fileSystem = fileSystem; |
| 34 | 0 | } |
| 35 | |
|
| 36 | |
protected DirEntry getDirEntry() throws Exception { |
| 37 | 0 | DirEntry dirEntry = styxFile.getDirEntry(); |
| 38 | 0 | if (dirEntry == null) { |
| 39 | 0 | styxFile.refresh(); |
| 40 | 0 | dirEntry = styxFile.getDirEntry(); |
| 41 | |
} |
| 42 | 0 | return dirEntry; |
| 43 | |
} |
| 44 | |
|
| 45 | |
protected boolean doIsHidden() throws Exception { |
| 46 | 0 | return false; |
| 47 | |
} |
| 48 | |
|
| 49 | |
protected boolean doIsReadable() throws Exception { |
| 50 | 0 | return fileSystem.isReadable(getDirEntry()); |
| 51 | |
} |
| 52 | |
|
| 53 | |
protected boolean doIsWritable() throws Exception { |
| 54 | 0 | return fileSystem.isWritable(getDirEntry()); |
| 55 | |
} |
| 56 | |
|
| 57 | |
protected void doAttach() throws Exception { |
| 58 | 0 | System.err.println(this + ".doAttach()"); |
| 59 | 0 | connection = ((Plan9FileSystem) getFileSystem()).getConnection(); |
| 60 | 0 | styxFile = connection.getFile(getName().getPath()); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
protected void doDetach() throws Exception { |
| 64 | 0 | System.err.println(this + ".doDetach()"); |
| 65 | 0 | styxFile.close(); |
| 66 | 0 | ((Plan9FileSystem) getFileSystem()).putConnection(connection); |
| 67 | 0 | styxFile = null; |
| 68 | 0 | connection = null; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
protected InputStream doGetInputStream() throws Exception { |
| 72 | 0 | final CStyxFile file = styxFile.getConnection().openFile(styxFile.getPath(), StyxUtils.OREAD); |
| 73 | 0 | return new CStyxFileInputStream(file) { |
| 74 | |
public void close() throws IOException { |
| 75 | 0 | file.close(); |
| 76 | 0 | super.close(); |
| 77 | 0 | } |
| 78 | |
}; |
| 79 | |
} |
| 80 | |
|
| 81 | |
protected OutputStream doGetOutputStream(boolean append) throws Exception { |
| 82 | 0 | int mode = StyxUtils.OWRITE; |
| 83 | 0 | if (!append) mode |= StyxUtils.OTRUNC; |
| 84 | 0 | final CStyxFile file = styxFile.getConnection().openFile(styxFile.getPath(), mode); |
| 85 | 0 | return new CStyxFileOutputStream(file) { |
| 86 | |
public void close() throws IOException { |
| 87 | 0 | file.close(); |
| 88 | 0 | super.close(); |
| 89 | 0 | } |
| 90 | |
}; |
| 91 | |
} |
| 92 | |
|
| 93 | |
protected long doGetContentSize() throws Exception { |
| 94 | 0 | return getDirEntry().getFileLength().asLong(); |
| 95 | |
} |
| 96 | |
|
| 97 | |
protected String[] doListChildren() throws Exception { |
| 98 | 0 | CStyxFile[] styxChildren = styxFile.getChildren(); |
| 99 | 0 | String[] children = new String[styxChildren.length]; |
| 100 | 0 | for (int i = 0; i < children.length; i++) { |
| 101 | 0 | children[i] = styxChildren[i].getName(); |
| 102 | |
} |
| 103 | 0 | return children; |
| 104 | |
} |
| 105 | |
|
| 106 | |
protected FileType doGetType() throws Exception { |
| 107 | |
|
| 108 | 0 | return styxFile.isDirectory() ? FileType.FOLDER : FileType.FILE; |
| 109 | |
} |
| 110 | |
|
| 111 | |
protected long doGetLastModifiedTime() throws Exception { |
| 112 | 0 | return getDirEntry().getLastModifiedTime() * 1000; |
| 113 | |
} |
| 114 | |
|
| 115 | |
protected void doSetLastModifiedTime(long time) throws Exception { |
| 116 | 0 | super.doSetLastModifiedTime(time); |
| 117 | 0 | } |
| 118 | |
|
| 119 | |
protected Map doGetAttributes() throws Exception { |
| 120 | 0 | return null; |
| 121 | |
} |
| 122 | |
|
| 123 | |
protected void doSetAttribute(String name, Object value) throws Exception { |
| 124 | 0 | } |
| 125 | |
|
| 126 | |
protected void doRemoveAttribute(String name) throws Exception { |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
protected Certificate[] doGetCertificates() throws Exception { |
| 130 | 0 | return super.doGetCertificates(); |
| 131 | |
} |
| 132 | |
|
| 133 | |
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception { |
| 134 | 0 | return super.doGetRandomAccessContent(mode); |
| 135 | |
} |
| 136 | |
} |