001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/layoutpage/CmsFreeMarkerTemplateCache.java,v $
003: * Date : $Date: 2008-02-27 12:05:46 $
004: * Version: $Revision: 1.4 $
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.frontend.layoutpage;
033:
034: import freemarker.cache.StringTemplateLoader;
035: import freemarker.template.Configuration;
036: import freemarker.template.Template;
037:
038: import org.opencms.file.CmsObject;
039: import org.opencms.main.CmsEvent;
040: import org.opencms.main.CmsException;
041: import org.opencms.main.CmsLog;
042: import org.opencms.main.I_CmsEventListener;
043: import org.opencms.main.OpenCms;
044:
045: import java.io.IOException;
046:
047: import org.apache.commons.logging.Log;
048:
049: /**
050: * Cache implementation that provides access to cached FreeMarker templates.<p>
051: *
052: * Use this class with caution! It might be moved to the OpenCms core packages in the future.<p>
053: *
054: * @author Andreas Zahner
055: *
056: * @since 6.2.0
057: */
058: public final class CmsFreeMarkerTemplateCache implements
059: I_CmsEventListener {
060:
061: /** The log object for this class. */
062: private static final Log LOG = CmsLog
063: .getLog(CmsFreeMarkerTemplateCache.class);
064:
065: /** The singleton instance. */
066: private static CmsFreeMarkerTemplateCache m_instance;
067:
068: /** The FreeMarker configuration object used for caching. */
069: private Configuration m_configuration;
070:
071: /**
072: * Hidden constructor.<p>
073: */
074: private CmsFreeMarkerTemplateCache() {
075:
076: // initialize the FreeMarker configuration
077: initConfiguration();
078:
079: // add an event listener to clear the cache on clear events
080: OpenCms.addCmsEventListener(this );
081: }
082:
083: /**
084: * Returns an instance of the class fetched from the application context attribute.<p>
085: *
086: * @return an instance of the class
087: */
088: public static CmsFreeMarkerTemplateCache getInstance() {
089:
090: if (m_instance == null) {
091: // initialize the Singleton instance
092: m_instance = new CmsFreeMarkerTemplateCache();
093: }
094: return m_instance;
095: }
096:
097: /**
098: * Implements the CmsEvent interface, clears the cached template parts on publish and clear cache events.<p>
099: *
100: * @param event CmsEvent that has occurred
101: */
102: public void cmsEvent(CmsEvent event) {
103:
104: switch (event.getType()) {
105: case I_CmsEventListener.EVENT_PUBLISH_PROJECT:
106: case I_CmsEventListener.EVENT_CLEAR_CACHES:
107: case I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR:
108: case I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY:
109: // flush cache
110: clearTemplateCache();
111: if (LOG.isDebugEnabled()) {
112: LOG.debug(Messages.get().getBundle().key(
113: Messages.LOG_CMSMACROMANAGER_CLEARED_0));
114: }
115: break;
116: default: // no operation
117: }
118: }
119:
120: /**
121: * Returns the template with the given key.<p>
122: *
123: * @param cms the OpenCms user context to use
124: * @param fileName the absolute path of the template file in the VFS of OpenCms
125: * @return the template with the given key
126: * @throws CmsException if the specified template file can not be found
127: * @throws IOException if the template with the given key can not be found in the cache
128: */
129: public Template getTemplate(CmsObject cms, String fileName)
130: throws CmsException, IOException {
131:
132: if (hasTemplate(fileName)) {
133: return m_configuration.getTemplate(getCacheKey(fileName));
134: } else {
135: String encoding = cms.getRequestContext().getEncoding();
136: // get the VFS file content with the FreeMarker macros
137: byte[] content = cms.readFile(fileName).getContents();
138: StringBuffer fTemplate = new StringBuffer(new String(
139: content, encoding));
140: // append the code to execute a single, specified macro of the template file
141: fTemplate.append("<@");
142: fTemplate.append(CmsMacroWrapperFreeMarker.MACRO_NAME);
143: fTemplate.append(" />");
144: // put the template to the template cache and return it
145: return putTemplate(fileName, fTemplate.toString());
146: }
147: }
148:
149: /**
150: * Returns true if the template with the given key exists in the cache, otherwise false.<p>
151: *
152: * @param fileName the absolute path of the template file in the VFS of OpenCms
153: * @return true if the template with the given key exists in the cache, otherwise false
154: */
155: public boolean hasTemplate(String fileName) {
156:
157: try {
158: m_configuration.getTemplate(getCacheKey(fileName));
159: return true;
160: } catch (IOException e) {
161: return false;
162: }
163: }
164:
165: /**
166: * Clears the template cache, removes all previously cached templates.<p>
167: */
168: private void clearTemplateCache() {
169:
170: m_configuration.clearTemplateCache();
171: // the loader has to be reset, too, otherwise clearing the cache will have no effect
172: m_configuration.setTemplateLoader(new StringTemplateLoader());
173: }
174:
175: /**
176: * Returns a cache key that is used for the template cache.<p>
177: *
178: * @param fileName the absolute path of the template file in the VFS of OpenCms to get the cache key from
179: * @return a cache key that is used for the template cache
180: */
181: private String getCacheKey(String fileName) {
182:
183: return String.valueOf(fileName.hashCode());
184: }
185:
186: /**
187: * Initializes the cache objects.<p>
188: */
189: private void initConfiguration() {
190:
191: m_configuration = new Configuration();
192: m_configuration.setDefaultEncoding(OpenCms.getSystemInfo()
193: .getDefaultEncoding());
194: m_configuration.setTemplateLoader(new StringTemplateLoader());
195: }
196:
197: /**
198: * Puts the template with the given key and source code to the template cache.<p>
199: *
200: * @param fileName the absolute path of the template file in the VFS of OpenCms
201: * @param templateSource the source code of the template
202: * @return the template that was stored in the cache
203: */
204: private Template putTemplate(String fileName, String templateSource) {
205:
206: String cacheKey = getCacheKey(fileName);
207: ((StringTemplateLoader) m_configuration.getTemplateLoader())
208: .putTemplate(cacheKey, templateSource);
209: try {
210: return m_configuration.getTemplate(cacheKey);
211: } catch (IOException e) {
212: return null;
213: }
214: }
215:
216: }
|