| 1 | |
package org.webslinger.ext.commons.vfs.plan9; |
| 2 | |
|
| 3 | |
import java.util.Arrays; |
| 4 | |
import java.util.Collection; |
| 5 | |
import java.util.Collections; |
| 6 | |
import java.net.URI; |
| 7 | |
|
| 8 | |
import org.apache.commons.vfs.FileName; |
| 9 | |
import org.apache.commons.vfs.FileObject; |
| 10 | |
import org.apache.commons.vfs.FileSystem; |
| 11 | |
import org.apache.commons.vfs.FileSystemException; |
| 12 | |
import org.apache.commons.vfs.FileSystemOptions; |
| 13 | |
import org.apache.commons.vfs.provider.AbstractFileSystem; |
| 14 | |
import org.apache.commons.vfs.provider.GenericFileName; |
| 15 | |
|
| 16 | |
import uk.ac.rdg.resc.jstyx.StyxException; |
| 17 | |
import uk.ac.rdg.resc.jstyx.StyxUtils; |
| 18 | |
import uk.ac.rdg.resc.jstyx.client.CStyxFile; |
| 19 | |
import uk.ac.rdg.resc.jstyx.client.StyxConnection; |
| 20 | |
import uk.ac.rdg.resc.jstyx.types.DirEntry; |
| 21 | |
|
| 22 | |
public class Plan9FileSystem extends AbstractFileSystem { |
| 23 | |
protected String userName; |
| 24 | |
protected String hostName; |
| 25 | |
protected int port; |
| 26 | |
protected StyxConnection connection; |
| 27 | |
protected int useCount; |
| 28 | |
|
| 29 | |
public Plan9FileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions) throws FileSystemException { |
| 30 | 0 | super(rootName, null, fileSystemOptions); |
| 31 | 0 | GenericFileName gfn = (GenericFileName) getRootName(); |
| 32 | 0 | userName = gfn.getUserName(); |
| 33 | 0 | hostName = gfn.getHostName(); |
| 34 | 0 | port = gfn.getPort(); |
| 35 | |
|
| 36 | |
try { |
| 37 | 0 | connection = getConnection(); |
| 38 | 0 | connection.connect(); |
| 39 | 0 | } catch (Exception e) { |
| 40 | 0 | if (e instanceof FileSystemException) throw (FileSystemException) e; |
| 41 | 0 | throw (FileSystemException) new FileSystemException(e.getMessage()).initCause(e); |
| 42 | 0 | } |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
protected boolean checkMode(DirEntry entry, int mask) { |
| 46 | 0 | long mode = entry.getMode(); |
| 47 | 0 | if (((mode >> 6) & mask) != 0) return true; |
| 48 | 0 | if ((mode & mask) != 0) return true; |
| 49 | 0 | return false; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public boolean isReadable(DirEntry entry) { |
| 53 | 0 | return checkMode(entry, 040); |
| 54 | |
} |
| 55 | |
|
| 56 | |
public boolean isWritable(DirEntry entry) { |
| 57 | 0 | return checkMode(entry, 020); |
| 58 | |
} |
| 59 | |
|
| 60 | |
protected FileObject createFile(final FileName name) throws FileSystemException { |
| 61 | 0 | return new Plan9FileObject(name, this); |
| 62 | |
} |
| 63 | |
|
| 64 | |
protected void addCapabilities(final Collection caps) { |
| 65 | 0 | caps.addAll(Plan9FileProvider.capabilities); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public StyxConnection getNewConnection() { |
| 69 | 0 | if (userName == null) { |
| 70 | 0 | return new StyxConnection(hostName, port); |
| 71 | |
} else { |
| 72 | 0 | return new StyxConnection(hostName, port, userName); |
| 73 | |
} |
| 74 | |
} |
| 75 | |
|
| 76 | |
public StyxConnection getConnection() throws Exception { |
| 77 | 0 | synchronized (this) { |
| 78 | 0 | if (connection == null) { |
| 79 | 0 | connection = getNewConnection(); |
| 80 | 0 | connection.connect(); |
| 81 | |
} |
| 82 | 0 | useCount++; |
| 83 | 0 | return connection; |
| 84 | 0 | } |
| 85 | |
} |
| 86 | |
|
| 87 | |
public void putConnection(StyxConnection connection) { |
| 88 | 0 | synchronized (this) { |
| 89 | 0 | useCount--; |
| 90 | 0 | if (useCount == 0) { |
| 91 | 0 | connection.close(); |
| 92 | 0 | connection = null; |
| 93 | |
} |
| 94 | 0 | } |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
protected void doCloseCommunicationLink() { |
| 98 | 0 | System.err.println("doCloseCommunicationLink()"); |
| 99 | 0 | connection.close(); |
| 100 | 0 | } |
| 101 | |
} |