KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > TaggingContext


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.util.Assert;
31 import org.springframework.util.StringUtils;
32
33 /**
34  * Provides static methods to tag cache items.
35  */

36 public class TaggingContext {
37
38     private static final String JavaDoc REQUEST_ATTRIBUTE =
39             TaggingContext.class.getName();
40     
41     private Log log = LogFactory.getLog(TaggingContext.class);
42     
43     private TaggingContext parent;
44         
45     private String JavaDoc[] tags;
46             
47     private TaggingContext(TaggingContext parent) {
48         this.parent = parent;
49     }
50     
51     public TaggingContext getParent() {
52         return this.parent;
53     }
54
55     public void addTag(String JavaDoc tag) {
56         if (tags == null) {
57             tags = new String JavaDoc[] { tag };
58         }
59         else {
60             tags = StringUtils.addStringToArray(tags, tag);
61         }
62         if (parent != null) {
63             parent.addTag(tag);
64         }
65         else {
66             log.debug("Adding tag: " + tag);
67         }
68     }
69     
70     public String JavaDoc[] getTags() {
71         return this.tags;
72     }
73
74     public static void tag(HttpServletRequest JavaDoc request, String JavaDoc tag) {
75         Assert.notNull(tag, "Tag must not be null!");
76         TaggingContext context = getContext(request);
77         if (context != null) {
78             context.addTag(tag);
79         }
80     }
81     
82     public static void openNestedContext(HttpServletRequest JavaDoc request) {
83         TaggingContext parent = getContext(request);
84         setContext(request, new TaggingContext(parent));
85     }
86     
87     public static String JavaDoc[] popTags(HttpServletRequest JavaDoc request) {
88         TaggingContext top = getContext(request);
89         if (top != null) {
90             setContext(request, top.getParent());
91             return top.getTags();
92         }
93         return null;
94     }
95     
96     private static TaggingContext getContext(HttpServletRequest JavaDoc request) {
97         return (TaggingContext) request.getAttribute(REQUEST_ATTRIBUTE);
98     }
99     
100     private static void setContext(HttpServletRequest JavaDoc request,
101             TaggingContext context) {
102         
103         request.setAttribute(REQUEST_ATTRIBUTE, context);
104     }
105     
106 }
107
Popular Tags