KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > cache > CmsVfsMemoryObjectCache


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/cache/CmsVfsMemoryObjectCache.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.cache;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.main.CmsEvent;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.I_CmsEventListener;
39 import org.opencms.main.OpenCms;
40 import org.opencms.xml.Messages;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import org.apache.commons.logging.Log;
47
48 /**
49  * Implements a memory cache, that stores objects related to VFS files,
50  * providing a cache for the "online" and another for the "offline" project.<p>
51  *
52  * @author Alexander Kandzior
53  * @author Michael Emmerich
54  *
55  * @version $Revision: 1.2 $
56  *
57  * @since 6.1.3
58  */

59 public final class CmsVfsMemoryObjectCache implements I_CmsEventListener {
60
61     /** The log object for this class. */
62     private static final Log LOG = CmsLog.getLog(CmsVfsMemoryObjectCache.class);
63
64     /** A cache that maps VFS resource names to Objects. */
65     private static CmsVfsMemoryObjectCache m_vfsMemoryObjectCache;
66
67     /** The cache map. */
68     private Map JavaDoc m_cache;
69
70     /**
71      * Constructor, creates a new CmsVfsMemoryObjectCache.<p>
72      */

73     private CmsVfsMemoryObjectCache() {
74
75         m_cache = new HashMap JavaDoc();
76         // register the cache in the memory monitor
77
OpenCms.getMemoryMonitor().register(CmsVfsMemoryObjectCache.class.getName() + ".m_cache", m_cache);
78         // register the event listeners
79
registerEventListener();
80     }
81
82     /**
83      * Returns the VFS memory Object cache.<p>
84      *
85      * @return the VFS memory Object cache
86      */

87     public static CmsVfsMemoryObjectCache getVfsMemoryObjectCache() {
88
89         if (m_vfsMemoryObjectCache == null) {
90             m_vfsMemoryObjectCache = new CmsVfsMemoryObjectCache();
91         }
92         return m_vfsMemoryObjectCache;
93     }
94
95     /**
96      * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent)
97      */

98     public void cmsEvent(CmsEvent event) {
99
100         CmsResource resource;
101         switch (event.getType()) {
102             case I_CmsEventListener.EVENT_PUBLISH_PROJECT:
103             case I_CmsEventListener.EVENT_CLEAR_CACHES:
104                 // flush cache
105
m_cache.clear();
106                 if (LOG.isDebugEnabled()) {
107                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_ER_FLUSHED_CACHES_0));
108                 }
109                 break;
110             case I_CmsEventListener.EVENT_RESOURCE_MODIFIED:
111                 resource = (CmsResource)event.getData().get("resource");
112                 uncacheSystemId(resource.getRootPath());
113                 break;
114             case I_CmsEventListener.EVENT_RESOURCE_DELETED:
115                 List JavaDoc resources = (List JavaDoc)event.getData().get("resources");
116                 for (int i = 0; i < resources.size(); i++) {
117                     resource = (CmsResource)resources.get(i);
118                     uncacheSystemId(resource.getRootPath());
119                 }
120                 break;
121             default:
122         // no operation
123
}
124     }
125
126     /**
127      * Get anobject from the cache.<p>
128      *
129      * @param cms the CmsObject
130      * @param rootPath the rootPath of the VFS resource to get the object for
131      * @return object form cache or null
132      */

133     public Object JavaDoc getCachedObject(CmsObject cms, String JavaDoc rootPath) {
134
135         String JavaDoc key = getCacheKeyForCurrentProject(cms, rootPath);
136         return m_cache.get(key);
137     }
138
139     /**
140      * Puts an object into the cache.<p>
141      *
142      * @param cms the CmsObject
143      * @param rootPath the rootPath of the VFS resource to store the object for
144      * @param value the object to store
145      */

146     public void putCachedObject(CmsObject cms, String JavaDoc rootPath, Object JavaDoc value) {
147
148         String JavaDoc key = getCacheKeyForCurrentProject(cms, rootPath);
149         m_cache.put(key, value);
150     }
151
152     /**
153      * Registers all required event listeners.<p>
154      */

155     protected void registerEventListener() {
156
157         // register this object as event listener
158
OpenCms.addCmsEventListener(this, new int[] {
159             I_CmsEventListener.EVENT_CLEAR_CACHES,
160             I_CmsEventListener.EVENT_PUBLISH_PROJECT,
161             I_CmsEventListener.EVENT_RESOURCE_MODIFIED,
162             I_CmsEventListener.EVENT_RESOURCE_DELETED});
163     }
164
165     /**
166      * Returns a cache key for the given system id (filename) based on the status
167      * of the given project flag.<p>
168      *
169      * @param systemId the system id (filename) to get the cache key for
170      * @param online indicates if this key is generated for the online project
171      *
172      * @return the cache key for the system id
173      */

174     private String JavaDoc getCacheKey(String JavaDoc systemId, boolean online) {
175
176         if (online) {
177             return "online_".concat(systemId);
178         }
179         return "offline_".concat(systemId);
180     }
181
182     /**
183      * Returns a cache key for the given root path based on the status
184      * of the given CmsObject.<p>
185      *
186      * @param systemId the system id (filename) to get the cache key for
187      *
188      * @return the cache key for the system id
189      */

190     private String JavaDoc getCacheKeyForCurrentProject(CmsObject cms, String JavaDoc rootPath) {
191
192         // check the project
193
boolean project = (cms != null) ? cms.getRequestContext().currentProject().isOnlineProject() : false;
194
195         return getCacheKey(rootPath, project);
196     }
197
198     /**
199      * Uncaches a system id (filename) from the internal offline temporary and content defintions caches.<p>
200      *
201      * The online resources cached for the online project are only flushed when a project is published.<p>
202      *
203      * @param systemId the system id (filename) to uncache
204      */

205     private void uncacheSystemId(String JavaDoc systemId) {
206
207         Object JavaDoc o;
208         o = m_cache.remove(getCacheKey(systemId, false));
209         if ((null != o) && LOG.isDebugEnabled()) {
210             LOG.debug(Messages.get().getBundle().key(Messages.LOG_ER_UNCACHED_SYS_ID_1, getCacheKey(systemId, false)));
211         }
212     }
213 }
Popular Tags