| 1 | |
package org.webslinger.commons.vfs; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
|
| 5 | |
import org.apache.commons.vfs.FileObject; |
| 6 | |
import org.apache.commons.vfs.Selectors; |
| 7 | |
import org.apache.commons.vfs.provider.UriParser; |
| 8 | |
|
| 9 | |
public class FileReplicator { |
| 10 | 1 | private static final char[] reserved = {'!', '#', '$', '&', '*', '(', ')', '/', '\\', '[', ']', '|', '<', '>', '?'}; |
| 11 | |
protected final FileObject cacheLocation; |
| 12 | |
|
| 13 | 22 | public FileReplicator(FileObject cacheLocation) { |
| 14 | 22 | this.cacheLocation = cacheLocation; |
| 15 | 22 | } |
| 16 | |
|
| 17 | |
public FileObject getCacheLocation() { |
| 18 | 22 | return cacheLocation; |
| 19 | |
} |
| 20 | |
|
| 21 | |
public FileObject getLocalFile(FileObject file) throws IOException { |
| 22 | 58 | String url = file.getURL().toExternalForm(); |
| 23 | 58 | if (url.startsWith("file:")) return file; |
| 24 | 55 | if (url.startsWith("wsfile:")) return file; |
| 25 | 0 | FileObject localFile = getCacheLocation().resolveFile(UriParser.encode(url, reserved)); |
| 26 | 0 | if (!localFile.exists()) { |
| 27 | 0 | } else if (localFile.getContent().getSize() != file.getContent().getSize()) { |
| 28 | 0 | } else if (localFile.getContent().getLastModifiedTime() != file.getContent().getLastModifiedTime()) { |
| 29 | |
} else { |
| 30 | 0 | return localFile; |
| 31 | |
} |
| 32 | 0 | localFile.copyFrom(file, Selectors.SELECT_SELF); |
| 33 | 0 | localFile.getContent().setLastModifiedTime(file.getContent().getLastModifiedTime()); |
| 34 | 0 | return localFile; |
| 35 | |
|
| 36 | |
} |
| 37 | |
|
| 38 | |
public void deleteLocalFile(String url) throws IOException { |
| 39 | 0 | if (url.startsWith("file:")) return; |
| 40 | 0 | FileObject ptr = getCacheLocation().resolveFile(UriParser.encode(url, reserved)); |
| 41 | 0 | while (ptr != getCacheLocation() && ptr.delete()) ptr = ptr.getParent(); |
| 42 | 0 | } |
| 43 | |
} |