| 1 | |
package org.webslinger.commons.vfs.ccache; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayInputStream; |
| 4 | |
import java.io.ByteArrayOutputStream; |
| 5 | |
import java.io.FilterOutputStream; |
| 6 | |
import java.io.InputStream; |
| 7 | |
import java.io.IOException; |
| 8 | |
import java.io.OutputStream; |
| 9 | |
import java.lang.ref.SoftReference; |
| 10 | |
import java.security.cert.Certificate; |
| 11 | |
import java.util.Map; |
| 12 | |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 13 | |
|
| 14 | |
import org.apache.commons.vfs.FileContent; |
| 15 | |
import org.apache.commons.vfs.FileName; |
| 16 | |
import org.apache.commons.vfs.FileObject; |
| 17 | |
import org.apache.commons.vfs.FileType; |
| 18 | |
import org.apache.commons.vfs.FileSystemException; |
| 19 | |
import org.apache.commons.vfs.RandomAccessContent; |
| 20 | |
import org.apache.commons.vfs.provider.AbstractFileObject; |
| 21 | |
import org.apache.commons.vfs.util.RandomAccessMode; |
| 22 | |
|
| 23 | |
import org.webslinger.io.IOUtil; |
| 24 | |
import org.webslinger.commons.vfs.FilteringFileObject; |
| 25 | |
|
| 26 | 0 | public class ContentCacheFileObject extends FilteringFileObject<FileName, ContentCacheFileObject, ContentCacheFileSystem> { |
| 27 | |
|
| 28 | |
protected FileName name; |
| 29 | |
protected ContentCacheFileSystem ccfs; |
| 30 | 0 | private final ReentrantReadWriteLock byteLock = new ReentrantReadWriteLock(false); |
| 31 | |
private SoftReference<?> ref; |
| 32 | |
private long lastModified; |
| 33 | |
|
| 34 | |
public ContentCacheFileObject(FileName name, FileObject base, ContentCacheFileSystem ccfs) throws FileSystemException { |
| 35 | 0 | super(name, ccfs, base); |
| 36 | 0 | this.name = name; |
| 37 | 0 | this.ccfs = ccfs; |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
protected Object getCachedData() throws FileSystemException { |
| 41 | 0 | byteLock.readLock().lock(); |
| 42 | |
try { |
| 43 | 0 | if (ref != null) { |
| 44 | 0 | if (lastModified != getRealFile().getContent().getLastModifiedTime()) { |
| 45 | 0 | ref = null; |
| 46 | |
} else { |
| 47 | 0 | Object data =ref.get(); |
| 48 | 0 | if (data != null) return data; |
| 49 | |
} |
| 50 | |
} |
| 51 | |
} finally { |
| 52 | 0 | byteLock.readLock().unlock(); |
| 53 | 0 | } |
| 54 | 0 | byteLock.writeLock().lock(); |
| 55 | |
try { |
| 56 | |
Object data; |
| 57 | 0 | if (ref != null) { |
| 58 | 0 | data = ref.get(); |
| 59 | 0 | if (data != null) return data; |
| 60 | |
} |
| 61 | 0 | FileContent content = getRealFile().getContent(); |
| 62 | 0 | if (content.getSize() < 64 * 1024) { |
| 63 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 64 | |
try { |
| 65 | 0 | IOUtil.copy(getRealFile().getContent().getInputStream(), baos); |
| 66 | 0 | byte[] bytes = baos.toByteArray(); |
| 67 | 0 | ref = new SoftReference<byte[]>(bytes); |
| 68 | 0 | lastModified = getRealFile().getContent().getLastModifiedTime(); |
| 69 | 0 | return bytes; |
| 70 | 0 | } catch (IOException e) { |
| 71 | |
} |
| 72 | |
} |
| 73 | 0 | ref = new SoftReference<FileContent>(content); |
| 74 | 0 | return content; |
| 75 | |
} finally { |
| 76 | 0 | byteLock.writeLock().unlock(); |
| 77 | |
} |
| 78 | |
} |
| 79 | |
|
| 80 | |
protected InputStream doGetInputStream() throws FileSystemException { |
| 81 | 0 | Object data = getCachedData(); |
| 82 | 0 | if (data instanceof FileContent) return ((FileContent) data).getInputStream(); |
| 83 | 0 | return new ByteArrayInputStream((byte[]) data); |
| 84 | |
} |
| 85 | |
|
| 86 | |
protected long doGetSize() throws FileSystemException { |
| 87 | 0 | Object data = getCachedData(); |
| 88 | 0 | if (data instanceof FileContent) return ((FileContent) data).getSize(); |
| 89 | 0 | return ((byte[]) data).length; |
| 90 | |
} |
| 91 | |
|
| 92 | |
protected String[] doListChildren() throws FileSystemException { |
| 93 | 0 | FileObject[] children = getRealFile().getChildren(); |
| 94 | 0 | String[] names = new String[children.length]; |
| 95 | 0 | for (int i = 0; i < names.length; i++) { |
| 96 | 0 | names[i] = children[i].getName().getBaseName(); |
| 97 | |
} |
| 98 | 0 | return names; |
| 99 | |
} |
| 100 | |
|
| 101 | |
protected void doDelete() throws FileSystemException { |
| 102 | 0 | super.doDelete(); |
| 103 | 0 | ref = null; |
| 104 | 0 | } |
| 105 | |
|
| 106 | |
protected void doRename(FileObject newFile) throws Exception { |
| 107 | 0 | getRealFile().moveTo(((ContentCacheFileObject) newFile)); |
| 108 | 0 | ref = null; |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
protected void doCreateFolder() throws FileSystemException { |
| 112 | 0 | super.doCreateFolder(); |
| 113 | 0 | ref = null; |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
protected void doSetLastModifiedTime(long time) throws FileSystemException { |
| 117 | 0 | lastModified = time; |
| 118 | 0 | super.doSetLastModifiedTime(time); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
protected RandomAccessContent doGetRandomAccessContent(RandomAccessMode mode) throws FileSystemException { |
| 122 | 0 | throw new UnsupportedOperationException("foo"); |
| 123 | |
} |
| 124 | |
|
| 125 | |
protected OutputStream doGetOutputStream(boolean append) throws FileSystemException { |
| 126 | 0 | return new FilterOutputStream(getRealFile().getContent().getOutputStream(append)) { |
| 127 | |
public void close() throws IOException { |
| 128 | 0 | super.close(); |
| 129 | 0 | ref = null; |
| 130 | 0 | } |
| 131 | |
}; |
| 132 | |
} |
| 133 | |
} |
| 134 | |
|