001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (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 GmbH & Co. KG, 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.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.main.CmsLog; 033import org.opencms.main.OpenCms; 034import org.opencms.monitor.CmsMemoryMonitor; 035 036import org.apache.commons.collections.Transformer; 037import org.apache.commons.logging.Log; 038 039/** 040 * Implements a memory cache, that stores objects related to VFS files, 041 * providing a cache for the "online" and another for the "offline" project.<p> 042 * 043 * @since 6.1.3 044 */ 045public class CmsVfsMemoryObjectCache extends CmsVfsCache { 046 047 /** Counts the number of instances created. */ 048 private static int instanceCounter; 049 050 /** The log object for this class. */ 051 private static final Log LOG = CmsLog.getLog(CmsVfsMemoryObjectCache.class); 052 053 /** The id for this instance, which is used for distinguishing cache keys of different instances. */ 054 private int m_id; 055 056 /** 057 * Constructor, creates a new CmsVfsMemoryObjectCache.<p> 058 */ 059 public CmsVfsMemoryObjectCache() { 060 061 m_id = instanceCounter; 062 instanceCounter += 1; 063 // register the event listeners 064 registerEventListener(); 065 } 066 067 /** 068 * Returns the VFS memory Object cache.<p> 069 * 070 * @return the VFS memory Object cache 071 */ 072 public static CmsVfsMemoryObjectCache getVfsMemoryObjectCache() { 073 074 // Changed this so instead of a static variable, it lazily initializes and 075 // returns a member of OpenCmsCore. This makes it better for test cases, because 076 // registerEventListener() only registers an event listener with the *current event manager*, 077 // but in the test cases, for each test class a new OpenCmsCore and event manager are created. 078 // This means that a CmsVfsMemoryObjectCache initialized in one test class will not get flushed in 079 // another, which makes using static variables a bad idea in this case. 080 return OpenCms.getVfsMemoryObjectCache(); 081 } 082 083 /** 084 * Return an object from the cache.<p> 085 * 086 * @param cms the current users OpenCms context 087 * @param rootPath the rootPath of the VFS resource to get the object for 088 * @return object form cache or null 089 */ 090 public Object getCachedObject(CmsObject cms, String rootPath) { 091 092 String key = getCacheKeyForCurrentProject(cms, rootPath); 093 return OpenCms.getMemoryMonitor().getCachedVfsObject(key); 094 } 095 096 /** 097 * Uses a transformer for loading an object from a path if it has not already been cached, and then caches it.<p> 098 * 099 * @param cms the CMS context 100 * @param rootPath the root path from which the object should be loaded 101 * @param function the function which should load the object from VFS if it isn't already cached 102 * 103 * @return the loaded object 104 */ 105 public Object loadVfsObject(CmsObject cms, String rootPath, Transformer function) { 106 107 Object result = getCachedObject(cms, rootPath); 108 if (result == null) { 109 result = function.transform(rootPath); 110 putCachedObject(cms, rootPath, result); 111 } 112 return result; 113 } 114 115 /** 116 * Puts an object into the cache.<p> 117 * 118 * @param cms the CmsObject 119 * @param rootPath the rootPath of the VFS resource to store the object for 120 * @param value the object to store 121 */ 122 public void putCachedObject(CmsObject cms, String rootPath, Object value) { 123 124 String key = getCacheKeyForCurrentProject(cms, rootPath); 125 OpenCms.getMemoryMonitor().cacheVfsObject(key, value); 126 } 127 128 /** 129 * @see org.opencms.cache.CmsVfsCache#flush(boolean) 130 */ 131 @Override 132 protected void flush(boolean online) { 133 134 OpenCms.getMemoryMonitor().flushCache(CmsMemoryMonitor.CacheType.VFS_OBJECT); 135 } 136 137 /** 138 * @see org.opencms.cache.CmsVfsCache#uncacheResource(org.opencms.file.CmsResource) 139 */ 140 @Override 141 protected void uncacheResource(CmsResource resource) { 142 143 uncacheSystemId(resource.getRootPath()); 144 } 145 146 /** 147 * Returns a cache key for the given system id (filename) based on the status 148 * of the given project flag.<p> 149 * 150 * @param systemId the system id (filename) to get the cache key for 151 * @param online indicates if this key is generated for the online project 152 * 153 * @return the cache key for the system id 154 */ 155 private String getCacheKey(String systemId, boolean online) { 156 157 if (online) { 158 return "online_(" + m_id + ")_" + systemId; 159 } 160 return "offline_(" + m_id + ")_" + systemId; 161 } 162 163 /** 164 * Returns a cache key for the given root path based on the status 165 * of the given CmsObject.<p> 166 * 167 * @param cms the cms context 168 * @param rootPath the filename to get the cache key for 169 * 170 * @return the cache key for the system id 171 */ 172 private String getCacheKeyForCurrentProject(CmsObject cms, String rootPath) { 173 174 // check the project 175 boolean project = (cms != null) ? cms.getRequestContext().getCurrentProject().isOnlineProject() : false; 176 return getCacheKey(rootPath, project); 177 } 178 179 /** 180 * Uncaches a system id (filename) from the internal offline temporary and content definitions caches.<p> 181 * 182 * The online resources cached for the online project are only flushed when a project is published.<p> 183 * 184 * @param systemId the system id (filename) to uncache 185 */ 186 private void uncacheSystemId(String systemId) { 187 188 String key = getCacheKey(systemId, false); 189 Object o = OpenCms.getMemoryMonitor().getCachedVfsObject(key); 190 OpenCms.getMemoryMonitor().uncacheVfsObject(key); 191 if ((null != o) && LOG.isDebugEnabled()) { 192 LOG.debug( 193 org.opencms.xml.Messages.get().getBundle().key( 194 org.opencms.xml.Messages.LOG_ERR_UNCACHED_SYS_ID_1, 195 key)); 196 } 197 } 198}