001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.cache; 029 030import org.opencms.file.CmsResource; 031import org.opencms.main.CmsLog; 032import org.opencms.util.CmsFileUtil; 033 034import java.io.File; 035import java.io.IOException; 036 037import org.apache.commons.logging.Log; 038 039/** 040 * Implements a name based RFS file based disk cache, that handles parameter based versions of VFS files.<p> 041 * 042 * This RFS cache operates on file names, plus a hash code calculated from 043 * {@link org.opencms.file.CmsResource#getDateLastModified()}, {@link org.opencms.file.CmsResource#getDateCreated()} 044 * and {@link org.opencms.file.CmsResource#getLength()}. Optional parameters can be appended to this name, 045 * which will be added as a second hash code. This way a file can have multiple versions based on different parameters.<p> 046 * 047 * This cache is usable for resources from the online AND the offline project at the same time, 048 * because any change to a resource will result in a changed hash code. This means a resource changed in the offline 049 * project will have a new hash code compared to the online project. If the resource is identical in the online and 050 * the offline project, the generated hash codes will be the same.<p> 051 * 052 * @since 6.2.0 053 */ 054public class CmsVfsNameBasedDiskCache { 055 056 /** Logger instance for this class. */ 057 private static final Log LOG = CmsLog.getLog(CmsVfsNameBasedDiskCache.class); 058 059 /** The name of the cache base repository folder in the RFS. */ 060 private String m_rfsRepository; 061 062 /** 063 * Creates a new disk cache.<p> 064 * 065 * @param basepath the base path for the cache in the RFS 066 * @param foldername the folder name for this cache, to be used a sub-folder for the base folder 067 */ 068 public CmsVfsNameBasedDiskCache(String basepath, String foldername) { 069 070 // normalize the given folder name 071 m_rfsRepository = CmsFileUtil.normalizePath(basepath + foldername + File.separatorChar); 072 } 073 074 /** 075 * Returns the content of the requested file in the disk cache, or <code>null</code> if the 076 * file is not found in the cache, or is found but outdated.<p> 077 * 078 * @param rfsName the file RFS name to look up in the cache 079 * 080 * @return the content of the requested file in the disk cache, or <code>null</code> 081 */ 082 public byte[] getCacheContent(String rfsName) { 083 084 try { 085 File f = new File(rfsName); 086 if (f.exists()) { 087 long age = f.lastModified(); 088 if ((System.currentTimeMillis() - age) > 3600000) { 089 // file has not been touched for 1 hour, touch the file with the current date 090 f.setLastModified(System.currentTimeMillis()); 091 } 092 return CmsFileUtil.readFile(f); 093 } 094 } catch (IOException e) { 095 // unable to read content 096 LOG.debug("Unable to read file " + rfsName, e); 097 } 098 return null; 099 } 100 101 /** 102 * Returns the RFS name to use for caching the given VFS resource with parameters in the disk cache.<p> 103 * 104 * @param resource the VFS resource to generate the cache name for 105 * @param parameters the parameters of the request to the VFS resource 106 * 107 * @return the RFS name to use for caching the given VFS resource with parameters 108 */ 109 public String getCacheName(CmsResource resource, String parameters) { 110 111 // calculate the base cache path for the resource 112 String rfsName = m_rfsRepository + resource.getRootPath(); 113 String extension = CmsFileUtil.getExtension(rfsName); 114 115 // create a StringBuffer for the result 116 StringBuffer buf = new StringBuffer(rfsName.length() + 24); 117 buf.append(rfsName.substring(0, rfsName.length() - extension.length())); 118 119 // calculate a hash code that contains the resource DateLastModified, DateCreated and Length 120 StringBuffer ext = new StringBuffer(48); 121 ext.append(resource.getDateLastModified()); 122 ext.append(';'); 123 ext.append(resource.getDateCreated()); 124 if (resource.getLength() > 0) { 125 ext.append(';'); 126 ext.append(resource.getLength()); 127 } 128 // append hash code to the result buffer 129 buf.append('_'); 130 buf.append(ext.toString().hashCode()); 131 132 // check if parameters are provided, if so add them as well 133 if (parameters != null) { 134 buf.append('_'); 135 buf.append(parameters.hashCode()); 136 } 137 138 // finally append the extension from the original file name 139 buf.append(extension); 140 return buf.toString(); 141 } 142 143 /** 144 * Returns the absolute path of the cache repository in the RFS.<p> 145 * 146 * @return the absolute path of the cache repository in the RFS 147 */ 148 public String getRepositoryPath() { 149 150 return m_rfsRepository; 151 } 152 153 /** 154 * Returns the the requested file is available within the cache.<p> 155 * 156 * @param rfsName the file name 157 * 158 * @return <code>true</code> if the file is available 159 */ 160 public boolean hasCacheContent(String rfsName) { 161 162 File f = new File(rfsName); 163 if (f.exists()) { 164 return true; 165 } 166 return false; 167 } 168 169 /** 170 * Saves the given file content in the disk cache.<p> 171 * 172 * @param rfsName the RFS name of the file to save the content in 173 * @param content the content of the file to save 174 * 175 * @throws IOException in case of disk access errors 176 */ 177 public void saveCacheFile(String rfsName, byte[] content) throws IOException { 178 179 CmsVfsDiskCache.saveFile(rfsName, content); 180 } 181}