1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.macvu.tiles.cache;
20
21 import com.macvu.tiles.CacheAttribute;
22 import com.macvu.tiles.CacheAttributeComparator;
23 import com.macvu.tiles.CacheInformation;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpSession;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /***
32 * User: MVu
33 */
34 public class SimpleCacheKeyFactory implements CacheKeyFactory {
35 public String getCacheKey(HttpServletRequest request, CacheInformation info) {
36 StringBuffer keyBuffer = new StringBuffer();
37 keyBuffer.append(info.getRepositoryName());
38 keyBuffer.append(':');
39
40 List cacheAttributeList = info.getCacheAttributes();
41 CacheAttributeComparator comparator = new CacheAttributeComparator();
42 Collections.sort(cacheAttributeList, comparator);
43
44 Iterator attItr = cacheAttributeList.iterator();
45 while (attItr.hasNext()) {
46 CacheAttribute cacheAttribute = (CacheAttribute) attItr.next();
47
48 appendCacheAttribute(request, keyBuffer, cacheAttribute);
49 }
50
51 return keyBuffer.toString();
52 }
53
54 void appendCacheAttribute(HttpServletRequest request, StringBuffer buffer, CacheAttribute cacheAttribute) {
55 buffer.append(cacheAttribute.getScope());
56 buffer.append('.');
57 buffer.append(cacheAttribute.getName());
58 buffer.append('=');
59
60 String value = "null";
61 if (CacheAttribute.PARAM_SCOPE.equalsIgnoreCase(cacheAttribute.getScope())) {
62 value = request.getParameter(cacheAttribute.getName());
63 if (value == null) {
64 value = "null";
65 }
66 } else if (CacheAttribute.REQUEST_SCOPE.equalsIgnoreCase(cacheAttribute.getScope())) {
67 Object object = request.getAttribute(cacheAttribute.getName());
68 if (object == null) {
69 value = "null";
70 } else {
71 value = object.toString();
72 }
73 } else if (CacheAttribute.SESSION_SCOPE.equalsIgnoreCase(cacheAttribute.getScope())) {
74 HttpSession session = request.getSession(false);
75
76 Object object= null;
77 if (session != null) {
78 object = session.getAttribute(cacheAttribute.getName());
79 }
80
81 if (object == null) {
82 value = "null";
83 } else {
84 value = object.toString();
85 }
86 }
87
88 buffer.append(value);
89 }
90 }