1 31 32 package org.opencms.cache; 33 34 import org.opencms.util.CmsFileUtil; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.io.File ; 38 import java.io.FileOutputStream ; 39 import java.io.IOException ; 40 41 51 public class CmsVfsDiskCache { 52 53 54 private String m_rfsRepository; 55 56 62 public CmsVfsDiskCache(String basepath, String foldername) { 63 64 m_rfsRepository = CmsFileUtil.normalizePath(basepath + foldername + File.separatorChar); 66 } 67 68 79 protected static File saveFile(String rfsName, byte[] content) throws IOException { 80 81 File f = new File (rfsName); 82 File p = f.getParentFile(); 83 if (!p.exists()) { 84 p.mkdirs(); 86 } 87 FileOutputStream fs = new FileOutputStream (f); 89 fs.write(content); 90 fs.close(); 91 return f; 92 } 93 94 103 public byte[] getCacheContent(String rfsName, long dateLastModified) { 104 105 dateLastModified = simplifyDateLastModified(dateLastModified); 106 try { 107 File f = new File (rfsName); 108 if (f.exists()) { 109 if (f.lastModified() != dateLastModified) { 110 f.delete(); 112 } else { 113 return CmsFileUtil.readFile(f); 114 } 115 } 116 } catch (IOException e) { 117 } 119 return null; 120 } 121 122 131 public String getCacheName(boolean online, String rootPath, String parameters) { 132 133 String rfsName = CmsFileUtil.getRepositoryName(m_rfsRepository, rootPath, online); 134 if (CmsStringUtil.isNotEmpty(parameters)) { 135 String extension = CmsFileUtil.getFileExtension(rfsName); 136 rfsName = CmsFileUtil.getRfsPath(rfsName, extension, parameters); 138 } 139 140 return rfsName; 141 } 142 143 148 public String getRepositoryPath() { 149 150 return m_rfsRepository; 151 } 152 153 162 public void saveCacheFile(String rfsName, byte[] content, long dateLastModified) throws IOException { 163 164 dateLastModified = simplifyDateLastModified(dateLastModified); 165 File f = saveFile(rfsName, content); 166 f.setLastModified(dateLastModified); 168 } 169 170 178 private long simplifyDateLastModified(long dateLastModified) { 179 180 return dateLastModified / 1000L; 181 } 182 } | Popular Tags |