1 24 package org.riotfamily.cachius; 25 26 import javax.servlet.http.HttpServletRequest ; 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 36 public class TaggingContext { 37 38 private static final String REQUEST_ATTRIBUTE = 39 TaggingContext.class.getName(); 40 41 private Log log = LogFactory.getLog(TaggingContext.class); 42 43 private TaggingContext parent; 44 45 private String [] 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 tag) { 56 if (tags == null) { 57 tags = new String [] { 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 [] getTags() { 71 return this.tags; 72 } 73 74 public static void tag(HttpServletRequest request, String 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 request) { 83 TaggingContext parent = getContext(request); 84 setContext(request, new TaggingContext(parent)); 85 } 86 87 public static String [] popTags(HttpServletRequest 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 request) { 97 return (TaggingContext) request.getAttribute(REQUEST_ATTRIBUTE); 98 } 99 100 private static void setContext(HttpServletRequest request, 101 TaggingContext context) { 102 103 request.setAttribute(REQUEST_ATTRIBUTE, context); 104 } 105 106 } 107 | Popular Tags |