View Javadoc

1   /*
2   MadCaching is a Tile Caching solution
3   Copyright (C) 2005  Mac Vu
4   
5   This program is free software; you can redistribute it and/or
6   modify it under the terms of the GNU General Public License
7   as published by the Free Software Foundation; either version 2
8   of the License, or (at your option) any later version.
9   
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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  }