KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > CmsTemplateParts


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateParts.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.20 $
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.frontend.templateone;
33
34 import org.opencms.i18n.CmsMessages;
35 import org.opencms.jsp.CmsJspActionElement;
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
41 import java.util.Collections JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.util.Map JavaDoc;
44
45 import org.apache.commons.collections.map.LRUMap;
46 import org.apache.commons.logging.Log;
47
48 /**
49  * Stores static Strings to generate HTML output parts for the template in a Map.<p>
50  *
51  * An instance of this class is stored in the OpenCms runtime properties.<p>
52  *
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.20 $
56  *
57  * @since 6.0.0
58  */

59 public final class CmsTemplateParts implements I_CmsEventListener {
60
61     /** Key name for an illegal key. */
62     public static final String JavaDoc KEY_ILLEGAL = "illpart";
63
64     /** The log object for this class. */
65     private static final Log LOG = CmsLog.getLog(CmsTemplateParts.class);
66
67     /** The Singleton instance. */
68     private static CmsTemplateParts m_instance;
69
70     /** The internal map of cached template parts. */
71     private Map JavaDoc m_parts;
72
73     /**
74      * Hidden constructor.<p>
75      *
76      * Use the getInstance(CmsJspActionElement) method to get an initialized instance of this class.<p>
77      */

78     private CmsTemplateParts() {
79
80         // create new Map
81
initPartsMap();
82         // add an event listener
83
OpenCms.addCmsEventListener(this);
84     }
85
86     /**
87      * Returns an instance of the class fetched from the application context attribute.<p>
88      *
89      * @return an instance of the class
90      */

91     public static CmsTemplateParts getInstance() {
92
93         if (m_instance == null) {
94             // initialize the Singleton instance
95
m_instance = new CmsTemplateParts();
96         }
97         return m_instance;
98     }
99
100     /**
101      * Sets a part in the cache with the specified key and value.<p>
102      *
103      * @param partKey the key to identify the part
104      * @param value the value to cache
105      */

106     public void addPart(String JavaDoc partKey, String JavaDoc value) {
107
108         if (!partKey.equals(KEY_ILLEGAL)) {
109             // only store part if valid part key was found
110
m_parts.put(partKey, value);
111         }
112     }
113
114     /**
115      * Implements the CmsEvent interface, clears the template parts on publish and clear cache events.<p>
116      *
117      * @param event CmsEvent that has occurred
118      */

119     public void cmsEvent(CmsEvent event) {
120
121         switch (event.getType()) {
122             case I_CmsEventListener.EVENT_PUBLISH_PROJECT:
123             case I_CmsEventListener.EVENT_CLEAR_CACHES:
124             case I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR:
125             case I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY:
126                 // flush Map
127
initPartsMap();
128                 if (LOG.isDebugEnabled()) {
129                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_CMSTEMPLATEPARTS_CLEARED_0));
130                 }
131                 break;
132             default: // no operation
133
}
134     }
135
136     /**
137      * Returns a previously cached part of template one with the specified key, or null, if no part is found.<p>
138      *
139      * @param partKey the key to identify the part
140      * @return a previously cached part of template one with the specified key
141      */

142     public String JavaDoc getPart(String JavaDoc partKey) {
143
144         return (String JavaDoc)m_parts.get(partKey);
145     }
146
147     /**
148      * Returns the content of the specified JSP target file depending on the element and the layout to display.<p>
149      *
150      * @param target the target uri of the file in the OpenCms VFS (can be relative or absolute)
151      * @param element the element (template selector) to display from the target
152      * @param layout the layout type of the template to get
153      * @param jsp the JSP page to generate the content with
154      *
155      * @return the content of the JSP target file
156      */

157     public String JavaDoc includePart(String JavaDoc target, String JavaDoc element, String JavaDoc layout, CmsJspActionElement jsp) {
158
159         if (OpenCms.getRunLevel() < OpenCms.RUNLEVEL_4_SERVLET_ACCESS) {
160             // OpenCms is not in servlet based operating mode, return empty String
161
return "";
162         }
163         String JavaDoc part = null;
164         String JavaDoc partKey = "";
165
166         try {
167             // generate a unique key for the included part
168
partKey = generateKey(
169                 target,
170                 element,
171                 layout,
172                 jsp.getRequestContext().getLocale(),
173                 jsp.getRequestContext().currentProject().getId());
174             // try to get the part
175
part = (String JavaDoc)m_parts.get(partKey);
176             if (part == null) {
177                 // part not found, get the content of the JSP element and put it to the Map store
178
part = jsp.getContent(target, element, jsp.getRequestContext().getLocale());
179                 if (part != null && !part.startsWith(CmsMessages.UNKNOWN_KEY_EXTENSION)) {
180                     // add part to map if a valid content was found
181
addPart(partKey, part);
182                 } else {
183                     // prevent displaying rubbish
184
part = "";
185                 }
186                 if (LOG.isDebugEnabled()) {
187                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_INCLUDE_PART_NOT_FOUND_1, partKey));
188                 }
189             } else if (LOG.isDebugEnabled()) {
190                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INCLUDE_PART_FOUND_1, partKey));
191             }
192         } catch (Throwable JavaDoc t) {
193             // catch all errors to avoid displaying rubbish
194
part = "";
195             if (LOG.isDebugEnabled()) {
196                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INCLUDE_PART_ERR_2, partKey, t));
197             }
198         }
199         return part;
200     }
201
202     /**
203      * Generates a part key depending on the included target, the element and the layout do display.<p>
204      *
205      * @param target the target uri of the file in the OpenCms VFS (can be relative or absolute)
206      * @param element the element (template selector) to display from the target
207      * @param layout the layout type of the template to get
208      * @return a unique part key
209      */

210     private String JavaDoc generateKey(String JavaDoc target, String JavaDoc element, String JavaDoc layout, Locale JavaDoc locale, int project) {
211
212         try {
213             if (element == null) {
214                 // set element name to empty String for key generation
215
element = "";
216             }
217             // generate the key to identify the current part
218
StringBuffer JavaDoc partKey = new StringBuffer JavaDoc(32);
219             partKey.append(target);
220             partKey.append("_");
221             partKey.append(element);
222             partKey.append("_");
223             partKey.append(layout);
224             partKey.append("_");
225             partKey.append(locale);
226             partKey.append("_");
227             partKey.append(project);
228             return partKey.toString();
229         } catch (Exception JavaDoc e) {
230             // error creating key
231
return KEY_ILLEGAL;
232         }
233     }
234
235     /**
236      * Initializes (also clears) the internal part cache map.<p>
237      */

238     private synchronized Map JavaDoc initPartsMap() {
239
240         LRUMap cacheParts = new LRUMap(512);
241         Map JavaDoc oldParts = m_parts;
242         m_parts = Collections.synchronizedMap(cacheParts);
243         if (oldParts != null) {
244             oldParts.clear();
245             oldParts = null;
246         }
247         return m_parts;
248     }
249 }
Popular Tags