001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/cache/CmsVfsMemoryObjectCache.java,v $
003: * Date : $Date: 2008-02-27 12:05:54 $
004: * Version: $Revision: 1.5 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.cache;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsResource;
036: import org.opencms.main.CmsEvent;
037: import org.opencms.main.CmsLog;
038: import org.opencms.main.I_CmsEventListener;
039: import org.opencms.main.OpenCms;
040: import org.opencms.xml.Messages;
041:
042: import java.util.List;
043:
044: import org.apache.commons.logging.Log;
045:
046: /**
047: * Implements a memory cache, that stores objects related to VFS files,
048: * providing a cache for the "online" and another for the "offline" project.<p>
049: *
050: * @author Alexander Kandzior
051: * @author Michael Emmerich
052: *
053: * @version $Revision: 1.5 $
054: *
055: * @since 6.1.3
056: */
057: public final class CmsVfsMemoryObjectCache implements
058: I_CmsEventListener {
059:
060: /** The log object for this class. */
061: private static final Log LOG = CmsLog
062: .getLog(CmsVfsMemoryObjectCache.class);
063:
064: /** A cache that maps VFS resource names to Objects. */
065: private static CmsVfsMemoryObjectCache m_vfsMemoryObjectCache;
066:
067: /**
068: * Constructor, creates a new CmsVfsMemoryObjectCache.<p>
069: */
070: private CmsVfsMemoryObjectCache() {
071:
072: // register the event listeners
073: registerEventListener();
074: }
075:
076: /**
077: * Returns the VFS memory Object cache.<p>
078: *
079: * @return the VFS memory Object cache
080: */
081: public static CmsVfsMemoryObjectCache getVfsMemoryObjectCache() {
082:
083: if (m_vfsMemoryObjectCache == null) {
084: m_vfsMemoryObjectCache = new CmsVfsMemoryObjectCache();
085: }
086: return m_vfsMemoryObjectCache;
087: }
088:
089: /**
090: * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent)
091: */
092: public void cmsEvent(CmsEvent event) {
093:
094: CmsResource resource;
095: switch (event.getType()) {
096: case I_CmsEventListener.EVENT_PUBLISH_PROJECT:
097: case I_CmsEventListener.EVENT_CLEAR_CACHES:
098: // flush cache
099: OpenCms.getMemoryMonitor().flushVfsObjects();
100: if (LOG.isDebugEnabled()) {
101: LOG.debug(Messages.get().getBundle().key(
102: Messages.LOG_ER_FLUSHED_CACHES_0));
103: }
104: break;
105: case I_CmsEventListener.EVENT_RESOURCE_MODIFIED:
106: resource = (CmsResource) event.getData().get("resource");
107: uncacheSystemId(resource.getRootPath());
108: break;
109: case I_CmsEventListener.EVENT_RESOURCE_DELETED:
110: case I_CmsEventListener.EVENT_RESOURCE_MOVED:
111: List resources = (List) event.getData().get("resources");
112: for (int i = 0; i < resources.size(); i++) {
113: resource = (CmsResource) resources.get(i);
114: uncacheSystemId(resource.getRootPath());
115: }
116: break;
117: default:
118: // no operation
119: }
120: }
121:
122: /**
123: * Return an object from the cache.<p>
124: *
125: * @param cms the current users OpenCms context
126: * @param rootPath the rootPath of the VFS resource to get the object for
127: * @return object form cache or null
128: */
129: public Object getCachedObject(CmsObject cms, String rootPath) {
130:
131: String key = getCacheKeyForCurrentProject(cms, rootPath);
132: return OpenCms.getMemoryMonitor().getCachedVfsObject(key);
133: }
134:
135: /**
136: * Puts an object into the cache.<p>
137: *
138: * @param cms the CmsObject
139: * @param rootPath the rootPath of the VFS resource to store the object for
140: * @param value the object to store
141: */
142: public void putCachedObject(CmsObject cms, String rootPath,
143: Object value) {
144:
145: String key = getCacheKeyForCurrentProject(cms, rootPath);
146: OpenCms.getMemoryMonitor().cacheVfsObject(key, value);
147: }
148:
149: /**
150: * Registers all required event listeners.<p>
151: */
152: protected void registerEventListener() {
153:
154: // register this object as event listener
155: OpenCms.addCmsEventListener(this , new int[] {
156: I_CmsEventListener.EVENT_CLEAR_CACHES,
157: I_CmsEventListener.EVENT_PUBLISH_PROJECT,
158: I_CmsEventListener.EVENT_RESOURCE_MODIFIED,
159: I_CmsEventListener.EVENT_RESOURCE_MOVED,
160: I_CmsEventListener.EVENT_RESOURCE_DELETED });
161: }
162:
163: /**
164: * Returns a cache key for the given system id (filename) based on the status
165: * of the given project flag.<p>
166: *
167: * @param systemId the system id (filename) to get the cache key for
168: * @param online indicates if this key is generated for the online project
169: *
170: * @return the cache key for the system id
171: */
172: private String getCacheKey(String systemId, boolean online) {
173:
174: if (online) {
175: return "online_".concat(systemId);
176: }
177: return "offline_".concat(systemId);
178: }
179:
180: /**
181: * Returns a cache key for the given root path based on the status
182: * of the given CmsObject.<p>
183: *
184: * @param cms the cms context
185: * @param rootPath the filename to get the cache key for
186: *
187: * @return the cache key for the system id
188: */
189: private String getCacheKeyForCurrentProject(CmsObject cms,
190: String rootPath) {
191:
192: // check the project
193: boolean project = (cms != null) ? cms.getRequestContext()
194: .currentProject().isOnlineProject() : false;
195:
196: return getCacheKey(rootPath, project);
197: }
198:
199: /**
200: * Uncaches a system id (filename) from the internal offline temporary and content defintions caches.<p>
201: *
202: * The online resources cached for the online project are only flushed when a project is published.<p>
203: *
204: * @param systemId the system id (filename) to uncache
205: */
206: private void uncacheSystemId(String systemId) {
207:
208: String key = getCacheKey(systemId, false);
209: Object o = OpenCms.getMemoryMonitor().getCachedVfsObject(key);
210: OpenCms.getMemoryMonitor().uncacheVfsObject(key);
211: if ((null != o) && LOG.isDebugEnabled()) {
212: LOG.debug(Messages.get().getBundle().key(
213: Messages.LOG_ER_UNCACHED_SYS_ID_1, key));
214: }
215: }
216: }
|