Source Code Cross Referenced for CmsFreeMarkerTemplateCache.java in  » Content-Management-System » opencms » org » opencms » frontend » layoutpage » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » opencms » org.opencms.frontend.layoutpage 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.