KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > PageElementCacheTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: PageElementCacheTag.java,v 1.10 2007/01/07 06:14:09 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.core.www;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.jsp.JspException JavaDoc;
31 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
32
33 import org.opensubsystems.core.util.CallContext;
34
35 /**
36  * Custom tag providing ability to cache elements of the page usually from
37  * custom tags and then display them at later point when requested. The main
38  * purpose is to allow other tags to specify elements, such as css includes or
39  * javascript includes at one spot, e.g. using initialization tag. These elements
40  * will then be place on a specific places at the page using this tag. It also
41  * allows derived tags to cache their content and then other tags to fetch them
42  * and display at other places.
43  *
44  * @version $Id: PageElementCacheTag.java,v 1.10 2007/01/07 06:14:09 bastafidli Exp $
45  * @author Miro Halas
46  * @code.reviewer Miro Halas
47  * @code.reviewed 1.7 2006/04/13 00:21:23 jlegeny
48  */

49 public class PageElementCacheTag extends BodyTagSupport JavaDoc
50 {
51    // Constants ////////////////////////////////////////////////////////////////
52

53    /**
54     * Messages collected during processing of current request.
55     */

56    public static final String JavaDoc PAGE_ELEMENTS_CACHE = "pageelements";
57    
58    /**
59     * CSS style sheets cache.
60     */

61    public static final String JavaDoc CSS_ELEMENT = "css";
62
63    /**
64     * JavaScript js import cache.
65     */

66    public static final String JavaDoc JS_ELEMENT = "js";
67
68    /**
69     * JavaScript script cache.
70     */

71    public static final String JavaDoc SCRIPT_ELEMENT = "script";
72
73    /**
74     * Indicator to use in attributes to signal to retrieve content from the cache.
75     */

76    public static final String JavaDoc CACHE_INDICATOR = "cache:";
77
78    // Attributes ///////////////////////////////////////////////////////////////
79

80    /**
81     * Generated serial version id for this class.
82     */

83    private static final long serialVersionUID = -2506816420084812132L;
84
85    /**
86     * Id of the cached element to retrieve.
87     */

88    protected String JavaDoc m_strId;
89    
90    /**
91     * If this id is specified then the content will not be printed into output
92     * but instead will be cached under id specified in this attribute.
93     */

94    protected String JavaDoc m_strCacheas;
95    
96    // Constructors /////////////////////////////////////////////////////////////
97

98    /**
99     * Constructor for custom tag.
100     */

101    public PageElementCacheTag()
102    {
103       super();
104       
105       m_strId = "";
106       m_strCacheas = "";
107    }
108    
109    // Business logic ///////////////////////////////////////////////////////////
110

111    /**
112     * {@inheritDoc}
113     */

114    public int doStartTag(
115    ) throws JspException JavaDoc
116    {
117       String JavaDoc strOutput;
118       
119       strOutput = getCachedContent(getId());
120       
121       if ((m_strCacheas != null) && (m_strCacheas.length() > 0))
122       {
123          if (strOutput != null)
124          {
125             cache(m_strCacheas, strOutput);
126          }
127       }
128       else
129       {
130          TagUtils.write(pageContext, strOutput);
131       }
132       
133       return (SKIP_BODY);
134    }
135
136    /**
137     * {@inheritDoc}
138     */

139    public int doEndTag(
140    ) throws JspException JavaDoc
141    {
142       return (EVAL_PAGE);
143    }
144
145    /**
146     * @return String - Id of the cached element to retrieve.
147     */

148    public String JavaDoc getId(
149    )
150    {
151       return m_strId;
152    }
153
154    /**
155     * @return String - New Id under which the output will be cached
156     */

157    public String JavaDoc getCacheas(
158    )
159    {
160       return m_strCacheas;
161    }
162
163    /**
164     * @param strId - Id of the cached element to retrieve.
165     */

166    public void setId(
167       String JavaDoc strId
168    )
169    {
170       m_strId = strId;
171    }
172    
173    /**
174     * @param strCacheas - New Id under which the output will be cached
175     */

176    public void setCacheas(
177       String JavaDoc strCacheas
178    )
179    {
180       m_strCacheas = strCacheas;
181    }
182
183    // Helper methods ///////////////////////////////////////////////////////////
184

185    /**
186     * Add item for specific page element to the cache.
187     *
188     * @param strPageElementId - constant representing HTML page element for which
189     * the item will be cached
190     * @param strItemToCache - element to cache
191     */

192    protected void cache(
193       String JavaDoc strPageElementId,
194       String JavaDoc strItemToCache
195    )
196    {
197       // We have to use thread based cache since if page is constracted using
198
// Tiles then each include has a separate page context
199
Map JavaDoc cache = (Map JavaDoc)CallContext.getInstance().getCache(PAGE_ELEMENTS_CACHE);
200       
201       List JavaDoc items;
202       
203       items = (List JavaDoc)cache.get(strPageElementId.toLowerCase());
204       if (items == null)
205       {
206          items = new ArrayList JavaDoc();
207          cache.put(strPageElementId.toLowerCase(), items);
208       }
209       items.add(strItemToCache);
210    }
211
212    /**
213     * Get the cached content.
214     *
215     * @param strPageElementId - id of the cached element to get
216     * @return String - cached content or empty string if nothing is cached,
217     * but never null
218     */

219    protected String JavaDoc getCachedContent(
220       String JavaDoc strPageElementId
221    )
222    {
223       return getCachedContent(strPageElementId, true);
224    }
225
226
227    /**
228     * Get the cached content.
229     *
230     * @param strPageElementId - id of the cached element to get
231     * @param bRemove - remove the element from the cache after it is retrieved
232     * @param strSeparator - separator for particular cached items
233     * @return String - cached content or empty string if nothing is cached,
234     * but never null
235     */

236    protected String JavaDoc getCachedContent(
237       String JavaDoc strPageElementId,
238       boolean bRemove,
239       String JavaDoc strSeparator
240    )
241    {
242       // We have to use thread based cache since if page is constracted using
243
// Tiles then each include has a separate page context
244
Map JavaDoc cache = (Map JavaDoc)CallContext.getInstance().getCache(PAGE_ELEMENTS_CACHE);
245       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
246       
247       if (cache != null)
248       {
249          List JavaDoc items;
250          
251          if (bRemove)
252          {
253             items = (List JavaDoc)cache.remove(strPageElementId.toLowerCase());
254          }
255          else
256          {
257             items = (List JavaDoc)cache.get(strPageElementId.toLowerCase());
258          }
259          if ((items != null) && (!items.isEmpty()))
260          {
261             Iterator JavaDoc iter;
262             
263             for (iter = items.iterator(); iter.hasNext();)
264             {
265                sbHtml.append(iter.next());
266                sbHtml.append(strSeparator);
267             }
268          }
269       }
270       
271       return sbHtml.toString();
272    }
273
274    /**
275     * Get the cached content.
276     *
277     * @param strPageElementId - id of the cached element to get
278     * @param bRemove - remove the element from the cache after it is retrieved
279     * @return String - cached content or empty string if nothing is cached,
280     * but never null
281     */

282    protected String JavaDoc getCachedContent(
283       String JavaDoc strPageElementId,
284       boolean bRemove
285    )
286    {
287       return getCachedContent(strPageElementId, bRemove, "\n");
288    }
289
290    /**
291     * Get the elements of cached content elements.
292     *
293     * @param strPageElementId - id of the cached element to get
294     * @param bRemove - remove the element from the cache after it is retrieved
295     * @return Lists - cached content or empty List if nothing is cached,
296     * but never null
297     */

298    protected List JavaDoc getCachedContentElements(
299       String JavaDoc strPageElementId,
300       boolean bRemove
301    )
302    {
303       // We have to use thread based cache since if page is constracted using
304
// Tiles then each include has a separate page context
305
Map JavaDoc cache = (Map JavaDoc)CallContext.getInstance().getCache(PAGE_ELEMENTS_CACHE);
306       List JavaDoc items = null;
307       
308       if (cache != null)
309       {
310          if (bRemove)
311          {
312             items = (List JavaDoc)cache.remove(strPageElementId.toLowerCase());
313          }
314          else
315          {
316             items = (List JavaDoc)cache.get(strPageElementId.toLowerCase());
317          }
318       }
319       if (items == null)
320       {
321          items = Collections.EMPTY_LIST;
322       }
323       
324       return items;
325    }
326 }
327
Popular Tags