| 1 | |
package org.webslinger.commons.vfs.tests; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
|
| 5 | |
import org.apache.commons.vfs.FileListener; |
| 6 | |
import org.apache.commons.vfs.FileObject; |
| 7 | |
import org.apache.commons.vfs.FileSystemException; |
| 8 | |
import org.apache.commons.vfs.provider.AbstractFileSystem; |
| 9 | |
import org.apache.commons.vfs.provider.local.LocalFile; |
| 10 | |
|
| 11 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 12 | |
import org.webslinger.io.FileMonitor; |
| 13 | |
import org.webslinger.io.JNotifyFileMonitor; |
| 14 | |
import org.webslinger.io.Monitor; |
| 15 | |
|
| 16 | 0 | public final class LocalFileObjectRoot extends AbstractFileObjectRoot<LocalFileObjectRoot, LocalFile> { |
| 17 | |
private final File realFile; |
| 18 | |
|
| 19 | |
public LocalFileObjectRoot(LocalFile file) throws FileSystemException { |
| 20 | 0 | super(file); |
| 21 | 0 | realFile = VFSUtil.getLocalFile(file); |
| 22 | 0 | } |
| 23 | |
|
| 24 | |
public LocalFileObjectRoot(FileObject file) throws FileSystemException { |
| 25 | 0 | this((LocalFile) file); |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
protected LocalFileObjectRoot create(LocalFile file) throws Exception { |
| 29 | 0 | return new LocalFileObjectRoot(file); |
| 30 | |
} |
| 31 | |
|
| 32 | |
public Listener createListener() throws Exception { |
| 33 | 0 | return new LocalFileObjectRootListener(); |
| 34 | |
} |
| 35 | |
|
| 36 | 0 | protected final class LocalFileObjectRootListener extends AbstractFileObjectRootListener implements FileMonitor.FileListener { |
| 37 | |
public void start() throws Exception { |
| 38 | 0 | System.err.println(file + ".start(): " + realFile); |
| 39 | 0 | super.start(); |
| 40 | 0 | JNotifyFileMonitor.SINGLETON.add(realFile, this); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public void stop() throws Exception { |
| 44 | 0 | JNotifyFileMonitor.SINGLETON.remove(realFile, this); |
| 45 | 0 | super.stop(); |
| 46 | 0 | } |
| 47 | |
|
| 48 | |
protected void refresh() { |
| 49 | |
try { |
| 50 | 0 | file.refresh(); |
| 51 | 0 | file.getType(); |
| 52 | 0 | } catch (FileSystemException e) { |
| 53 | 0 | e.printStackTrace(); |
| 54 | 0 | } |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
public void acceptEvent(File item, Monitor.Listener.Event event) { |
| 58 | 0 | System.err.println("acceptEvent(" + item + ", " + event + ")"); |
| 59 | 0 | assert item == realFile; |
| 60 | 0 | AbstractFileSystem afs = (AbstractFileSystem) file.getFileSystem(); |
| 61 | 0 | switch (event) { |
| 62 | |
case CREATE: |
| 63 | 0 | refresh(); |
| 64 | 0 | afs.fireFileCreated(file); |
| 65 | 0 | break; |
| 66 | |
case CHANGE: |
| 67 | 0 | afs.fireFileChanged(file); |
| 68 | 0 | break; |
| 69 | |
case DELETE: |
| 70 | 0 | refresh(); |
| 71 | 0 | afs.fireFileDeleted(file); |
| 72 | |
break; |
| 73 | |
} |
| 74 | 0 | } |
| 75 | |
} |
| 76 | |
} |