1 38 package org.jpublish.resource; 39 40 import java.io.ByteArrayOutputStream ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 45 import com.anthonyeden.lib.util.IOUtilities; 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import org.apache.commons.vfs.FileContent; 49 import org.apache.commons.vfs.FileObject; 50 import org.apache.commons.vfs.FileSystemManager; 51 import org.jpublish.SiteContext; 52 import org.jpublish.cache.CacheEntry; 53 import org.jpublish.cache.CacheStorage; 54 import org.jpublish.cache.HashMapCacheStorage; 55 import org.jpublish.util.PathUtilities; 56 57 62 63 public class DefaultResourceManager extends AbstractResourceManager { 64 65 private Log log = LogFactory.getLog(DefaultResourceManager.class); 66 private CacheStorage resourceCache = null; 67 68 73 public synchronized CacheStorage getResourceCache() { 74 if (resourceCache == null) { 75 resourceCache = new HashMapCacheStorage(); 76 } 77 return resourceCache; 78 } 79 80 85 public void setResourceCache(CacheStorage resourceCache) { 86 this.resourceCache = resourceCache; 87 } 88 89 96 public synchronized FileSystemManager getFileSystemManager() 97 throws IOException { 98 if (fileSystemManager == null) { 99 configureDefaultFileSystemManager(SiteContext.DEFAULT_STATIC_ROOT); 100 } 101 return fileSystemManager; 102 } 103 104 111 public boolean exists(String path) throws IOException { 112 FileSystemManager fileSystemManager = getFileSystemManager(); 113 if (log.isDebugEnabled()) { 114 log.debug("FileSystemManager: " + fileSystemManager); 115 } 116 FileObject baseFile = fileSystemManager.getBaseFile(); 117 if (log.isDebugEnabled()) { 118 log.debug("Base file: " + baseFile); 119 } 120 FileObject file = fileSystemManager.resolveFile(baseFile, 121 PathUtilities.toRelativePath(path)); 122 if (log.isDebugEnabled()) { 123 log.debug("File: " + file); 124 } 125 boolean exists = file.exists(); 126 if (log.isDebugEnabled()) { 127 log.debug("File exists? " + exists); 128 } 129 return exists; 130 } 131 132 139 140 public long getLastModified(String path) throws IOException { 141 FileSystemManager fileSystemManager = getFileSystemManager(); 142 FileObject baseFile = fileSystemManager.getBaseFile(); 143 FileObject file = fileSystemManager.resolveFile(baseFile, 144 PathUtilities.toRelativePath(path)); 145 FileContent content = file.getContent(); 146 return content.getLastModifiedTime(); 147 } 148 149 157 158 public long getContentLength(String path) throws IOException { 159 FileSystemManager fileSystemManager = getFileSystemManager(); 160 FileObject baseFile = fileSystemManager.getBaseFile(); 161 FileObject file = fileSystemManager.resolveFile(baseFile, 162 PathUtilities.toRelativePath(path)); 163 FileContent content = file.getContent(); 164 return content.getSize(); 165 } 166 167 175 176 public void load(String path, OutputStream out) throws IOException , 177 ResourceNotFoundException { 178 if (log.isDebugEnabled()) { 179 log.debug("Loading resource: " + path); 180 } 181 182 FileSystemManager fileSystemManager = getFileSystemManager(); 183 FileObject baseFile = fileSystemManager.getBaseFile(); 184 FileObject file = fileSystemManager.resolveFile(baseFile, 185 PathUtilities.toRelativePath(path)); 186 if (!file.exists()) { 187 throw new ResourceNotFoundException("Resource " + path + 188 " not found"); 189 } 190 191 FileContent content = file.getContent(); 192 long lastModified = content.getLastModifiedTime(); 193 194 CacheStorage resourceCache = getResourceCache(); 196 CacheEntry cacheEntry = (CacheEntry) resourceCache.get(path); 197 byte[] data = null; 198 199 if (cacheEntry == null) { 200 log.debug("Resource (" + path + ") not found in cache."); 201 data = loadResourceData(content); 202 log.debug("Loaded data length: " + data.length); 203 resourceCache.put(path, new CacheEntry(data, lastModified)); 204 } else { 205 if (log.isDebugEnabled()) { 206 log.debug("Resource (" + path + ") found in cache."); 207 } 208 data = (byte[]) cacheEntry.getObject(); 209 if (cacheEntry.getLastModified() != lastModified) { 210 log.debug("Resource modification dates do not match."); 211 log.debug("Reloading resource."); 212 213 data = loadResourceData(content); 214 resourceCache.put(path, new CacheEntry(data, lastModified)); 215 } 216 } 217 218 if (data != null) { 219 log.debug("Writing data to output stream"); 220 out.write(data); 222 log.debug("Wrote " + data.length + " bytes"); 223 } else { 226 log.error("Data array was null"); 227 } 228 } 229 230 237 private byte[] loadResourceData(FileContent content) throws IOException { 238 InputStream in = null; 239 ByteArrayOutputStream bout = null; 240 try { 241 in = content.getInputStream(); 242 bout = new ByteArrayOutputStream (); 243 244 int c = -1; 245 while ((c = in.read()) != -1) { 246 bout.write(c); 247 } 248 bout.flush(); 249 250 return bout.toByteArray(); 251 } finally { 252 IOUtilities.close(in); 253 IOUtilities.close(bout); 254 } 255 } 256 257 } 258 259 | Popular Tags |