001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.cachius;
025:
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: import javax.servlet.http.HttpServletRequest;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.springframework.util.Assert;
034: import org.springframework.web.context.request.RequestAttributes;
035: import org.springframework.web.context.request.RequestContextHolder;
036:
037: /**
038: * Provides static methods to tag cache items.
039: */
040: public class TaggingContext {
041:
042: private static final String REQUEST_ATTRIBUTE = TaggingContext.class
043: .getName();
044:
045: private Log log = LogFactory.getLog(TaggingContext.class);
046:
047: private HttpServletRequest request;
048:
049: private TaggingContext parent;
050:
051: private HashSet tags;
052:
053: private boolean preventCaching;
054:
055: /**
056: * Private constructor that creates a nested context.
057: */
058: private TaggingContext(HttpServletRequest request,
059: TaggingContext parent) {
060: this .request = request;
061: this .parent = parent;
062: }
063:
064: /**
065: * Returns the parent context, or <code>null</code> if it is the root
066: * context.
067: */
068: public TaggingContext getParent() {
069: return this .parent;
070: }
071:
072: /**
073: * Adds the given tag. If the context is a nested context, the ancestors are
074: * also tagged.
075: *
076: * @throws IllegalArgumentException if the tag is <code>null</code>
077: */
078: public void addTag(String tag) {
079: Assert.notNull(tag, "Tag must not be null.");
080: if (tags == null) {
081: tags = new HashSet();
082: }
083: tags.add(tag);
084: if (parent != null) {
085: parent.addTag(tag);
086: } else {
087: if (log.isDebugEnabled()) {
088: log.debug("Adding tag: " + tag);
089: }
090: }
091: }
092:
093: /**
094: * Sets whether caching should be prevented, i.e. the CacheItem should
095: * be discarded.
096: */
097: public void setPreventCaching(boolean preventCaching) {
098: this .preventCaching = preventCaching;
099: if (parent != null) {
100: parent.setPreventCaching(preventCaching);
101: }
102: }
103:
104: public boolean isPreventCaching() {
105: return preventCaching;
106: }
107:
108: /**
109: * Returns the tags assigned via the {@link #addTag(String)} method.
110: */
111: public Set getTags() {
112: return this .tags;
113: }
114:
115: /**
116: * Closes the context making its parent the new current context.
117: */
118: public void close() {
119: request.setAttribute(REQUEST_ATTRIBUTE, parent);
120: }
121:
122: // -- Static methods ------------------------------------------------------
123:
124: public static void tag(HttpServletRequest request, String tag) {
125: TaggingContext context = getContext(request);
126: if (context != null) {
127: context.addTag(tag);
128: }
129: }
130:
131: public static void tag(String tag) {
132: TaggingContext context = getContext();
133: if (context != null) {
134: context.addTag(tag);
135: }
136: }
137:
138: public static void preventCaching(HttpServletRequest request) {
139: TaggingContext context = getContext(request);
140: if (context != null) {
141: context.setPreventCaching(true);
142: }
143: }
144:
145: public static void preventCaching() {
146: TaggingContext context = getContext();
147: if (context != null) {
148: context.setPreventCaching(true);
149: }
150: }
151:
152: /**
153: * Opens a nested context.
154: */
155: public static TaggingContext openNestedContext(
156: HttpServletRequest request) {
157: TaggingContext parent = getContext(request);
158: TaggingContext context = new TaggingContext(request, parent);
159: request.setAttribute(REQUEST_ATTRIBUTE, context);
160: return context;
161: }
162:
163: /**
164: * Retrieves the current context from the given request. The method will
165: * return <code>null</code> if no open context exists.
166: */
167: public static TaggingContext getContext(HttpServletRequest request) {
168: return (TaggingContext) request.getAttribute(REQUEST_ATTRIBUTE);
169: }
170:
171: /**
172: * Retrieves the current context using Spring's
173: * {@link RequestContextHolder}.
174: */
175: public static TaggingContext getContext() {
176: return (TaggingContext) RequestContextHolder
177: .getRequestAttributes().getAttribute(REQUEST_ATTRIBUTE,
178: RequestAttributes.SCOPE_REQUEST);
179: }
180:
181: }
|