KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/cache/CmsMemoryObjectCache.java,v $
3  * Date : $Date: 2006/10/26 12:25:35 $
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.main.CmsEvent;
35 import org.opencms.main.CmsLog;
36 import org.opencms.main.I_CmsEventListener;
37 import org.opencms.main.OpenCms;
38 import org.opencms.xml.Messages;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import org.apache.commons.logging.Log;
44
45 /**
46  * A singleton memory cache, that stores objects related with keys.<p>
47  *
48  * This cache listens the a {@link I_CmsEventListener#EVENT_CLEAR_CACHES} event only.<p>
49  *
50  * @author Alexander Kandzior
51  *
52  * @version $Revision: 1.2 $
53  *
54  * @since 6.2.3
55  */

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

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

84     public static CmsMemoryObjectCache getInstance() {
85
86         if (m_instance == null) {
87             m_instance = new CmsMemoryObjectCache();
88         }
89         return m_instance;
90     }
91
92     /**
93      * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent)
94      */

95     public void cmsEvent(CmsEvent event) {
96
97         switch (event.getType()) {
98             case I_CmsEventListener.EVENT_CLEAR_CACHES:
99                 // flush cache
100
m_cache.clear();
101                 if (LOG.isDebugEnabled()) {
102                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_ER_FLUSHED_CACHES_0));
103                 }
104                 break;
105             default:
106         // no operation
107
}
108     }
109
110     /**
111      * Returns an object from the cache.<p>
112      *
113      * @param owner the owner class of the cached object (used to ensure keys don't overlapp)
114      * @param key the key to lookup the object for
115      *
116      * @return an object from the cache, or <code>null</code> if no object matches the given key
117      */

118     public Object JavaDoc getCachedObject(Class JavaDoc owner, String JavaDoc key) {
119
120         key = owner.getName().concat(key);
121         return m_cache.get(key);
122     }
123
124     /**
125      * Puts an object into the cache.<p>
126      *
127      * @param owner the owner class of the cached object (used to ensure keys don't overlapp)
128      * @param key the key to store the object at
129      * @param value the object to store
130      */

131     public void putCachedObject(Class JavaDoc owner, String JavaDoc key, Object JavaDoc value) {
132
133         key = owner.getName().concat(key);
134         m_cache.put(key, value);
135     }
136
137     /**
138      * Registers all required event listeners.<p>
139      */

140     protected void registerEventListener() {
141
142         // register this object as event listener
143
OpenCms.addCmsEventListener(this, new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES});
144     }
145 }
Popular Tags